pm_block_user.pages.inc

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

User menu callbacks for pm_block_user.module.

File

pm_block_user/pm_block_user.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User menu callbacks for pm_block_user.module.
  5. */
  6. function pm_block_user_form($form, $form_state, $author) {
  7. global $user;
  8. $form['author'] = array(
  9. '#type' => 'value',
  10. '#value' => $author,
  11. );
  12. $form['recipient'] = array(
  13. '#type' => 'value',
  14. '#value' => $user,
  15. );
  16. $form['destination'] = array(
  17. '#type' => 'value',
  18. '#value' => isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
  19. );
  20. if (pm_block_user_has_blocked($author, $user)) {
  21. $form['block_action'] = array(
  22. '#type' => 'value',
  23. '#value' => 'unblock_user',
  24. );
  25. return confirm_form($form,
  26. t('You have previously blocked "@author" from sending you any more messages. Are you sure you want to unblock this user?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
  27. isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
  28. '',
  29. t('Unblock @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
  30. t('Cancel')
  31. );
  32. }
  33. else {
  34. $form['block_action'] = array(
  35. '#type' => 'value',
  36. '#value' => 'block_user',
  37. );
  38. return confirm_form($form,
  39. t('Are you sure you want to block "@author" from sending you any more messages?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
  40. isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
  41. '',
  42. t('Block @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
  43. t('Cancel')
  44. );
  45. }
  46. }
  47. /**
  48. * Submit callback for block user confirmation form.
  49. */
  50. function pm_block_user_form_submit($form, &$form_state) {
  51. if ($form_state['values']['confirm']) {
  52. switch ($form_state['values']['block_action']) {
  53. case 'block_user':
  54. db_insert('pm_block_user')
  55. ->fields(array(
  56. 'author' => $form_state['values']['author']->uid,
  57. 'recipient' => $form_state['values']['recipient']->uid,
  58. ))
  59. ->execute();
  60. drupal_set_message(t('@author has been blocked from sending you any further messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
  61. break;
  62. case 'unblock_user':
  63. db_delete('pm_block_user')
  64. ->condition('author', $form_state['values']['author']->uid)
  65. ->condition('recipient', $form_state['values']['recipient']->uid)
  66. ->execute();
  67. drupal_set_message(t('@author is now allowed to send you new messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
  68. break;
  69. }
  70. }
  71. $form_state['redirect'] = $form_state['values']['destination'];
  72. }
  73. /**
  74. * Formbuilder function to build a simple form to block users.
  75. */
  76. function pm_block_user_list() {
  77. global $user;
  78. $form['new'] = array(
  79. '#type' => 'fieldset',
  80. '#title' => t('Block a user'),
  81. );
  82. $form['new']['name'] = array(
  83. '#type' => 'textfield',
  84. '#title' => t('Username'),
  85. '#autocomplete_path' => 'messages/user/autocomplete',
  86. '#description' => t('Separate multiple names with commas.'),
  87. '#required' => TRUE,
  88. );
  89. $form['new']['submit'] = array(
  90. '#type' => 'submit',
  91. '#value' => t('Block user'),
  92. '#validate' => array('pm_block_user_block_validate'),
  93. '#submit' => array('pm_block_user_block_submit'),
  94. );
  95. $header = array(
  96. array(
  97. 'data' => t('Username'),
  98. 'field' => 'u.name',
  99. 'sort' => 'asc',
  100. ),
  101. array(
  102. 'data' => t('Operations'),
  103. ),
  104. );
  105. $select = db_select('pm_block_user', 'pmb')->extend('PagerDefault')->extend('TableSort')
  106. ->fields('pmb', array('author'))
  107. ->condition('pmb.recipient', $user->uid)
  108. ->limit(20)
  109. ->orderByHeader($header);
  110. // Only show existing users, therefore join users.
  111. $select->innerJoin('users', 'u', 'u.uid = pmb.author');
  112. $rows = array();
  113. foreach ($select->execute() as $row) {
  114. $rows[] = array(
  115. theme('username', array('account' => user_load($row->author))),
  116. l(t('unblock'), 'messages/block/' . $row->author, array('query' => drupal_get_destination())),
  117. );
  118. }
  119. $form['#header'] = $header;
  120. $form['#rows'] = $rows;
  121. return $form;
  122. }
  123. /**
  124. * Validate user names.
  125. */
  126. function pm_block_user_block_validate($form, &$form_state) {
  127. global $user;
  128. list($accounts, $invalid) = _privatemsg_parse_userstring($form_state['values']['name'], array('user'));
  129. // Remove accounts that can not be blocked.
  130. if (!empty($accounts)) {
  131. foreach ($accounts as $id => $account) {
  132. // Only authors can be blocked.
  133. if ($account->type != 'user') {
  134. drupal_set_message(t('Only users can be blocked.'));
  135. unset($accounts[$id]);
  136. continue;
  137. }
  138. // Check if the user can not be blocked because of a rule.
  139. if (_pm_block_user_rule_exists($account, $user, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
  140. drupal_set_message(t('You are not allowed to block !account.', array('!account' => theme('username', array('account' => $account)))), 'warning');
  141. unset($accounts[$id]);
  142. continue;
  143. }
  144. // Check if the user is already blocked.
  145. if (pm_block_user_has_blocked($account, $user)) {
  146. drupal_set_message(t('You have already blocked !account.', array('!account' => theme('username', array('account' => $account)))), 'warning');
  147. unset($accounts[$id]);
  148. continue;
  149. }
  150. // Do not allow users to block themself.
  151. if ($user->uid == $account->uid) {
  152. drupal_set_message(t('You can not block yourself.'), 'warning');
  153. unset($accounts[$id]);
  154. continue;
  155. }
  156. }
  157. }
  158. // Display warning about invalid user names.
  159. if (!empty($invalid)) {
  160. drupal_set_message(t('The following users do not exist: @invalid.', array('@invalid' => implode(", ", $invalid))), 'warning');
  161. }
  162. // If there are no accounts left, display error.
  163. if (empty($accounts)) {
  164. form_set_error('name', t('You are either not allowed to block these users or the users do not exist.'));
  165. }
  166. else {
  167. $form_state['valid_accounts'] = $accounts;
  168. }
  169. }
  170. /**
  171. * Submit callback for block user form.
  172. */
  173. function pm_block_user_block_submit($form, &$form_state) {
  174. global $user;
  175. $insert = db_insert('pm_block_user')->fields(array('author', 'recipient'));
  176. foreach ($form_state['valid_accounts'] as $account) {
  177. $insert->values(array(
  178. 'author' => $account->uid,
  179. 'recipient' => $user->uid,
  180. ));
  181. drupal_set_message(t('!author has been blocked from sending you any further messages.', array('!author' => theme('username', array('account' => $account)))));
  182. }
  183. $insert->execute();
  184. }
  185. /**
  186. * Theme function to theme the blocked user listing.
  187. */
  188. function theme_pm_block_user_list($variables) {
  189. $form = $variables['form'];
  190. return drupal_render_children($form) . theme('table', array('header' => $form['#header'], 'rows' => $form['#rows'])) . theme('pager');
  191. }

Functions

Namesort descending Description
pm_block_user_block_submit Submit callback for block user form.
pm_block_user_block_validate Validate user names.
pm_block_user_form
pm_block_user_form_submit Submit callback for block user confirmation form.
pm_block_user_list Formbuilder function to build a simple form to block users.
theme_pm_block_user_list Theme function to theme the blocked user listing.