Code coverage for /20081101/modules/comment/comment.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: comment.admin.inc,v 1.10 2008/07/16 21:59:26 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callbacks for the comment module.
7
 */
8
9
/**
10
 * Menu callback; present an administrative comment listing.
11
 */
1222
function comment_admin($type = 'new') {
1318
  $edit = $_POST;
14
1518
  if (isset($edit['operation']) && ($edit['operation'] == 'delete') &&
isset($edit['comments']) && $edit['comments']) {
162
    return drupal_get_form('comment_multiple_delete_confirm');
170
  }
18
  else {
1916
    return drupal_get_form('comment_admin_overview', $type, arg(4));
20
  }
210
}
22
23
/**
24
 * Form builder; Builds the comment overview form for the admin.
25
 *
26
 * @param $type
27
 *   Not used.
28
 * @param $arg
29
 *   Current path's fourth component deciding the form type (Published
comments/Approval queue).
30
 * @return
31
 *   The form structure.
32
 * @ingroup forms
33
 * @see comment_admin_overview_validate()
34
 * @see comment_admin_overview_submit()
35
 * @see theme_comment_admin_overview()
36
 */
3722
function comment_admin_overview($type = 'new', $arg) {
38
  // Build an 'Update options' form.
3916
  $form['options'] = array(
4016
    '#type' => 'fieldset',
4116
    '#title' => t('Update options'),
4216
    '#prefix' => '<div class="container-inline">',
4316
    '#suffix' => '</div>',
44
  );
4516
  $options = array();
4616
  foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish')
as $key => $value) {
4716
    $options[$key] = $value[0];
4816
  }
4916
  $form['options']['operation'] = array(
5016
    '#type' => 'select',
5116
    '#options' => $options,
5216
    '#default_value' => 'publish',
53
  );
5416
  $form['options']['submit'] = array(
5516
    '#type' => 'submit',
5616
    '#value' => t('Update'),
57
  );
58
59
  // Load the comments that need to be displayed.
6016
  $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED :
COMMENT_PUBLISHED;
6116
  $form['header'] = array(
6216
    '#type' => 'value',
63
    '#value' => array(
6416
      theme('table_select_header_cell'),
6516
      array('data' => t('Subject'), 'field' => 'subject'),
6616
      array('data' => t('Author'), 'field' => 'name'),
6716
      array('data' => t('Posted in'), 'field' => 'node_title'),
6816
      array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
6916
      array('data' => t('Operations')),
7016
  ));
7116
  $result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment,
c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name,
u.uid, n.title as node_title FROM {comments} c INNER JOIN {users} u ON
u.uid = c.uid INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d' .
tablesort_sql($form['header']['#value']), 50, 0, NULL, $status);
72
73
  // Build a table listing the appropriate comments.
7416
  $destination = drupal_get_destination();
7516
  while ($comment = db_fetch_object($result)) {
7616
    $comments[$comment->cid] = '';
7716
    $comment->name = $comment->uid ? $comment->registered_name :
$comment->name;
7816
    $form['subject'][$comment->cid] = array(
7916
      '#markup' => l($comment->subject, 'node/' . $comment->nid,
array('attributes' => array('title' => truncate_utf8($comment->comment,
128)), 'fragment' => 'comment-' . $comment->cid))
8016
    );
8116
    $form['username'][$comment->cid] = array(
8216
      '#markup' => theme('username', $comment)
8316
    );
8416
    $form['node_title'][$comment->cid] = array(
8516
      '#markup' => l($comment->node_title, 'node/' . $comment->nid)
8616
    );
8716
    $form['timestamp'][$comment->cid] = array(
8816
      '#markup' => format_date($comment->timestamp, 'small')
8916
    );
9016
    $form['operations'][$comment->cid] = array(
9116
      '#markup' => l(t('edit'), 'comment/edit/' . $comment->cid,
array('query' => $destination))
9216
    );
9316
  }
9416
  $form['comments'] = array(
9516
    '#type' => 'checkboxes',
9616
    '#options' => isset($comments) ? $comments: array()
9716
  );
9816
  $form['pager'] = array(
9916
    '#markup' => theme('pager', NULL, 50, 0)
10016
  );
101
10216
  return $form;
1030
}
104
105
/**
106
 * Validate comment_admin_overview form submissions.
107
 */
10822
function comment_admin_overview_validate($form, &$form_state) {
1093
  $form_state['values']['comments'] =
array_diff($form_state['values']['comments'], array(0));
110
  // We can't execute any 'Update options' if no comments were selected.
1113
  if (count($form_state['values']['comments']) == 0) {
1120
    form_set_error('', t('Please select one or more comments to perform the
update on.'));
1130
    drupal_goto('admin/content/comment');
1140
  }
1153
}
116
117
/**
118
 * Process comment_admin_overview form submissions.
119
 *
120
 * Execute the chosen 'Update option' on the selected comments, such as
121
 * publishing, unpublishing or deleting.
122
 */
12322
function comment_admin_overview_submit($form, &$form_state) {
1243
  $operations = comment_operations();
1253
  if ($operations[$form_state['values']['operation']][1]) {
126
    // Extract the appropriate database query operation.
1273
    $query = $operations[$form_state['values']['operation']][1];
1283
    foreach ($form_state['values']['comments'] as $cid => $value) {
1293
      if ($value) {
130
        // Perform the update action, then refresh node statistics.
1313
        db_query($query, $cid);
1323
        $comment = comment_load($cid);
1333
        _comment_update_node_statistics($comment->nid);
134
        // Allow modules to respond to the updating of a comment.
1353
        comment_invoke_comment($comment,
$form_state['values']['operation']);
136
        // Add an entry to the watchdog log.
1373
        watchdog('content', 'Comment: updated %subject.', array('%subject'
=> $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/' .
$comment->nid, array('fragment' => 'comment-' . $comment->cid)));
1383
      }
1393
    }
1403
    cache_clear_all();
1413
    drupal_set_message(t('The update has been performed.'));
1423
    $form_state['redirect'] = 'admin/content/comment';
1433
  }
1443
}
145
146
/**
147
 * Theme the comment admin form.
148
 *
149
 * @param $form
150
 *   An associative array containing the structure of the form.
151
 * @ingroup themeable
152
 */
15322
function theme_comment_admin_overview($form) {
15413
  $output = drupal_render($form['options']);
15513
  if (isset($form['subject']) && is_array($form['subject'])) {
15613
    foreach (element_children($form['subject']) as $key) {
15713
      $row = array();
15813
      $row[] = drupal_render($form['comments'][$key]);
15913
      $row[] = drupal_render($form['subject'][$key]);
16013
      $row[] = drupal_render($form['username'][$key]);
16113
      $row[] = drupal_render($form['node_title'][$key]);
16213
      $row[] = drupal_render($form['timestamp'][$key]);
16313
      $row[] = drupal_render($form['operations'][$key]);
16413
      $rows[] = $row;
16513
    }
16613
  }
167
  else {
1680
    $rows[] = array(array('data' => t('No comments available.'), 'colspan'
=> '6'));
169
  }
170
17113
  $output .= theme('table', $form['header']['#value'], $rows);
17213
  if ($form['pager']['#markup']) {
1730
    $output .= drupal_render($form['pager']);
1740
  }
175
17613
  $output .= drupal_render($form);
177
17813
  return $output;
1790
}
180
181
/**
182
 * List the selected comments and verify that the admin wants to delete
them.
183
 *
184
 * @param $form_state
185
 *   An associative array containing the current state of the form.
186
 * @return
187
 *   TRUE if the comments should be deleted, FALSE otherwise.
188
 * @ingroup forms
189
 * @see comment_multiple_delete_confirm_submit()
190
 */
19122
function comment_multiple_delete_confirm(&$form_state) {
1922
  $edit = $form_state['post'];
193
1942
  $form['comments'] = array(
1952
    '#prefix' => '<ul>',
1962
    '#suffix' => '</ul>',
1972
    '#tree' => TRUE,
198
  );
199
  // array_filter() returns only elements with actual values.
2002
  $comment_counter = 0;
2012
  foreach (array_filter($edit['comments']) as $cid => $value) {
2022
    $comment = comment_load($cid);
2032
    if (is_object($comment) && is_numeric($comment->cid)) {
2042
      $subject = db_result(db_query('SELECT subject FROM {comments} WHERE
cid = %d', $cid));
2052
      $form['comments'][$cid] = array('#type' => 'hidden', '#value' =>
$cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
2062
      $comment_counter++;
2072
    }
2082
  }
2092
  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
210
2112
  if (!$comment_counter) {
2120
    drupal_set_message(t('There do not appear to be any comments to delete,
or your selected comment was deleted by another administrator.'));
2130
    drupal_goto('admin/content/comment');
2140
  }
215
  else {
2162
    return confirm_form($form,
2172
                        t('Are you sure you want to delete these comments
and all their children?'),
2182
                        'admin/content/comment', t('This action cannot be
undone.'),
2192
                        t('Delete comments'), t('Cancel'));
220
  }
2210
}
222
223
/**
224
 * Process comment_multiple_delete_confirm form submissions.
225
 */
22622
function comment_multiple_delete_confirm_submit($form, &$form_state) {
2271
  if ($form_state['values']['confirm']) {
2281
    foreach ($form_state['values']['comments'] as $cid => $value) {
2291
      $comment = comment_load($cid);
230
      // Perform the actual comment deletion.
2311
      _comment_delete_thread($comment);
2321
      _comment_update_node_statistics($comment->nid);
2331
    }
2341
    cache_clear_all();
2351
    drupal_set_message(t('The comments have been deleted.'));
2361
  }
2371
  $form_state['redirect'] = 'admin/content/comment';
2381
}
239
240
/**
241
 * Menu callback; delete a comment.
242
 *
243
 * @param $cid
244
 *   The comment to be deleted.
245
 */
24622
function comment_delete($cid = NULL) {
2474
  $comment = db_fetch_object(db_query('SELECT c.*, u.name AS
registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid =
c.uid WHERE c.cid = %d', $cid));
2484
  $comment->name = $comment->uid ? $comment->registered_name :
$comment->name;
2494
  $output = '';
250
2514
  if (is_object($comment) && is_numeric($comment->cid)) {
2524
    $output = drupal_get_form('comment_confirm_delete', $comment);
2532
  }
254
  else {
2550
    drupal_set_message(t('The comment no longer exists.'));
256
  }
257
2582
  return $output;
2590
}
260
261
/**
262
 * Form builder; Builds the confirmation form for deleting a single
comment.
263
 *
264
 * @ingroup forms
265
 * @see comment_confirm_delete_submit()
266
 */
26722
function comment_confirm_delete(&$form_state, $comment) {
2684
  $form = array();
2694
  $form['#comment'] = $comment;
2704
  return confirm_form(
2714
    $form,
2724
    t('Are you sure you want to delete the comment %title?', array('%title'
=> $comment->subject)),
2734
    'node/' . $comment->nid,
2744
    t('Any replies to this comment will be lost. This action cannot be
undone.'),
2754
    t('Delete'),
2764
    t('Cancel'),
2774
    'comment_confirm_delete');
2780
}
279
280
/**
281
 * Process comment_confirm_delete form submissions.
282
 */
28322
function comment_confirm_delete_submit($form, &$form_state) {
2842
  drupal_set_message(t('The comment and all its replies have been
deleted.'));
2852
  $comment = $form['#comment'];
286
  // Delete the comment and its replies.
2872
  _comment_delete_thread($comment);
2882
  _comment_update_node_statistics($comment->nid);
289
  // Clear the cache so an anonymous user sees that his comment was
deleted.
2902
  cache_clear_all();
291
2922
  $form_state['redirect'] = "node/$comment->nid";
2932
}
294
295
/**
296
 * Perform the actual deletion of a comment and all its replies.
297
 *
298
 * @param $comment
299
 *   An associative array describing the comment to be deleted.
300
 */
30122
function _comment_delete_thread($comment) {
3023
  if (!is_object($comment) || !is_numeric($comment->cid)) {
3030
    watchdog('content', 'Cannot delete non-existent comment.', array(),
WATCHDOG_WARNING);
304
3050
    return;
3060
  }
307
308
  // Delete the comment.
3093
  db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
3103
  watchdog('content', 'Comment: deleted %subject.', array('%subject' =>
$comment->subject));
3113
  comment_invoke_comment($comment, 'delete');
312
313
  // Delete the comment's replies.
3143
  $result = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM
{comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE pid = %d',
$comment->cid);
3153
  while ($comment = db_fetch_object($result)) {
3161
    $comment->name = $comment->uid ? $comment->registered_name :
$comment->name;
3171
    _comment_delete_thread($comment);
3181
  }
3193
}
32022