Guest User

Untitled

a guest
Aug 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file
  5. * Custom functionality for NCAC site.
  6. */
  7.  
  8. /**
  9. * Implementation of hook_init().
  10. */
  11. function ncac_init() {
  12. // Add dialog specific js. Used for 'Book now' dialog.
  13. dialog_add_js();
  14. }
  15.  
  16. /**
  17. * Implementation of hook_menu().
  18. *
  19. * @return array of menu items
  20. */
  21. function ncac_menu() {
  22. $items = array();
  23. $items['book/%node/%ctools_js'] = array(
  24. 'title' => 'Book this activity',
  25. 'page callback' => 'ncac_dialog_node_view',
  26. 'page arguments' => array(1, 2),
  27. 'access arguments' => array('access content'),
  28. 'type' => MENU_CALLBACK,
  29. );
  30. return $items;
  31. }
  32.  
  33. /**
  34. * Implementation of hook_nodeapi().
  35. */
  36. function ncac_nodeapi(&$node, $op) {
  37. // Remove add_to_cart and display_price portions of the node display when in dialog mode
  38. if (($op == 'view') && (isset($node->build_mode)) && ($node->build_mode === 'dialog')) {
  39. unset($node->content['add_to_cart']);
  40. unset($node->content['display_price']);
  41. }
  42. }
  43.  
  44. /**
  45. * Implementation of hook_content_build_modes().
  46. *
  47. * @return
  48. * An array describing the build modes used by the module.
  49. * They are grouped by secondary tabs on CCK's 'Display fields' screens.
  50. */
  51. function ncac_content_build_modes() {
  52. $modes = array();
  53. $modes['dialog'] = array(
  54. 'title' => t('Dialog'),
  55. 'build modes' => array(
  56. 'dialog' => array(
  57. 'title' => t('Dialog'),
  58. 'views style' => FALSE,
  59. ),
  60. ),
  61. );
  62. return $modes;
  63. }
  64.  
  65. /**
  66. * Implementation of hook_form_alter().
  67. */
  68. function ncac_form_alter(&$form, $form_state, $form_id) {
  69. // Only do this for node forms
  70. if (isset($form['#id']) && ($form['#id'] == 'node-form') && arg(0) == 'node') {
  71. $node = $form['#node'];
  72.  
  73. // If a path has already been set for this node, don't automatically tick
  74. // the 'Automatic alias' box. Keeps existing aliases from being changed.
  75. if (drupal_strlen($node->path) > 0) {
  76. $node->pathauto_perform_alias = FALSE;
  77. }
  78. }
  79.  
  80. // Add item's date validation to add to cart and buy it now forms
  81. $forms = array('uc_product_add_to_cart_form', 'uc_catalog_buy_it_now_form');
  82. foreach ($forms as $id) {
  83. if (drupal_substr($form_id, 0, drupal_strlen($id)) == $id) {
  84. $form['#validate'][] = 'ncac_validate_form_addtocart';
  85. }
  86. }
  87.  
  88. // Add validation to cart view
  89. if ($form_id == 'uc_cart_view_form') {
  90. $form['#validate'][] = 'ncac_validate_form_cart';
  91. }
  92.  
  93. // Add validation to checkout and checkout review forms
  94. if ($form_id == 'uc_cart_checkout_form' || $form_id == 'uc_cart_checkout_review_form') {
  95. $form['#validate'][] = 'ncac_validate_form_checkout';
  96.  
  97. // Collapse and slim down the uc_discounts field. It's HUGE by default!
  98. $form['panes']['uc_discounts']['#collapsed'] = TRUE;
  99. $form['panes']['uc_discounts']['uc-discounts-codes']['#rows'] = 1;
  100. $form['panes']['uc_discounts']['uc-discounts-codes']['#cols'] = 20;
  101. }
  102.  
  103. // Rename submit button on request node add form
  104. if ($form_id == 'request_node_form') {
  105. $form['buttons']['submit']['#value'] = 'Submit request';
  106. }
  107. }
  108.  
  109. /**
  110. * AJAX aware view of activity registration node. Uses ctools and dialog
  111. *
  112. * @param $node
  113. * Node to be rendered
  114. * @param $ajax
  115. * Is this an AJAX request?
  116. */
  117. function ncac_dialog_node_view($node, $ajax = FALSE) {
  118. // Is this an AJAX request? If not just render the standard page view
  119. if (!$ajax) {
  120. return node_page_view($node);
  121. }
  122.  
  123. // Set the build_mode so we know to get rid of the default uc_product
  124. // add_to_cart form using hook_nodeapi()
  125. $node->build_mode = 'dialog';
  126.  
  127. ctools_include('ajax');
  128.  
  129. // Start building the form that will work in the dialog
  130. $form_state = array(
  131. 'ajax' => TRUE,
  132. 'title' => $node->title,
  133. 'args' => array($node),
  134. );
  135.  
  136. $form_id = 'uc_product_add_to_cart_form_' . $node->nid;
  137.  
  138. ctools_include('form');
  139. // This won't override settings already in.
  140. $form_state += array(
  141. 're_render' => FALSE,
  142. 'no_redirect' => !empty($form_state['ajax']),
  143. );
  144.  
  145. $output = ctools_build_form($form_id, $form_state);
  146. if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
  147. $title = empty($form_state['title']) ? '' : $form_state['title'];
  148. // If there are messages for the form, render them. Includes min qty/date
  149. // errors.
  150. if ($messages = theme('status_messages')) {
  151. $output = $messages . $output;
  152. }
  153. // Render the node view and add it to the output
  154. $node_view = node_view($node, FALSE, TRUE);
  155. $output = $output . $node_view;
  156.  
  157. $commands = array();
  158. if (isset($form_state['js settings'])) {
  159. $commands[] = ctools_ajax_command_settings($form_state['js settings']);
  160. }
  161. // Add dialog display of the node to the ctools ajax renderer stack
  162. $commands[] = dialog_command_display($title, $output);
  163. $output = $commands;
  164. }
  165.  
  166. if (empty($output)) {
  167. // On form submission display a message in the dialog, then redirect to the cart
  168. $output[] = dialog_command_display(t('Book this activity'), t('Activity added to your cart...'));
  169. $output[] = ctools_ajax_command_redirect('cart');
  170. }
  171.  
  172. // Use ctools to make the ajax magic happen
  173. ctools_ajax_render($output);
  174. }
Add Comment
Please, Sign In to add comment