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

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

8
use Drupal\comment\Comment;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10

11
/**
12 13 14 15 16 17 18 19
 * 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()
20 21 22 23 24
 */
function comment_admin($type = 'new') {
  $edit = $_POST;

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

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

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

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

72
  // Load the comments that need to be displayed.
73
  $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
74 75
  $header = array(
    'subject' => array('data' => t('Subject'), 'field' => 'subject'),
76 77 78
    '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)),
79
    'operations' => t('Operations'),
80 81
  );

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

95
  $cids = array();
96

97 98 99 100 101 102 103
  // We collect a sorted list of node_titles during the query to attach to the
  // comments later.
  foreach ($result as $row) {
    $cids[] = $row->cid;
    $node_titles[] = $row->node_title;
  }
  $comments = comment_load_multiple($cids);
104

105
  // Build a table listing the appropriate comments.
106
  $options = array();
107
  $destination = drupal_get_destination();
108

109
  foreach ($comments as $comment) {
110
    // Remove the first node title from the node_titles array and attach to
111 112
    // the comment.
    $comment->node_title = array_shift($node_titles);
113
    $comment_body = field_get_items('comment', $comment, 'comment_body');
114
    $options[$comment->cid] = array(
115 116 117 118 119
      'subject' => array(
        'data' => array(
          '#type' => 'link',
          '#title' => $comment->subject,
          '#href' => 'comment/' . $comment->cid,
120
          '#options' => array('attributes' => array('title' => truncate_utf8($comment_body[0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
121 122
        ),
      ),
123
      'author' => theme('username', array('account' => $comment)),
124 125 126 127 128 129 130
      'posted_in' => array(
        'data' => array(
          '#type' => 'link',
          '#title' => $comment->node_title,
          '#href' => 'node/' . $comment->nid,
        ),
      ),
131
      'changed' => format_date($comment->changed, 'short'),
132 133 134 135 136 137 138 139 140 141
    );
    $links = array();
    $links['edit'] = array(
      'title' => t('edit'),
      'href' => 'comment/' . $comment->cid . '/edit',
      'query' => $destination,
    );
    $options[$comment->cid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
142
    );
143
  }
144

145
  $form['comments'] = array(
146 147 148 149
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No comments available.'),
150
  );
151

152
  $form['pager'] = array('#theme' => 'pager');
153

154 155 156 157
  return $form;
}

/**
158 159 160
 * Form validation handler for comment_admin_overview().
 *
 * @see comment_admin_overview_submit()
161 162 163
 */
function comment_admin_overview_validate($form, &$form_state) {
  $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
164
  // We can't execute any 'Update options' if no comments were selected.
165
  if (count($form_state['values']['comments']) == 0) {
166
    form_set_error('', t('Select one or more comments to perform the update on.'));
167 168 169 170
  }
}

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

  if ($operation == 'delete') {
    comment_delete_multiple($cids);
  }
  else {
    foreach ($cids as $cid => $value) {
      $comment = comment_load($value);

      if ($operation == 'unpublish') {
        $comment->status = COMMENT_NOT_PUBLISHED;
      }
192
      elseif ($operation == 'publish') {
193
        $comment->status = COMMENT_PUBLISHED;
194
      }
195
      comment_save($comment);
196 197
    }
  }
198 199
  drupal_set_message(t('The update has been performed.'));
  $form_state['redirect'] = 'admin/content/comment';
200
  cache_invalidate(array('content' => TRUE));
201 202 203
}

/**
204
 * Form constructor for the confirmation form for bulk comment deletion.
205 206
 *
 * @ingroup forms
207
 * @see comment_admin()
208
 * @see comment_multiple_delete_confirm_submit()
209
 */
210
function comment_multiple_delete_confirm($form, &$form_state) {
211
  $edit = $form_state['input'];
212

213 214 215 216 217 218
  $form['comments'] = array(
    '#prefix' => '<ul>',
    '#suffix' => '</ul>',
    '#tree' => TRUE,
  );
  // array_filter() returns only elements with actual values.
219 220
  $comment_counter = 0;
  foreach (array_filter($edit['comments']) as $cid => $value) {
221
    $comment = comment_load($cid);
222
    if (is_object($comment) && is_numeric($comment->cid)) {
223
      $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
224
      $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
225 226 227 228 229 230
      $comment_counter++;
    }
  }
  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');

  if (!$comment_counter) {
231
    drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
232
    drupal_goto('admin/content/comment');
233 234 235 236
  }
  else {
    return confirm_form($form,
                        t('Are you sure you want to delete these comments and all their children?'),
237
                        'admin/content/comment', t('This action cannot be undone.'),
238 239 240 241 242
                        t('Delete comments'), t('Cancel'));
  }
}

/**
243
 * Form submission handler for comment_multiple_delete_confirm().
244 245 246
 */
function comment_multiple_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
247
    comment_delete_multiple(array_keys($form_state['values']['comments']));
248
    cache_invalidate(array('content' => TRUE));
249 250
    $count = count($form_state['values']['comments']);
    watchdog('content', 'Deleted @count comments.', array('@count' => $count));
251
    drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
252
  }
253
  $form_state['redirect'] = 'admin/content/comment';
254 255
}

256
/**
257 258 259 260 261 262 263
 * Page callback: Shows a confirmation page for comment deletions.
 *
 * @param $cid
 *   The ID of the comment that is about to be deleted.
 *
 * @see comment_menu()
 * @see comment_confirm_delete()
264 265 266 267 268
 */
function comment_confirm_delete_page($cid) {
  if ($comment = comment_load($cid)) {
    return drupal_get_form('comment_confirm_delete', $comment);
  }
269
  throw new NotFoundHttpException();
270 271
}

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

297
/**
298
 * Form submission handler for comment_confirm_delete().
299
 */
300
function comment_confirm_delete_submit($form, &$form_state) {
301
  $comment = $form_state['comment'];
302
  // Delete the comment and its replies.
303 304
  comment_delete($comment->cid);
  drupal_set_message(t('The comment and all its replies have been deleted.'));
305
  watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
306
  // Clear the cache so an anonymous user sees that his comment was deleted.
307
  cache_invalidate(array('content' => TRUE));
308 309 310

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