comment_save

Versions
4.6
comment_save($id, $edit)
4.7 – 6
comment_save($edit)
7
comment_save($comment)

Accepts a submission of new or changed comment content.

Parameters

$comment A comment object.

▾ 3 functions call comment_save()

comment_approve in modules/comment/comment.pages.inc
Menu callback; publish specified comment.
comment_form_submit in modules/comment/comment.module
Process comment form submissions; prepare the comment, store it, and set a redirection target.
comment_save_action in modules/comment/comment.module
Action to save a comment.

Code

modules/comment/comment.module, line 1244

<?php
function comment_save($comment) {
  global $user;

  $transaction = db_transaction();
  try {
    $defaults =  array(
      'mail' => '',
      'homepage' => '',
      'name' => '',
      'status' => user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
    );
    foreach ($defaults as $key => $default) {
      if (!isset($comment->$key)) {
        $comment->$key = $default;
      }
    }
    // Make sure we have a bundle name.
    if (!isset($comment->node_type)) {
      $node = node_load($comment->nid);
      $comment->node_type = 'comment_node_' . $node->type;
    }

    field_attach_presave('comment', $comment);

    // Allow modules to alter the comment before saving.
    module_invoke_all('comment_presave', $comment);

    if ($comment->cid) {
      // Update the comment in the database.
      db_update('comment')
        ->fields(array(
          'status' => $comment->status,
          'created' => $comment->created,
          'changed' => $comment->changed,
          'subject' => $comment->subject,
          'comment' => $comment->comment,
          'format' => $comment->comment_format,
          'uid' => $comment->uid,
          'name' => $comment->name,
          'mail' => $comment->mail,
          'homepage' => $comment->homepage,
          'language' => $comment->language,
        ))
        ->condition('cid', $comment->cid)
        ->execute();
      field_attach_update('comment', $comment);
      // Allow modules to respond to the updating of a comment.
      module_invoke_all('comment_update', $comment);
      // Add an entry to the watchdog log.
      watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)));
    }
    else {
      // Add the comment to database. This next section builds the thread field.
      // Also see the documentation for comment_build().
      if ($comment->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' => $comment->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($comment->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' => $comment->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($comment->created)) {
        $comment->created = REQUEST_TIME;
      }

      if (empty($comment->changed)) {
        $comment->changed = $comment->created;
      }

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

      $comment->cid = db_insert('comment')
        ->fields(array(
          'nid' => $comment->nid,
          'pid' => empty($comment->pid) ? 0 : $comment->pid,
          'uid' => $comment->uid,
          'subject' => $comment->subject,
          'comment' => $comment->comment,
          'format' => $comment->comment_format,
          'hostname' => ip_address(),
          'created' => $comment->created,
          'changed' => $comment->changed,
          'status' => $comment->status,
          'thread' => $thread,
          'name' => $comment->name,
          'mail' => $comment->mail,
          'homepage' => $comment->homepage,
          'language' => $comment->language,
        ))
        ->execute();

      // Ignore slave server temporarily to give time for the
      // saved node to be propagated to the slave.
      db_ignore_slave();

      field_attach_insert('comment', $comment);

      // Tell the other modules a new comment has been submitted.
      module_invoke_all('comment_insert', $comment);
      // Add an entry to the watchdog log.
      watchdog('content', 'Comment: added %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)));
    }
    _comment_update_node_statistics($comment->nid);
    // Clear the cache so an anonymous user can see his comment being added.
    cache_clear_all();

    if ($comment->status == COMMENT_PUBLISHED) {
      module_invoke_all('comment_publish', $comment);
    }
  }
  catch (Exception $e) {
    $transaction->rollback('comment', $e->getMessage(), array(), WATCHDOG_ERROR);
  }
  
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.