[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/modules/comment/ -> comment.admin.inc (source)

   1  <?php
   2  // $Id: comment.admin.inc,v 1.4.2.3 2010/01/28 10:15:11 goba 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   */
  12  function comment_admin($type = 'new') {
  13    $edit = $_POST;
  14  
  15    if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
  16      return drupal_get_form('comment_multiple_delete_confirm');
  17    }
  18    else {
  19      return drupal_get_form('comment_admin_overview', $type, arg(4));
  20    }
  21  }
  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   */
  37  function comment_admin_overview($type = 'new', $arg) {
  38    // build an 'Update options' form
  39    $form['options'] = array(
  40      '#type' => 'fieldset', '#title' => t('Update options'),
  41      '#prefix' => '<div class="container-inline">', '#suffix' => '</div>'
  42    );
  43    $options = array();
  44    foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
  45      $options[$key] = $value[0];
  46    }
  47    $form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish');
  48    $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
  49  
  50    // load the comments that we want to display
  51    $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
  52    $form['header'] = array('#type' => 'value', '#value' => array(
  53      theme('table_select_header_cell'),
  54      array('data' => t('Subject'), 'field' => 'subject'),
  55      array('data' => t('Author'), 'field' => 'name'),
  56      array('data' => t('Posted in'), 'field' => 'node_title'),
  57      array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
  58      array('data' => t('Operations'))
  59    ));
  60    $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);
  61  
  62    // build a table listing the appropriate comments
  63    $destination = drupal_get_destination();
  64    while ($comment = db_fetch_object($result)) {
  65      $comments[$comment->cid] = '';
  66      $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  67      $form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-'. $comment->cid)));
  68      $form['username'][$comment->cid] = array('#value' => theme('username', $comment));
  69      $form['node_title'][$comment->cid] = array('#value' => l($comment->node_title, 'node/'. $comment->nid));
  70      $form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
  71      $form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination)));
  72    }
  73    $form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array());
  74    $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
  75    return $form;
  76  }
  77  
  78  /**
  79   * Validate comment_admin_overview form submissions.
  80   *
  81   * We can't execute any 'Update options' if no comments were selected.
  82   */
  83  function comment_admin_overview_validate($form, &$form_state) {
  84    $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
  85    if (count($form_state['values']['comments']) == 0) {
  86      form_set_error('', t('Please select one or more comments to perform the update on.'));
  87    }
  88  }
  89  
  90  /**
  91   * Process comment_admin_overview form submissions.
  92   *
  93   * Execute the chosen 'Update option' on the selected comments, such as
  94   * publishing, unpublishing or deleting.
  95   */
  96  function comment_admin_overview_submit($form, &$form_state) {
  97    $operations = comment_operations();
  98    if (!empty($operations[$form_state['values']['operation']][1])) {
  99      // extract the appropriate database query operation
 100      $query = $operations[$form_state['values']['operation']][1];
 101      foreach ($form_state['values']['comments'] as $cid => $value) {
 102        if ($value) {
 103          // perform the update action, then refresh node statistics
 104          db_query($query, $cid);
 105          $comment = _comment_load($cid);
 106          _comment_update_node_statistics($comment->nid);
 107          // Allow modules to respond to the updating of a comment.
 108          comment_invoke_comment($comment, $form_state['values']['operation']);
 109          // Add an entry to the watchdog log.
 110          watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)));
 111        }
 112      }
 113      cache_clear_all();
 114      drupal_set_message(t('The update has been performed.'));
 115      $form_state['redirect'] = 'admin/content/comment';
 116    }
 117  }
 118  
 119  /**
 120   * Theme the comment admin form.
 121   *
 122   * @param $form
 123   *   An associative array containing the structure of the form.
 124   * @ingroup themeable
 125   */
 126  function theme_comment_admin_overview($form) {
 127    $output = drupal_render($form['options']);
 128    if (isset($form['subject']) && is_array($form['subject'])) {
 129      foreach (element_children($form['subject']) as $key) {
 130        $row = array();
 131        $row[] = drupal_render($form['comments'][$key]);
 132        $row[] = drupal_render($form['subject'][$key]);
 133        $row[] = drupal_render($form['username'][$key]);
 134        $row[] = drupal_render($form['node_title'][$key]);
 135        $row[] = drupal_render($form['timestamp'][$key]);
 136        $row[] = drupal_render($form['operations'][$key]);
 137        $rows[] = $row;
 138      }
 139    }
 140    else {
 141      $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
 142    }
 143  
 144    $output .= theme('table', $form['header']['#value'], $rows);
 145    if ($form['pager']['#value']) {
 146      $output .= drupal_render($form['pager']);
 147    }
 148  
 149    $output .= drupal_render($form);
 150  
 151    return $output;
 152  }
 153  
 154  /**
 155   * List the selected comments and verify that the admin really wants to delete
 156   * them.
 157   *
 158   * @param $form_state
 159   *   An associative array containing the current state of the form.
 160   * @return
 161   *   TRUE if the comments should be deleted, FALSE otherwise.
 162   * @ingroup forms
 163   * @see comment_multiple_delete_confirm_submit()
 164   */
 165  function comment_multiple_delete_confirm(&$form_state) {
 166    $edit = $form_state['post'];
 167  
 168    $form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
 169    // array_filter() returns only elements with actual values
 170    $comment_counter = 0;
 171    foreach (array_filter($edit['comments']) as $cid => $value) {
 172      $comment = _comment_load($cid);
 173      if (is_object($comment) && is_numeric($comment->cid)) {
 174        $subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid));
 175        $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) .'</li>');
 176        $comment_counter++;
 177      }
 178    }
 179    $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
 180  
 181    if (!$comment_counter) {
 182      drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.'));
 183      drupal_goto('admin/content/comment');
 184    }
 185    else {
 186      return confirm_form($form,
 187                          t('Are you sure you want to delete these comments and all their children?'),
 188                          'admin/content/comment', t('This action cannot be undone.'),
 189                          t('Delete comments'), t('Cancel'));
 190    }
 191  }
 192  
 193  /**
 194   * Process comment_multiple_delete_confirm form submissions.
 195   *
 196   * Perform the actual comment deletion.
 197   */
 198  function comment_multiple_delete_confirm_submit($form, &$form_state) {
 199    if ($form_state['values']['confirm']) {
 200      foreach ($form_state['values']['comments'] as $cid => $value) {
 201        $comment = _comment_load($cid);
 202        _comment_delete_thread($comment);
 203        _comment_update_node_statistics($comment->nid);
 204      }
 205      cache_clear_all();
 206      drupal_set_message(t('The comments have been deleted.'));
 207    }
 208    $form_state['redirect'] = 'admin/content/comment';
 209  }
 210  
 211  /**
 212   * Menu callback; delete a comment.
 213   *
 214   * @param $cid
 215   *   The comment do be deleted.
 216   */
 217  function comment_delete($cid = NULL) {
 218    $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));
 219    $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
 220  
 221    $output = '';
 222  
 223    if (is_object($comment) && is_numeric($comment->cid)) {
 224      $output = drupal_get_form('comment_confirm_delete', $comment);
 225    }
 226    else {
 227      drupal_set_message(t('The comment no longer exists.'));
 228    }
 229  
 230    return $output;
 231  }
 232  
 233  /**
 234   * Form builder; Builds the confirmation form for deleting a single comment.
 235   *
 236   * @ingroup forms
 237   * @see comment_confirm_delete_submit()
 238   */
 239  function comment_confirm_delete(&$form_state, $comment) {
 240    $form = array();
 241    $form['#comment'] = $comment;
 242    return confirm_form(
 243      $form,
 244      t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
 245      'node/'. $comment->nid,
 246      t('Any replies to this comment will be lost. This action cannot be undone.'),
 247      t('Delete'),
 248      t('Cancel'),
 249      'comment_confirm_delete');
 250  }
 251  
 252  /**
 253   * Process comment_confirm_delete form submissions.
 254   */
 255  function comment_confirm_delete_submit($form, &$form_state) {
 256    drupal_set_message(t('The comment and all its replies have been deleted.'));
 257  
 258    $comment = $form['#comment'];
 259  
 260    // Delete comment and its replies.
 261    _comment_delete_thread($comment);
 262  
 263    _comment_update_node_statistics($comment->nid);
 264  
 265    // Clear the cache so an anonymous user sees that his comment was deleted.
 266    cache_clear_all();
 267  
 268    $form_state['redirect'] = "node/$comment->nid";
 269  }
 270  
 271  /**
 272   * Perform the actual deletion of a comment and all its replies.
 273   *
 274   * @param $comment
 275   *   An associative array describing the comment to be deleted.
 276   */
 277  function _comment_delete_thread($comment) {
 278    if (!is_object($comment) || !is_numeric($comment->cid)) {
 279      watchdog('content', 'Cannot delete non-existent comment.', array(), WATCHDOG_WARNING);
 280      return;
 281    }
 282  
 283    // Delete the comment:
 284    db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
 285    watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject));
 286  
 287    comment_invoke_comment($comment, 'delete');
 288  
 289    // Delete the comment's replies
 290    $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);
 291    while ($comment = db_fetch_object($result)) {
 292      $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
 293      _comment_delete_thread($comment);
 294    }
 295  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7