forum.module

  1. 7.x drupal-7.x/modules/forum/forum.module
  2. 6.x drupal-6.x/modules/forum/forum.module

Enable threaded discussions about general topics.

File

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