? diff.patch
Index: uc_userpoints_product.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_product.info,v
retrieving revision 1.6
diff -u -p -r1.6 uc_userpoints_product.info
--- uc_userpoints_product.info	10 Jul 2008 02:44:25 -0000	1.6
+++ uc_userpoints_product.info	7 May 2009 23:02:57 -0000
@@ -1,7 +1,12 @@
 ; $Id: uc_userpoints_product.info,v 1.6 2008/07/10 02:44:25 bmagistro Exp $
 name = Userpoints Product
-description = Provideds necessary modifications to award a user points when a product is purchased if not using workflow-ng
+description = Provides an interface to add features that award users points to products without using workflow-ng
 version = "5.x-1.0"
 package = Ubercart - Userpoints
 dependencies = userpoints uc_order uc_cart uc_payment
 
+; Information added by drupal.org packaging script on 2008-07-28
+version = "5.x-2.2"
+project = "userpoints_ubercart"
+datestamp = "1217208620"
+
Index: uc_userpoints_product.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_product.install,v
retrieving revision 1.3
diff -u -p -r1.3 uc_userpoints_product.install
--- uc_userpoints_product.install	13 Jun 2008 23:29:09 -0000	1.3
+++ uc_userpoints_product.install	7 May 2009 23:02:57 -0000
@@ -1,5 +1,7 @@
 <?php
 /* major contributions and development by torgosPizza */
+/* Additional changes made by BillyMG */
+
 /**
  * Install the initial schema.
  */
@@ -7,12 +9,12 @@ function uc_userpoints_product_install()
 	switch ($GLOBALS['db_type']) {
 		case 'mysql':
 		case 'mysqli':
-		 db_query("CREATE TABLE {uc_userpoints_products} (
-		upid int(11) NOT NULL,
-		pfid mediumint(9) NOT NULL default '0',
-		nid mediumint(9) NOT NULL default '0',
-		points mediumint(9) NOT NULL default '0',
-		PRIMARY KEY  (upid)
+			db_query("CREATE TABLE {uc_userpoints_products} (
+				upid int(11) NOT NULL,
+				pfid mediumint(9) NOT NULL default '0',
+				nid mediumint(9) NOT NULL default '0',
+				points mediumint(9) NOT NULL default '0',
+				PRIMARY KEY (upid)
 			) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;");
 			break;
 		case 'pgsql':
@@ -27,3 +29,22 @@ function uc_userpoints_product_uninstall
 	db_query('DROP TABLE {uc_userpoints_products}');
 }
 
+
+/**
+* Update 1, for adding categories to individual products
+*/
+function uc_userpoints_update_1() {
+	$ret = array();
+	switch ($GLOBALS['db_type']) {
+		case 'mysql':
+		case 'mysqli':
+			// Add the category column
+			$ret[] = update_sql("ALTER TABLE {uc_userpoints_products} ADD COLUMN category MEDIUMINT(9) NOT NULL DEFAULT '0' AFTER points;");
+			break;
+		case 'pgsql':
+			break;
+	}
+	return $ret;
+}
+
+
Index: uc_userpoints_product.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_product.module,v
retrieving revision 1.5
diff -u -p -r1.5 uc_userpoints_product.module
--- uc_userpoints_product.module	27 Jul 2008 23:05:40 -0000	1.5
+++ uc_userpoints_product.module	7 May 2009 23:02:57 -0000
@@ -1,21 +1,23 @@
 <?php
 /* major contributions and development by torgosPizza */
-define('USERPOINTS_PRODUCT_MODERATE',		'userpoints_product_moderate');
-define('USERPOINTS_PRODUCT_CATEGORY',		'userpoints_product_category');
+/* Additional changes made by BillyMG */
 
+define('USERPOINTS_PRODUCT_MODERATE', 'userpoints_product_moderate');
 
 function uc_userpoints_product_help($section) {
   switch ($section) {
     case 'admin/modules#description':
-      $output = t('<strong>Ubercart Userpoints Product:</strong> Interfaces userpoints with Ubercart, so users can purchase points with products.');
+      $output = t('<strong>Ubercart Userpoints Product:</strong> Interfaces userpoints with Ubercart, so users can receive !points with products.', userpoints_translation());
       break;
   }
   return $output;
 }
 
-
+/**
+* Implementation of hook_product_feature().
+*/
 function uc_userpoints_product_userpoints($op, $points = 0, $uid = 0, $event = '') {
-  switch($op) {
+  switch ($op) {
     case 'setting':
       if (module_exists('uc_cart') && module_exists('uc_payment')) {
         $group = 'uc_userpoints_product';
@@ -29,17 +31,8 @@ function uc_userpoints_product_userpoint
           '#type' => 'radios',
           '#title' => t('Moderate userpoints transaction'),
           '#default_value' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0),
-					'#options' => array(t('No'), t('Yes')),
+          '#options' => array(t('No'), t('Yes')),
         );
-
-				$form[$group][USERPOINTS_PRODUCT_CATEGORY] = array(
-					'#type' => 'select',
-					'#title' => t('Product Category'),
-					'#default_value' => variable_get(USERPOINTS_PRODUCT_CATEGORY, NULL),
-					'#options' =>  userpoints_get_categories(),
-					'#description' => t('Category to assign awarded !points.', userpoints_translation()),
-				);
-
       }
       return $form;
       break;
@@ -53,10 +46,10 @@ function uc_userpoints_product_userpoint
 function uc_userpoints_product_product_feature() {
 	$features[] = array(
 		'id' => 'userpoints',
-		'title' => t(variable_get(USERPOINTS_TRANS_UCPOINTS, 'Userpoints')),
+		'title' => t('!Points on purchase', userpoints_translation()),
 		'callback' => 'uc_userpoints_product_feature_form',
 		'delete' => 'uc_userpoints_product_feature_delete',
-		'settings' => 'uc_userpoints_product_feature_settings',
+		//'settings' => 'uc_userpoints_product_feature_settings',
 	);
 	return $features;
 }
@@ -65,105 +58,142 @@ function uc_userpoints_product_product_f
 * Build the userpoints feature form
 */
 function uc_userpoints_product_feature_form($node, $feature) {
+	if(!empty($feature)) {
+		$defaults = db_fetch_array(db_query("SELECT points, category FROM {uc_userpoints_products} WHERE pfid = %d LIMIT 1", $feature['pfid']));
+	} else {
+		$defaults = array();
+	}
 	$form['nid'] = array(
-		'#type' => 'value',
+		'#type' => 'hidden',
 		'#value' => $node->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_values' => $points,
+		'#title' => t('Number of !points', userpoints_translation()),
+		'#description' => t('How many !points to give when this product is purchased', userpoints_translation()),
+		'#default_value' => !empty($defaults)? $defaults['points'] : null,
+	);
+	$form['category'] = array(
+		'#type' => 'select',
+		'#title' => t('Category of !points to give to user', userpoints_translation()),
+		'#description' => t('What category of !points will be given to the user', userpoints_translation()),
+		'#options' => userpoints_get_categories(),
+		'#default_value' => !empty($defaults)? $defaults['category'] : null,
 	);
 	return uc_product_feature_form($form);
 }
 
 
 function uc_userpoints_product_feature_form_validate($form_id, $form_values) {
-	// Not validating anything yet
+	$error = 0;
+	if (!(!empty($form_values['points']) && is_numeric($form_values['points']) && intval($form_values['points']) == $form_values['points'])) {
+		form_set_error('points', t('!Points must be a valid whole number', userpoints_translation()));
+		$error = 1;
+	}
+	$catName = taxonomy_get_term($form_values['category']);
+	if(empty($catName)) {
+		form_set_error('category', t('Invalid category selected'));
+		$error = 1;
+	}
+	if($error == 0) {
+		$query = "SELECT COUNT(*) FROM {uc_userpoints_products} WHERE nid = %d AND category = %d";
+		if(!empty($form_values['pfid'])) {
+			$query .= " AND pfid <> %d ";
+		}
+		$query .= " LIMIT 1";
+		// Can pass the query the pfid value, even if it doesn't need it, it just won't get used unless necessary
+		$result = db_result(db_query($query, $form_values['nid'], $form_values['category'], $form_values['pfid']));
+		if(!empty($result)) {
+			form_set_error('category', t('This node already has a !points feature for this category', userpoints_translation()));
+		}
+	}
 }
 
-
 /**
 * Submit the form
 */
 function uc_userpoints_product_feature_form_submit($form_id, $form_values) {
 	if (empty($form_values['pfid'])) {
-		$pfid = db_next_id('{uc_product_features}_pfid') + 1;
+		$pfid = db_next_id('{uc_product_features}_pfid');
+		$newItem = 1;
 	}
 	else {
 		$pfid = $form_values['pfid'];
+		$newItem = 0;
 	}
-
+	
+	$catName = taxonomy_get_term($form_values['category']);
+	$catName = $catName->name;
+	
 	$data = array(
 		'pfid' => $pfid,
 		'nid' => $form_values['nid'],
-		'fid' => 'file',
-		'description' => 'Product is now worth '.$form_values['points'].' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
+		'fid' => t('userpoints'),
+		'description' => t('Users will receive '.$form_values['points'].' !points of category "' . $catName .'" when purchasing this item', userpoints_translation()),
 	);
-
-	db_query("INSERT INTO {uc_product_features} (pfid, nid, fid, description) VALUES (%d, %d, '%s', '%s')",$pfid,$form_values['nid'],'userpoints',$data['description']);
-	db_query("INSERT INTO {uc_userpoints_products} (upid, pfid, nid, points) VALUES (%d, %d, %d, %d)",db_next_id('{uc_userpoints_products}_upid'),$pfid,$form_values['nid'],$form_values['points']);
-
+	
+	if($newItem) {
+		db_query("INSERT INTO {uc_userpoints_products} (upid, pfid, nid, points, category) VALUES (%d, %d, %d, %d, %d)", db_next_id('{uc_userpoints_products}_upid'), $pfid, $form_values['nid'],$form_values['points'], $form_values['category']);
+	} else {
+		db_query("UPDATE {uc_userpoints_products} SET points = %d, category = %d", $form_values['points'], $form_values['category']);
+	}
+	
 	return uc_product_feature_save($data);
 }
 
 
 /**
- * Feature settings
+ * Feature settings (Not implemented, because there aren't any settings)
+ * Having it implemented but not returning anything causes an array_merge warning
  */
+/*
 function uc_userpoints_product_feature_settings() {
-	// Nothing yet.
 }
-
+*/
 
 /**
  * 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']);
+	$result = db_query("DELETE FROM {uc_userpoints_products} WHERE pfid = %d",$feature['pfid']);
 }
 
-
 /**
  * Implementation of hook_order();
  */
 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...
+			// We might want to add the "Order Status" to the Userpoints Ubercart options...
 			if (($status == 'completed' && $order->uid > 0) && ($order_user = user_load(array('uid' => $order->uid))) !== FALSE) {
 				foreach ($order->products as $product) {
-					$points = db_result(db_query("SELECT points 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');
-						$totalPoints .= ($product->qty * $points); // Make sure we account for multiple qty of the Points product
+					$results = db_query("SELECT nid, points, category FROM {uc_userpoints_products} WHERE `nid` = %d", $product->nid);
+					while($points = db_fetch_object($results)) {
+						$totalPoints = ($product->qty * $points->points); // Make sure we account for multiple qty of the Points product @todo: Make this an option
+						$catName = taxonomy_get_term($points->category);
+						$catName = $catName->name;
+						$params = array (
+							'tid' => $points->category,
+							'uid' => $order->uid,
+							'points' => $totalPoints,
+							'operation' => 'add',
+							'description' => t('A user purchased a product with a nid of ' . $points->nid . ' and received ' .$totalPoints.' !points of category "' . $catName .'"', userpoints_translation()),
+							'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!', userpoints_translation()), 'admin');
 					}
 				}
-
-			$params = array (
-				'tid' => variable_get(USERPOINTS_PRODUCT_CATEGORY, 0),
-				'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')),
-				'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');
-		}
-	break;
+			}
+			break;
 	}
 }
 
 /**
 * Check and make sure the user isn't trying to buy points with points.
-*  @todo: Make this configurable?
+* @todo: Make this configurable?
 */
 function uc_userpoints_product_form_alter($form_id, &$form) {
   // We may need to alter the checkout form to remove invalid payment methods.
