You are here

function _tableofcontents_comments in Table of Contents 7

Same name and namespace in other branches
  1. 6.3 tableofcontents.pages.inc \_tableofcontents_comments()

This function adds comments to the toc.

Comments follow the table of content.

Parameters

$toc The toc to update:

$format The format used to parse the node comments:

1 call to _tableofcontents_comments()
_tableofcontents_replace_toc in ./tableofcontents.pages.inc
This function replaces the table of contents.

File

./tableofcontents.pages.inc, line 509
Applies the filter functions.

Code

function _tableofcontents_comments(&$toc, $format) {

  // hack to get the $nid...
  if (arg(0) != 'node' || !is_numeric(arg(1))) {
    return;
  }
  $nid = arg(1);
  $node = node_load($nid);
  $mode = _comment_get_display_setting('mode', $node);
  $order = _comment_get_display_setting('sort', $node);
  $comments_per_page = _comment_get_display_setting('comments_per_page', $node);

  // define query
  $query = 'SELECT c.cid, c.subject, c.thread, c.status' . ' FROM {comments} c WHERE c.nid = %d AND c.status = %d';
  $query_count = 'SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d AND c.status = %d';
  $query_args = array(
    $nid,
    COMMENT_PUBLISHED,
  );

  // define ordering
  if ($order == COMMENT_ORDER_NEWEST_FIRST) {
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
      $query .= ' ORDER BY c.cid DESC';
    }
    else {
      $query .= ' ORDER BY c.thread DESC';
    }
  }
  elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
    if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
      $query .= ' ORDER BY c.cid';
    }
    else {
      $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
    }
  }
  $query = db_rewrite_sql($query, 'c', 'cid');
  $query_count = db_rewrite_sql($query_count, 'c', 'cid');
  $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
  $comment_level = variable_get('tableofcontents_comments_level_' . $format, 3);
  while ($comment = db_fetch_object($result)) {
    $level = $comment_level;
    if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
      $level += count(explode('.', $comment->thread)) - 1;
    }
    if ($level < 1 || $level > 6) {
      continue;
    }

    // this is the object we return to generate the TOC
    $h = new TOC_Header();
    $h->level = $level;
    $h->title = check_plain($comment->subject);

    // increase this level and reset all the sub-levels
    ++$toc->counters[$level];
    for ($l = $level + 1; $l <= 6; ++$l) {
      $toc->counters[$l] = 0;
    }
    if ($toc->level_from > $level) {
      $toc->level_from = $level;
    }
    $toc->level_to = $level;

    // min/max adjustment
    if ($toc->header_min) {
      if ($toc->header_min > $level) {
        $toc->header_min = $level;
      }
    }
    else {
      $toc->header_min = $level;
    }
    if ($toc->header_max) {
      if ($toc->header_max < $level) {
        $toc->header_max = $level;
      }
    }
    else {
      $toc->header_max = $level;
    }

    // identifier (as added by the comment module)
    $h->identifier = 'comment-' . $comment->cid;
    $h->attributes = ' id="' . $h->identifier . '"';
    $h->number = theme('tableofcontents_number', $toc);
    $toc->headers[] = $h;
  }
}