From 950888b321710ee2ed0ee8c558a74f0d9ad2d48a Mon Sep 17 00:00:00 2001
From: Peter <aegir@create.simplistechsolutions.com>
Date: Thu, 4 Nov 2010 10:59:46 -0500
Subject: [PATCH] added ability to change cart button text and use a custom redirect

---
 uc_atctweaks.install |   40 ++++++++++++++++++++++++++++++++++++++++
 uc_atctweaks.module  |   37 +++++++++++++++++++++++++++++++++----
 2 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/uc_atctweaks.install b/uc_atctweaks.install
index 1a7389a..348b72e 100644
--- a/uc_atctweaks.install
+++ b/uc_atctweaks.install
@@ -22,6 +22,12 @@ function uc_atctweaks_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'nid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
       'cart_empty' => array(
         'description' => t('Cart empty setting for this instance of the feature.'),
         'type' => 'int',
@@ -36,6 +42,14 @@ function uc_atctweaks_schema() {
         'not null' => TRUE,
         'default' => '',
       ),
+      'extradata' => array(
+        'description' => t('Used to store extra "features" data added to this module..'),
+        'type' => 'varchar',
+        'length' => 1000,
+	'serialize' => TRUE,
+        'not null' => TRUE,
+        'default' => '',
+      ),
     ),
     'indexes' => array(
       'pfid' => array('pfid'),
@@ -54,3 +68,29 @@ function uc_atctweaks_uninstall() {
   drupal_uninstall_schema('uc_atctweaks');
   db_query("DELETE FROM {uc_product_features} WHERE fid = 'atctweaks'");
 }
+
+function uc_atctweaks_update_6001() {
+  $ret = array();
+  db_add_field($ret, 'uc_atctweaks_products', 'extradata', array(
+        'description' => t('Used to store extra "features" data added to this module..'),
+        'type' => 'varchar',
+        'length' => 1000,
+	'serialize' => TRUE,
+        'not null' => TRUE,
+        'default' => '',
+      )
+  );
+  return $ret;
+}
+
+function uc_atctweaks_update_6002() {
+  $ret = array();
+  db_add_field($ret, 'uc_atctweaks_products', 'nid', array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ));
+  return $ret;
+}
+
diff --git a/uc_atctweaks.module b/uc_atctweaks.module
index c357d13..8faf1dd 100644
--- a/uc_atctweaks.module
+++ b/uc_atctweaks.module
@@ -17,6 +17,16 @@
  * 3) Alter the product class form to set default add to cart tweaks.
  */
 function uc_atctweaks_form_alter(&$form, &$form_state, $form_id) {
+  if ($form['submit']['#value'] == variable_get('uc_product_add_to_cart_text', t('Add to cart')) && $form['nid']['#value']) {
+    $extradata = unserialize(db_result(db_query("SELECT extradata FROM {uc_atctweaks_products} WHERE nid = %d", $form['nid']['#value'])));
+    if ($extradata['addtocarttext'] != '') {
+      $form['submit']['#value'] = $extradata['addtocarttext'];
+      if (variable_get('uc_quotes_override_check', FALSE)) {
+        $form['#submit'][0] = 'uc_quotes_addtocart_override';
+      }
+    }
+  }
+
   // 1) Alter the product feature add form.
   if ($form_id == 'uc_product_feature_add_form') {
     // If an add to cart tweak has already been added to this product...
@@ -98,6 +108,10 @@ function uc_atctweaks_add_to_cart_post_submit($form, &$form_state) {
       case 'none':
         $form_state['redirect'] = 'node/'. $form_state['values']['nid'];
         break;
+      case 'customurl':
+	$extradata = unserialize(db_result(db_query("SELECT extradata FROM {uc_atctweaks_products} WHERE nid = %d", $form['nid']['#value'])));
+        $form_state['redirect'] = $extradata['redirecturl'];
+        break;
       case 'global':
       default:
         break;
@@ -165,7 +179,6 @@ function uc_atctweaks_product_feature() {
 // Settings form for individual Add to Cart Tweaks.
 function uc_atctweaks_feature_form($form_state, $node, $feature) {
   $form = array();
-
   // Load the Add to Cart Tweaks specific to this product.
   $data = db_fetch_object(db_query("SELECT * FROM {uc_atctweaks_products} WHERE pfid = %d", $feature['pfid']));
 
@@ -185,7 +198,6 @@ function uc_atctweaks_feature_form($form_state, $node, $feature) {
 
 function _uc_atctweaks_feature_form($data = FALSE) {
   $form = array();
-
   $form['cart_empty'] = array(
     '#type' => 'checkbox',
     '#title' => t('Empty the shopping cart of all other items when this product is added to it.'),
@@ -198,11 +210,24 @@ function _uc_atctweaks_feature_form($data = FALSE) {
       'global' => t('Global default'),
       'cart' => t('Cart view page'),
       'checkout' => t('Checkout form'),
+      'customurl' => t('Custom URL'),
       'none' => t('No redirect'),
     ),
     '#default_value' => $data ? $data->redirect : 'global',
   );
-
+  $extradata = unserialize($data->extradata);
+  $form['redirecturl'] = array(
+    '#type' => 'textfield',
+    '#title' => t("Custom Redirect url"),
+    '#description' => t('If you selected "Custom URL" above, Add the url you want to redirect top.'),
+    '#default_value' => $extradata ? $extradata['redirecturl'] : '',
+  );
+  $form['addtocarttext'] = array(
+    '#type' => 'textfield',
+    '#title' => t("Custom Add to Cart Text"),
+    '#description' => t('Enter in the custom add to cart text you want to use.  Leave blank for default.'),
+    '#default_value' => $extradata ? $extradata['addtocarttext'] : '',
+  );
   return $form;
 }
 
@@ -210,8 +235,13 @@ function uc_atctweaks_feature_form_submit($form, &$form_state) {
   // Build an array of Add to Cart Tweaks data from the form submission.
   $vp_data = array(
     'pfid' => $form_state['values']['pfid'],
+    'nid' => $form_state['values']['nid'],
     'cart_empty' => $form_state['values']['cart_empty'],
     'redirect' => $form_state['values']['redirect'],
+    'extradata' => array(
+	'redirecturl' => $form_state['values']['redirecturl'],
+	'addtocarttext' => check_plain($form_state['values']['addtocarttext']),
+    )
   );
 
   // Build the product feature description.
@@ -244,7 +274,6 @@ function uc_atctweaks_feature_form_submit($form, &$form_state) {
     $vp_data['vpid'] = db_result(db_query("SELECT vpid FROM {uc_atctweaks_products} WHERE pfid = %d", $data['pfid']));
     $key = 'vpid';
   }
-
   drupal_write_record('uc_atctweaks_products', $vp_data, $key);
 }
 
-- 
1.5.4.3

