[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7