Definition

comment_save($edit)
drupal/modules/comment/comment.module, line 755

Description

Accepts a submission of new or changed comment content.

Parameters

$edit A comment array.

Return value

If the comment is successfully saved the comment ID is returned. If the comment is not saved, FALSE is returned.

Code

function comment_save($edit) {
  global $user;
  $node = node_load($edit['nid']);
  if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_READ_WRITE)) {
    if (!form_get_errors()) {
      $edit += array(
        'mail' => '',
        'homepage' => '',
        'name' => '',
        'status' => user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
      );
      if ($edit['cid']) {
        // Update the comment in the database.
        db_update('comment')
          ->fields(array(
            'status' => $edit['status'],
            'timestamp' => $edit['timestamp'],
            'subject' => $edit['subject'],
            'comment' => $edit['comment'],
            'format' => $edit['comment_format'],
            'uid' => $edit['uid'],
            'name' => $edit['name'],
            'mail' => $edit['mail'],
            'homepage' => $edit['homepage']
          ))
          ->condition('cid', $edit['cid'])
          ->execute();
        // Allow modules to respond to the updating of a comment.
        comment_invoke_comment($edit, 'update');
        // Add an entry to the watchdog log.
        watchdog('content', 'Comment: updated %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/' . $edit['nid'], array('fragment' => 'comment-' . $edit['cid'])));
      }
      else {
        // Add the comment to database. This next section builds the thread field.
        // Also see the documentation for comment_render().
        if ($edit['pid'] == 0) {
          // This is a comment with no parent comment (depth 0): we start
          // by retrieving the maximum thread level.
          $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $edit['nid']))->fetchField();
          // Strip the "/" from the end of the thread.
          $max = rtrim($max, '/');
          // Finally, build the thread field for this new comment.
          $thread = int2vancode(vancode2int($max) + 1) . '/';
        }
        else {
          // This is a comment with a parent comment, so increase
          // the part of the thread value at the proper depth.

          // Get the parent comment:
          $parent = comment_load($edit['pid']);
          // Strip the "/" from the end of the parent thread.
          $parent->thread = (string) rtrim((string) $parent->thread, '/');
          // Get the max value in *this* thread.
          $max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array(
            ':thread' => $parent->thread .'.%',
            ':nid' => $edit['nid']))
            ->fetchField();

          if ($max == '') {
            // First child of this parent.
            $thread = $parent->thread . '.' . int2vancode(0) . '/';
          }
          else {
            // Strip the "/" at the end of the thread.
            $max = rtrim($max, '/');
            // Get the value at the correct depth.
            $parts = explode('.', $max);
            $parent_depth = count(explode('.', $parent->thread));
            $last = $parts[$parent_depth];
            // Finally, build the thread field for this new comment.
            $thread = $parent->thread . '.' . int2vancode(vancode2int($last) + 1) . '/';
          }
        }

        if (empty($edit['timestamp'])) {
          $edit['timestamp'] = REQUEST_TIME;
        }

        if ($edit['uid'] === $user->uid && isset($user->name)) { // '===' Need to modify anonymous users as well.
          $edit['name'] = $user->name;
        }

        $edit['cid'] = db_insert('comment')
          ->fields(array(
           'nid' => $edit['nid'],
            'pid' => empty($edit['pid']) ? 0 : $edit['pid'],
            'uid' => $edit['uid'],
            'subject' => $edit['subject'],
            'comment' => $edit['comment'],
            'format' => $edit['comment_format'],
            'hostname' => ip_address(),
            'timestamp' => $edit['timestamp'],
            'status' => $edit['status'],
            'thread' => $thread,
            'name' => $edit['name'],
            'mail' => $edit['mail'],
            'homepage' => $edit['homepage']
          ))
          ->execute();
        // Tell the other modules a new comment has been submitted.
        comment_invoke_comment($edit, 'insert');
        // Add an entry to the watchdog log.
        watchdog('content', 'Comment: added %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/' . $edit['nid'], array('fragment' => 'comment-' . $edit['cid'])));
      }
      _comment_update_node_statistics($edit['nid']);
      // Clear the cache so an anonymous user can see his comment being added.
      cache_clear_all();

      // Explain the approval queue if necessary, and then
      // redirect the user to the node he's commenting on.
      if ($edit['status'] == COMMENT_NOT_PUBLISHED) {
        if (!user_access('administer comments')) {
          drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
        }
      }
      else {
        drupal_set_message(t('Your comment has been posted.'));
        comment_invoke_comment($edit, 'publish');
      }

      return $edit['cid'];
    }
    else {
      return FALSE;
    }
  }
  else {
    watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
    drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject'])), 'error');

    return FALSE;
  }
}