annotate modules/forum/forum.module @ 20:e3d20ebd63d1 tip

Added tag 6.9 for changeset 3edae6ecd6c6
author Franck Deroche <franck@defr.org>
date Thu, 15 Jan 2009 10:16:10 +0100
parents 4347c45bb494
children
rev   line source
webmaster@1 1 <?php
webmaster@15 2 // $Id: forum.module,v 1.448.2.5 2008/10/24 19:07:47 dries Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Enable threaded discussions about general topics.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Implementation of hook_help().
webmaster@1 11 */
webmaster@1 12 function forum_help($path, $arg) {
webmaster@1 13 switch ($path) {
webmaster@1 14 case 'admin/help#forum':
webmaster@1 15 $output = '<p>'. t('The forum module lets you create threaded discussion forums with functionality similar to other message board systems. Forums are useful because they allow community members to discuss topics with one another while ensuring those conversations are archived for later reference. The <a href="@create-topic">forum topic</a> menu item (under <em>Create content</em> on the Navigation menu) creates the initial post of a new threaded discussion, or thread.', array('@create-topic' => url('node/add/forum'))) .'</p>';
webmaster@1 16 $output .= '<p>'. t('A threaded discussion occurs as people leave comments on a forum topic (or on other comments within that topic). A forum topic is contained within a forum, which may hold many similar or related forum topics. Forums are (optionally) nested within a container, which may hold many similar or related forums. Both containers and forums may be nested within other containers and forums, and provide structure for your message board. By carefully planning this structure, you make it easier for users to find and comment on a specific forum topic.') .'</p>';
webmaster@1 17 $output .= '<p>'. t('When administering a forum, note that:') .'</p>';
webmaster@1 18 $output .= '<ul><li>'. t('a forum topic (and all of its comments) may be moved between forums by selecting a different forum while editing a forum topic.') .'</li>';
webmaster@1 19 $output .= '<li>'. t('when moving a forum topic between forums, the <em>Leave shadow copy</em> option creates a link in the original forum pointing to the new location.') .'</li>';
webmaster@1 20 $output .= '<li>'. t('selecting <em>Read only</em> under <em>Comment settings</em> while editing a forum topic will lock (prevent new comments) on the thread.') .'</li>';
webmaster@1 21 $output .= '<li>'. t('selecting <em>Disabled</em> under <em>Comment settings</em> while editing a forum topic will hide all existing comments on the thread, and prevent new ones.') .'</li></ul>';
webmaster@1 22 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@forum">Forum module</a>.', array('@forum' => 'http://drupal.org/handbook/modules/forum/')) .'</p>';
webmaster@1 23 return $output;
webmaster@1 24 case 'admin/content/forum':
webmaster@1 25 return '<p>'. t('This page displays a list of existing forums and containers. Containers (optionally) hold forums, and forums hold forum topics (a forum topic is the initial post to a threaded discussion). To provide structure, both containers and forums may be placed inside other containers and forums. To rearrange forums and containers, grab a drag-and-drop handle under the <em>Name</em> column and drag the forum or container to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save</em> button at the bottom of the page.') .'</p>';
webmaster@1 26 case 'admin/content/forum/add/container':
webmaster@1 27 return '<p>'. t('By grouping related or similar forums, containers help organize forums. For example, a container named "Food" may hold two forums named "Fruit" and "Vegetables", respectively.') .'</p>';
webmaster@1 28 case 'admin/content/forum/add/forum':
webmaster@1 29 return '<p>'. t('A forum holds related or similar forum topics (a forum topic is the initial post to a threaded discussion). For example, a forum named "Fruit" may contain forum topics titled "Apples" and "Bananas", respectively.') .'</p>';
webmaster@1 30 case 'admin/content/forum/settings':
webmaster@1 31 return '<p>'. t('These settings allow you to adjust the display of your forum topics. The content types available for use within a forum may be selected by editing the <em>Content types</em> on the <a href="@forum-vocabulary">forum vocabulary page</a>.', array('@forum-vocabulary' => url('admin/content/taxonomy/edit/vocabulary/'. variable_get('forum_nav_vocabulary', '')))) .'</p>';
webmaster@1 32 }
webmaster@1 33 }
webmaster@1 34
webmaster@1 35 /**
webmaster@1 36 * Implementation of hook_theme()
webmaster@1 37 */
webmaster@1 38 function forum_theme() {
webmaster@1 39 return array(
webmaster@1 40 'forums' => array(
webmaster@1 41 'template' => 'forums',
webmaster@1 42 'arguments' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
webmaster@1 43 ),
webmaster@1 44 'forum_list' => array(
webmaster@1 45 'template' => 'forum-list',
webmaster@1 46 'arguments' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
webmaster@1 47 ),
webmaster@1 48 'forum_topic_list' => array(
webmaster@1 49 'template' => 'forum-topic-list',
webmaster@1 50 'arguments' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
webmaster@1 51 ),
webmaster@1 52 'forum_icon' => array(
webmaster@1 53 'template' => 'forum-icon',
webmaster@1 54 'arguments' => array('new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0),
webmaster@1 55 ),
webmaster@1 56 'forum_topic_navigation' => array(
webmaster@1 57 'template' => 'forum-topic-navigation',
webmaster@1 58 'arguments' => array('node' => NULL),
webmaster@1 59 ),
webmaster@1 60 'forum_submitted' => array(
webmaster@1 61 'template' => 'forum-submitted',
webmaster@1 62 'arguments' => array('topic' => NULL),
webmaster@1 63 ),
webmaster@1 64 );
webmaster@1 65 }
webmaster@1 66
webmaster@1 67 /**
webmaster@1 68 * Fetch a forum term.
webmaster@1 69 *
webmaster@1 70 * @param $tid
webmaster@1 71 * The ID of the term which should be loaded.
webmaster@1 72 *
webmaster@1 73 * @return
webmaster@1 74 * An associative array containing the term data or FALSE if the term cannot be loaded, or is not part of the forum vocabulary.
webmaster@1 75 */
webmaster@1 76 function forum_term_load($tid) {
webmaster@1 77 $result = db_query(db_rewrite_sql('SELECT t.tid, t.vid, t.name, t.description, t.weight FROM {term_data} t WHERE t.tid = %d AND t.vid = %d', 't', 'tid'), $tid, variable_get('forum_nav_vocabulary', ''));
webmaster@1 78 return db_fetch_array($result);
webmaster@1 79 }
webmaster@1 80
webmaster@1 81 /**
webmaster@1 82 * Implementation of hook_menu().
webmaster@1 83 */
webmaster@1 84 function forum_menu() {
webmaster@1 85 $items['forum'] = array(
webmaster@1 86 'title' => 'Forums',
webmaster@1 87 'page callback' => 'forum_page',
webmaster@1 88 'access arguments' => array('access content'),
webmaster@1 89 'type' => MENU_SUGGESTED_ITEM,
webmaster@1 90 'file' => 'forum.pages.inc',
webmaster@1 91 );
webmaster@1 92 $items['admin/content/forum'] = array(
webmaster@1 93 'title' => 'Forums',
webmaster@1 94 'description' => 'Control forums and their hierarchy and change forum settings.',
webmaster@1 95 'page callback' => 'drupal_get_form',
webmaster@1 96 'page arguments' => array('forum_overview'),
webmaster@1 97 'access arguments' => array('administer forums'),
webmaster@1 98 'file' => 'forum.admin.inc',
webmaster@1 99 );
webmaster@1 100 $items['admin/content/forum/list'] = array(
webmaster@1 101 'title' => 'List',
webmaster@1 102 'type' => MENU_DEFAULT_LOCAL_TASK,
webmaster@1 103 'weight' => -10,
webmaster@1 104 );
webmaster@1 105 $items['admin/content/forum/add/container'] = array(
webmaster@1 106 'title' => 'Add container',
webmaster@1 107 'page callback' => 'forum_form_main',
webmaster@1 108 'page arguments' => array('container'),
webmaster@5 109 'access arguments' => array('administer forums'),
webmaster@1 110 'type' => MENU_LOCAL_TASK,
webmaster@1 111 'parent' => 'admin/content/forum',
webmaster@1 112 'file' => 'forum.admin.inc',
webmaster@1 113 );
webmaster@1 114 $items['admin/content/forum/add/forum'] = array(
webmaster@1 115 'title' => 'Add forum',
webmaster@1 116 'page callback' => 'forum_form_main',
webmaster@1 117 'page arguments' => array('forum'),
webmaster@5 118 'access arguments' => array('administer forums'),
webmaster@1 119 'type' => MENU_LOCAL_TASK,
webmaster@1 120 'parent' => 'admin/content/forum',
webmaster@1 121 'file' => 'forum.admin.inc',
webmaster@1 122 );
webmaster@1 123 $items['admin/content/forum/settings'] = array(
webmaster@1 124 'title' => 'Settings',
webmaster@1 125 'page callback' => 'drupal_get_form',
webmaster@1 126 'page arguments' => array('forum_admin_settings'),
webmaster@5 127 'access arguments' => array('administer forums'),
webmaster@1 128 'weight' => 5,
webmaster@1 129 'type' => MENU_LOCAL_TASK,
webmaster@1 130 'parent' => 'admin/content/forum',
webmaster@1 131 'file' => 'forum.admin.inc',
webmaster@1 132 );
webmaster@1 133 $items['admin/content/forum/edit/%forum_term'] = array(
webmaster@1 134 'page callback' => 'forum_form_main',
webmaster@5 135 'access arguments' => array('administer forums'),
webmaster@1 136 'type' => MENU_CALLBACK,
webmaster@1 137 'file' => 'forum.admin.inc',
webmaster@1 138 );
webmaster@1 139 $items['admin/content/forum/edit/container/%forum_term'] = array(
webmaster@1 140 'title' => 'Edit container',
webmaster@1 141 'page callback' => 'forum_form_main',
webmaster@1 142 'page arguments' => array('container', 5),
webmaster@5 143 'access arguments' => array('administer forums'),
webmaster@1 144 'type' => MENU_CALLBACK,
webmaster@1 145 'file' => 'forum.admin.inc',
webmaster@1 146 );
webmaster@1 147 $items['admin/content/forum/edit/forum/%forum_term'] = array(
webmaster@1 148 'title' => 'Edit forum',
webmaster@1 149 'page callback' => 'forum_form_main',
webmaster@1 150 'page arguments' => array('forum', 5),
webmaster@5 151 'access arguments' => array('administer forums'),
webmaster@1 152 'type' => MENU_CALLBACK,
webmaster@1 153 'file' => 'forum.admin.inc',
webmaster@1 154 );
webmaster@1 155 return $items;
webmaster@1 156 }
webmaster@1 157
webmaster@1 158
webmaster@1 159 /**
webmaster@1 160 * Implementation of hook_init().
webmaster@1 161 */
webmaster@1 162 function forum_init() {
webmaster@1 163 drupal_add_css(drupal_get_path('module', 'forum') .'/forum.css');
webmaster@1 164 }
webmaster@1 165
webmaster@1 166 /**
webmaster@1 167 * Implementation of hook_nodeapi().
webmaster@1 168 */
webmaster@1 169 function forum_nodeapi(&$node, $op, $teaser, $page) {
webmaster@1 170 // We are going to return if $node->type is not one of the node
webmaster@1 171 // types assigned to the forum vocabulary. If forum_nav_vocabulary
webmaster@1 172 // is undefined or the vocabulary does not exist, it clearly cannot
webmaster@1 173 // be assigned to $node->type, so return to avoid E_ALL warnings.
webmaster@1 174 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 175 $vocabulary = taxonomy_vocabulary_load($vid);
webmaster@1 176 if (empty($vocabulary)) {
webmaster@1 177 return;
webmaster@1 178 }
webmaster@1 179
webmaster@1 180 // Operate only on node types assigned for the forum vocabulary.
webmaster@1 181 if (!in_array($node->type, $vocabulary->nodes)) {
webmaster@1 182 return;
webmaster@1 183 }
webmaster@1 184
webmaster@1 185 switch ($op) {
webmaster@1 186 case 'view':
webmaster@1 187 if ($page && taxonomy_node_get_terms_by_vocabulary($node, $vid) && $tree = taxonomy_get_tree($vid)) {
webmaster@1 188 // Get the forum terms from the (cached) tree
webmaster@1 189 foreach ($tree as $term) {
webmaster@1 190 $forum_terms[] = $term->tid;
webmaster@1 191 }
webmaster@1 192 foreach ($node->taxonomy as $term_id => $term) {
webmaster@1 193 if (in_array($term_id, $forum_terms)) {
webmaster@1 194 $node->tid = $term_id;
webmaster@1 195 }
webmaster@1 196 }
webmaster@1 197 // Breadcrumb navigation
webmaster@1 198 $breadcrumb[] = l(t('Home'), NULL);
webmaster@1 199 $breadcrumb[] = l($vocabulary->name, 'forum');
webmaster@1 200 if ($parents = taxonomy_get_parents_all($node->tid)) {
webmaster@1 201 $parents = array_reverse($parents);
webmaster@1 202 foreach ($parents as $p) {
webmaster@1 203 $breadcrumb[] = l($p->name, 'forum/'. $p->tid);
webmaster@1 204 }
webmaster@1 205 }
webmaster@1 206 drupal_set_breadcrumb($breadcrumb);
webmaster@1 207
webmaster@1 208 if (!$teaser) {
webmaster@1 209 $node->content['forum_navigation'] = array(
webmaster@1 210 '#value' => theme('forum_topic_navigation', $node),
webmaster@1 211 '#weight' => 100,
webmaster@1 212 );
webmaster@1 213 }
webmaster@1 214 }
webmaster@1 215 break;
webmaster@1 216
webmaster@1 217 case 'prepare':
webmaster@1 218 if (empty($node->nid)) {
webmaster@1 219 // New topic
webmaster@1 220 $node->taxonomy[arg(3)]->vid = $vid;
webmaster@1 221 $node->taxonomy[arg(3)]->tid = arg(3);
webmaster@1 222 }
webmaster@1 223 break;
webmaster@1 224
webmaster@1 225 // Check in particular that only a "leaf" term in the associated taxonomy
webmaster@1 226 // vocabulary is selected, not a "container" term.
webmaster@1 227 case 'validate':
webmaster@1 228 if ($node->taxonomy) {
webmaster@1 229 // Extract the node's proper topic ID.
webmaster@1 230 $vocabulary = $vid;
webmaster@1 231 $containers = variable_get('forum_containers', array());
webmaster@1 232 foreach ($node->taxonomy as $term) {
webmaster@1 233 if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
webmaster@1 234 if (in_array($term, $containers)) {
webmaster@1 235 $term = taxonomy_get_term($term);
webmaster@1 236 form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
webmaster@1 237 }
webmaster@1 238 }
webmaster@1 239 }
webmaster@1 240 }
webmaster@1 241 break;
webmaster@1 242
webmaster@1 243 // Assign forum taxonomy when adding a topic from within a forum.
webmaster@1 244 case 'presave':
webmaster@1 245 // Make sure all fields are set properly:
webmaster@1 246 $node->icon = !empty($node->icon) ? $node->icon : '';
webmaster@1 247
webmaster@1 248 if ($node->taxonomy && $tree = taxonomy_get_tree($vid)) {
webmaster@1 249 // Get the forum terms from the (cached) tree if we have a taxonomy.
webmaster@1 250 foreach ($tree as $term) {
webmaster@1 251 $forum_terms[] = $term->tid;
webmaster@1 252 }
webmaster@1 253 foreach ($node->taxonomy as $term_id) {
webmaster@1 254 if (in_array($term_id, $forum_terms)) {
webmaster@1 255 $node->tid = $term_id;
webmaster@1 256 }
webmaster@1 257 }
webmaster@1 258 $old_tid = db_result(db_query_range("SELECT t.tid FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.nid = %d ORDER BY t.vid DESC", $node->nid, 0, 1));
webmaster@1 259 if ($old_tid && isset($node->tid) && ($node->tid != $old_tid) && !empty($node->shadow)) {
webmaster@1 260 // A shadow copy needs to be created. Retain new term and add old term.
webmaster@1 261 $node->taxonomy[] = $old_tid;
webmaster@1 262 }
webmaster@1 263 }
webmaster@1 264 break;
webmaster@1 265
webmaster@1 266 case 'update':
webmaster@1 267 if (empty($node->revision) && db_result(db_query('SELECT tid FROM {forum} WHERE nid=%d', $node->nid))) {
webmaster@1 268 if (!empty($node->tid)) {
webmaster@1 269 db_query('UPDATE {forum} SET tid = %d WHERE vid = %d', $node->tid, $node->vid);
webmaster@1 270 }
webmaster@1 271 // The node is removed from the forum.
webmaster@1 272 else {
webmaster@1 273 db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
webmaster@1 274 }
webmaster@1 275 break;
webmaster@1 276 }
webmaster@1 277 // Deliberate no break -- for new revisions and for previously unassigned terms we need an insert.
webmaster@1 278
webmaster@1 279 case 'insert':
webmaster@1 280 if (!empty($node->tid)) {
webmaster@1 281 db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)', $node->tid, $node->vid, $node->nid);
webmaster@1 282 }
webmaster@1 283 break;
webmaster@1 284
webmaster@1 285 case 'delete':
webmaster@1 286 db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
webmaster@1 287 break;
webmaster@1 288
webmaster@1 289 case 'load':
webmaster@1 290 return db_fetch_array(db_query('SELECT tid AS forum_tid FROM {forum} WHERE vid = %d', $node->vid));
webmaster@1 291 }
webmaster@1 292
webmaster@1 293 return;
webmaster@1 294 }
webmaster@1 295
webmaster@1 296 /**
webmaster@1 297 * Implementation of hook_node_info().
webmaster@1 298 */
webmaster@1 299 function forum_node_info() {
webmaster@1 300 return array(
webmaster@1 301 'forum' => array(
webmaster@1 302 'name' => t('Forum topic'),
webmaster@1 303 'module' => 'forum',
webmaster@1 304 'description' => t('A <em>forum topic</em> is the initial post to a new discussion thread within a forum.'),
webmaster@1 305 'title_label' => t('Subject'),
webmaster@1 306 )
webmaster@1 307 );
webmaster@1 308 }
webmaster@1 309
webmaster@1 310 /**
webmaster@1 311 * Implementation of hook_access().
webmaster@1 312 */
webmaster@1 313 function forum_access($op, $node, $account) {
webmaster@1 314 switch ($op) {
webmaster@1 315 case 'create':
webmaster@7 316 return user_access('create forum topics', $account) ? TRUE : NULL;
webmaster@1 317 case 'update':
webmaster@7 318 return user_access('edit any forum topic', $account) || (user_access('edit own forum topics', $account) && ($account->uid == $node->uid)) ? TRUE : NULL;
webmaster@1 319 case 'delete':
webmaster@7 320 return user_access('delete any forum topic', $account) || (user_access('delete own forum topics', $account) && ($account->uid == $node->uid)) ? TRUE : NULL;
webmaster@1 321 }
webmaster@1 322 }
webmaster@1 323
webmaster@1 324 /**
webmaster@1 325 * Implementation of hook_perm().
webmaster@1 326 */
webmaster@1 327 function forum_perm() {
webmaster@1 328 return array('create forum topics', 'delete own forum topics', 'delete any forum topic', 'edit own forum topics', 'edit any forum topic', 'administer forums');
webmaster@1 329 }
webmaster@1 330
webmaster@1 331 /**
webmaster@1 332 * Implementation of hook_taxonomy().
webmaster@1 333 */
webmaster@1 334 function forum_taxonomy($op, $type, $term = NULL) {
webmaster@1 335 if ($op == 'delete' && $term['vid'] == variable_get('forum_nav_vocabulary', '')) {
webmaster@1 336 switch ($type) {
webmaster@1 337 case 'term':
webmaster@1 338 $results = db_query('SELECT tn.nid FROM {term_node} tn WHERE tn.tid = %d', $term['tid']);
webmaster@1 339 while ($node = db_fetch_object($results)) {
webmaster@1 340 // node_delete will also remove any association with non-forum vocabularies.
webmaster@1 341 node_delete($node->nid);
webmaster@1 342 }
webmaster@1 343
webmaster@1 344 // For containers, remove the tid from the forum_containers variable.
webmaster@1 345 $containers = variable_get('forum_containers', array());
webmaster@1 346 $key = array_search($term['tid'], $containers);
webmaster@1 347 if ($key !== FALSE) {
webmaster@1 348 unset($containers[$key]);
webmaster@1 349 }
webmaster@1 350 variable_set('forum_containers', $containers);
webmaster@1 351 break;
webmaster@1 352 case 'vocabulary':
webmaster@1 353 variable_del('forum_nav_vocabulary');
webmaster@1 354 }
webmaster@1 355 }
webmaster@1 356 }
webmaster@1 357
webmaster@1 358 /**
webmaster@1 359 * Implementation of hook_form_alter().
webmaster@1 360 */
webmaster@1 361 function forum_form_alter(&$form, $form_state, $form_id) {
webmaster@1 362 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 363 if (isset($form['vid']) && $form['vid']['#value'] == $vid) {
webmaster@1 364 // Hide critical options from forum vocabulary.
webmaster@1 365 if ($form_id == 'taxonomy_form_vocabulary') {
webmaster@1 366 $form['help_forum_vocab'] = array(
webmaster@1 367 '#value' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
webmaster@1 368 '#weight' => -1,
webmaster@1 369 );
webmaster@1 370 $form['content_types']['nodes']['#required'] = TRUE;
webmaster@1 371 $form['hierarchy'] = array('#type' => 'value', '#value' => 1);
webmaster@1 372 $form['settings']['required'] = array('#type' => 'value', '#value' => FALSE);
webmaster@1 373 $form['settings']['relations'] = array('#type' => 'value', '#value' => FALSE);
webmaster@1 374 $form['settings']['tags'] = array('#type' => 'value', '#value' => FALSE);
webmaster@1 375 $form['settings']['multiple'] = array('#type' => 'value', '#value' => FALSE);
webmaster@1 376 unset($form['delete']);
webmaster@1 377 }
webmaster@1 378 // Hide multiple parents select from forum terms.
webmaster@1 379 elseif ($form_id == 'taxonomy_form_term') {
webmaster@1 380 $form['advanced']['parent']['#access'] = FALSE;
webmaster@15 381 }
webmaster@1 382 }
webmaster@1 383 if ($form_id == 'forum_node_form') {
webmaster@1 384 // Make the vocabulary required for 'real' forum-nodes.
webmaster@1 385 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 386 $form['taxonomy'][$vid]['#required'] = TRUE;
webmaster@1 387 $form['taxonomy'][$vid]['#options'][''] = t('- Please choose -');
webmaster@1 388 }
webmaster@1 389 }
webmaster@1 390
webmaster@1 391 /**
webmaster@1 392 * Implementation of hook_load().
webmaster@1 393 */
webmaster@1 394 function forum_load($node) {
webmaster@1 395 $forum = db_fetch_object(db_query('SELECT * FROM {term_node} WHERE vid = %d', $node->vid));
webmaster@1 396
webmaster@1 397 return $forum;
webmaster@1 398 }
webmaster@1 399
webmaster@1 400 /**
webmaster@1 401 * Implementation of hook_block().
webmaster@1 402 *
webmaster@1 403 * Generates a block containing the currently active forum topics and the
webmaster@1 404 * most recently added forum topics.
webmaster@1 405 */
webmaster@1 406 function forum_block($op = 'list', $delta = 0, $edit = array()) {
webmaster@1 407 switch ($op) {
webmaster@1 408 case 'list':
webmaster@1 409 $blocks[0]['info'] = t('Active forum topics');
webmaster@1 410 $blocks[1]['info'] = t('New forum topics');
webmaster@1 411 return $blocks;
webmaster@1 412
webmaster@1 413 case 'configure':
webmaster@1 414 $form['forum_block_num_'. $delta] = array('#type' => 'select', '#title' => t('Number of topics'), '#default_value' => variable_get('forum_block_num_'. $delta, '5'), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
webmaster@1 415 return $form;
webmaster@1 416
webmaster@1 417 case 'save':
webmaster@1 418 variable_set('forum_block_num_'. $delta, $edit['forum_block_num_'. $delta]);
webmaster@1 419 break;
webmaster@1 420
webmaster@1 421 case 'view':
webmaster@1 422 if (user_access('access content')) {
webmaster@1 423 switch ($delta) {
webmaster@1 424 case 0:
webmaster@1 425 $title = t('Active forum topics');
webmaster@1 426 $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count, l.last_comment_timestamp FROM {node} n INNER JOIN {term_node} tn ON tn.vid = n.vid INNER JOIN {term_data} td ON td.tid = tn.tid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND td.vid = %d ORDER BY l.last_comment_timestamp DESC");
webmaster@1 427 $result = db_query_range($sql, variable_get('forum_nav_vocabulary', ''), 0, variable_get('forum_block_num_0', '5'));
webmaster@1 428 $content = node_title_list($result);
webmaster@1 429 break;
webmaster@1 430
webmaster@1 431 case 1:
webmaster@1 432 $title = t('New forum topics');
webmaster@1 433 $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n INNER JOIN {term_node} tn ON tn.vid = n.vid INNER JOIN {term_data} td ON td.tid = tn.tid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND td.vid = %d ORDER BY n.nid DESC");
webmaster@1 434 $result = db_query_range($sql, variable_get('forum_nav_vocabulary', ''), 0, variable_get('forum_block_num_1', '5'));
webmaster@1 435 $content = node_title_list($result);
webmaster@1 436 break;
webmaster@1 437 }
webmaster@1 438
webmaster@1 439 if (!empty($content)) {
webmaster@1 440 $block['subject'] = $title;
webmaster@1 441 $block['content'] = $content . theme('more_link', url('forum'), t('Read the latest forum topics.'));
webmaster@1 442 return $block;
webmaster@1 443 }
webmaster@1 444 }
webmaster@1 445 }
webmaster@1 446 }
webmaster@1 447
webmaster@1 448 /**
webmaster@1 449 * Implementation of hook_form().
webmaster@1 450 */
webmaster@1 451 function forum_form(&$node, $form_state) {
webmaster@1 452 $type = node_get_types('type', $node);
webmaster@1 453 $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#default_value' => !empty($node->title) ? $node->title : '', '#required' => TRUE, '#weight' => -5);
webmaster@1 454
webmaster@1 455 if (!empty($node->nid)) {
webmaster@1 456 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 457 $forum_terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
webmaster@1 458 // if editing, give option to leave shadows
webmaster@1 459 $shadow = (count($forum_terms) > 1);
webmaster@1 460 $form['shadow'] = array('#type' => 'checkbox', '#title' => t('Leave shadow copy'), '#default_value' => $shadow, '#description' => t('If you move this topic, you can leave a link in the old forum to the new forum.'));
webmaster@1 461 }
webmaster@1 462
webmaster@1 463 $form['body_field'] = node_body_field($node, $type->body_label, 1);
webmaster@1 464
webmaster@1 465 $form['#submit'][] = 'forum_submit';
webmaster@1 466 // Assign the forum topic submit handler.
webmaster@1 467
webmaster@1 468 return $form;
webmaster@1 469 }
webmaster@1 470
webmaster@1 471 function forum_link_alter(&$links, $node) {
webmaster@1 472 foreach ($links as $module => $link) {
webmaster@1 473 if (strstr($module, 'taxonomy_term')) {
webmaster@1 474 // Link back to the forum and not the taxonomy term page. We'll only
webmaster@1 475 // do this if the taxonomy term in question belongs to forums.
webmaster@1 476 $tid = str_replace('taxonomy/term/', '', $link['href']);
webmaster@1 477 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 478 $term = taxonomy_get_term($tid);
webmaster@1 479 if ($term->vid == $vid) {
webmaster@1 480 $links[$module]['href'] = str_replace('taxonomy/term', 'forum', $link['href']);
webmaster@1 481 }
webmaster@1 482 }
webmaster@1 483 }
webmaster@1 484 }
webmaster@1 485
webmaster@1 486 /**
webmaster@1 487 * Returns a list of all forums for a given taxonomy id
webmaster@1 488 *
webmaster@1 489 * Forum objects contain the following fields
webmaster@1 490 * -num_topics Number of topics in the forum
webmaster@1 491 * -num_posts Total number of posts in all topics
webmaster@1 492 * -last_post Most recent post for the forum
webmaster@1 493 *
webmaster@1 494 * @param $tid
webmaster@1 495 * Taxonomy ID of the vocabulary that holds the forum list.
webmaster@1 496 * @return
webmaster@1 497 * Array of object containing the forum information.
webmaster@1 498 */
webmaster@1 499 function forum_get_forums($tid = 0) {
webmaster@1 500
webmaster@1 501 $forums = array();
webmaster@1 502 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 503 $_forums = taxonomy_get_tree($vid, $tid);
webmaster@1 504
webmaster@1 505 if (count($_forums)) {
webmaster@1 506
webmaster@1 507 $counts = array();
webmaster@1 508
webmaster@1 509 $sql = "SELECT r.tid, COUNT(n.nid) AS topic_count, SUM(l.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.vid = r.vid WHERE n.status = 1 GROUP BY r.tid";
webmaster@1 510 $sql = db_rewrite_sql($sql);
webmaster@1 511 $_counts = db_query($sql);
webmaster@1 512 while ($count = db_fetch_object($_counts)) {
webmaster@1 513 $counts[$count->tid] = $count;
webmaster@1 514 }
webmaster@1 515 }
webmaster@1 516
webmaster@1 517 foreach ($_forums as $forum) {
webmaster@1 518 if (in_array($forum->tid, variable_get('forum_containers', array()))) {
webmaster@1 519 $forum->container = 1;
webmaster@1 520 }
webmaster@1 521
webmaster@1 522 if (!empty($counts[$forum->tid])) {
webmaster@1 523 $forum->num_topics = $counts[$forum->tid]->topic_count;
webmaster@1 524 $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
webmaster@1 525 }
webmaster@1 526 else {
webmaster@1 527 $forum->num_topics = 0;
webmaster@1 528 $forum->num_posts = 0;
webmaster@1 529 }
webmaster@1 530
webmaster@1 531 // This query does not use full ANSI syntax since MySQL 3.x does not support
webmaster@1 532 // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
webmaster@1 533 // used to join node_comment_statistics to users.
webmaster@1 534 $sql = "SELECT ncs.last_comment_timestamp, IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name, ncs.last_comment_uid FROM {node} n INNER JOIN {users} u1 ON n.uid = u1.uid INNER JOIN {term_node} tn ON n.vid = tn.vid INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid WHERE n.status = 1 AND tn.tid = %d ORDER BY ncs.last_comment_timestamp DESC";
webmaster@1 535 $sql = db_rewrite_sql($sql);
webmaster@1 536 $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
webmaster@1 537
webmaster@1 538 $last_post = new stdClass();
webmaster@1 539 if (!empty($topic->last_comment_timestamp)) {
webmaster@1 540 $last_post->timestamp = $topic->last_comment_timestamp;
webmaster@1 541 $last_post->name = $topic->last_comment_name;
webmaster@1 542 $last_post->uid = $topic->last_comment_uid;
webmaster@1 543 }
webmaster@1 544 $forum->last_post = $last_post;
webmaster@1 545
webmaster@1 546 $forums[$forum->tid] = $forum;
webmaster@1 547 }
webmaster@1 548
webmaster@1 549 return $forums;
webmaster@1 550 }
webmaster@1 551
webmaster@1 552 /**
webmaster@1 553 * Calculate the number of nodes the user has not yet read and are newer
webmaster@1 554 * than NODE_NEW_LIMIT.
webmaster@1 555 */
webmaster@1 556 function _forum_topics_unread($term, $uid) {
webmaster@1 557 $sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid AND tn.tid = %d LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d WHERE n.status = 1 AND n.created > %d AND h.nid IS NULL";
webmaster@1 558 $sql = db_rewrite_sql($sql);
webmaster@1 559 return db_result(db_query($sql, $term, $uid, NODE_NEW_LIMIT));
webmaster@1 560 }
webmaster@1 561
webmaster@1 562 function forum_get_topics($tid, $sortby, $forum_per_page) {
webmaster@1 563 global $user, $forum_topic_list_header;
webmaster@1 564
webmaster@1 565 $forum_topic_list_header = array(
webmaster@15 566 NULL,
webmaster@1 567 array('data' => t('Topic'), 'field' => 'n.title'),
webmaster@1 568 array('data' => t('Replies'), 'field' => 'l.comment_count'),
webmaster@1 569 array('data' => t('Created'), 'field' => 'n.created'),
webmaster@1 570 array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
webmaster@1 571 );
webmaster@1 572
webmaster@1 573 $order = _forum_get_topic_order($sortby);
webmaster@1 574 for ($i = 0; $i < count($forum_topic_list_header); $i++) {
webmaster@1 575 if ($forum_topic_list_header[$i]['field'] == $order['field']) {
webmaster@1 576 $forum_topic_list_header[$i]['sort'] = $order['sort'];
webmaster@1 577 }
webmaster@1 578 }
webmaster@1 579
webmaster@1 580 $term = taxonomy_get_term($tid);
webmaster@1 581
webmaster@1 582 $sql = db_rewrite_sql("SELECT n.nid, r.tid, n.title, n.type, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments, f.tid AS forum_tid FROM {node_comment_statistics} l INNER JOIN {node} n ON n.nid = l.nid INNER JOIN {users} cu ON l.last_comment_uid = cu.uid INNER JOIN {term_node} r ON n.vid = r.vid INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {forum} f ON n.vid = f.vid WHERE n.status = 1 AND r.tid = %d");
webmaster@1 583 $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
webmaster@1 584 $sql .= ', n.created DESC'; // Always add a secondary sort order so that the news forum topics are on top.
webmaster@1 585
webmaster@1 586 $sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.vid = r.vid AND r.tid = %d WHERE n.status = 1");
webmaster@1 587
webmaster@1 588 $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
webmaster@1 589 $topics = array();
webmaster@1 590 while ($topic = db_fetch_object($result)) {
webmaster@1 591 if ($user->uid) {
webmaster@1 592 // folder is new if topic is new or there are new comments since last visit
webmaster@1 593 if ($topic->tid != $tid) {
webmaster@1 594 $topic->new = 0;
webmaster@1 595 }
webmaster@1 596 else {
webmaster@1 597 $history = _forum_user_last_visit($topic->nid);
webmaster@1 598 $topic->new_replies = comment_num_new($topic->nid, $history);
webmaster@1 599 $topic->new = $topic->new_replies || ($topic->timestamp > $history);
webmaster@1 600 }
webmaster@1 601 }
webmaster@1 602 else {
webmaster@1 603 // Do not track "new replies" status for topics if the user is anonymous.
webmaster@1 604 $topic->new_replies = 0;
webmaster@1 605 $topic->new = 0;
webmaster@1 606 }
webmaster@1 607
webmaster@1 608 if ($topic->num_comments > 0) {
webmaster@1 609 $last_reply = new stdClass();
webmaster@1 610 $last_reply->timestamp = $topic->last_comment_timestamp;
webmaster@1 611 $last_reply->name = $topic->last_comment_name;
webmaster@1 612 $last_reply->uid = $topic->last_comment_uid;
webmaster@1 613 $topic->last_reply = $last_reply;
webmaster@1 614 }
webmaster@1 615 $topics[] = $topic;
webmaster@1 616 }
webmaster@1 617
webmaster@1 618 return $topics;
webmaster@1 619 }
webmaster@1 620
webmaster@1 621 /**
webmaster@1 622 * Finds the first unread node for a given forum.
webmaster@1 623 */
webmaster@1 624 function _forum_new($tid) {
webmaster@1 625 global $user;
webmaster@1 626
webmaster@1 627 $sql = "SELECT n.nid FROM {node} n LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND h.nid IS NULL AND n.created > %d ORDER BY created";
webmaster@1 628 $sql = db_rewrite_sql($sql);
webmaster@1 629 $nid = db_result(db_query_range($sql, $user->uid, $tid, NODE_NEW_LIMIT, 0, 1));
webmaster@1 630
webmaster@1 631 return $nid ? $nid : 0;
webmaster@1 632 }
webmaster@1 633
webmaster@1 634 /**
webmaster@1 635 * Process variables for forums.tpl.php
webmaster@1 636 *
webmaster@1 637 * The $variables array contains the following arguments:
webmaster@1 638 * - $forums
webmaster@1 639 * - $topics
webmaster@1 640 * - $parents
webmaster@1 641 * - $tid
webmaster@1 642 * - $sortby
webmaster@1 643 * - $forum_per_page
webmaster@1 644 *
webmaster@1 645 * @see forums.tpl.php
webmaster@1 646 */
webmaster@1 647 function template_preprocess_forums(&$variables) {
webmaster@1 648 global $user;
webmaster@1 649
webmaster@1 650 $vid = variable_get('forum_nav_vocabulary', '');
webmaster@1 651 $vocabulary = taxonomy_vocabulary_load($vid);
webmaster@1 652 $title = !empty($vocabulary->name) ? $vocabulary->name : '';
webmaster@1 653
webmaster@1 654 // Breadcrumb navigation:
webmaster@1 655 $breadcrumb[] = l(t('Home'), NULL);
webmaster@1 656 if ($variables['tid']) {
webmaster@1 657 $breadcrumb[] = l($vocabulary->name, 'forum');
webmaster@1 658 }
webmaster@1 659 if ($variables['parents']) {
webmaster@1 660 $variables['parents'] = array_reverse($variables['parents']);
webmaster@1 661 foreach ($variables['parents'] as $p) {
webmaster@1 662 if ($p->tid == $variables['tid']) {
webmaster@1 663 $title = $p->name;
webmaster@1 664 }
webmaster@1 665 else {
webmaster@1 666 $breadcrumb[] = l($p->name, 'forum/'. $p->tid);
webmaster@1 667 }
webmaster@1 668 }
webmaster@1 669 }
webmaster@1 670 drupal_set_breadcrumb($breadcrumb);
webmaster@1 671 drupal_set_title(check_plain($title));
webmaster@1 672
webmaster@1 673 if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {
webmaster@1 674 // Format the "post new content" links listing.
webmaster@1 675 $forum_types = array();
webmaster@1 676
webmaster@1 677 // Loop through all node types for forum vocabulary.
webmaster@1 678 foreach ($vocabulary->nodes as $type) {
webmaster@1 679 // Check if the current user has the 'create' permission for this node type.
webmaster@1 680 if (node_access('create', $type)) {
webmaster@1 681 // Fetch the "General" name of the content type;
webmaster@1 682 // Push the link with title and url to the array.
webmaster@1 683 $forum_types[$type] = array('title' => t('Post new @node_type', array('@node_type' => node_get_types('name', $type))), 'href' => 'node/add/'. str_replace('_', '-', $type) .'/'. $variables['tid']);
webmaster@1 684 }
webmaster@1 685 }
webmaster@1 686
webmaster@1 687 if (empty($forum_types)) {
webmaster@1 688 // The user is logged-in; but denied access to create any new forum content type.
webmaster@1 689 if ($user->uid) {
webmaster@1 690 $forum_types['disallowed'] = array('title' => t('You are not allowed to post new content in forum.'));
webmaster@1 691 }
webmaster@1 692 // The user is not logged-in; and denied access to create any new forum content type.
webmaster@1 693 else {
webmaster@1 694 $forum_types['login'] = array('title' => t('<a href="@login">Login</a> to post new content in forum.', array('@login' => url('user/login', array('query' => drupal_get_destination())))), 'html' => TRUE);
webmaster@1 695 }
webmaster@1 696 }
webmaster@1 697 $variables['links'] = $forum_types;
webmaster@1 698
webmaster@1 699 if (!empty($variables['forums'])) {
webmaster@1 700 $variables['forums'] = theme('forum_list', $variables['forums'], $variables['parents'], $variables['tid']);
webmaster@1 701 }
webmaster@1 702 else {
webmaster@1 703 $variables['forums'] = '';
webmaster@1 704 }
webmaster@1 705
webmaster@1 706 if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
webmaster@1 707 $variables['topics'] = theme('forum_topic_list', $variables['tid'], $variables['topics'], $variables['sortby'], $variables['forum_per_page']);
webmaster@1 708 drupal_add_feed(url('taxonomy/term/'. $variables['tid'] .'/0/feed'), 'RSS - '. $title);
webmaster@1 709 }
webmaster@1 710 else {
webmaster@1 711 $variables['topics'] = '';
webmaster@1 712 }
webmaster@1 713
webmaster@1 714 // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
webmaster@1 715 // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
webmaster@1 716 if ($variables['forums'] && !$variables['topics']) {
webmaster@1 717 $variables['template_files'][] = 'forums-containers';
webmaster@1 718 $variables['template_files'][] = 'forums-'. $variables['tid'];
webmaster@1 719 $variables['template_files'][] = 'forums-containers-'. $variables['tid'];
webmaster@1 720 }
webmaster@1 721 elseif (!$variables['forums'] && $variables['topics']) {
webmaster@1 722 $variables['template_files'][] = 'forums-topics';
webmaster@1 723 $variables['template_files'][] = 'forums-'. $variables['tid'];
webmaster@1 724 $variables['template_files'][] = 'forums-topics-'. $variables['tid'];
webmaster@1 725 }
webmaster@1 726 else {
webmaster@1 727 $variables['template_files'][] = 'forums-'. $variables['tid'];
webmaster@1 728 }
webmaster@1 729
webmaster@1 730 }
webmaster@1 731 else {
webmaster@1 732 drupal_set_title(t('No forums defined'));
webmaster@1 733 $variables['links'] = array();
webmaster@1 734 $variables['forums'] = '';
webmaster@1 735 $variables['topics'] = '';
webmaster@1 736 }
webmaster@1 737 }
webmaster@1 738
webmaster@1 739 /**
webmaster@1 740 * Process variables to format a forum listing.
webmaster@1 741 *
webmaster@1 742 * $variables contains the following information:
webmaster@1 743 * - $forums
webmaster@1 744 * - $parents
webmaster@1 745 * - $tid
webmaster@1 746 *
webmaster@1 747 * @see forum-list.tpl.php
webmaster@1 748 * @see theme_forum_list()
webmaster@1 749 */
webmaster@1 750 function template_preprocess_forum_list(&$variables) {
webmaster@1 751 global $user;
webmaster@1 752 $row = 0;
webmaster@1 753 // Sanitize each forum so that the template can safely print the data.
webmaster@1 754 foreach ($variables['forums'] as $id => $forum) {
webmaster@1 755 $variables['forums'][$id]->description = !empty($forum->description) ? filter_xss_admin($forum->description) : '';
webmaster@1 756 $variables['forums'][$id]->link = url("forum/$forum->tid");
webmaster@1 757 $variables['forums'][$id]->name = check_plain($forum->name);
webmaster@1 758 $variables['forums'][$id]->is_container = !empty($forum->container);
webmaster@1 759 $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
webmaster@1 760 $row++;
webmaster@1 761
webmaster@1 762 $variables['forums'][$id]->new_text = '';
webmaster@1 763 $variables['forums'][$id]->new_url = '';
webmaster@1 764 $variables['forums'][$id]->new_topics = 0;
webmaster@1 765 $variables['forums'][$id]->old_topics = $forum->num_topics;
webmaster@1 766 if ($user->uid) {
webmaster@1 767 $variables['forums'][$id]->new_topics = _forum_topics_unread($forum->tid, $user->uid);
webmaster@1 768 if ($variables['forums'][$id]->new_topics) {
webmaster@1 769 $variables['forums'][$id]->new_text = format_plural($variables['forums'][$id]->new_topics, '1 new', '@count new');
webmaster@1 770 $variables['forums'][$id]->new_url = url("forum/$forum->tid", array('fragment' => 'new'));
webmaster@1 771 }
webmaster@1 772 $variables['forums'][$id]->old_topics = $forum->num_topics - $variables['forums'][$id]->new_topics;
webmaster@1 773 }
webmaster@1 774 $variables['forums'][$id]->last_reply = theme('forum_submitted', $forum->last_post);
webmaster@1 775 }
webmaster@1 776 // Give meaning to $tid for themers. $tid actually stands for term id.
webmaster@1 777 $variables['forum_id'] = $variables['tid'];
webmaster@1 778 unset($variables['tid']);
webmaster@1 779 }
webmaster@1 780
webmaster@1 781 /**
webmaster@1 782 * Preprocess variables to format the topic listing.
webmaster@1 783 *
webmaster@1 784 * $variables contains the following data:
webmaster@1 785 * - $tid
webmaster@1 786 * - $topics
webmaster@1 787 * - $sortby
webmaster@1 788 * - $forum_per_page
webmaster@1 789 *
webmaster@1 790 * @see forum-topic-list.tpl.php
webmaster@1 791 * @see theme_forum_topic_list()
webmaster@1 792 */
webmaster@1 793 function template_preprocess_forum_topic_list(&$variables) {
webmaster@1 794 global $forum_topic_list_header;
webmaster@1 795
webmaster@1 796 // Create the tablesorting header.
webmaster@1 797 $ts = tablesort_init($forum_topic_list_header);
webmaster@1 798 $header = '';
webmaster@1 799 foreach ($forum_topic_list_header as $cell) {
webmaster@1 800 $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
webmaster@1 801 $header .= _theme_table_cell($cell, TRUE);
webmaster@1 802 }
webmaster@1 803 $variables['header'] = $header;
webmaster@1 804
webmaster@1 805 if (!empty($variables['topics'])) {
webmaster@1 806 $row = 0;
webmaster@1 807 foreach ($variables['topics'] as $id => $topic) {
webmaster@1 808 $variables['topics'][$id]->icon = theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky);
webmaster@1 809 $variables['topics'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
webmaster@1 810 $row++;
webmaster@1 811
webmaster@1 812 // We keep the actual tid in forum table, if it's different from the
webmaster@1 813 // current tid then it means the topic appears in two forums, one of
webmaster@1 814 // them is a shadow copy.
webmaster@1 815 if ($topic->forum_tid != $variables['tid']) {
webmaster@1 816 $variables['topics'][$id]->moved = TRUE;
webmaster@1 817 $variables['topics'][$id]->title = check_plain($topic->title);
webmaster@1 818 $variables['topics'][$id]->message = l(t('This topic has been moved'), "forum/$topic->forum_tid");
webmaster@1 819 }
webmaster@1 820 else {
webmaster@1 821 $variables['topics'][$id]->moved = FALSE;
webmaster@1 822 $variables['topics'][$id]->title = l($topic->title, "node/$topic->nid");
webmaster@1 823 $variables['topics'][$id]->message = '';
webmaster@1 824 }
webmaster@1 825 $variables['topics'][$id]->created = theme('forum_submitted', $topic);
webmaster@1 826 $variables['topics'][$id]->last_reply = theme('forum_submitted', isset($topic->last_reply) ? $topic->last_reply : NULL);
webmaster@1 827
webmaster@1 828 $variables['topics'][$id]->new_text = '';
webmaster@1 829 $variables['topics'][$id]->new_url = '';
webmaster@1 830 if ($topic->new_replies) {
webmaster@1 831 $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new', '@count new');
webmaster@1 832 $variables['topics'][$id]->new_url = url("node/$topic->nid", array('query' => comment_new_page_count($topic->num_comments, $topic->new_replies, $topic), 'fragment' => 'new'));
webmaster@1 833 }
webmaster@1 834
webmaster@1 835 }
webmaster@1 836 }
webmaster@1 837 else {
webmaster@1 838 // Make this safe for the template
webmaster@1 839 $variables['topics'] = array();
webmaster@1 840 }
webmaster@1 841 // Give meaning to $tid for themers. $tid actually stands for term id.
webmaster@1 842 $variables['topic_id'] = $variables['tid'];
webmaster@1 843 unset($variables['tid']);
webmaster@1 844
webmaster@1 845 $variables['pager'] = theme('pager', NULL, $variables['forum_per_page'], 0);
webmaster@1 846 }
webmaster@1 847
webmaster@1 848 /**
webmaster@1 849 * Process variables to format the icon for each individual topic.
webmaster@1 850 *
webmaster@1 851 * $variables contains the following data:
webmaster@1 852 * - $new_posts
webmaster@1 853 * - $num_posts = 0
webmaster@1 854 * - $comment_mode = 0
webmaster@1 855 * - $sticky = 0
webmaster@1 856 *
webmaster@1 857 * @see forum-icon.tpl.php
webmaster@1 858 * @see theme_forum_icon()
webmaster@1 859 */
webmaster@1 860 function template_preprocess_forum_icon(&$variables) {
webmaster@1 861 $variables['hot_threshold'] = variable_get('forum_hot_topic', 15);
webmaster@1 862 if ($variables['num_posts'] > $variables['hot_threshold']) {
webmaster@1 863 $variables['icon'] = $variables['new_posts'] ? 'hot-new' : 'hot';
webmaster@1 864 }
webmaster@1 865 else {
webmaster@1 866 $variables['icon'] = $variables['new_posts'] ? 'new' : 'default';
webmaster@1 867 }
webmaster@1 868
webmaster@1 869 if ($variables['comment_mode'] == COMMENT_NODE_READ_ONLY || $variables['comment_mode'] == COMMENT_NODE_DISABLED) {
webmaster@1 870 $variables['icon'] = 'closed';
webmaster@1 871 }
webmaster@1 872
webmaster@1 873 if ($variables['sticky'] == 1) {
webmaster@1 874 $variables['icon'] = 'sticky';
webmaster@1 875 }
webmaster@1 876 }
webmaster@1 877
webmaster@1 878 /**
webmaster@1 879 * Preprocess variables to format the next/previous forum topic navigation links.
webmaster@1 880 *
webmaster@1 881 * $variables contains $node.
webmaster@1 882 *
webmaster@1 883 * @see forum-topic-navigation.tpl.php
webmaster@1 884 * @see theme_forum_topic_navigation()
webmaster@1 885 */
webmaster@1 886 function template_preprocess_forum_topic_navigation(&$variables) {
webmaster@1 887 $output = '';
webmaster@1 888
webmaster@1 889 // get previous and next topic
webmaster@1 890 $sql = "SELECT n.nid, n.title, n.sticky, l.comment_count, l.last_comment_timestamp FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 ORDER BY n.sticky DESC, ". _forum_get_topic_order_sql(variable_get('forum_order', 1));
webmaster@1 891 $result = db_query(db_rewrite_sql($sql), isset($variables['node']->tid) ? $variables['node']->tid : 0);
webmaster@1 892
webmaster@1 893 $stop = $variables['prev'] = $variables['next'] = 0;
webmaster@1 894
webmaster@1 895 while ($topic = db_fetch_object($result)) {
webmaster@1 896 if ($stop == 1) {
webmaster@1 897 $variables['next'] = $topic->nid;
webmaster@1 898 $variables['next_title'] = check_plain($topic->title);
webmaster@1 899 $variables['next_url'] = url("node/$topic->nid");
webmaster@1 900 break;
webmaster@1 901 }
webmaster@1 902 if ($topic->nid == $variables['node']->nid) {
webmaster@1 903 $stop = 1;
webmaster@1 904 }
webmaster@1 905 else {
webmaster@1 906 $variables['prev'] = $topic->nid;
webmaster@1 907 $variables['prev_title'] = check_plain($topic->title);
webmaster@1 908 $variables['prev_url'] = url("node/$topic->nid");
webmaster@1 909 }
webmaster@1 910 }
webmaster@1 911 }
webmaster@1 912
webmaster@1 913 /**
webmaster@1 914 * Process variables to format submission info for display in the forum list and topic list.
webmaster@1 915 *
webmaster@1 916 * $variables will contain: $topic
webmaster@1 917 *
webmaster@1 918 * @see forum-submitted.tpl.php
webmaster@1 919 * @see theme_forum_submitted()
webmaster@1 920 */
webmaster@1 921 function template_preprocess_forum_submitted(&$variables) {
webmaster@1 922 $variables['author'] = isset($variables['topic']->uid) ? theme('username', $variables['topic']) : '';
webmaster@1 923 $variables['time'] = isset($variables['topic']->timestamp) ? format_interval(time() - $variables['topic']->timestamp) : '';
webmaster@1 924 }
webmaster@1 925
webmaster@1 926 function _forum_user_last_visit($nid) {
webmaster@1 927 global $user;
webmaster@1 928 static $history = array();
webmaster@1 929
webmaster@1 930 if (empty($history)) {
webmaster@1 931 $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = %d', $user->uid);
webmaster@1 932 while ($t = db_fetch_object($result)) {
webmaster@1 933 $history[$t->nid] = $t->timestamp > NODE_NEW_LIMIT ? $t->timestamp : NODE_NEW_LIMIT;
webmaster@1 934 }
webmaster@1 935 }
webmaster@1 936 return isset($history[$nid]) ? $history[$nid] : NODE_NEW_LIMIT;
webmaster@1 937 }
webmaster@1 938
webmaster@1 939 function _forum_get_topic_order($sortby) {
webmaster@1 940 switch ($sortby) {
webmaster@1 941 case 1:
webmaster@1 942 return array('field' => 'l.last_comment_timestamp', 'sort' => 'desc');
webmaster@1 943 break;
webmaster@1 944 case 2:
webmaster@1 945 return array('field' => 'l.last_comment_timestamp', 'sort' => 'asc');
webmaster@1 946 break;
webmaster@1 947 case 3:
webmaster@1 948 return array('field' => 'l.comment_count', 'sort' => 'desc');
webmaster@1 949 break;
webmaster@1 950 case 4:
webmaster@1 951 return array('field' => 'l.comment_count', 'sort' => 'asc');
webmaster@1 952 break;
webmaster@1 953 }
webmaster@1 954 }
webmaster@1 955
webmaster@1 956 function _forum_get_topic_order_sql($sortby) {
webmaster@1 957 $order = _forum_get_topic_order($sortby);
webmaster@1 958 return $order['field'] .' '. strtoupper($order['sort']);
webmaster@1 959 }