pm_block_user.admin.inc

  1. 7-1 pm_block_user/pm_block_user.admin.inc
  2. 7-2 pm_block_user/pm_block_user.admin.inc
  3. 6-2 pm_block_user/pm_block_user.admin.inc

Administration menu callbacks for pm_block_user.module.

File

pm_block_user/pm_block_user.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administration menu callbacks for pm_block_user.module.
  5. */
  6. /**
  7. * Builds row of sending, receiving roles and actions that go with them.
  8. *
  9. * @param $details
  10. * Details of the row: default values and the unique row number (delta).
  11. * @param $blacklist
  12. * When the functionality has been added, this will allow building actions
  13. * based on a whitelist or blacklist. The current code only covers the use
  14. * case of a blacklist, where blocking everyone is allowed by default and
  15. * rules are exceptions to that. Conversely, a whitelist will disallow
  16. * blocking by default and rules will configure roles that are allowed to
  17. * block.
  18. *
  19. * @return
  20. * Part of a form with controls for sending, receiving and actions.
  21. */
  22. function _pm_block_user_actions_form($details, $blacklist = TRUE) {
  23. $form = array(
  24. '#tree' => TRUE,
  25. );
  26. $delta = $details['delta'];
  27. // FALSE by default, or if the user has checked the 'Enabled' check box for
  28. // this row.
  29. $row_disabled = (isset($details['enabled']) ? !$details['enabled'] : FALSE);
  30. $form['author'] = array(
  31. '#type' => 'select',
  32. '#options' => user_roles(TRUE),
  33. '#default_value' => (isset($details['author']) ? $details['author'] : DRUPAL_AUTHENTICATED_RID),
  34. '#disabled' => $row_disabled,
  35. );
  36. $form['recipient'] = array(
  37. '#type' => 'select',
  38. '#options' => user_roles(TRUE),
  39. '#default_value' => (isset($details['recipient']) ? $details['recipient'] : DRUPAL_AUTHENTICATED_RID),
  40. '#disabled' => $row_disabled,
  41. );
  42. // Provide different action radios if we're using a whitelist or a blacklist.
  43. if ($blacklist) {
  44. $options = array(
  45. PM_BLOCK_USER_DISALLOW_BLOCKING => t('Disallow blocking author'),
  46. PM_BLOCK_USER_DISALLOW_SENDING => t('Disallow sending message'),
  47. );
  48. $default_value = (isset($details['action']) ? $details['action'] : PM_BLOCK_USER_DISALLOW_BLOCKING);
  49. }
  50. else {
  51. // TODO: add whitelist options/default_value here.
  52. }
  53. $form['action'] = array(
  54. '#type' => 'radios',
  55. '#options' => $options,
  56. '#disabled' => $row_disabled,
  57. '#default_value' => $default_value,
  58. );
  59. $form['enabled'] = array(
  60. '#type' => 'checkbox',
  61. '#default_value' => (isset($details['enabled']) ? $details['enabled'] : TRUE),
  62. );
  63. $form['remove'] = array(
  64. '#type' => 'submit',
  65. '#submit' => array('pm_block_user_remove_submit'),
  66. '#value' => t('Remove_' . $delta),
  67. '#attributes' => array('class' => array('remove-action')),
  68. '#prefix' => '<div id="remove-rule-button">',
  69. '#suffix' => '<label for="edit-remove">' . t('Remove rule') . '</label></div>',
  70. '#ajax' => array(
  71. 'callback' => 'pm_block_user_js',
  72. 'wrapper' => 'block-actions',
  73. ),
  74. );
  75. return $form;
  76. }
  77. /**
  78. * Takes an array of settings and filters out anything that's un-needed.
  79. * Leaving only settings to be saved.
  80. *
  81. * @param $settings
  82. * The array of settings to filter.
  83. *
  84. * @return
  85. * Array of settings, ready to be stored in the database.
  86. *
  87. * @see pm_block_user_settings_submit()
  88. */
  89. function _pm_block_user_settings_filter($settings) {
  90. // Add-in the names of any settings to be saved into the array below.
  91. $save_keys = array('author', 'recipient', 'action', 'enabled');
  92. $matching = array();
  93. // Run through each of the keys we want to save, creating a new array.
  94. // It's not possible to simply check for unwanted values and unset() them as
  95. // the array is multi-dimensional.
  96. foreach ($save_keys as $save_key) {
  97. if (isset($settings[$save_key])) {
  98. $matching[$save_key] = $settings[$save_key];
  99. }
  100. }
  101. if (count($matching) > 0) {
  102. return $matching;
  103. }
  104. else {
  105. return array_map('_pm_block_user_settings_filter', $settings);
  106. }
  107. }
  108. /**
  109. * Menu callback for AHAH handling.
  110. */
  111. function pm_block_user_js($form, $form_state) {
  112. return drupal_render($form['block_actions']);
  113. }
  114. /**
  115. * Submit handler for 'More' button, adds a new action.
  116. *
  117. * @see pm_block_user_remove_submit()
  118. */
  119. function pm_block_user_more_submit($form, &$form_state) {
  120. unset($form_state['submit_handlers']);
  121. form_execute_handlers('submit', $form, $form_state);
  122. // Get the submitted actions, then put them into a special area of
  123. // the $form_state.
  124. $submitted_values = $form_state['values'];
  125. // Add an empty action.
  126. $submitted_values['block_actions'][] = array();
  127. $form_state['pm_block_user'] = $submitted_values;
  128. // Rebuild the form by passing our $form_state through the
  129. // pm_block_user_settings() builder function.
  130. $form_state['rebuild'] = TRUE;
  131. }
  132. /**
  133. * Submit handler for 'Remove' button, removes an action.
  134. *
  135. * @see pm_block_user_more_submit()
  136. */
  137. function pm_block_user_remove_submit($form, &$form_state) {
  138. unset($form_state['submit_handlers']);
  139. form_execute_handlers('submit', $form, $form_state);
  140. $submitted_values = $form_state['values'];
  141. // Remove the requested action.
  142. $delta = $form_state['clicked_button']['#parents'][1];
  143. unset($submitted_values['block_actions'][$delta]);
  144. $form_state['pm_block_user'] = $submitted_values;
  145. $form_state['rebuild'] = TRUE;
  146. }
  147. /**
  148. * Menu callback for blocked user settings.
  149. */
  150. function pm_block_user_settings($form, &$form_state) {
  151. // Need to cache form for AHAH, so it can be rebuilt from cache later.
  152. $form = array(
  153. '#cache' => TRUE,
  154. '#attached' => array(
  155. 'css' => array(
  156. drupal_get_path('module', 'pm_block_user') . '/pm_block_user.css',
  157. ),
  158. ),
  159. );
  160. // Container for just the actions, used for AHAH.
  161. $form['block_actions'] = array(
  162. '#tree' => TRUE,
  163. '#prefix' => '<div id="block-actions">',
  164. '#suffix' => '</div>',
  165. '#theme' => 'pm_block_user_actions',
  166. );
  167. // Should we populate the form with data from $form_state or the database?
  168. if (!isset($form_state['pm_block_user']['block_actions'])) {
  169. $block_actions = variable_get('pm_block_user_actions', array());
  170. }
  171. else {
  172. $block_actions = $form_state['pm_block_user']['block_actions'];
  173. }
  174. // Work through each rule, adding it as a new element in
  175. // $form['block_actions'] ready to be themed later.
  176. foreach ($block_actions as $delta => $details) {
  177. $details['delta'] = $delta;
  178. $form['block_actions'][$delta] = _pm_block_user_actions_form($details);
  179. }
  180. // The magic AHAH callback button that adds more rows.
  181. $form['pm_block_actions_more'] = array(
  182. '#type' => 'submit',
  183. '#value' => t('More'),
  184. '#weight' => 1,
  185. '#prefix' => '<div id="add-rule-button">',
  186. '#suffix' => '<label for="edit-pm-block-actions-more">' . t('Add new rule') . '</label></div>',
  187. '#submit' => array('pm_block_user_more_submit'),
  188. '#ajax' => array(
  189. 'callback' => 'pm_block_user_js',
  190. 'wrapper' => 'block-actions',
  191. 'method' => 'replace',
  192. 'effect' => 'fade',
  193. ),
  194. );
  195. $form['submit_form'] = array(
  196. '#type' => 'submit',
  197. '#weight' => 10,
  198. '#value' => t('Save configuration'),
  199. );
  200. return $form;
  201. }
  202. /**
  203. * Submit handler for admin form.
  204. */
  205. function pm_block_user_settings_submit($form, &$form_state) {
  206. // We don't want it to submit when we're adding/removing actions.
  207. if ($form_state['clicked_button']['#id'] == 'edit-submit-form') {
  208. // If the form's 'block_actions' aren't set, the user has deleted all the
  209. // rows in the table, so we save an empty array to stop errors in the form
  210. // builder.
  211. if (isset($form_state['values']['block_actions'])) {
  212. variable_set('pm_block_user_actions', _pm_block_user_settings_filter($form_state['values']['block_actions']));
  213. }
  214. else {
  215. variable_set('pm_block_user_actions', array());
  216. }
  217. drupal_set_message(t('The configuration options have been saved.'));
  218. }
  219. }

Functions

Namesort descending Description
pm_block_user_js Menu callback for AHAH handling.
pm_block_user_more_submit Submit handler for 'More' button, adds a new action.
pm_block_user_remove_submit Submit handler for 'Remove' button, removes an action.
pm_block_user_settings Menu callback for blocked user settings.
pm_block_user_settings_submit Submit handler for admin form.
_pm_block_user_actions_form Builds row of sending, receiving roles and actions that go with them.
_pm_block_user_settings_filter Takes an array of settings and filters out anything that's un-needed. Leaving only settings to be saved.