expire > time()) { $comments_pages = $cache->data; } foreach (comment_get_recent() as $comment) { $page = 0; if(isset($comments_pages[$comment->cid])) { $page = $comments_pages[$comment->cid]; } else { // Get the node type for this comment - we cache them // to save a few DB calls $nid = $comment->nid; if(!isset($node_types[$nid])) { $node_types[$nid] = db_result(db_query("SELECT type FROM {node} WHERE nid=%d", $nid)); } $node_type = $node_types[$nid]; if(!isset($node_type)) { $node_type = 'unknown'; } // Now get the number of comments per page for this node type // again, we cache them to save a few variable_get() calls if(!isset($comments_per_page[$node_type])) { $comments_per_page[$node_type] = variable_get('comment_default_per_page_' . $node_type, 10); } $cpp = $comments_per_page[$node_type]; // Get the sort mode... $sort_mode = variable_get('comment_default_mode_'. $node_type, COMMENT_MODE_THREADED_EXPANDED); // ...and the display mode $display_order = variable_get('comment_default_order_'. $node_type, COMMENT_ORDER_NEWEST_FIRST); // This section stolen from comment_render() in comment.module $order = ''; if ($display_order == COMMENT_ORDER_NEWEST_FIRST) { if ($sort_mode == COMMENT_MODE_FLAT_COLLAPSED || $sort_mode == COMMENT_MODE_FLAT_EXPANDED) { $order .= ' ORDER BY cid DESC'; } else { $order .= ' ORDER BY thread DESC'; } } else if ($display_order == COMMENT_ORDER_OLDEST_FIRST) { if ($sort_mode == COMMENT_MODE_FLAT_COLLAPSED || $sort_mode == COMMENT_MODE_FLAT_EXPANDED) { $order .= ' ORDER BY cid'; } else { $order .= ' ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1))'; } } $sql = sprintf("SELECT cid FROM {comments} WHERE nid=%d AND cid<=%d$order", $comment->nid, $comment->cid); $result = db_query("SELECT cid FROM {comments} WHERE nid=%d AND cid<=%d$order", $comment->nid, $comment->cid); $count = 0; while($row = db_fetch_object($result)) { if($row->cid == $comment->cid) { break; } $count++; } //$count = db_result(db_query("SELECT count(1) FROM {comments} WHERE nid=%d AND cid<%d", $comment->nid, $comment->cid)); $page = floor($count / $cpp); // And save it into the cache $comments_pages[$comment->cid] = $page; } // end of if cid was a cache hit...else... // Work out a query string $query = ($page > 0) ? 'page=' . $page : ''; $items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid, 'query' => $query)) .'
'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp))); } // Now save whatever we just got into the cache... // Watch argument ordering!! It changes in Drupal 5->6!! // Expire the data in 24 hours if(DRUPAL_CORE_COMPATIBILITY == '6.x') { cache_set('clever_comments_block', $comments_pages, 'cache', time() + 86400); } else { cache_set('clever_comments_block', 'cache', $comments_pages, time() + 86400); } if ($items) { return theme('item_list', $items); } } echo clever_comment_block(); // The following line is for Vim users - please don't delete it. // vim: set filetype=php expandtab tabstop=2 shiftwidth=2: