Projet

Général

Profil

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

root / drupal7 / modules / comment / comment.admin.inc @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin page callbacks for the comment module.
6
 */
7

    
8
/**
9
 * Menu callback; present an administrative comment listing.
10
 */
11
function comment_admin($type = 'new') {
12
  $edit = $_POST;
13

    
14
  if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
15
    return drupal_get_form('comment_multiple_delete_confirm');
16
  }
17
  else {
18
    return drupal_get_form('comment_admin_overview', $type);
19
  }
20
}
21

    
22
/**
23
 * Form builder for the comment overview administration form.
24
 *
25
 * @param $arg
26
 *   Current path's fourth component: the type of overview form ('approval' or
27
 *   'new').
28
 *
29
 * @ingroup forms
30
 * @see comment_admin_overview_validate()
31
 * @see comment_admin_overview_submit()
32
 * @see theme_comment_admin_overview()
33
 */
34
function comment_admin_overview($form, &$form_state, $arg) {
35
  // Build an 'Update options' form.
36
  $form['options'] = array(
37
    '#type' => 'fieldset',
38
    '#title' => t('Update options'),
39
    '#attributes' => array('class' => array('container-inline')),
40
  );
41

    
42
  if ($arg == 'approval') {
43
    $options['publish'] = t('Publish the selected comments');
44
  }
45
  else {
46
    $options['unpublish'] = t('Unpublish the selected comments');
47
  }
48
  $options['delete'] = t('Delete the selected comments');
49

    
50
  $form['options']['operation'] = array(
51
    '#type' => 'select',
52
    '#title' => t('Operation'),
53
    '#title_display' => 'invisible',
54
    '#options' => $options,
55
    '#default_value' => 'publish',
56
  );
57
  $form['options']['submit'] = array(
58
    '#type' => 'submit',
59
    '#value' => t('Update'),
60
  );
61

    
62
  // Load the comments that need to be displayed.
63
  $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
64
  $header = array(
65
    'subject' => array('data' => t('Subject'), 'field' => 'subject'),
66
    'author' => array('data' => t('Author'), 'field' => 'name'),
67
    'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
68
    'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc'),
69
    'operations' => array('data' => t('Operations')),
70
  );
71

    
72
  $query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
73
  $query->join('node', 'n', 'n.nid = c.nid');
74
  $query->addField('n', 'title', 'node_title');
75
  $query->addTag('node_access');
76
  $result = $query
77
    ->fields('c', array('cid', 'subject', 'name', 'changed'))
78
    ->condition('c.status', $status)
79
    ->limit(50)
80
    ->orderByHeader($header)
81
    ->execute();
82

    
83
  $cids = array();
84

    
85
  // We collect a sorted list of node_titles during the query to attach to the
86
  // comments later.
87
  foreach ($result as $row) {
88
    $cids[] = $row->cid;
89
    $node_titles[] = $row->node_title;
90
  }
91
  $comments = comment_load_multiple($cids);
92

    
93
  // Build a table listing the appropriate comments.
94
  $options = array();
95
  $destination = drupal_get_destination();
96

    
97
  foreach ($comments as $comment) {
98
    // Remove the first node title from the node_titles array and attach to
99
    // the comment.
100
    $comment->node_title = array_shift($node_titles);
101
    $comment_body = field_get_items('comment', $comment, 'comment_body');
102
    $options[$comment->cid] = array(
103
      'subject' => array(
104
        'data' => array(
105
          '#type' => 'link',
106
          '#title' => $comment->subject,
107
          '#href' => 'comment/' . $comment->cid,
108
          '#options' => array('attributes' => array('title' => truncate_utf8($comment_body[0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
109
        ),
110
      ),
111
      'author' => theme('username', array('account' => $comment)),
112
      'posted_in' => array(
113
        'data' => array(
114
          '#type' => 'link',
115
          '#title' => $comment->node_title,
116
          '#href' => 'node/' . $comment->nid,
117
        ),
118
      ),
119
      'changed' => format_date($comment->changed, 'short'),
120
      'operations' => array(
121
        'data' => array(
122
          '#type' => 'link',
123
          '#title' => t('edit'),
124
          '#href' => 'comment/' . $comment->cid . '/edit',
125
          '#options' => array('query' => $destination),
126
        ),
127
      ),
128
    );
129
  }
130

    
131
  $form['comments'] = array(
132
    '#type' => 'tableselect',
133
    '#header' => $header,
134
    '#options' => $options,
135
    '#empty' => t('No comments available.'),
136
  );
137

    
138
  $form['pager'] = array('#theme' => 'pager');
139

    
140
  return $form;
141
}
142

    
143
/**
144
 * Validate comment_admin_overview form submissions.
145
 */
146
function comment_admin_overview_validate($form, &$form_state) {
147
  $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
148
  // We can't execute any 'Update options' if no comments were selected.
149
  if (count($form_state['values']['comments']) == 0) {
150
    form_set_error('', t('Select one or more comments to perform the update on.'));
151
  }
152
}
153

    
154
/**
155
 * Process comment_admin_overview form submissions.
156
 *
157
 * Execute the chosen 'Update option' on the selected comments, such as
158
 * publishing, unpublishing or deleting.
159
 */
160
function comment_admin_overview_submit($form, &$form_state) {
161
  $operation = $form_state['values']['operation'];
162
  $cids = $form_state['values']['comments'];
163

    
164
  if ($operation == 'delete') {
165
    comment_delete_multiple($cids);
166
  }
167
  else {
168
    foreach ($cids as $cid => $value) {
169
      $comment = comment_load($value);
170

    
171
      if ($operation == 'unpublish') {
172
        $comment->status = COMMENT_NOT_PUBLISHED;
173
      }
174
      elseif ($operation == 'publish') {
175
        $comment->status = COMMENT_PUBLISHED;
176
      }
177
      comment_save($comment);
178
    }
179
  }
180
  drupal_set_message(t('The update has been performed.'));
181
  $form_state['redirect'] = 'admin/content/comment';
182
  cache_clear_all();
183
}
184

    
185
/**
186
 * List the selected comments and verify that the admin wants to delete them.
187
 *
188
 * @param $form_state
189
 *   An associative array containing the current state of the form.
190
 * @return
191
 *   TRUE if the comments should be deleted, FALSE otherwise.
192
 * @ingroup forms
193
 * @see comment_multiple_delete_confirm_submit()
194
 */
195
function comment_multiple_delete_confirm($form, &$form_state) {
196
  $edit = $form_state['input'];
197

    
198
  $form['comments'] = array(
199
    '#prefix' => '<ul>',
200
    '#suffix' => '</ul>',
201
    '#tree' => TRUE,
202
  );
203
  // array_filter() returns only elements with actual values.
204
  $comment_counter = 0;
205
  foreach (array_filter($edit['comments']) as $cid => $value) {
206
    $comment = comment_load($cid);
207
    if (is_object($comment) && is_numeric($comment->cid)) {
208
      $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
209
      $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
210
      $comment_counter++;
211
    }
212
  }
213
  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
214

    
215
  if (!$comment_counter) {
216
    drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
217
    drupal_goto('admin/content/comment');
218
  }
219
  else {
220
    return confirm_form($form,
221
                        t('Are you sure you want to delete these comments and all their children?'),
222
                        'admin/content/comment', t('This action cannot be undone.'),
223
                        t('Delete comments'), t('Cancel'));
224
  }
225
}
226

    
227
/**
228
 * Process comment_multiple_delete_confirm form submissions.
229
 */
230
function comment_multiple_delete_confirm_submit($form, &$form_state) {
231
  if ($form_state['values']['confirm']) {
232
    comment_delete_multiple(array_keys($form_state['values']['comments']));
233
    cache_clear_all();
234
    $count = count($form_state['values']['comments']);
235
    watchdog('content', 'Deleted @count comments.', array('@count' => $count));
236
    drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
237
  }
238
  $form_state['redirect'] = 'admin/content/comment';
239
}
240

    
241
/**
242
 * Page callback for comment deletions.
243
 */
244
function comment_confirm_delete_page($cid) {
245
  if ($comment = comment_load($cid)) {
246
    return drupal_get_form('comment_confirm_delete', $comment);
247
  }
248
  return MENU_NOT_FOUND;
249
}
250

    
251
/**
252
 * Form builder; Builds the confirmation form for deleting a single comment.
253
 *
254
 * @ingroup forms
255
 * @see comment_confirm_delete_submit()
256
 */
257
function comment_confirm_delete($form, &$form_state, $comment) {
258
  $form['#comment'] = $comment;
259
  // Always provide entity id in the same form key as in the entity edit form.
260
  $form['cid'] = array('#type' => 'value', '#value' => $comment->cid);
261
  return confirm_form(
262
    $form,
263
    t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
264
    'node/' . $comment->nid,
265
    t('Any replies to this comment will be lost. This action cannot be undone.'),
266
    t('Delete'),
267
    t('Cancel'),
268
    'comment_confirm_delete');
269
}
270

    
271
/**
272
 * Process comment_confirm_delete form submissions.
273
 */
274
function comment_confirm_delete_submit($form, &$form_state) {
275
  $comment = $form['#comment'];
276
  // Delete the comment and its replies.
277
  comment_delete($comment->cid);
278
  drupal_set_message(t('The comment and all its replies have been deleted.'));
279
  watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
280
  // Clear the cache so an anonymous user sees that his comment was deleted.
281
  cache_clear_all();
282

    
283
  $form_state['redirect'] = "node/$comment->nid";
284
}