uc_atctweaks.module 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @file
  4. * Defines a product feature to tweak the add to cart form behavior.
  5. */
  6. /**
  7. * Implements hook_form_alter().
  8. *
  9. * Summary of alterations:
  10. * 1) Alters the product feature add form to restrict multiple Add to Cart
  11. * Tweak features from being added to a single product.
  12. * 2) Alters the add to cart form for variable priced products.
  13. * 3) Alter the product class form to set default add to cart tweaks.
  14. */
  15. function uc_atctweaks_form_alter(&$form, &$form_state, $form_id) {
  16. // 1) Alter the product feature add form.
  17. if ($form_id == 'uc_product_feature_add_form') {
  18. // If an add to cart tweak has already been added to this product...
  19. if (db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = :nid AND fid = :fid", array(':nid' => arg(1), ':fid' => 'atctweaks'))->fetchField()) {
  20. // Remove Add to Cart Tweak from the available list of features to add.
  21. unset($form['feature']['#options']['atctweaks']);
  22. }
  23. }
  24. // 2) Alters the add to cart form for variable priced products.
  25. if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0) {
  26. $data = uc_atctweaks_product_load($form['nid']['#value']);
  27. if ($data) {
  28. $form['atctweaks_data'] = array(
  29. '#type' => 'value',
  30. '#value' => $data,
  31. );
  32. $form['#submit'] = array_merge(array('uc_atctweaks_add_to_cart_pre_submit'), $form['#submit']);
  33. $form['#submit'][] = 'uc_atctweaks_add_to_cart_post_submit';
  34. }
  35. }
  36. // 3) Alter the product class form to set default add to cart tweaks.
  37. if ($form_id == 'uc_product_class_form') {
  38. $data = FALSE;
  39. if (!empty($form['pcid']['#value'])) {
  40. $class_defaults = variable_get('ucatc_class_def_' . $form['pcid']['#value'], array());
  41. if (!empty($class_defaults)) {
  42. $data = (object) unserialize($class_defaults);
  43. }
  44. }
  45. $form['atctweaks'] = array(
  46. '#type' => 'fieldset',
  47. '#title' => t('Default Add to Cart Tweaks'),
  48. '#collapsible' => TRUE,
  49. '#collapsed' => TRUE,
  50. '#weight' => 5,
  51. );
  52. $form['atctweaks']['default_atctweaks'] = array(
  53. '#type' => 'checkbox',
  54. '#title' => t('Check this box to add default Add to Cart Tweaks to every product of this class using these settings.'),
  55. '#default_value' => $data === FALSE ? FALSE : TRUE,
  56. );
  57. $form['atctweaks'] += _uc_atctweaks_feature_form($data);
  58. $form['#submit'][] = 'uc_atctweaks_product_class_submit';
  59. $form['submit']['#weight'] = 10;
  60. }
  61. }
  62. /**
  63. * Submit handler for the add to cart form to process after the normal handler.
  64. */
  65. function uc_atctweaks_add_to_cart_pre_submit($form, &$form_state) {
  66. if (!empty($form_state['values']['atctweaks_data'])) {
  67. $data = $form_state['values']['atctweaks_data'];
  68. // Empty the shopping cart if the feature specifies to do so.
  69. if ($data->cart_empty) {
  70. uc_cart_empty(uc_cart_get_id());
  71. }
  72. }
  73. }
  74. /**
  75. * Submit handler for the add to cart form to process after the normal handler.
  76. */
  77. function uc_atctweaks_add_to_cart_post_submit($form, &$form_state) {
  78. if (!empty($form_state['values']['atctweaks_data'])) {
  79. $data = $form_state['values']['atctweaks_data'];
  80. switch ($data->redirect) {
  81. case 'cart':
  82. $form_state['redirect'] = 'cart';
  83. break;
  84. case 'checkout':
  85. $form_state['redirect'] = 'cart/checkout';
  86. break;
  87. case 'none':
  88. $form_state['redirect'] = 'node/' . $form_state['values']['nid'];
  89. break;
  90. case 'global':
  91. default:
  92. break;
  93. }
  94. }
  95. }
  96. /**
  97. * Submit handler for the product class form for default Variable Price features.
  98. */
  99. function uc_atctweaks_product_class_submit($form, &$form_state) {
  100. if ($form_state['values']['default_atctweaks']) {
  101. $data = array(
  102. 'cart_empty' => $form_state['values']['cart_empty'],
  103. 'redirect' => $form_state['values']['redirect'],
  104. );
  105. variable_set('ucatc_class_def_' . $form_state['values']['pcid'], serialize($data));
  106. }
  107. else {
  108. variable_del('ucatc_class_def_' . $form_state['values']['pcid']);
  109. }
  110. }
  111. /**
  112. * Implements hook_node_insert().
  113. * Inserts add to cart tweak product feature on product node creation.
  114. */
  115. function uc_atctweaks_node_insert($node) {
  116. if (uc_product_is_product($node)) {
  117. $data = variable_get('ucatc_class_def_' . $node->type, array());
  118. // If the product class has default Add to Cart Tweaks...
  119. if ($data) {
  120. // Prepare the data as if it were from a form submission.
  121. $data = unserialize($data);
  122. $data['nid'] = $node->nid;
  123. $data['pfid'] = '';
  124. $form_state = array('values' => $data);
  125. // Add the feature to the product by spoofing the normal form submission.
  126. uc_atctweaks_feature_form_submit(array(), $form_state);
  127. }
  128. }
  129. }
  130. /**
  131. * Implements hook_uc_product_feature().
  132. */
  133. function uc_atctweaks_uc_product_feature() {
  134. $features = array();
  135. $features[] = array(
  136. 'id' => 'atctweaks',
  137. 'title' => t('Add to cart tweaks'),
  138. 'callback' => 'uc_atctweaks_feature_form',
  139. 'delete' => 'uc_atctweaks_feature_delete',
  140. 'multiple' => FALSE,
  141. );
  142. return $features;
  143. }
  144. /**
  145. * Settings form for individual Add to Cart Tweaks.
  146. */
  147. function uc_atctweaks_feature_form($form, &$form_state, $node, $feature) {
  148. // Load the Add to Cart Tweaks specific to this product.
  149. if (!empty($feature)) {
  150. $atctweaks_feature = db_query('SELECT * FROM {uc_atctweaks_products} WHERE pfid = :pfid', array(':pfid' => $feature['pfid']))->fetchObject();
  151. }
  152. else {
  153. $atctweaks_feature = new stdClass();
  154. $atctweaks_feature->pfid = '';
  155. $atctweaks_feature->cart_empty = FALSE;
  156. $atctweaks_feature->redirect = 'global';
  157. }
  158. $form['nid'] = array(
  159. '#type' => 'value',
  160. '#value' => $node->nid,
  161. );
  162. $form['pfid'] = array(
  163. '#type' => 'value',
  164. '#value' => $atctweaks_feature->pfid,
  165. );
  166. $form += _uc_atctweaks_feature_form($atctweaks_feature);
  167. return $form;
  168. }
  169. function _uc_atctweaks_feature_form($atctweaks_feature = FALSE) {
  170. $form = array();
  171. $form['cart_empty'] = array(
  172. '#type' => 'checkbox',
  173. '#title' => t('Empty the shopping cart of all other items when this product is added to it.'),
  174. '#default_value' => $atctweaks_feature ? $atctweaks_feature->cart_empty : FALSE,
  175. );
  176. $form['redirect'] = array(
  177. '#type' => 'radios',
  178. '#title' => t('Add to cart form redirect'),
  179. '#options' => array(
  180. 'global' => t('Global default'),
  181. 'cart' => t('Cart view page'),
  182. 'checkout' => t('Checkout form'),
  183. 'none' => t('No redirect'),
  184. ),
  185. '#default_value' => $atctweaks_feature ? $atctweaks_feature->redirect : 'global',
  186. );
  187. return $form;
  188. }
  189. /**
  190. * @see uc_atctweaks_feature_form()
  191. */
  192. function uc_atctweaks_feature_form_submit($form, &$form_state) {
  193. // Build an array of Add to Cart Tweaks data from the form submission.
  194. $atctweaks_data = array(
  195. 'pfid' => $form_state['values']['pfid'],
  196. 'cart_empty' => $form_state['values']['cart_empty'],
  197. 'redirect' => $form_state['values']['redirect'],
  198. );
  199. // Build the product feature description.
  200. $tweaks_form = _uc_atctweaks_feature_form();
  201. $description = array(
  202. t('<b>Add to cart form redirect:</b> @redirect', array('@redirect' => $tweaks_form['redirect']['#options'][$atctweaks_data['redirect']])),
  203. );
  204. if ($atctweaks_data['cart_empty']) {
  205. $description[] = t('The cart will be emptied prior to adding this item to it.');
  206. }
  207. // Save the basic product feature data.
  208. $data = array(
  209. 'pfid' => $atctweaks_data['pfid'],
  210. 'nid' => $form_state['values']['nid'],
  211. 'fid' => 'atctweaks',
  212. 'description' => implode('<br />', $description),
  213. );
  214. $form_state['redirect'] = uc_product_feature_save($data);
  215. $atctweaks_data['pfid'] = $data['pfid'];
  216. // Insert or update the data in the Variable Price products table.
  217. $key = array();
  218. if ($vpid = _uc_atctweaks_get_vpid($atctweaks_data['pfid'])) {
  219. $key = 'vpid';
  220. $atctweaks_data['vpid'] = $vpid;
  221. }
  222. drupal_write_record('uc_atctweaks_products', $atctweaks_data, $key);
  223. }
  224. /**
  225. * Add to Cart Tweaks product feature delete function.
  226. */
  227. function uc_atctweaks_feature_delete($feature) {
  228. db_delete('uc_atctweaks_products')
  229. ->condition('pfid', $feature['pfid'])
  230. ->execute();
  231. }
  232. /**
  233. * Load the product feature data for a given node.
  234. */
  235. function uc_atctweaks_product_load($nid) {
  236. return db_query('SELECT atc.* FROM {uc_product_features} AS pf
  237. LEFT JOIN {uc_atctweaks_products} AS atc ON pf.pfid = atc.pfid
  238. WHERE pf.fid = :pf_fid AND pf.nid = :pf_nid',
  239. array(':pf_fid' => 'atctweaks', ':pf_nid' => $nid))->fetchObject();
  240. }
  241. /**
  242. * Gets a uc_atctweaks id from a product feature id.
  243. */
  244. function _uc_atctweaks_get_vpid($pfid) {
  245. return db_query('SELECT vpid FROM {uc_atctweaks_products} WHERE pfid = :pfid', array(':pfid' => $pfid))->fetchField();
  246. }