You are here

protected function FeedsCommentProcessor::calculateThread in Feeds Comment Processor 7

Calculates the thread value for a comment.

Parameters

stdClass $comment: The comment.

Return value

string The Drupal thread value.

1 call to FeedsCommentProcessor::calculateThread()
FeedsCommentProcessor::entitySave in ./FeedsCommentProcessor.inc

File

./FeedsCommentProcessor.inc, line 429
Contains FeedsCommentProcessor.

Class

FeedsCommentProcessor
Creates comments from feed items.

Code

protected function calculateThread(stdClass $comment) {
  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, '/');

    // 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.
    return int2vancode(vancode2int($firstsegment) + 1) . '/';
  }

  // 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 = 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();

  // First child of this parent.
  if ($max == '') {
    return $parent->thread . '.' . int2vancode(0) . '/';
  }

  // 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.
  return $parent->thread . '.' . int2vancode(vancode2int($last) + 1) . '/';
}