mb_comment.module 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @file
  4. * Provides a Cancel button for comments.
  5. *
  6. * Currently available buttons:
  7. * - Cancel
  8. */
  9. /**
  10. * Implements hook_permission().
  11. */
  12. function mb_comment_permission() {
  13. return array(
  14. 'access mb comment' => array(
  15. 'title' => t('Use More Comment Buttons'),
  16. 'description' => t('Use the buttons defined by More Buttons Comment.')
  17. )
  18. );
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function mb_comment_menu() {
  24. $items = array();
  25. $items['admin/config/mb/buttons/more-buttons-comment'] = array(
  26. 'page callback' => 'drupal_get_form',
  27. 'page arguments' => array('mb_comment_admin'),
  28. 'title' => 'Comments',
  29. 'access arguments' => array('administer site configuration'),
  30. 'description' => 'An overview of what content type uses buttons/functions of the MB Comment module.',
  31. 'file' => 'mb_comment.admin.inc',
  32. 'type' => MENU_LOCAL_TASK,
  33. 'weight' => 10
  34. );
  35. $items['admin/config/mb/buttons/more-buttons-comment/reset'] = array(
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('mb_comment_reset'),
  38. 'access arguments' => array('administer site configuration'),
  39. 'type' => MENU_CALLBACK,
  40. 'file' => 'mb_comment.admin.inc'
  41. );
  42. return $items;
  43. }
  44. /**
  45. * Implements hook_theme().
  46. */
  47. function mb_comment_theme() {
  48. return array(
  49. 'mb_comment_admin' => array(
  50. 'variables' => array('form' => NULL),
  51. )
  52. );
  53. }
  54. /**
  55. * Implements hook_form_alter().
  56. */
  57. function mb_comment_form_alter(&$form, &$form_state, $form_id) {
  58. $module = 'mb_comment';
  59. if ($form['#id']) {
  60. if ($form['#id'] == 'comment-form') {
  61. $node = $form['#node'];
  62. if (arg(0) == 'comment') {
  63. $default_values = mb_default_values();
  64. $mb_content_values = mb_get_values('mb');
  65. $destination = drupal_get_destination();
  66. $settings = array();
  67. $settings['cancel'] = variable_get($module . '_cancel_' . $node->type, 0);
  68. if (!preg_match('/comment\/reply/', url($_SERVER["HTTP_REFERER"]))) {
  69. $form['comment_referer'] = array(
  70. '#type' => 'value',
  71. '#value' => url($_SERVER["HTTP_REFERER"])
  72. );
  73. }
  74. $form_state['cache'] = TRUE;
  75. // Define the "Cancel" form element.
  76. if ($settings['cancel'] > 0) {
  77. if ($settings['cancel'] == 1) {
  78. $weight_cancel = $form['actions']['submit']['#weight'] - 1;
  79. }
  80. elseif ($settings['cancel'] == 2) {
  81. $weight_cancel = 16;
  82. }
  83. $form['actions']['cancel'] = array(
  84. '#type' => 'submit',
  85. '#value' => isset($mb_comment_values['cancel']) ? t('@cancel-value', array('@cancel-value' => t($mb_comment_values['cancel']))) : t($default_values['cancel']),
  86. '#weight' => 30,
  87. '#validate' => array('mb_comment_cancel_validate')
  88. );
  89. }
  90. }
  91. }
  92. }
  93. switch ($form_id) {
  94. case 'node_type_delete_confirm':
  95. // Delete MB Comment content type system variables
  96. // if content types will be deleted.
  97. $form['#submit'][] = 'mb_comment_delete_confirm_submit';
  98. break;
  99. case 'node_type_form':
  100. // Provide the prepared additional MB Comment button settings.
  101. // Check the specific case add content type form.
  102. if (empty($form['#node_type']->type)) {
  103. // Add content type.
  104. $type = 'mb_comment_type_dummy';
  105. }
  106. else {
  107. // Edit an content type.
  108. $type = $form['#node_type']->type;
  109. }
  110. // It makes no sense to use the MB Comment module with the content type panel.
  111. if ($type == 'panel') {
  112. return;
  113. }
  114. // The additional button settings.
  115. $form['comment_buttons'] = array(
  116. '#type' => 'fieldset',
  117. '#title' => t('Button settings - comments'),
  118. '#group' => 'additional_settings',
  119. '#collapsible' => TRUE,
  120. '#collapsed' => TRUE,
  121. '#weight' => 5,
  122. '#attached' => array(
  123. 'js' => array(drupal_get_path('module', $module) . '/' . $module . '_node_form.js')
  124. )
  125. );
  126. // Provide "Cancel" button settings.
  127. $form['comment_buttons'][$module . '_cancel'] = array(
  128. '#type' => 'select',
  129. '#title' => t('Cancel button'),
  130. '#description' => t('Please select using the button or its position.'),
  131. '#options' => mb_cancel_button_positions(),
  132. '#default_value' => variable_get($module . '_cancel_' . $type, 0)
  133. );
  134. break;
  135. }
  136. }
  137. /**
  138. * Implements hook_validate().
  139. *
  140. * Handle the "Cancel" button validation.
  141. */
  142. function mb_comment_cancel_validate(&$form, &$form_state) {
  143. // This is the cancel action. No validation required.
  144. mb_comment_cancel_action($form, $form_state);
  145. }
  146. /**
  147. * The "Cancel" action.
  148. *
  149. * Handle different submit actions and make different redirects.
  150. *
  151. * @see mb_comment_cancel_validate()
  152. */
  153. function mb_comment_cancel_action(&$form, &$form_state) {
  154. // Hide the error messages.
  155. drupal_get_messages('error');
  156. $redirect = 'node/' . $form['nid']['#value'];
  157. if (isset($form['comment_referer']['#value'])) {
  158. $redirect = $form['comment_referer']['#value'];
  159. }
  160. drupal_goto($redirect);
  161. }
  162. /**
  163. * Submit callback to delete MB Comment content type system variables
  164. * if content types will be deleted.
  165. */
  166. function mb_comment_delete_confirm_submit($form, &$form_state) {
  167. $module = 'mb_comment';
  168. foreach ($form_state['build_info']['args'] as $type) {
  169. variable_del($module . '_cancel_' . $type->type);
  170. }
  171. }