You are here

function advanced_forum_post_position in Advanced Forum 7.2

Same name and namespace in other branches
  1. 6.2 advanced_forum.module \advanced_forum_post_position()

Returns the display position of a given reply post ID on a given node.

1 call to advanced_forum_post_position()
_advanced_forum_preprocess_comment in includes/advanced_forum_preprocess_comment.inc
Preprocess comment.

File

./advanced_forum.module, line 1395
Enables the look and feel of other popular forum software.

Code

function advanced_forum_post_position($node, $comment) {
  static $post_order = array();
  if (empty($node) || empty($comment)) {
    return 0;
  }
  $node_id = $node->nid;
  $post_id = $comment->cid;
  if (!isset($post_order[$node_id])) {

    // Initialize the spot for this node's list.
    $post_order[$node_id] = array();
    $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);

    // Get the list of CIDs from the database in order of oldest first.
    // We are going to make that assumption for now for simplicity but may
    // revisit in the future if there are requests for newest first.
    $query = db_select('comment', 'c')
      ->fields('c', array(
      'cid',
    ))
      ->condition('c.nid', $node_id)
      ->addTag('node_access')
      ->addTag('comment_filter');
    if ($mode === COMMENT_MODE_FLAT) {
      $query
        ->orderBy('c.cid', 'ASC');
    }
    else {
      $query
        ->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
      $query
        ->orderBy('torder', 'ASC');
    }
    $query = $query
      ->execute();

    // Cycle through the results and fill in the array.
    while ($post = $query
      ->fetchAssoc()) {
      $post_order[$node_id][] = reset($post);
    }
  }

  // Find the position of the passed in post ID.
  $post_position = 0;
  if (is_array($post_order[$node_id])) {
    if (($index = array_search($post_id, $post_order[$node_id])) !== FALSE) {
      $post_position = $index;
      $advanced_forum_styled_node_types = variable_get('advanced_forum_styled_node_types', array(
        'forum',
      ));

      // We need to add 1 because the topic node is post #1 on display but is not included in the index.
      if (in_array($node->type, $advanced_forum_styled_node_types)) {
        $post_position = $post_position + 1;
      }
    }
  }
  return $post_position;
}