Same name and namespace in other branches
  1. 4.7.x modules/comment.module \comment_reply()
  2. 5.x modules/comment/comment.module \comment_reply()
  3. 6.x modules/comment/comment.pages.inc \comment_reply()
  4. 7.x modules/comment/comment.pages.inc \comment_reply()

File

modules/comment.module, line 370
Enables users to comment on published content.

Code

function comment_reply($nid, $pid = NULL) {

  // set the breadcrumb trail
  $node = node_load(array(
    'nid' => $nid,
  ));
  menu_set_location(array(
    array(
      'path' => "node/{$nid}",
      'title' => $node->title,
    ),
    array(
      'path' => "comment/reply/{$nid}",
    ),
  ));
  $output = '';

  // are we posting or previewing a reply?
  if ($_POST['op'] == t('Post comment')) {
    $edit = $_POST['edit'];
    $edit = comment_validate_form($edit);
    drupal_set_title(t('Post comment'));
    print theme('page', comment_post($edit));
    return;
  }
  else {
    if ($_POST['op'] == t('Preview comment')) {
      $edit = $_POST['edit'];
      $edit = comment_validate_form($edit);
      drupal_set_title(t('Preview comment'));
      print theme('page', comment_preview($edit));
      return;
    }
  }

  // or are we merely showing the form?
  if (user_access('access comments')) {

    // if this is a reply to another comment, show that comment first
    // else, we'll just show the user the node they're commenting on.
    if ($pid) {
      $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0', $pid));
      if ($comment->nid != $nid) {

        // Attempting to reply to a comment not belonging to the current nid.
        drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
        drupal_goto("node/{$nid}");
      }
      $comment = drupal_unpack($comment);
      $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
      $output .= theme('comment_view', $comment);
    }
    else {
      if (user_access('access content')) {
        $output .= node_view($node);
        $pid = 0;
      }
    }

    // should we show the reply box?
    if (node_comment_mode($nid) != 2) {
      $output .= theme('box', t('Reply'), t("This discussion is closed: you can't post new comments."));
    }
    else {
      if (user_access('post comments')) {
        $output .= theme('comment_form', array(
          'pid' => $pid,
          'nid' => $nid,
        ), t('Reply'));
      }
      else {
        $output .= theme('box', t('Reply'), t('You are not authorized to post comments.'));
      }
    }
  }
  else {
    $output .= theme('box', t('Reply'), t('You are not authorized to view comments.'));
  }
  drupal_set_title(t('Add new comment'));
  print theme('page', $output);
}