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