--- D:/temp/PURGE/userpoints_ubercart/uc_userpoints_product.module	Thu May 14 12:18:27 2009
+++ Z:/auction/sites/all/modules/userpoints_ubercart/uc_userpoints_product.module	Fri Jun 12 16:57:22 2009
@@ -62,33 +62,37 @@
 /**
 * Build the userpoints feature form
 */
-function uc_userpoints_product_feature_form($node, $feature) {
-	$points = 0;
+function uc_userpoints_product_feature_form($form_state, $node, $feature) {
+  $form = array();
 
+  // Use the feature's values to fill the form, if they exist.
+  if (!empty($feature)) {
+    $nid = $feature['nid'];
+    $default_points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE nid=%d", $nid));
+  }
+  else {
 	$nid = arg(1);
-
-	$res = db_query("SELECT * FROM {uc_userpoints_products} ucup, {uc_product_features} ucpf WHERE ucup.pfid = ucpf.pfid AND ucup.nid = %d",$nid);
-	while ($pf = db_fetch_object($res)) {
-	  $points = $pf->points;
+    $default_points = 1;  // default should be the minimum, i.e. 1
 	}
 
 	$form['nid'] = array(
 		'#type' => 'value',
-		'#value' => $node->nid,
+    '#value' => $nid,
 	);
 	$form['points'] = array(
 		'#type' => 'textfield',
-		'#title' => t('Number of '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
-		'#description' => t('How many '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points').' to give when this product is purchased.'),
-		'#default_value' => $points,
+    '#title' => t('Number of @points', array('@points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'))),
+    '#description' => t('How many @points to give when this product is purchased.', array('@points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'))),
+    '#default_value' => $default_points,
 	);
+
 	return uc_product_feature_form($form);
 }
 
 
 function uc_userpoints_product_feature_form_validate($form, &$form_state) {
   if (!$form_state['values']['points'] || !is_numeric($form_state['values']['points']) || $form_state['values']['points'] < 1) {
-    form_set_error('points','You must enter a numeric value, greater than zero for points');
+    form_set_error('points', t('You must enter a numeric value, greater than zero for points'));
   }
 }
 
@@ -97,26 +101,35 @@
 * Submit the form
 */
 function uc_userpoints_product_feature_form_submit($form, &$form_state) {
-	$data = array(
+  if (empty($form_state['values']['pfid'])) {
+    $form_state['values']['pfid'] = db_last_insert_id('uc_userpoints_products', 'upid');
+  }
+
+  $userpoints_product = array(
 		'pfid' => $form_state['values']['pfid'],
 		'nid' => $form_state['values']['nid'],
 		'points' => $form_state['values']['points'],
-		'fid' => 'userpoints',
-		'description' => 'Product is now worth '.$form_state['values']['points'].' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
 	);
 	
-	$ret = uc_product_feature_save($data);
-	if (empty($data['pfid'])) {
-		$pf = db_fetch_object(db_query("select pfid from {uc_userpoints_products} where nid = %d and fid = %d",$data['nid'],$data['fid']));
-		$data['pfid'] = $pf->pfid;
+  $data = array_merge($userpoints_product, array(
+    'fid' => 'userpoints',
+    'description' => t('Product is worth @value @points.', array('@value' => $userpoints_product['points'], '@points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'))),
+  ));
+
+  $form_state['redirect'] = uc_product_feature_save($data);
 
-		db_query("INSERT INTO {uc_userpoints_products} (pfid,nid,points) VALUES (%d,%d,%d)",$data['pfid'],$data['nid'],$data['points']);
+  // Insert or update uc_userpoints_products table
+  if (empty($userpoints_product['pfid'])) {
+    $userpoints_product['pfid'] = db_last_insert_id('uc_product_features', 'pfid');
 	}
-	else {
-		db_query("UPDATE {uc_userpoints_products} SET points = %d WHERE nid = %d AND pfid = %d",$data['points'],$data['nid'],$data['pfid']);
+
+  $key = NULL;
+  if ($upid = db_result(db_query("SELECT upid FROM {uc_userpoints_products} WHERE pfid = %d", $userpoints_product['pfid']))) {
+    $key = 'upid';
+    $userpoints_product['upid'] = $upid;
 	}
 
-	$form_state['redirect'] = $ret;
+  drupal_write_record('uc_userpoints_products', $userpoints_product, $key);
 }
 
 
@@ -160,14 +173,14 @@
 				'uid' => $order->uid,
 				'points' => $totalPoints,
 				'operation' => 'add',
-				'description' => t('A user purchased a product containing points for a total of ' .$totalPoints.' '. variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
+        'description' => t('A user purchased a product containing points for a total of @total @points', array('@total' => $totalPoints, '@points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'))),
 				'entity_id' => $order->oid,
 				'entity_type' => 'Ubercart Transaction',
 				'moderate' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0),
 			);
 
 		 userpoints_userpointsapi($params);
-		 uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' points!'), 'admin');
+     uc_order_comment_save($order->order_id, 0, t('User earned @total @points', array('@total' => $totalPoints, '@points' => variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'))), 'admin');
 		}
 	break;
 	}
