--- uc_userpoints_product.module	Sat Mar 26 14:47:12 2011
+++ uc_userpoints_product.module	Thu Jun 09 11:42:10 2011
@@ -45,14 +45,18 @@
 	}
 }
 
-
+/**
+ * Implementation of hook_product_feature().
+ */
 function uc_userpoints_product_product_feature() {
-	$features[] = array(
-		'id' => 'userpoints',
-		'title' => t(variable_get(USERPOINTS_TRANS_UCPOINTS, 'Userpoints')),
-		'callback' => 'uc_userpoints_product_feature_form',
-		'delete' => 'uc_userpoints_product_feature_delete',
-		'settings' => array('uc_userpoints_product_feature_settings'),
+	$features = array(
+      array(
+        'id' => 'userpoints',
+        'title' => t(variable_get(USERPOINTS_TRANS_UCPOINTS, 'Userpoints')),
+        'callback' => 'uc_userpoints_product_feature_form',
+        'delete' => 'uc_userpoints_product_feature_delete',
+        // 'settings' => 'uc_userpoints_product_feature_settings',
+      ),
 	);
 	return $features;
 }
@@ -143,7 +147,7 @@
  * Feature settings
  */
 function uc_userpoints_product_feature_settings() {
-	// Nothing yet.
+  return array();
 }
 
 
@@ -151,16 +155,42 @@
  * Product Feature delete function
  */
 function uc_userpoints_product_feature_delete($feature) {
-		db_query("DELETE FROM {uc_userpoints_products} WHERE pfid = %d",$feature['pfid']);
-		db_query("DELETE FROM {uc_product_features} WHERE pfid = %d", $feature['pfid']);
+  db_query("DELETE FROM {uc_userpoints_products} WHERE pfid = %d",$feature['pfid']);
+  db_query("DELETE FROM {uc_product_features} WHERE pfid = %d", $feature['pfid']);
 }
 
+/**
+ * Retrieve the number of points for the given product
+ * @param product: the product node
+ * @param total: return the points * quantity total for the product
+ */
+function _uc_userpoints_product_get_product_points($product, $total = FALSE) {
+  $points = db_result(db_query("SELECT sum(points) FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
+  
+  if ($total) {
+    $points = ($points > 0) ? $points * $product->qty : 0;
+  }
+  return $points;
+}
 
 /**
- * Implementation of hook_order();
+ * Retrieve the Category TID of the points for the given product
+ * @param product: the product node
  */
-function uc_userpoints_product_order($op, $order, $status) {
+function _uc_userpoints_product_get_product_points_tid($product) {
+  $tid = db_result(db_query("SELECT tid FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
+  if (($tid == '') || ($tid == '-1')) {
+    $tid = variable_get(USERPOINTS_PRODUCT_CATEGORY, 0);
+  }  
+  return $tid;
+}
 
+/**
+ * Implementation of hook_order();
+ * Removed: use Rules instead!
+ *
+ 
+function uc_userpoints_product_order($op, $order, $status) {
 	switch ($op) {
 		case 'update':
 		// We might want to add the "Order Status" to the Userpoints Ubercart options...
@@ -168,17 +198,11 @@
       if ($order->order_status != 'completed' && ($status == 'completed' && $order->uid > 0) && ($order_user = user_load(array('uid' => $order->uid))) !== FALSE) {
 				$totalPoints = 0; // this will be the total order points
 				foreach ($order->products as $product) {
-					$productPoints = 0; // This will be the product points * quantity
-					$points = db_result(db_query("SELECT sum(points) FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
-					$tid = db_result(db_query("SELECT tid FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
-					if ($points > 0) {
-						//drupal_set_message($product->title.' has '.$points.' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points').' attached');
-						$productPoints = ($product->qty * $points); // Make sure we account for multiple qty of the Points product
-					}
-					if (($tid == '') || ($tid == '-1')) {
-			            $tid = variable_get(USERPOINTS_PRODUCT_CATEGORY, 0);
-			        }
-			    
+					
+          // This will be the product points * quantity
+					$productPoints = _uc_userpoints_product_get_product_points($product, TRUE);
+					$tid = _uc_userpoints_product_get_product_points_tid($product);
+
 			    $params = array (
 				    'tid' => $tid,
 				    'uid' => $order->uid,
@@ -205,6 +229,7 @@
 	break;
 	}
 }
+*/
 
 /**
 * Check and make sure the user isn't trying to buy points with points.
@@ -265,3 +290,42 @@
 
   return $pointsproducts;
 }
+
+/**
+ * Implementation of hook_token_list(). (token.module)
+ */
+function uc_userpoints_product_token_list($type = 'all') {
+  $tokens = array();
+
+  if ($type == 'node' || $type == 'product' || $type == 'ubercart' || $type == 'uc_line_item' || $type == 'all') {
+    $tokens['product']['userpoints'] = t("The product's userpoints value.");
+    $tokens['product']['userpoints-total'] = t("The total amount of product's userpoints (product userpoints * product quantity)");
+    $tokens['product']['userpoints-tid'] = t("The product's userpoints category TID.");
+  }
+  // @TODO
+  if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
+    $tokens['order']['userpoints'] = t("The total amount of points awarned for this order. -- TODO");
+  }
+
+  return $tokens;
+}
+
+/**
+ * Implementation of hook_token_values(). (token.module)
+ */
+function uc_userpoints_product_token_values($type, $object = NULL) {
+
+  if (($type == 'node' && uc_product_is_product($object)) || $type == 'uc_line_item') {
+    $tokens = array();
+    $tokens['userpoints'] = _uc_userpoints_product_get_product_points($object);
+    $tokens['userpoints-total'] = _uc_userpoints_product_get_product_points($object, TRUE);
+    $tokens['userpoints-tid'] = _uc_userpoints_product_get_product_points_tid($object);
+    return $tokens;
+  }
+  if ($type == 'order') {
+    $tokens = array();
+    // @TODO:
+    $tokens['userpoints'] = 0; //_uc_userpoints_product_get_order_points();
+    return $tokens;
+  }
+}
\ No newline at end of file
