comment.admin.inc 10.5 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Admin page callbacks for the Comment module.
6 7
 */

8
use Drupal\comment\Plugin\Core\Entity\Comment;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11

12
/**
13 14 15 16 17 18 19 20
 * Page callback: Presents an administrative comment listing.
 *
 * @param $type
 *   The type of the overview form ('approval' or 'new'). See
 *   comment_admin_overview() for details.
 *
 * @see comment_menu()
 * @see comment_multiple_delete_confirm()
21 22 23 24 25
 */
function comment_admin($type = 'new') {
  $edit = $_POST;

  if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
26
    return drupal_get_form('comment_multiple_delete_confirm');
27 28
  }
  else {
29
    return drupal_get_form('comment_admin_overview', $type);
30 31 32 33
  }
}

/**
34
 * Form constructor for the comment overview administration form.
35
 *
36
 * @param $arg
37
 *   The type of overview form ('approval' or 'new').
38
 *
39
 * @ingroup forms
40
 * @see comment_admin()
41 42 43
 * @see comment_admin_overview_validate()
 * @see comment_admin_overview_submit()
 * @see theme_comment_admin_overview()
44
 */
45
function comment_admin_overview($form, &$form_state, $arg) {
46
  // Build an 'Update options' form.
47
  $form['options'] = array(
48
    '#type' => 'details',
49
    '#title' => t('Update options'),
50
    '#attributes' => array('class' => array('container-inline')),
51
  );
52 53 54 55 56 57

  if ($arg == 'approval') {
    $options['publish'] = t('Publish the selected comments');
  }
  else {
    $options['unpublish'] = t('Unpublish the selected comments');
58
  }
59 60
  $options['delete'] = t('Delete the selected comments');

61 62
  $form['options']['operation'] = array(
    '#type' => 'select',
63
    '#title' => t('Action'),
64
    '#title_display' => 'invisible',
65 66 67 68 69 70 71
    '#options' => $options,
    '#default_value' => 'publish',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
72

73
  // Load the comments that need to be displayed.
74
  $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
75 76
  $header = array(
    'subject' => array('data' => t('Subject'), 'field' => 'subject'),
77 78 79
    'author' => array('data' => t('Author'), 'field' => 'name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
    'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
    'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
80
    'operations' => t('Operations'),
81 82
  );

83 84
  $query = db_select('comment', 'c')
    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
85
    ->extend('Drupal\Core\Database\Query\TableSortExtender');
86
  $query->join('node_field_data', 'n', 'n.nid = c.nid');
87
  $query->addTag('node_access');
88
  $result = $query
89
    ->fields('c', array('cid', 'nid', 'subject', 'name', 'changed'))
90 91
    ->condition('c.status', $status)
    ->limit(50)
92 93
    ->orderByHeader($header)
    ->execute();
94

95
  $nids = array();
96
  $cids = array();
97

98 99 100
  // We collect a sorted list of node_titles during the query to attach to the
  // comments later.
  foreach ($result as $row) {
101
    $nids[] = $row->nid;
102 103
    $cids[] = $row->cid;
  }
104 105 106
  // Ensure all nodes are statically cached so that we do not have to load them
  // individually when getting their labels below.
  node_load_multiple($nids);
107
  $comments = comment_load_multiple($cids);
108

109
  // Build a table listing the appropriate comments.
110
  $options = array();
111
  $destination = drupal_get_destination();
112

113
  foreach ($comments as $comment) {
114
    // Remove the first node title from the node_titles array and attach to
115
    // the comment.
116
    $node_title = $comment->nid->entity->label();
117
    $options[$comment->id()] = array(
118 119 120
      'subject' => array(
        'data' => array(
          '#type' => 'link',
121 122 123
          '#title' => $comment->subject->value,
          '#href' => 'comment/' . $comment->id(),
          '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body->value, 128)), 'fragment' => 'comment-' . $comment->id()),
124 125
        ),
      ),
126
      'author' => theme('username', array('account' => comment_prepare_author($comment))),
127 128 129
      'posted_in' => array(
        'data' => array(
          '#type' => 'link',
130
          '#title' => $node_title,
131
          '#href' => 'node/' . $comment->nid->target_id,
132 133
        ),
      ),
134
      'changed' => format_date($comment->changed->value, 'short'),
135 136 137 138
    );
    $links = array();
    $links['edit'] = array(
      'title' => t('edit'),
139
      'href' => 'comment/' . $comment->id() . '/edit',
140 141
      'query' => $destination,
    );
142 143 144
    if (module_invoke('translation_entity', 'translate_access', $comment)) {
      $links['translate'] = array(
        'title' => t('translate'),
145
        'href' => 'comment/' . $comment->id() . '/translations',
146 147 148
        'query' => $destination,
      );
    }
149
    $options[$comment->id()]['operations']['data'] = array(
150 151
      '#type' => 'operations',
      '#links' => $links,
152
    );
153
  }
154

155
  $form['comments'] = array(
156 157 158 159
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No comments available.'),
160
  );
161

162
  $form['pager'] = array('#theme' => 'pager');
163

164 165 166 167
  return $form;
}

/**
168 169 170
 * Form validation handler for comment_admin_overview().
 *
 * @see comment_admin_overview_submit()
171 172 173
 */
function comment_admin_overview_validate($form, &$form_state) {
  $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
174
  // We can't execute any 'Update options' if no comments were selected.
175
  if (count($form_state['values']['comments']) == 0) {
176
    form_set_error('', t('Select one or more comments to perform the update on.'));
177 178 179 180
  }
}

/**
181
 * Form submission handler for comment_admin_overview().
182
 *
183
 * Executes the chosen 'Update option' on the selected comments, such as
184
 * publishing, unpublishing or deleting.
185 186
 *
 * @see comment_admin_overview_validate()
187 188
 */
function comment_admin_overview_submit($form, &$form_state) {
189 190 191 192
  $operation = $form_state['values']['operation'];
  $cids = $form_state['values']['comments'];

  if ($operation == 'delete') {
193
    entity_delete_multiple('comment', $cids);
194 195 196 197 198 199
  }
  else {
    foreach ($cids as $cid => $value) {
      $comment = comment_load($value);

      if ($operation == 'unpublish') {
200
        $comment->status->value = COMMENT_NOT_PUBLISHED;
201
      }
202
      elseif ($operation == 'publish') {
203
        $comment->status->value = COMMENT_PUBLISHED;
204
      }
205
      $comment->save();
206 207
    }
  }
208 209
  drupal_set_message(t('The update has been performed.'));
  $form_state['redirect'] = 'admin/content/comment';
210
  cache_invalidate_tags(array('content' => TRUE));
211 212 213
}

/**
214
 * Form constructor for the confirmation form for bulk comment deletion.
215 216
 *
 * @ingroup forms
217
 * @see comment_admin()
218
 * @see comment_multiple_delete_confirm_submit()
219
 */
220
function comment_multiple_delete_confirm($form, &$form_state) {
221
  $edit = $form_state['input'];
222

223 224 225 226 227 228
  $form['comments'] = array(
    '#prefix' => '<ul>',
    '#suffix' => '</ul>',
    '#tree' => TRUE,
  );
  // array_filter() returns only elements with actual values.
229 230
  $comment_counter = 0;
  foreach (array_filter($edit['comments']) as $cid => $value) {
231
    $comment = comment_load($cid);
232
    if (is_object($comment) && is_numeric($comment->id())) {
233
      $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
234
      $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
235 236 237 238 239 240
      $comment_counter++;
    }
  }
  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');

  if (!$comment_counter) {
241
    drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
242
    return new RedirectResponse(url('admin/content/comment', array('absolute' => TRUE)));
243 244 245 246
  }
  else {
    return confirm_form($form,
                        t('Are you sure you want to delete these comments and all their children?'),
247
                        'admin/content/comment', t('This action cannot be undone.'),
248 249 250 251 252
                        t('Delete comments'), t('Cancel'));
  }
}

/**
253
 * Form submission handler for comment_multiple_delete_confirm().
254 255 256
 */
function comment_multiple_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
257
    entity_delete_multiple('comment', array_keys($form_state['values']['comments']));
258
    cache_invalidate_tags(array('content' => TRUE));
259 260
    $count = count($form_state['values']['comments']);
    watchdog('content', 'Deleted @count comments.', array('@count' => $count));
261
    drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
262
  }
263
  $form_state['redirect'] = 'admin/content/comment';
264 265
}

266
/**
267 268
 * Page callback: Shows a confirmation page for comment deletions.
 *
269 270
 * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
 *   The comment entity that is about to be deleted.
271 272 273
 *
 * @see comment_menu()
 * @see comment_confirm_delete()
274
 */
275 276
function comment_confirm_delete_page(Comment $comment) {
  return drupal_get_form('comment_confirm_delete', $comment);
277 278
}

279
/**
280 281
 * Form constructor for the confirmation form for comment deletion.
 *
282
 * @param Drupal\comment\Comment $comment
283
 *   The comment that is about to be deleted.
284 285
 *
 * @ingroup forms
286
 * @see comment_confirm_delete_page()
287
 * @see comment_confirm_delete_submit()
288
 * @see confirm_form()
289
 */
290
function comment_confirm_delete($form, &$form_state, Comment $comment) {
291
  $form_state['comment'] = $comment;
292
  // Always provide entity id in the same form key as in the entity edit form.
293
  $form['cid'] = array('#type' => 'value', '#value' => $comment->id());
294 295
  return confirm_form(
    $form,
296
    t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject->value)),
297
    'node/' . $comment->nid->target_id,
298 299 300 301 302 303
    t('Any replies to this comment will be lost. This action cannot be undone.'),
    t('Delete'),
    t('Cancel'),
    'comment_confirm_delete');
}

304
/**
305
 * Form submission handler for comment_confirm_delete().
306
 */
307
function comment_confirm_delete_submit($form, &$form_state) {
308
  $comment = $form_state['comment'];
309
  // Delete the comment and its replies.
310
  $comment->delete();
311
  drupal_set_message(t('The comment and all its replies have been deleted.'));
312
  watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->id()));
313
  // Clear the cache so an anonymous user sees that his comment was deleted.
314
  cache_invalidate_tags(array('content' => TRUE));
315

316
  $form_state['redirect'] = "node/{$comment->nid->target_id}";
317
}