Projet

Général

Profil

Paste
Télécharger (9,03 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / privatemsg_filter / privatemsg_filter.admin.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin menu callbacks for privatemsg_filter module.
6
 */
7

    
8
/**
9
 * Display a list of tags to administer them.
10
 */
11
function privatemsg_tags_admin() {
12
  drupal_set_title(t('Tags'));
13

    
14
  $header = array(
15
    'tag' => array(
16
      'data'    => t('Tag'),
17
      'field'   => 'tag',
18
      'class'   => array('privatemsg-filter-header-tag'),
19
      'sort'    => 'asc',
20
    ),
21
    'usage' => array(
22
      'data'    => t('Usage'),
23
      'field'   => 'count',
24
      'class'   => array('privatemsg-filter-header-usage'),
25
    ),
26
    'public' => array(
27
      'data'    => t('Public'),
28
      'field'   => 'public',
29
      'class'   => array('privatemsg-filter-header-public'),
30
    ),
31
    'operations' => array(
32
      'data'    => t('Operations'),
33
      'class'   => array('privatemsg-filter-header-operations'),
34
    ),
35
  );
36
  $query = _privatemsg_assemble_query(array('tags', 'privatemsg_filter'))->extend('PagerDefault')->extend('TableSort');
37
  $query
38
    ->orderByHeader($header)
39
    ->limit();
40

    
41
  $rows = array();
42
  foreach ($query->execute() as $tag) {
43
    $row = array();
44
    $row['tag'] = check_plain($tag->tag);
45
    $row['count'] = $tag->count;
46
    $row['public'] = $tag->public ? t('Yes') : '-';
47
    $row['operations'] = l(t('edit'), 'admin/config/messaging/privatemsg/tags/edit/' . $tag->tag_id);
48
    $row['operations'] .= ' ' . l(t('delete'), 'admin/config/messaging/privatemsg/tags/delete/' . $tag->tag_id);
49

    
50
    $rows[] = $row;
51
  }
52

    
53
  if (empty($rows)) {
54
    // Display a message if now tags are available.
55
    $rows[] = array(array('data' => t('No tags available.'), 'colspan' => count($header)));
56
  }
57

    
58
  return theme('table',  array('header' => $header, 'rows' =>  $rows)) . theme('pager');
59
}
60

    
61
/**
62
 * A form to edit existing or create new tags.
63
 *
64
 * @param $form_state Form state array
65
 * @param $tag_id Tag Id if an edit form should be displayed, NULL for a create
66
 *   new tag form.
67
 */
68
function privatemsg_tags_form($form, &$form_state, $tag_id = NULL) {
69
  $form = array();
70

    
71
  if ($tag_id) {
72
    $tag = db_query('SELECT * from {pm_tags} pmt WHERE pmt.tag_id = :tag_id', array(':tag_id' => $tag_id))->fetchAssoc();
73
    $form['tag_id'] = array(
74
      '#value' => $tag_id,
75
      '#type' => 'value',
76
    );
77
    drupal_set_title(t('Edit @tag', array('@tag' => $tag['tag'])));
78
  }
79

    
80
  $form['tag'] = array(
81
    '#title' => t('Name'),
82
    '#type' => 'textfield',
83
    '#default_value' => empty($tag_id) ? NULL : $tag['tag'],
84
  );
85

    
86
  $form['public'] = array(
87
    '#title' => t('Public'),
88
    '#type' => 'checkbox',
89
    '#default_value' => empty($tag_id) ? TRUE : $tag['public'],
90
    '#description' => t('Public tags are visible for all users, private tags are only visible if they use them.'),
91
  );
92

    
93
  $form['submit'] = array(
94
    '#value' => empty($tag_id) ? t('Create tag') : t('Save tag'),
95
    '#type'  => 'submit',
96
  );
97

    
98
  return $form;
99
}
100

    
101
/**
102
 * Form validate function for tags admin page.
103
 *
104
 * Make sure that a tag name is unique.
105
 */
106
function privatemsg_tags_form_validate($form, &$form_state) {
107
  $tag_id = isset($form_state['values']['tag_id']) ? $form_state['values']['tag_id'] : 0;
108
  if ($tag_id) {
109
    // We are editing an existing tag, exclude the current tag from the search.
110
    $exists = db_query("SELECT 1 FROM {pm_tags} WHERE tag = :tag AND tag_id <> :tag_id", array(
111
        ':tag' => $form_state['values']['tag'],
112
        ':tag_id' => $tag_id,
113
    ))->fetchField();
114
  }
115
  else {
116
    $exists = db_query("SELECT 1 FROM {pm_tags} WHERE tag = :tag", array(
117
        ':tag' => $form_state['values']['tag'],
118
    ))->fetchField();
119
  }
120
  if ($exists) {
121
    form_set_error('tag', t('Tag already exists, choose a different name.'));
122
  }
123
}
124

    
125
/**
126
 * Form submit handler for add/edit forms.
127
 */
128
function privatemsg_tags_form_submit($form, &$form_state) {
129
  if (!empty($form_state['values']['tag_id'])) {
130
    // Tag already exists, update the existing tag.
131
    db_update('pm_tags')
132
      ->fields(array(
133
        'tag' => $form_state['values']['tag'],
134
        'public' => $form_state['values']['public'],
135
      ))
136
      ->condition('tag_id', $form_state['values']['tag_id'])
137
      ->execute();
138
    drupal_set_message(t('Tag updated.'));
139
  }
140
  else {
141
    // Tag does not yet exist, create a new one.
142
    db_insert('pm_tags')
143
      ->fields(array(
144
        'tag' => $form_state['values']['tag'],
145
        'public' => $form_state['values']['public'],
146
      ))
147
      ->execute();
148
    drupal_set_message(t('Tag created.'));
149
  }
150
  $form_state['redirect'] = 'admin/config/messaging/privatemsg/tags';
151
}
152

    
153
/**
154
 * Confirmation form for deleting tags.
155
 *
156
 * @param $tag_id Id of the tag that should be deleted.
157
 */
158
function privatemsg_filter_tags_delete($form, &$form_state, $tag_id) {
159
  $form['tag_id'] = array(
160
    '#type' => 'value',
161
    '#value' => $tag_id,
162
  );
163
  return confirm_form($form,
164
    t('Are you sure you want to delete?'),
165
    isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/messaging/privatemsg/tags',
166
    t('This action cannot be undone.'),
167
    t('Delete'),
168
    t('Cancel')
169
  );
170
}
171

    
172
/**
173
 * Submit handler for the confirm delete form.
174
 */
175
function privatemsg_filter_tags_delete_submit($form, &$form_state) {
176
  if ($form_state['values']['confirm']) {
177
    db_delete('pm_tags')
178
      ->condition('tag_id', $form_state['values']['tag_id'])
179
      ->execute();
180
    db_delete('pm_tags_index')
181
      ->condition('tag_id', $form_state['values']['tag_id'])
182
      ->execute();
183

    
184
    drupal_set_message(t('Tag has been deleted'));
185
  }
186
  $form_state['redirect'] = 'admin/config/messaging/privatemsg/tags';
187
}
188

    
189
/**
190
 * Display confirmation message before rebuilding inbox tag.
191
 */
192
function privatemsg_filter_inbox_rebuid_form() {
193
  $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>';
194

    
195
  $form['status'] = array('#markup' => $status);
196
  $form['rebuild'] = array(
197
    '#type' => 'submit',
198
    '#value' => t('Rebuild inbox'),
199
  );
200

    
201
  return $form;
202
}
203

    
204
/**
205
 * Submit callback for inbox rebuild form.
206
 */
207
function privatemsg_filter_inbox_rebuid_form_submit($form, &$form_state) {
208
  $batch = array(
209
    'title' => t('Rebuilding inbox'),
210
    'operations' => array(
211
      array('privatemsg_filter_inbox_rebuild_process', array()),
212
    ),
213
    'finished' => 'privatemsg_filter_inbox_rebuild_finished',
214
    'file' => drupal_get_path('module', 'privatemsg_filter') . '/privatemsg_filter.admin.inc',
215
  );
216
  batch_set($batch);
217
}
218

    
219
/**
220
 * Batch processing function for rebuilding the index.
221
 */
222
function privatemsg_filter_inbox_rebuild_process(&$context) {
223
  // First run, initialize sandbox.
224
  if (!isset($context['sandbox']['current_thread_id'])) {
225
    $context['sandbox']['current_thread_id'] = 0;
226
    // Assume that the thread ids are distributed more or less equally over the
227
    // whole data set. This allows us to calculate the approximate progress.
228
    $context['sandbox']['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
229
    $context['results']['count'] = 0;
230
  }
231

    
232
  // Fetch the 10 next thread_ids.
233
  $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();
234

    
235
  if (!empty($threads)) {
236
    // By limiting this slow query (having condition with 2 depending subqueries)
237
    // on a specific set of threads, this allows us to process the slow having
238
    // part on a relatively small subset of pm_index that can be selected based on
239
    // the thread_id index.
240
    $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";
241
    $result = db_query($sql, array(':threads' => $threads));
242
    foreach ($result as $row) {
243
      $row->uid = $row->recipient;
244
      // $row is an object with uid property, so we pass it to the function as a
245
      // pseudo user object.
246
      privatemsg_filter_add_tags(array($row->thread_id), variable_get('privatemsg_filter_inbox_tag', ''), $row);
247
      $context['results']['count']++;
248
    }
249
    $context['sandbox']['current_thread_id'] = max($threads);
250
  }
251
  // Set #finished based on sandbox.
252
  $context['finished'] = empty($context['sandbox']['max']) ? 1 : ($context['sandbox']['current_thread_id'] / $context['sandbox']['max']);
253
}
254

    
255
/**
256
 * Finished batch callback for index rebuild.
257
 */
258
function privatemsg_filter_inbox_rebuild_finished($success, $results, $operations) {
259
  if ($success) {
260
    drupal_set_message(format_plural($results['count'], 'One thread tagged.', '@count threads tagged.'));
261
  }
262
  else {
263
    drupal_set_message(t('Finished with an error.'), 'error');
264
  };
265
}