forms.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Ubercart Cart Links Builder Form
  4. */
  5. function uc_cartlinksbuilder_builder_form($form, $form_state, $product_nid = NULL) {
  6. if (module_exists('ctools')) {
  7. ctools_include('dependent');
  8. }
  9. if ($form_state['submitted']) {
  10. $form['results'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t('Generated URL'),
  13. '#description' => t('Copy and paste the following to use the cart link that was generated'),
  14. );
  15. $form['results']['cart_url'] = array(
  16. '#type' => 'item',
  17. '#title' => t('Generated URL'),
  18. '#markup' => $form_state['storage']['cart_link'],
  19. );
  20. if ($form_state['storage']['cart_html']) {
  21. $form['results']['cart_html'] = array(
  22. '#type' => 'textarea',
  23. '#title' => t('Generated HTML for Link Tag'),
  24. '#default_value' => $form_state['storage']['cart_html'],
  25. '#description' => t('Copy and paste this link code into a web page, Drupal block, email blast, etc.'),
  26. '#disabled' => FALSE,
  27. '#resizable' => FALSE,
  28. '#rows' => 3,
  29. '#cols' => 80,
  30. );
  31. $form['results']['link_preview'] = array(
  32. '#type' => 'item',
  33. '#title' => t('HTML Link Preview'),
  34. '#markup' => $form_state['storage']['cart_html'],
  35. );
  36. }
  37. //$form['hr'] = array('#value' => '<hr />');
  38. // $product_nid = $form_state['values']['product_nid'];
  39. $default_product = $form_state['values']['product_nid'];
  40. }
  41. else {
  42. if ($product_nid) {
  43. $product_node = node_load($product_nid);
  44. $default_product = sprintf('%s [nid:%d]', $product_node->title, $product_nid);
  45. }
  46. }
  47. $form['product_nid'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Select a Product'),
  50. //'#process' => array('ctools_dependent_process'),
  51. //'#dependency' => array('radio:ref_type_custom' => array('user')),
  52. '#autocomplete_path' => 'uc-cartlinksbuilder/autocomplete',
  53. '#required' => TRUE,
  54. '#default_value' => $default_product,
  55. );
  56. $form['qty'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Quantity'),
  59. '#default_value' => ($form_state['values']) ? $form_state['values']['qty'] : 1,
  60. );
  61. $form['dest'] = array(
  62. '#type' => 'radios',
  63. '#title' => t('Redirect to this destination when link is clicked'),
  64. '#options' => array(
  65. 'cart' => t('Shopping Cart Summary'),
  66. 'checkout' => t('Checkout Page'),
  67. 'home' => t('Home Page'),
  68. 'custom' => t('Custom Destination (Please Specify)'),
  69. ),
  70. '#default_value' => ($form_state['values']) ? $form_state['values']['dest'] : 'cart',
  71. );
  72. $form['custom_destination'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Enter a Custom Destination'),
  75. '#process' => array('ctools_dependent_process'),
  76. '#dependency' => array('radio:dest' => array('custom')),
  77. '#default_value' => ($form_state['values']) ? $form_state['values']['custom_destination'] : '',
  78. );
  79. $form['link_options'] = array(
  80. '#tree' => FALSE,
  81. '#type' => 'fieldset',
  82. '#title' => t('HTML Link Options'),
  83. '#collapsible' => TRUE,
  84. '#collapsed' => FALSE,
  85. );
  86. $form['link_options']['make_hyperlink'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('Also Generate HTML Link Code'),
  89. '#description' => t('In addition to generating a raw URL, checking this option will give you the HTML codes for a hyperlink'),
  90. '#default_value' => ($form_state['values']) ? $form_state['values']['make_hyperlink'] : FALSE,
  91. );
  92. $form['link_options']['link_label'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Link Label'),
  95. '#default_value' => ($form_state['values']) ? $form_state['values']['link_label'] : t('Click here'),
  96. //'#process' => array('ctools_dependent_process'),
  97. //'#dependency' => array('edit-make-hyperlink' => array(TRUE)),
  98. );
  99. $form['link_options']['link_action'] = array(
  100. '#type' => 'radios',
  101. '#title' => t('Link Action'),
  102. '#prefix' => '<div id="edit-link-action-wrapper"><div id="edit-link-action">',
  103. '#suffix' => '</div></div>',
  104. '#options' => array(
  105. '_blank' => t('Open in New Window'),
  106. '_self' => t('Open in Same Window'),
  107. ),
  108. '#default_value' => ($form_state['values']) ? $form_state['values']['link_action'] : '_self',
  109. //'#process' => array('ctools_dependent_process'),
  110. //'#dependency' => array('edit-make-hyperlink' => array(TRUE)),
  111. );
  112. $form['options'] = array(
  113. '#type' => 'fieldset',
  114. '#title' => t('Additional Options'),
  115. '#collapsible' => TRUE,
  116. '#collapsed' => FALSE,
  117. );
  118. $form['options']['empty_cart'] = array(
  119. '#type' => 'checkbox',
  120. '#title' => t('Empty existing cart when this link is clicked'),
  121. '#default_value' => ($form_state['values']) ? $form_state['values']['empty_cart'] : FALSE,
  122. );
  123. $form['options']['hide_default_message'] = array(
  124. '#type' => 'checkbox',
  125. '#title' => t('Hide default "Added to Cart" message when this link is clicked'),
  126. '#default_value' => ($form_state['values']) ? $form_state['values']['hide_default_message'] : FALSE,
  127. );
  128. $form['options']['display_message'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => t('Display a message when link is clicked'),
  131. '#default_value' => ($form_state['values']) ? $form_state['values']['display_message'] : FALSE,
  132. );
  133. $form['options']['message'] = array(
  134. '#type' => 'textfield',
  135. '#title' => t('ID of Message to Display'),
  136. '#description' => t('You may set these messages up on the Cart Links Settings page (!link)', array('!link' => l(t('click here'), 'admin/store/settings/cart_links'))),
  137. '#process' => array('ctools_dependent_process'),
  138. '#dependency' => array('edit-display-message' => array(TRUE)),
  139. '#default_value' => ($form_state['values']) ? $form_state['values']['message'] : '',
  140. );
  141. $form['options']['link_id'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Custom Link ID'),
  144. '#description' => t('You can leave this field blank if you don\'t know what to do with it.'),
  145. '#default_value' => ($form_state['values']) ? $form_state['values']['link_id'] : '',
  146. );
  147. $form['submit'] = array(
  148. '#type' => 'submit',
  149. '#value' => t('Generate Link'),
  150. );
  151. return $form;
  152. }
  153. /**
  154. * Submission handler for Ubercart Cart Links Builder Form (uc_cartlinksbuilder_builder_form())
  155. */
  156. function uc_cartlinksbuilder_builder_form_submit($form, &$form_state) {
  157. $v = $form_state['values'];
  158. $actions = array();
  159. if ($v['empty_cart']) {
  160. $actions[] = 'e';
  161. }
  162. $matches = array();
  163. preg_match('/\[nid:(\d+)\]/', $v['product_nid'], $matches);
  164. $product_nid = $matches[1];
  165. $product = sprintf('p%s_q%d', $product_nid, $v['qty']);
  166. $actions[] = $product;
  167. if ($v['hide_default_message']) {
  168. $actions[] = 'm0';
  169. }
  170. if ($v['display_message'] && $v['message']) {
  171. $actions[] = 'm' . $v['message'];
  172. }
  173. if ($v['link_id']) {
  174. $actions[] = 'i' . $v['link_id'];
  175. }
  176. $url = sprintf('cart/add/%s', implode('-', $actions));
  177. switch ($v['dest']) {
  178. case 'home':
  179. $destination = '<front>';
  180. break;
  181. case 'cart':
  182. $destination = 'cart';
  183. break;
  184. case 'checkout':
  185. $destination = 'cart/checkout';
  186. break;
  187. case 'custom':
  188. $destination = $v['custom_destination'];
  189. break;
  190. }
  191. if ($destination) {
  192. $query = array('destination' => $destination);
  193. }
  194. $url = url($url, array('query' => $query, 'absolute' => TRUE));
  195. if ($v['make_hyperlink']) {
  196. $html = l($v['link_label'], $url, array('attributes' => array('target' => $v['link_action'])));
  197. }
  198. $form_state['rebuild'] = TRUE;
  199. $form_state['storage']['cart_link'] = $url;
  200. $form_state['storage']['cart_html'] = $html;
  201. }