You are here

function apachesolr_commentsearch_page_count in Apache Solr Search 6.2

Calculate the page number for the given comment.

Parameters

int $num_comments: Total number of comments.

object $node: The node the comment belong to.

int $cid: The comment's cid.

string $thread: The comment's thread.

Return value

string "page=X" if the page number is greater than zero; NULL otherwise.

See also

comment_render()

1 call to apachesolr_commentsearch_page_count()
apachesolr_commentsearch_apachesolr_search_result_alter in contrib/apachesolr_commentsearch/apachesolr_commentsearch.module

File

contrib/apachesolr_commentsearch/apachesolr_commentsearch.module, line 198

Code

function apachesolr_commentsearch_page_count($num_comments, $node, $cid, $thread) {
  $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
  if ($num_comments <= $comments_per_page) {

    // If only one page of comments, the comment will always be on first page.
    return NULL;
  }
  $mode = _comment_get_display_setting('mode', $node);
  $order = _comment_get_display_setting('sort', $node);

  // Count the number of comments to display before the given comment.
  $query = 'SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d';
  $query_args = array(
    $node->nid,
  );
  if (!user_access('administer comments')) {
    $query .= ' AND c.status = %d';
    $query_args[] = COMMENT_PUBLISHED;
  }
  if ($order == COMMENT_ORDER_NEWEST_FIRST) {
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
      $query .= ' AND c.cid > %d ORDER BY c.cid DESC';
      $query_args[] = $cid;
    }
    else {
      $query .= ' AND c.thread > "%s" ORDER BY c.thread DESC';
      $query_args[] = $thread;
    }
  }
  else {
    if ($order == COMMENT_ORDER_OLDEST_FIRST) {
      if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
        $query .= ' AND c.cid < %d ORDER BY c.cid';
        $query_args[] = $cid;
      }
      else {
        $query .= ' AND SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1)) < "%s" ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
        $query_args[] = $thread;
      }
    }
  }
  $query = db_rewrite_sql($query, 'c', 'cid');
  $count = db_result(db_query($query, $query_args));
  $pageno = $count / $comments_per_page;
  if ($pageno >= 1) {
    return 'page=' . intval($pageno);
  }
  return NULL;
}