1.20.x comment.entity.inc protected CommentStorageController::preSave(EntityInterface $comment)

Overrides EntityDatabaseStorageController::preSave().

Overrides EntityDatabaseStorageController::preSave

See also

comment_int_to_alphadecimal()

comment_increment_alphadecimal()

File

modules/comment/comment.entity.inc, line 258
Entity controller and class for comments.

Class

CommentStorageController
Defines the controller class for comments.

Code

protected function preSave(EntityInterface $comment) {
  global $user;

  if (!isset($comment->status)) {
    $comment->status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
  }
  // Make sure we have a proper bundle name.
  if (!isset($comment->node_type)) {
    $node = node_load($comment->nid);
    $comment->node_type = 'comment_node_' . $node->type;
  }
  if (!$comment->cid) {
    // Add the comment to database. This next section builds the thread field.
    // Also see the documentation for comment_view().
    if (!empty($comment->thread)) {
      // Allow calling code to set thread itself.
      $thread = $comment->thread;
    }
    elseif ($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, '/');
      // We need to get the value at the correct depth.
      $parts = explode('.', $max);
      $firstsegment = $parts[0];
      // Finally, build the thread field for this new comment.
      $thread = comment_increment_alphadecimal($firstsegment) . '/';
    }
    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 . '.' . comment_int_to_alphadecimal(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 . '.' . comment_increment_alphadecimal($last) . '/';
      }
    }
    if (empty($comment->created)) {
      $comment->created = REQUEST_TIME;
    }
    if (empty($comment->changed)) {
      $comment->changed = $comment->created;
    }
    // We test the value with '===' because we need to modify anonymous
    // users as well.
    if ($comment->uid === $user->uid && isset($user->name)) {
      $comment->name = $user->name;
    }
    // Add the values which aren't passed into the function.
    $comment->thread = $thread;
    $comment->hostname = ip_address();
  }
}