'Helper page to mark forums read.',
'page callback' => 'advanced_forum_markasread',
'access callback' => 'advanced_forum_markasread_access',
'type' => MENU_CALLBACK,
);
// Add menu entry for settings page
$items['admin/settings/advanced-forum'] = array(
'title' => 'Advanced Forum',
'page callback' => 'drupal_get_form',
'page arguments' => array('advanced_forum_settings_page'),
'access callback' => 'user_access',
'access arguments' => array('administer advanced forum'),
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function advanced_forum_theme() {
$items = array();
$items['forum_user'] = array(
'template' => 'advf-forum-user',
'path' => drupal_get_path('module', 'advanced_forum') . '/themes/advforum',
'arguments' => array('accountid' => NULL),
);
return $items;
}
/**
* Implementation of hook_theme_registry_alter().
*/
function advanced_forum_theme_registry_alter($theme_registry) {
// Kill the next/previous forum topic navigation links.
foreach ($theme_registry['forum_topic_navigation']['preprocess functions'] as $key => $value) {
if ($value = 'template_preprocess_forum_topic_navigation') {
unset($theme_registry['forum_topic_navigation']['preprocess functions'][$key]);
}
}
// Convince the registry that advforum in the module directory is a theme
// so our templates are found.
$templates = array('node', 'comment', 'forums', 'forum_list', 'forum_topic_list', 'forum_icon', 'forum_submitted');
foreach ($templates as $template) {
$originalpath = array_shift($theme_registry[$template]['theme paths']);
$modulepath = drupal_get_path('module', 'advanced_forum') . '/themes/advforum';
array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}
}
/*****************************************************************************/
/*** SETTINGS PAGE ***********************************************************/
/*****************************************************************************/
/**
* Defines settings form.
*/
function advanced_forum_settings_page() {
$form['advanced_forum_themedir'] = array(
'#type' => 'textfield',
'#title' => t('Advanced forum theme directory'),
'#size' => 25,
'#description' => t('Path from site theme directory to advanced forum theme directory. Defaults to "advforum"'),
'#default_value' => variable_get('advanced_forum_themedir', 'advforum'),
);
$form['advanced_forum_use_supplied_icons'] = array(
'#type' => 'radios',
'#title' => t('Use icons in module directory'),
'#options' => array(t("No"), t("Yes")),
'#description' => t('If you select "No", advanced forum will look for icons in a directory called "images" under the advanced forum theme directory.'),
'#default_value' => variable_get('advanced_forum_theme_all_comments', 1),
);
$form['advanced_forum_theme_all_comments'] = array(
'#type' => 'radios',
'#title' => t('Treat all site comments like forum comments'),
'#options' => array(t("No"), t("Yes")),
'#description' => t('Choosing yes causes advanced forum to consider every comment a forum comment and attempt to theme it that way. Some changes to your theme may be required.'),
'#default_value' => variable_get('advanced_forum_theme_all_comments', 0),
);
// Send our form to Drupal to make a settings page
return system_settings_form($form);
}
/*****************************************************************************/
/*** TEMPLATE PREPROCESS *****************************************************/
/*****************************************************************************/
/**
* Load advanced forum preprocessors includes on behalf of modules.
*/
function _advanced_forum_load_preprocessors() {
static $finished = FALSE;
if ($finished) {
return;
}
// forum.inc is the extension on the includes such as contact.forum.inc
advanced_forum_include('forum.inc');
}
/**
* Load advanced forum files on behalf of modules.
*
* Blatent rip of views include system.
*/
function advanced_forum_include($file) {
$cache = cache_get('advforum_includes', 'cache');
if (isset($cache->data) && $cache->data) {
$includes = unserialize($cache->data);
}
else {
$advanced_forum_path = drupal_get_path('module', 'advanced_forum') . '/modules';
foreach (module_list() as $module) {
$module_path = drupal_get_path('module', $module);
if (file_exists("$module_path/$module.$file")) {
$includes[] = "./$module_path/$module.$file";
}
else if (file_exists("$module_path/includes/$module.$file")) {
$includes[] = "./$module_path/includes/$module.$file";
}
else if (file_exists("$advanced_forum_path/$module.$file")) {
$includes[] = "./$advanced_forum_path/$module.$file";
}
}
cache_set('advforum_includes', 'cache', serialize($includes));
}
foreach ($includes as $include) {
require_once $include;
}
}
/**
* Preprocesses template variables for the node template.
*/
function advanced_forum_preprocess_node(&$vars) {
if (advanced_forum_treat_as_forum_post('node', $vars)) {
_advanced_forum_load_preprocessors();
if ($_GET['page'] > 0) {
// This is the repeated node on the top of subsequent pages.
// We send this to a special .tpl so people can wipe it out or whatever
$vars['template_files'][] = "advf-forum-repeat-post";
}
else {
// Use our combined node/comment template file
$vars['template_files'][] = "advf-forum-post";
}
// The node is the first post, aka topic
$vars['top_post'] = TRUE;
// Node is being shown in the forum (not on the front page or in a view)
$vars['is_forum'] = TRUE;
if (!empty($vars['node']->links) && !empty($vars['node']->links['comment_add'])) {
// Change first post from "add comment" to "Reply"
$vars['node']->links['comment_add']['title'] = t('Reply');
$vars['links'] = theme('links', $vars['node']->links, array('class' => 'links inline forumlinks'));
// Make a separate variable for the reply link so it can be put on top of the thread
$reply_link = $vars['node']->links['comment_add'];
$reply_link['title'] = t("Post Reply");
$vars['reply_link'] = theme('links', array('topic_reply' => $reply_link), array('class' => 'forumlinks'));
}
// Make an array version of $links
$vars['links_array'] = $vars['node']->links;
// Jump to first unread comment
$comment_count = $vars['comment_count'];
if ($comment_count > 0) {
$nid = $vars['node']->nid;
$current_page = $_GET['page'];
$number_new_comments = comment_num_new($nid);
if ($number_new_comments > 0) {
$page_of_first_new = comment_new_page_count($comment_count, $number_new_comments, $vars['node']);
$cid_of_first_new = advanced_forum_first_new_comment($nid);
$link_text = format_plural($number_new_comments, '1 new reply', '@count replies');
$vars['jump_first_new'] = l($link_text,
'node/' . $nid,
array('query' => $page_of_first_new, 'fragment' => "comment-$cid_of_first_new"));
}
}
// User information
$vars['account'] = user_load(array('uid' => $vars['node']->uid));
$vars['user_info_pane'] = theme('forum_user', $vars['account']);
// Load the signature since Drupal doesn't put it on nodes
if (variable_get('user_signatures', 0)) {
$vars['signature'] = check_markup($vars['account']->signature, $vars['node']->format);
}
}
}
/**
* Preprocesses template variables for the comment template.
*/
function advanced_forum_preprocess_comment(&$vars) {
if (advanced_forum_treat_as_forum_post('comment', $vars)) {
// Use our combined node/comment template file
$vars['template_files'][] = 'advf-forum-post';
// Thread is being shown in the forum (not on the front page or in a view)
$vars['is_forum'] = TRUE;
// This is a comment, not the node.
$vars['top_post'] = FALSE;
// Title
if (variable_get('comment_subject_field', 1) == 0) {
// if comment titles are disabled, don't display it.
$vars['title'] = '';
}
else {
// Assign the subject to the title variable for consistancy with nodes.
$vars['title'] = check_plain($vars['comment']->subject);
}
// Just use the date for the submitted on.
$vars['submitted'] = format_date($vars['comment']->timestamp);
// Assign the comment to the content variable for consistancy with nodes.
$vars['content'] = $vars['comment']->comment;
// User information
$accountid = $vars['comment']->uid;
if ($accountid == 0) {
// Anonymous user. Make a fake user object for theme_username
$vars['account']->name = $vars['comment']->name;
$vars['account']->homepage = $vars['comment']->homepage;
}
else {
// Load up the real user object
$vars['account'] = user_load(array('uid' => $vars['comment']->uid));
}
// Create the user info pane
$vars['user_info_pane'] = theme('forum_user', $vars['account']);
// Because the $links array isn't available here, we recreate it
if (arg(1) != 'reply') {
$node = node_load($vars['comment']->nid);
$links = module_invoke_all('link', 'comment', $vars['comment'], 1);
foreach (module_implements('link_alter') as $module) {
$function = $module .'_link_alter';
$function($node, $links);
}
unset($links['comment_parent']);
$vars['links'] = theme('links', $links,array('class' => 'links forumlinks'));
$vars['links_array'] = $links;
}
// Since we have to load the node anyway for our links trick, make it avail
$vars['node'] = $node;
// Comment number with link
if (!isset($post_number)) {
static $post_number = 0;
}
_advanced_forum_topic_nid($vars['node']->nid);
$post_per_page = _comment_get_display_setting('comments_per_page', $vars['node']);
$page_number = $_GET['page'];
if (!$page_number) {
$page_number = 0;
}
$post_number++;
$fragment = 'comment-' . $vars['comment']->cid;
$query = ($page_number) ? 'page=' . $page_number : NULL;
$linktext = '#' . (($page_number * $post_per_page) + $post_number);
$linkpath = 'node/' . _advanced_forum_topic_nid();
$vars['comment_link'] = l($linktext, $linkpath, array('query' => $query, 'fragment' => $fragment));;
// Link to page created by Comment Page module, if it exists
if (!empty($vars['comment']->page_url) && !(arg(0) == 'comment' && arg(1) == $vars['comment']->cid)) {
$vars['page_link'] = l('(permalink)', $vars['comment']->page_url);
}
}
}
/**
* Preprocesses template variables for the user info template.
*/
function template_preprocess_forum_user(&$variables) {
// The passed in $variables['account'] refers to the user who's info is in the pane.
$account = $variables['accountid'];
$accountid = $account->uid;
// Get a reference to the currently logged in user.
global $user;
// Username
$variables['name_raw'] = theme('username', $account);
$variables['name'] = '
' .$variables['name_raw'] . '
';
// Avatar
$variables['picture'] = theme('user_picture', $account);
// Nothing else applies to anon users, so just stop here
if ($accountid == 0) {
return;
}
$themedir = advanced_forum_get_forum_theme_directory();
// Join date / since
$variables['joined_raw'] = format_date($account->created, 'custom', 'Y-m-d');
$variables['joined'] = '' . t('Joined: ') . '' . $variables['joined_raw'] . '
';
$variables['member_since_raw'] = format_interval(time() - $account->created);
$variables['member_since'] = '' . t('Member since: ') . $variables['member_since_raw'] . '
';
// Online status
if (round((time()-$account->access)/60) < 15) {
$variables['online_class'] = 'user-online';
$variables['online_icon'] = theme('image', advanced_forum_get_icon_directory() . 'status_online.png', 'User is online', 'User is online', NULL, TRUE);
$variables['online_text'] = t('Online');
$variables['online_status'] = '' . $variables['online_icon'] . ' ' . $variables['online_text'] . '
';
}
else {
$variables['online_class'] = 'user-offline';
$variables['online_icon'] = theme('image', advanced_forum_get_icon_directory() . 'status_offline.png', 'User is offline', 'User is offline', NULL, TRUE);
$variables['online_text'] = t('Offline');
$variables['online_status'] = '' . $variables['online_icon'] . ' ' . $variables['online_text'] . '
';
}
}
/*****************************************************************************/
/*** FORUM MODULE THEME OVERRIDES ********************************************/
/*****************************************************************************/
/**
* Preprocesses template variables for the forum template.
*/
function advanced_forum_preprocess_forums($variables) {
$variables['template_files'][] = 'advf-forums';
if (empty($variables['topics'])) {
// We don't want the links on the top of the forum overview
$variables['links_orig'] = $variables['links'];
$variables['links'] = array();
}
else {
// Grab the forum description and make it available to the template file
$forum = taxonomy_get_term($variables['tid']);
$variables['forum_description'] = $forum->description;
}
// Add in the mark as read link if user has access
if (advanced_forum_markasread_access()) {
$tid = $variables['tid'];
if ($tid) {
$title = t('Mark all topics read');
$link = "forum/markasread/$tid";
}
else {
$title = t('Mark all forums read');
$link = "forum/markasread";
}
$variables['links']['markasread'] = array('title' => $title, 'href'=>$link);
$variables['links_orig']['markasread'] = array('title' => $title, 'href'=>$link);
}
}
/**
* Preprocesses template variables for the forum list template.
*/
function advanced_forum_preprocess_forum_list(&$variables) {
$variables['template_files'][] = 'advf-forum-list';
// In the forum overview, we want to overwrite the last reply with our custom one
foreach ($variables['forums'] as $tid => $forum) {
$topic = _advanced_forum_get_last_topic($tid);
$variables['forums'][$tid]->last_reply = theme('forum_submitted', $topic);
}
}
/**
* Preprocesses template variables for the submitted by/in template.
*/
function advanced_forum_preprocess_forum_submitted(&$variables) {
$variables['template_files'][] = 'advf-forum-submitted';
// This check makes sure we only do the extra variables when this function is
// called by advforum and not by core forum.
if ($variables['topic']->topic_id) {
$nid = $variables['topic']->topic_id;
// Format the node title with a link
$short_topic_title = truncate_utf8($variables['topic']->topic_title, 15, TRUE, TRUE);
$variables['topic_link'] = l($short_topic_title, "node/" . $variables['topic']->topic_id);
// Format the comment title with a link (if there is a comment)
if (!empty($variables['topic']->reply_title)) {
$short_reply_title = truncate_utf8($variables['topic']->reply_title, 15, TRUE, TRUE);
$comment_fragment = 'comment-' . $variables['topic']->reply_id;
// Figure out the link to the last comment
$fake_node = new stdClass();
$fake_node->type = $variables['topic']->topic_type;
$posts_per_page = _comment_get_display_setting('comments_per_page', $fake_node);
$comment_count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d', $nid));
$last_page = ceil($comment_count / $posts_per_page) - 1;
if ($last_page > 0) {
$pager = 'page=' . $last_page;
}
else {
$pager = NULL;
}
$variables['reply_link'] = l($short_reply_title, "node/" . $variables['topic']->topic_id,
array('query' => $pager, 'fragment' => $comment_fragment));
// Create a linked icon to be used with or instead of the comment title
advanced_forum_preprocess_forum_icon($icon);
$image = theme('image', $icon['iconpath'] . "/newest_comment.gif");
$variables['reply_icon'] = l($image, "node/" . $variables['topic']->topic_id,
array('query' => $pager, 'fragment' => $comment_fragment, 'html' => true));
}
// Format the time ago of the last post (node or comment)
if ($variables['topic']->reply_timestamp > 0) {
$variables['time'] = format_interval(time() - $variables['topic']->reply_timestamp);
}
else {
$variables['time'] = format_interval(time() - $variables['topic']->topic_timestamp);
}
// Format the author of the last post (node or comment)
$author = new stdClass();
if (!empty($variables['topic']->reply_author_id)) {
$author->uid = $variables['topic']->reply_author_id;
$author->name = $variables['topic']->reply_author_name;
}
else {
$author->uid = $variables['topic']->topic_author_id;
$author->name = $variables['topic']->topic_author_name;
}
$variables['author'] = theme('username', $author);
}
}
/**
* Preprocesses template variables for the topic list template.
*/
function advanced_forum_preprocess_forum_topic_list(&$variables) {
// Take control of the template file.
$variables['template_files'][] = 'advf-forum-topic-list';
// Redo the table header so we can add in views. This is mostly copied from
// core with just the views bit stuck in.
global $forum_topic_list_header;
$forum_topic_list_header = array(
array('data' => ' ', 'field' => NULL),
array('data' => t('Topic'), 'field' => 'n.title'),
array('data' => t('Replies'), 'field' => 'l.comment_count'),
array('data' => t('Views'), 'field' => NULL),
array('data' => t('Created'), 'field' => 'n.created'),
array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
);
// Create the tablesorting header.
$ts = tablesort_init($forum_topic_list_header);
$header = '';
foreach ($forum_topic_list_header as $cell) {
$cell = tablesort_header($cell, $forum_topic_list_header, $ts);
$header .= _theme_table_cell($cell, TRUE);
}
$variables['header'] = $header;
// Grab the forum description and make it available to the template file
$forum = taxonomy_get_term($variables['topic_id']);
$variables['forum_description'] = $forum->description;
// Do our own topic processing.
if (!empty($variables['topics'])) {
$row = 0;
// Find out how many pages to show on the topic pager. We do this outside
// the loop because it will be the same for all topics.
$max_pages_to_display = variable_get('advforum_topic_pager_max', 5);
foreach ($variables['topics'] as $id => $topic) {
// Get a pager to add to the topic, if there is one
$variables['topics'][$id]->pager = _advanced_forum_create_topic_pager($max_pages_to_display, $topic);
// Make shadow copy point to actual thread and tell you new the forum name
if ($variables['topics'][$id]->moved == TRUE) {
$term = taxonomy_get_term($topic->tid);
$variables['topics'][$id]->message = l(t('This topic has been moved to ') . $term->name, "node/$topic->nid");
}
// Send the NID into the icon theme code so it can be linked to the topic
$variables['topics'][$id]->icon = theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky, $topic->nid);
// Add in the number of views
$variables['topics'][$id]->views = _advanced_forum_get_topic_views_count($topic->nid);
}
}
}
/**
* Preprocesses template variables for the forum icon template.
*/
function advanced_forum_preprocess_forum_icon(&$variables) {
$variables['template_files'][] = 'advf-forum-icon';
$variables['iconpath'] = advanced_forum_get_icon_directory();
}
// This one is exclusive to advanced forum
function advanced_forum_preprocess_forum_post(&$variables) {
$variables['template_files'][] = 'advf-forum-post';
}
/*****************************************************************************/
/*** THEME RELATED ***********************************************************/
/*****************************************************************************/
/**
* This function returns true if the node/comment should be themed and
* otherwise treated as a forum post.
*/
function advanced_forum_treat_as_forum_post($hook, $vars) {
// Setting this static means the check only needs to be done once per thread
static $forum_node_id;
switch ($hook) {
case 'node':
$vid = variable_get('forum_nav_vocabulary', '');
$vocabulary = taxonomy_vocabulary_load($vid);
if (empty($vocabulary) || !in_array($vars['node']->type, $vocabulary->nodes)) {
// No forum vocabulary or the node type cannot be used in the forums
unset($forum_node_id);
return false;
}
// Get a list of the terms attached to this node
$terms = taxonomy_node_get_terms_by_vocabulary($vars['node'], $vid);
if (count($terms) > 0 && (arg(0) == 'node' && is_numeric(arg(1)) || arg(0) == 'comment')) {
// The node has at least one forum term attached to it and is not being
// shown on some other page (like a view or promoted to front page)
$forum_node_id = $vars['node']->nid;
return true;
}
else {
// We've hit a non forum node
unset($forum_node_id);
return false;
}
case 'comment':
if (isset($forum_node_id) && ($vars['comment']->nid == $forum_node_id)) {
// We already know this comment is either part of a forum thread
// or that comments on this thread are known exceptions.
return true;
}
else {
// Not part of a forum thread. Check for exceptions.
if (variable_get("advanced_forum_theme_all_comments", 0) == 1) {
// This site wants all comments to use the forum comment template
$forum_node_id = $vars['comment']->nid;
return true;
}
if (arg(0) == 'comment' && is_numeric(arg(1))) {
// Comment is being shown alone via the comment_page module
// For now, assume those should be themed like the forums.
// TODO: Possibly add a setting for this?
$forum_node_id = $vars['comment']->nid;
return true;
}
// Comment is not part of a forum thread and there are no exceptions.
return false;
}
default:
// We only deal with nodes and comments
return false;
}
}
/**
* Returns the directory of the forum theme files.
*/
function advanced_forum_get_forum_theme_directory() {
return variable_get('advanced_forum_themedir', 'advforum');
}
/**
* Returns the directory of the forum icon files.
*/
function advanced_forum_get_icon_directory() {
if (variable_get('advanced_forum_use_supplied_icons', 1)) {
return drupal_get_path('module', 'advanced_forum') . '/themes/advforum/images/';
}
else {
// Use the icons in the forum theme
return path_to_theme() . "/" . advanced_forum_get_forum_theme_directory() . "/images/";
}
}
/*****************************************************************************/
/*** LAST UPDATED TOPIC ******************************************************/
/*****************************************************************************/
function _advanced_forum_get_last_topic($tid) {
// Get the most recently created node, in the given forum, that the user has access to
$query = "SELECT n.nid AS topic_id,
n.title AS topic_title,
n.created AS topic_timestamp,
n.type AS topic_type,
n.uid topic_author_id,
u.name AS topic_author_name
FROM {node} n
INNER JOIN {term_node} tn on n.nid = tn.nid
INNER JOIN {users} u on n.uid = u.uid
WHERE tn.tid = $tid AND n.status = 1
ORDER BY n.created DESC";
$node = db_fetch_object(db_query_range(db_rewrite_sql($query), array(), 0, 1));
// If there is at least one node, then we need to look for the most recent comment
if ($node) {
$query = "SELECT c.nid AS topic_id,
c.cid AS reply_id,
c.timestamp AS reply_timestamp,
c.subject AS reply_title,
c.uid topic_author_id,
n.title AS topic_title,
n.created AS topic_timestamp,
n.type AS topic_type,
c.name AS reply_author_name
FROM {comments} c
INNER JOIN {term_node} tn ON c.nid = tn.nid
INNER JOIN {node} n ON c.nid = n.nid
WHERE tn.tid = $tid AND n.status = 1 AND c.status = 0
ORDER BY c.timestamp DESC";
$comment = db_fetch_object(db_query_range(db_rewrite_sql($query,'c','cid'), array(), 0, 1));
if ($node->topic_timestamp >= $comment->reply_timestamp) {
$node->tid = $tid;
$node->reply_id = 0;
return $node;
}
else {
$comment->tid = $tid;
return $comment;
}
}
}
/*****************************************************************************/
/*** MARK AS READ ************************************************************/
/*****************************************************************************/
/**
* Marks all posts in forums or in a given forum as read by the current user.
*/
function advanced_forum_markasread() {
global $user;
// See if we're on a forum or on the forum overview
// Path will be /forum/markasread or /forum/markasread/tid
$current_forum_id = arg(2);
if ($current_forum_id) {
// Combine INSERT with SELECT+SUBSELECT that will return rows that are not
// present in the history table, marking all unread nodes as read
$sql = 'INSERT INTO {history} (uid,nid,timestamp)'
.' SELECT %d, n.nid, %d FROM {node} AS n'
.' INNER JOIN {term_node} AS tn ON n.nid=tn.nid'
.' INNER JOIN {node_comment_statistics} AS ncs ON ncs.nid=n.nid'
.' WHERE (n.created > %d OR ncs.last_comment_timestamp > %d)'
.' AND tn.tid = %d'
.' and n.nid not in ( SELECT nid FROM {history} AS h WHERE h.nid = n.nid AND h.uid = %d)';
$args = array($user->uid, time(), NODE_NEW_LIMIT, NODE_NEW_LIMIT, $current_forum_id, $user->uid);
db_query($sql, $args);
drupal_set_message(t('All content in this forum has been marked as read'));
drupal_goto('forum/'. $current_forum_id);
}
else {
// We are on the forum overview, requesting all forums be marked read
$forum_vocabulary_id = variable_get('forum_nav_vocabulary', '');
// Combine INSERT with SELECT+SUBSELECT that will return rows that are not
// present in the history table, marking all unread nodes as read.
// This version adds another subselect to get all forums
$sql = 'INSERT INTO {history} (uid,nid,timestamp)'
.' SELECT %d, n.nid, %d FROM {node} AS n'
.' INNER JOIN {term_node} AS tn ON n.nid=tn.nid'
.' INNER JOIN {node_comment_statistics} AS ncs ON ncs.nid=n.nid'
.' WHERE (n.created > %d OR ncs.last_comment_timestamp > %d)'
.' and n.nid not in ( SELECT nid FROM {history} AS h WHERE h.nid = n.nid AND h.uid = %d)'
.' AND tn.tid in (SELECT tid FROM {term_data} WHERE vid = %d)';
$args = array($user->uid, time(), NODE_NEW_LIMIT, NODE_NEW_LIMIT, $user->uid, $forum_vocabulary_id);
db_query($sql, $args);
drupal_set_message(t('All forum content been marked as read'));
drupal_goto('forum');
}
}
function advanced_forum_markasread_access() {
// This separate function is needed because the new menu system doesn't run
// hook_menu() every time and the logged in status of the user can get cached
// and re-used for other users.
global $user;
return user_access('access content') && $user->uid;
}
/*****************************************************************************/
/*** GENERAL FUNCTIONS *******************************************************/
/*****************************************************************************/
/**
* Holds the node ID of the thread we are on.
*
* Used for linking the comment numbers.
*
* @param $nid
* Node ID
*/
function _advanced_forum_topic_nid($nodeid = 0) {
static $nid;
if (!empty($nodeid)) {
$nid = $nodeid;
}
return $nid;
}
/**
* Returns the ID of the first unread comment.
*
* @param $nid
* Node ID
* @param $timestamp
* Date/time used to override when the user last viewed the node.
* @return
* Comment ID
*/
function advanced_forum_first_new_comment($nid, $timestamp = 0) {
global $user;
if ($user->uid) {
// Retrieve the timestamp at which the current user last viewed the
// specified node.
if (!$timestamp) {
$timestamp = node_last_viewed($nid);
}
$timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
// Use the timestamp to retrieve the oldest new comment.
$result = db_result(db_query('SELECT c.cid FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d ORDER BY c.cid LIMIT 1', $nid, $timestamp, COMMENT_PUBLISHED));
return $result;
}
else {
return 0;
}
}
/**
* Creates a pager to place on each multi-page topic of the topic listing page.
*
* @param $max_pages_to_display
* Number of pages to include on the pager.
* @param $topic
* Topic object to create a pager for.
* @return
* Asembled pager.
*/
function _advanced_forum_create_topic_pager($max_pages_to_display, $topic) {
// Find the number of comments per page for the node type of the topic.
// It's the same for all types in D5, but varies in D6.
$comments_per_page = _comment_get_display_setting('comments_per_page', $topic);
if ($max_pages_to_display > 0 && $topic->num_comments > $comments_per_page) {
// Topic has more than one page and a pager is wanted. Start off the
// first page because that doesn't have a query.
$pager_array = array();
$current_display_page = 1;
$pager_array[] = l('1', "node/$topic->nid");
// Find the ending point. The pager URL is always 1 less than
// the number being displayed because the first page is 0.
$last_display_page = ceil($topic->num_comments / $comments_per_page);
$last_pager_page = $last_display_page - 1;
// Add pages until we run out or until we hit the max to show.
while (($current_display_page < $last_display_page) && ($current_display_page < $max_pages_to_display)) {
// Move to the next page
$current_display_page++;
// The page number we link to is 1 less than what's displayed
$link_to_page = $current_display_page - 1;
// Add the link to the array
$pager_array[] = l($current_display_page, "node/$topic->nid", array('query' => 'page=' . $link_to_page));
}
// Move to the next page
$current_display_page++;
if ($current_display_page == $last_display_page) {
// We are one past the max to display, but it's the last page,
// so putting the ...last is silly. Just display it normally.
$link_to_page = $current_display_page - 1;
$pager_array[] = l($current_display_page, "node/$topic->nid", array('query' => 'page=' . $link_to_page));
}
$pager_last = '';
if ($current_display_page < $last_display_page) {
// We are one past the max to display and still aren't
// on the last page, so put in ... Last Page(N)
$text = t('Last Page') . '(' . $last_display_page . ')';
$pager_last = ' … ' . l($text, "node/$topic->nid", array('query' => 'page=' . $last_pager_page));
}
// Put it all together
return '[' . t('Page') . ' '. implode(", ", $pager_array) . $pager_last . ']';
}
}
/**
* Retrieves a forum topic's "views count".
*
* @param $nid
* Node ID
* @return
* Total number of times that node has been viewed.
*/
function _advanced_forum_get_topic_views_count($nid) {
if ($nid > 0) {
$views_count = db_result(db_query('SELECT totalcount FROM {node_counter} WHERE nid = %d', $nid));
}
// Make sure it's 0, not blank, for better display.
if (empty($views_count)) {
$views_count = 0;
}
return $views_count;
}