Drupal PHP Cross Reference Content Management Systems

Source: /modules/comment/comment.admin.inc - 283 lines - 9277 bytes - Summary - Text - Print

   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);
  19    }
  20  }
  21  
  22  /**
  23   * Form builder for the comment overview administration form.
  24   *
  25   * @param $arg
  26   *   Current path's fourth component: the type of overview form ('approval' or
  27   *   'new').
  28   *
  29   * @ingroup forms
  30   * @see comment_admin_overview_validate()
  31   * @see comment_admin_overview_submit()
  32   * @see theme_comment_admin_overview()
  33   */
  34  function comment_admin_overview($form, &$form_state, $arg) {
  35    // Build an 'Update options' form.
  36    $form['options'] = array(
  37      '#type' => 'fieldset',
  38      '#title' => t('Update options'),
  39      '#attributes' => array('class' => array('container-inline')),
  40    );
  41  
  42    if ($arg == 'approval') {
  43      $options['publish'] = t('Publish the selected comments');
  44    }
  45    else {
  46      $options['unpublish'] = t('Unpublish the selected comments');
  47    }
  48    $options['delete'] = t('Delete the selected comments');
  49  
  50    $form['options']['operation'] = array(
  51      '#type' => 'select',
  52      '#title' => t('Operation'),
  53      '#title_display' => 'invisible',
  54      '#options' => $options,
  55      '#default_value' => 'publish',
  56    );
  57    $form['options']['submit'] = array(
  58      '#type' => 'submit',
  59      '#value' => t('Update'),
  60    );
  61  
  62    // Load the comments that need to be displayed.
  63    $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
  64    $header = array(
  65      'subject' => array('data' => t('Subject'), 'field' => 'subject'),
  66      'author' => array('data' => t('Author'), 'field' => 'name'),
  67      'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
  68      'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc'),
  69      'operations' => array('data' => t('Operations')),
  70    );
  71  
  72    $query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
  73    $query->join('node', 'n', 'n.nid = c.nid');
  74    $query->addField('n', 'title', 'node_title');
  75    $query->addTag('node_access');
  76    $result = $query
  77      ->fields('c', array('cid', 'subject', 'name', 'changed'))
  78      ->condition('c.status', $status)
  79      ->limit(50)
  80      ->orderByHeader($header)
  81      ->execute();
  82  
  83    $cids = array();
  84  
  85    // We collect a sorted list of node_titles during the query to attach to the
  86    // comments later.
  87    foreach ($result as $row) {
  88      $cids[] = $row->cid;
  89      $node_titles[] = $row->node_title;
  90    }
  91    $comments = comment_load_multiple($cids);
  92  
  93    // Build a table listing the appropriate comments.
  94    $options = array();
  95    $destination = drupal_get_destination();
  96  
  97    foreach ($comments as $comment) {
  98      // Remove the first node title from the node_titles array and attach to
  99      // the comment.
 100      $comment->node_title = array_shift($node_titles);
 101      $options[$comment->cid] = array(
 102        'subject' => array(
 103          'data' => array(
 104            '#type' => 'link',
 105            '#title' => $comment->subject,
 106            '#href' => 'comment/' . $comment->cid,
 107            '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body[LANGUAGE_NONE][0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
 108          ),
 109        ),
 110        'author' => theme('username', array('account' => $comment)),
 111        'posted_in' => array(
 112          'data' => array(
 113            '#type' => 'link',
 114            '#title' => $comment->node_title,
 115            '#href' => 'node/' . $comment->nid,
 116          ),
 117        ),
 118        'changed' => format_date($comment->changed, 'short'),
 119        'operations' => array(
 120          'data' => array(
 121            '#type' => 'link',
 122            '#title' => t('edit'),
 123            '#href' => 'comment/' . $comment->cid . '/edit',
 124            '#options' => array('query' => $destination),
 125          ),
 126        ),
 127      );
 128    }
 129  
 130    $form['comments'] = array(
 131      '#type' => 'tableselect',
 132      '#header' => $header,
 133      '#options' => $options,
 134      '#empty' => t('No comments available.'),
 135    );
 136  
 137    $form['pager'] = array('#theme' => 'pager');
 138  
 139    return $form;
 140  }
 141  
 142  /**
 143   * Validate comment_admin_overview form submissions.
 144   */
 145  function comment_admin_overview_validate($form, &$form_state) {
 146    $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
 147    // We can't execute any 'Update options' if no comments were selected.
 148    if (count($form_state['values']['comments']) == 0) {
 149      form_set_error('', t('Select one or more comments to perform the update on.'));
 150    }
 151  }
 152  
 153  /**
 154   * Process comment_admin_overview form submissions.
 155   *
 156   * Execute the chosen 'Update option' on the selected comments, such as
 157   * publishing, unpublishing or deleting.
 158   */
 159  function comment_admin_overview_submit($form, &$form_state) {
 160    $operation = $form_state['values']['operation'];
 161    $cids = $form_state['values']['comments'];
 162  
 163    if ($operation == 'delete') {
 164      comment_delete_multiple($cids);
 165    }
 166    else {
 167      foreach ($cids as $cid => $value) {
 168        $comment = comment_load($value);
 169  
 170        if ($operation == 'unpublish') {
 171          $comment->status = COMMENT_NOT_PUBLISHED;
 172        }
 173        elseif ($operation == 'publish') {
 174          $comment->status = COMMENT_PUBLISHED;
 175        }
 176        comment_save($comment);
 177      }
 178    }
 179    drupal_set_message(t('The update has been performed.'));
 180    $form_state['redirect'] = 'admin/content/comment';
 181    cache_clear_all();
 182  }
 183  
 184  /**
 185   * List the selected comments and verify that the admin wants to delete them.
 186   *
 187   * @param $form_state
 188   *   An associative array containing the current state of the form.
 189   * @return
 190   *   TRUE if the comments should be deleted, FALSE otherwise.
 191   * @ingroup forms
 192   * @see comment_multiple_delete_confirm_submit()
 193   */
 194  function comment_multiple_delete_confirm($form, &$form_state) {
 195    $edit = $form_state['input'];
 196  
 197    $form['comments'] = array(
 198      '#prefix' => '<ul>',
 199      '#suffix' => '</ul>',
 200      '#tree' => TRUE,
 201    );
 202    // array_filter() returns only elements with actual values.
 203    $comment_counter = 0;
 204    foreach (array_filter($edit['comments']) as $cid => $value) {
 205      $comment = comment_load($cid);
 206      if (is_object($comment) && is_numeric($comment->cid)) {
 207        $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
 208        $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
 209        $comment_counter++;
 210      }
 211    }
 212    $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
 213  
 214    if (!$comment_counter) {
 215      drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
 216      drupal_goto('admin/content/comment');
 217    }
 218    else {
 219      return confirm_form($form,
 220                          t('Are you sure you want to delete these comments and all their children?'),
 221                          'admin/content/comment', t('This action cannot be undone.'),
 222                          t('Delete comments'), t('Cancel'));
 223    }
 224  }
 225  
 226  /**
 227   * Process comment_multiple_delete_confirm form submissions.
 228   */
 229  function comment_multiple_delete_confirm_submit($form, &$form_state) {
 230    if ($form_state['values']['confirm']) {
 231      comment_delete_multiple(array_keys($form_state['values']['comments']));
 232      cache_clear_all();
 233      $count = count($form_state['values']['comments']);
 234      watchdog('content', 'Deleted @count comments.', array('@count' => $count));
 235      drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
 236    }
 237    $form_state['redirect'] = 'admin/content/comment';
 238  }
 239  
 240  /**
 241   * Page callback for comment deletions.
 242   */
 243  function comment_confirm_delete_page($cid) {
 244    if ($comment = comment_load($cid)) {
 245      return drupal_get_form('comment_confirm_delete', $comment);
 246    }
 247    return MENU_NOT_FOUND;
 248  }
 249  
 250  /**
 251   * Form builder; Builds the confirmation form for deleting a single comment.
 252   *
 253   * @ingroup forms
 254   * @see comment_confirm_delete_submit()
 255   */
 256  function comment_confirm_delete($form, &$form_state, $comment) {
 257    $form['#comment'] = $comment;
 258    // Always provide entity id in the same form key as in the entity edit form.
 259    $form['cid'] = array('#type' => 'value', '#value' => $comment->cid);
 260    return confirm_form(
 261      $form,
 262      t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
 263      'node/' . $comment->nid,
 264      t('Any replies to this comment will be lost. This action cannot be undone.'),
 265      t('Delete'),
 266      t('Cancel'),
 267      'comment_confirm_delete');
 268  }
 269  
 270  /**
 271   * Process comment_confirm_delete form submissions.
 272   */
 273  function comment_confirm_delete_submit($form, &$form_state) {
 274    $comment = $form['#comment'];
 275    // Delete the comment and its replies.
 276    comment_delete($comment->cid);
 277    drupal_set_message(t('The comment and all its replies have been deleted.'));
 278    watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
 279    // Clear the cache so an anonymous user sees that his comment was deleted.
 280    cache_clear_all();
 281  
 282    $form_state['redirect'] = "node/$comment->nid";
 283  }

title

Description

title

Description

title

Description

title

title

Body