privatemsg_filter.admin.inc

  1. 1 privatemsg_filter/privatemsg_filter.admin.inc
  2. 7-1 privatemsg_filter/privatemsg_filter.admin.inc
  3. 7-2 privatemsg_filter/privatemsg_filter.admin.inc
  4. 6-2 privatemsg_filter/privatemsg_filter.admin.inc

Admin menu callbacks for privatemsg_filter module.

File

privatemsg_filter/privatemsg_filter.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin menu callbacks for privatemsg_filter module.
  5. */
  6. /**
  7. * Display a list of tags to administer them.
  8. */
  9. function privatemsg_tags_admin() {
  10. drupal_set_title(t('Tags'));
  11. $header = array(
  12. 'tag' => array(
  13. 'data' => t('Tag'),
  14. 'field' => 'tag',
  15. 'class' => array('privatemsg-filter-header-tag'),
  16. 'sort' => 'asc',
  17. ),
  18. 'usage' => array(
  19. 'data' => t('Usage'),
  20. 'field' => 'count',
  21. 'class' => array('privatemsg-filter-header-usage'),
  22. ),
  23. 'public' => array(
  24. 'data' => t('Public'),
  25. 'field' => 'public',
  26. 'class' => array('privatemsg-filter-header-public'),
  27. ),
  28. 'operations' => array(
  29. 'data' => t('Operations'),
  30. 'class' => array('privatemsg-filter-header-operations'),
  31. ),
  32. );
  33. $query = _privatemsg_assemble_query(array('tags', 'privatemsg_filter'))->extend('PagerDefault')->extend('TableSort');
  34. $query
  35. ->orderByHeader($header)
  36. ->limit();
  37. $rows = array();
  38. foreach ($query->execute() as $tag) {
  39. $row = array();
  40. $row['tag'] = check_plain($tag->tag);
  41. $row['count'] = $tag->count;
  42. $row['public'] = $tag->public ? t('Yes') : '-';
  43. $row['operations'] = l(t('edit'), 'admin/config/messaging/privatemsg/tags/edit/' . $tag->tag_id);
  44. $row['operations'] .= ' ' . l(t('delete'), 'admin/config/messaging/privatemsg/tags/delete/' . $tag->tag_id);
  45. $rows[] = $row;
  46. }
  47. if (empty($rows)) {
  48. // Display a message if now tags are available.
  49. $rows[] = array(array('data' => t('No tags available.'), 'colspan' => count($header)));
  50. }
  51. return theme('table', array('header' => $header, 'rows' => $rows)) . theme('pager');
  52. }
  53. /**
  54. * A form to edit existing or create new tags.
  55. *
  56. * @param $form_state Form state array
  57. * @param $tag_id Tag Id if an edit form should be displayed, NULL for a create
  58. * new tag form.
  59. */
  60. function privatemsg_tags_form($form, &$form_state, $tag_id = NULL) {
  61. $form = array();
  62. if ($tag_id) {
  63. $tag = db_query('SELECT * from {pm_tags} pmt WHERE pmt.tag_id = :tag_id', array(':tag_id' => $tag_id))->fetchAssoc();
  64. $form['tag_id'] = array(
  65. '#value' => $tag_id,
  66. '#type' => 'value',
  67. );
  68. drupal_set_title(t('Edit @tag', array('@tag' => $tag['tag'])));
  69. }
  70. $form['tag'] = array(
  71. '#title' => t('Name'),
  72. '#type' => 'textfield',
  73. '#default_value' => empty($tag_id) ? NULL : $tag['tag'],
  74. );
  75. $form['public'] = array(
  76. '#title' => t('Public'),
  77. '#type' => 'checkbox',
  78. '#default_value' => empty($tag_id) ? TRUE : $tag['public'],
  79. '#description' => t('Public tags are visible for all users, private tags are only visible if they use them.'),
  80. );
  81. $form['submit'] = array(
  82. '#value' => empty($tag_id) ? t('Create tag') : t('Save tag'),
  83. '#type' => 'submit',
  84. );
  85. return $form;
  86. }
  87. /**
  88. * Form validate function for tags admin page.
  89. *
  90. * Make sure that a tag name is unique.
  91. */
  92. function privatemsg_tags_form_validate($form, &$form_state) {
  93. $tag_id = isset($form_state['values']['tag_id']) ? $form_state['values']['tag_id'] : 0;
  94. if ($tag_id) {
  95. // We are editing an existing tag, exclude the current tag from the search.
  96. $exists = db_query("SELECT 1 FROM {pm_tags} WHERE tag = :tag AND tag_id <> :tag_id", array(
  97. ':tag' => $form_state['values']['tag'],
  98. ':tag_id' => $tag_id,
  99. ))->fetchField();
  100. }
  101. else {
  102. $exists = db_query("SELECT 1 FROM {pm_tags} WHERE tag = :tag", array(
  103. ':tag' => $form_state['values']['tag'],
  104. ))->fetchField();
  105. }
  106. if ($exists) {
  107. form_set_error('tag', t('Tag already exists, choose a different name.'));
  108. }
  109. }
  110. /**
  111. * Form submit handler for add/edit forms.
  112. */
  113. function privatemsg_tags_form_submit($form, &$form_state) {
  114. if (!empty($form_state['values']['tag_id'])) {
  115. // Tag already exists, update the existing tag.
  116. db_update('pm_tags')
  117. ->fields(array(
  118. 'tag' => $form_state['values']['tag'],
  119. 'public' => $form_state['values']['public'],
  120. ))
  121. ->condition('tag_id', $form_state['values']['tag_id'])
  122. ->execute();
  123. drupal_set_message(t('Tag updated.'));
  124. }
  125. else {
  126. // Tag does not yet exist, create a new one.
  127. db_insert('pm_tags')
  128. ->fields(array(
  129. 'tag' => $form_state['values']['tag'],
  130. 'public' => $form_state['values']['public'],
  131. ))
  132. ->execute();
  133. drupal_set_message(t('Tag created.'));
  134. }
  135. $form_state['redirect'] = 'admin/config/messaging/privatemsg/tags';
  136. }
  137. /**
  138. * Confirmation form for deleting tags.
  139. *
  140. * @param $tag_id Id of the tag that should be deleted.
  141. */
  142. function privatemsg_filter_tags_delete($form, &$form_state, $tag_id) {
  143. $form['tag_id'] = array(
  144. '#type' => 'value',
  145. '#value' => $tag_id,
  146. );
  147. return confirm_form($form,
  148. t('Are you sure you want to delete?'),
  149. isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/messaging/privatemsg/tags',
  150. t('This action cannot be undone.'),
  151. t('Delete'),
  152. t('Cancel')
  153. );
  154. }
  155. /**
  156. * Submit handler for the confirm delete form.
  157. */
  158. function privatemsg_filter_tags_delete_submit($form, &$form_state) {
  159. if ($form_state['values']['confirm']) {
  160. db_delete('pm_tags')
  161. ->condition('tag_id', $form_state['values']['tag_id'])
  162. ->execute();
  163. db_delete('pm_tags_index')
  164. ->condition('tag_id', $form_state['values']['tag_id'])
  165. ->execute();
  166. drupal_set_message(t('Tag has been deleted'));
  167. }
  168. $form_state['redirect'] = 'admin/config/messaging/privatemsg/tags';
  169. }
  170. /**
  171. * Display confirmation message before rebuilding inbox tag.
  172. */
  173. function privatemsg_filter_inbox_rebuild_form() {
  174. $status = '<p>' . t('Newly sent messages are automatically tagged with a hidden system tag. To also have existing messages show up in the inbox, you need to process these by starting the rebuild process. Rebuilding may take some time if there are many messages. After rebuilding has completed messages will automatically show up in the inbox.') . '</p>';
  175. $form['status'] = array('#markup' => $status);
  176. $form['rebuild'] = array(
  177. '#type' => 'submit',
  178. '#value' => t('Rebuild inbox'),
  179. );
  180. return $form;
  181. }
  182. /**
  183. * Submit callback for inbox rebuild form.
  184. */
  185. function privatemsg_filter_inbox_rebuild_form_submit($form, &$form_state) {
  186. $batch = array(
  187. 'title' => t('Rebuilding inbox'),
  188. 'operations' => array(
  189. array('privatemsg_filter_inbox_rebuild_process', array()),
  190. ),
  191. 'finished' => 'privatemsg_filter_inbox_rebuild_finished',
  192. 'file' => drupal_get_path('module', 'privatemsg_filter') . '/privatemsg_filter.admin.inc',
  193. );
  194. batch_set($batch);
  195. }
  196. /**
  197. * Batch processing function for rebuilding the index.
  198. */
  199. function privatemsg_filter_inbox_rebuild_process(&$context) {
  200. // First run, initialize sandbox.
  201. if (!isset($context['sandbox']['current_thread_id'])) {
  202. $context['sandbox']['current_thread_id'] = 0;
  203. // Assume that the thread ids are distributed more or less equally over the
  204. // whole data set. This allows us to calculate the approximate progress.
  205. $context['sandbox']['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
  206. $context['results']['count'] = 0;
  207. }
  208. // Fetch the 10 next thread_ids.
  209. $threads = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > :thread_id ORDER BY thread_id ASC', 0, 20, array(':thread_id' => $context['sandbox']['current_thread_id']))->fetchCol();
  210. if (!empty($threads)) {
  211. // By limiting this slow query (having condition with 2 depending subqueries)
  212. // on a specific set of threads, this allows us to process the slow having
  213. // part on a relatively small subset of pm_index that can be selected based on
  214. // the thread_id index.
  215. $sql = "SELECT pmi.thread_id, pmi.recipient, pmi.type FROM {pm_index} pmi WHERE thread_id IN (:threads) GROUP BY pmi.thread_id, pmi.recipient, pmi.type HAVING ((SELECT pmf.author FROM {pm_message} pmf WHERE pmf.mid = pmi.thread_id) = pmi.recipient AND pmi.type IN ('user', 'hidden') AND COUNT(pmi.thread_id) > 1) OR (SELECT COUNT(*) FROM {pm_message} pmf INNER JOIN {pm_index} pmif ON (pmf.mid = pmif.mid) WHERE pmif.thread_id = pmi.thread_id AND pmf.author <> pmi.recipient AND pmi.type IN ('user', 'hidden')) > 0";
  216. $result = db_query($sql, array(':threads' => $threads));
  217. foreach ($result as $row) {
  218. $row->uid = $row->recipient;
  219. // $row is an object with uid property, so we pass it to the function as a
  220. // pseudo user object.
  221. privatemsg_filter_add_tags(array($row->thread_id), variable_get('privatemsg_filter_inbox_tag', ''), $row);
  222. $context['results']['count']++;
  223. }
  224. $context['sandbox']['current_thread_id'] = max($threads);
  225. }
  226. // Set #finished based on sandbox.
  227. $context['finished'] = empty($context['sandbox']['max']) ? 1 : ($context['sandbox']['current_thread_id'] / $context['sandbox']['max']);
  228. }
  229. /**
  230. * Finished batch callback for index rebuild.
  231. */
  232. function privatemsg_filter_inbox_rebuild_finished($success, $results, $operations) {
  233. if ($success) {
  234. drupal_set_message(format_plural($results['count'], 'One thread tagged.', '@count threads tagged.'));
  235. }
  236. else {
  237. drupal_set_message(t('Finished with an error.'), 'error');
  238. };
  239. }

Functions

Namesort descending Description
privatemsg_filter_inbox_rebuild_finished Finished batch callback for index rebuild.
privatemsg_filter_inbox_rebuild_form Display confirmation message before rebuilding inbox tag.
privatemsg_filter_inbox_rebuild_form_submit Submit callback for inbox rebuild form.
privatemsg_filter_inbox_rebuild_process Batch processing function for rebuilding the index.
privatemsg_filter_tags_delete Confirmation form for deleting tags.
privatemsg_filter_tags_delete_submit Submit handler for the confirm delete form.
privatemsg_tags_admin Display a list of tags to administer them.
privatemsg_tags_form A form to edit existing or create new tags.
privatemsg_tags_form_submit Form submit handler for add/edit forms.
privatemsg_tags_form_validate Form validate function for tags admin page.