Same name and namespace in other branches
  1. 4.6.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 468
Enables users to comment on published content.

Code

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

  // set the breadcrumb trail
  $node = node_load($nid);
  menu_set_location(array(
    array(
      'path' => "node/{$nid}",
      'title' => $node->title,
    ),
    array(
      'path' => "comment/reply/{$nid}",
    ),
  ));
  $op = isset($_POST['op']) ? $_POST['op'] : '';
  $output = '';

  // or are we merely showing the form?
  if (user_access('access comments')) {
    if ($op == t('Preview comment')) {
      if (user_access('post comments')) {
        $output .= comment_form(array(
          'pid' => $pid,
          'nid' => $nid,
        ), NULL);
      }
      else {
        drupal_set_message(t('You are not authorized to post comments.'), 'error');
        drupal_goto("node/{$nid}");
      }
    }
    else {

      // 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) {
        if ($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 = %d', $pid, COMMENT_PUBLISHED))) {
          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 {
          drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
          drupal_goto("node/{$nid}");
        }
      }
      else {
        if (user_access('access content')) {
          $output .= node_view($node);
        }
      }

      // should we show the reply box?
      if (node_comment_mode($nid) != COMMENT_NODE_READ_WRITE) {
        drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
        drupal_goto("node/{$nid}");
      }
      else {
        if (user_access('post comments')) {
          $output .= comment_form(array(
            'pid' => $pid,
            'nid' => $nid,
          ), t('Reply'));
        }
        else {
          drupal_set_message(t('You are not authorized to post comments.'), 'error');
          drupal_goto("node/{$nid}");
        }
      }
    }
  }
  else {
    drupal_set_message(t('You are not authorized to view comments.'), 'error');
    drupal_goto("node/{$nid}");
  }
  return $output;
}