forum.module 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. <?php
  2. /**
  3. * @file
  4. * Provides discussion forums.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function forum_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#forum':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $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. In a forum, users post topics and threads in nested hierarchies, allowing discussions to be categorized and grouped. The forum hierarchy consists of:') . '</p>';
  15. $output .= '<ul>';
  16. $output .= '<li>' . t('Optional containers (for example, <em>Support</em>), which can hold:') . '</li>';
  17. $output .= '<ul><li>' . t('Forums (for example, <em>Installing Drupal</em>), which can hold:') . '</li>';
  18. $output .= '<ul><li>' . t('Forum topics submitted by users (for example, <em>How to start a Drupal 6 Multisite</em>), which start discussions and are starting points for:') . '</li>';
  19. $output .= '<ul><li>' . t('Threaded comments submitted by users (for example, <em>You have these options...</em>).') . '</li>';
  20. $output .= '</ul>';
  21. $output .= '</ul>';
  22. $output .= '</ul>';
  23. $output .= '</ul>';
  24. $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@forum">Forum module</a>.', array('@forum' => 'http://drupal.org/documentation/modules/forum')) . '</p>';
  25. $output .= '<h3>' . t('Uses') . '</h3>';
  26. $output .= '<dl>';
  27. $output .= '<dt>' . t('Setting up forum structure') . '</dt>';
  28. $output .= '<dd>' . t('Visit the <a href="@forums">Forums page</a> to set up containers and forums to hold your discussion topics.', array('@forums' => url('admin/structure/forum'))) . '</dd>';
  29. $output .= '<dt>' . t('Starting a discussion') . '</dt>';
  30. $output .= '<dd>' . t('The <a href="@create-topic">Forum topic</a> link on the <a href="@content-add">Add new content</a> page creates the first post of a new threaded discussion, or thread.', array('@create-topic' => url('node/add/forum'), '@content-add' => url('node/add'))) . '</dd>';
  31. $output .= '<dt>' . t('Navigation') . '</dt>';
  32. $output .= '<dd>' . t('Enabling the Forum module provides a default <em>Forums</em> menu item in the navigation menu that links to the <a href="@forums">Forums page</a>.', array('@forums' => url('forum'))) . '</dd>';
  33. $output .= '<dt>' . t('Moving forum topics') . '</dt>';
  34. $output .= '<dd>' . t('A forum topic (and all of its comments) may be moved between forums by selecting a different forum while editing a forum topic. 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.') . '</dd>';
  35. $output .= '<dt>' . t('Locking and disabling comments') . '</dt>';
  36. $output .= '<dd>' . t('Selecting <em>Closed</em> under <em>Comment settings</em> while editing a forum topic will lock (prevent new comments on) the thread. Selecting <em>Hidden</em> under <em>Comment settings</em> while editing a forum topic will hide all existing comments on the thread, and prevent new ones.') . '</dd>';
  37. $output .= '</dl>';
  38. return $output;
  39. case 'admin/structure/forum':
  40. $output = '<p>' . t('Forums contain forum topics. Use containers to group related forums.') . '</p>';
  41. $output .= theme('more_help_link', array('url' => 'admin/help/forum'));
  42. return $output;
  43. case 'admin/structure/forum/add/container':
  44. return '<p>' . t('Use containers to group related forums.') . '</p>';
  45. case 'admin/structure/forum/add/forum':
  46. return '<p>' . t('A forum holds related forum topics.') . '</p>';
  47. case 'admin/structure/forum/settings':
  48. return '<p>' . t('Adjust the display of your forum topics. Organize the forums on the <a href="@forum-structure">forum structure page</a>.', array('@forum-structure' => url('admin/structure/forum'))) . '</p>';
  49. }
  50. }
  51. /**
  52. * Implements hook_theme().
  53. */
  54. function forum_theme() {
  55. return array(
  56. 'forums' => array(
  57. 'template' => 'forums',
  58. 'variables' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  59. ),
  60. 'forum_list' => array(
  61. 'template' => 'forum-list',
  62. 'variables' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
  63. ),
  64. 'forum_topic_list' => array(
  65. 'template' => 'forum-topic-list',
  66. 'variables' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  67. ),
  68. 'forum_icon' => array(
  69. 'template' => 'forum-icon',
  70. 'variables' => array('new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0, 'first_new' => FALSE),
  71. ),
  72. 'forum_submitted' => array(
  73. 'template' => 'forum-submitted',
  74. 'variables' => array('topic' => NULL),
  75. ),
  76. 'forum_form' => array(
  77. 'render element' => 'form',
  78. 'file' => 'forum.admin.inc',
  79. ),
  80. );
  81. }
  82. /**
  83. * Implements hook_menu().
  84. */
  85. function forum_menu() {
  86. $items['forum'] = array(
  87. 'title' => 'Forums',
  88. 'page callback' => 'forum_page',
  89. 'access arguments' => array('access content'),
  90. 'file' => 'forum.pages.inc',
  91. );
  92. $items['forum/%forum_forum'] = array(
  93. 'title' => 'Forums',
  94. 'page callback' => 'forum_page',
  95. 'page arguments' => array(1),
  96. 'access arguments' => array('access content'),
  97. 'file' => 'forum.pages.inc',
  98. );
  99. $items['admin/structure/forum'] = array(
  100. 'title' => 'Forums',
  101. 'description' => 'Control forum hierarchy settings.',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('forum_overview'),
  104. 'access arguments' => array('administer forums'),
  105. 'file' => 'forum.admin.inc',
  106. );
  107. $items['admin/structure/forum/list'] = array(
  108. 'title' => 'List',
  109. 'type' => MENU_DEFAULT_LOCAL_TASK,
  110. 'weight' => -10,
  111. );
  112. $items['admin/structure/forum/add/container'] = array(
  113. 'title' => 'Add container',
  114. 'page callback' => 'forum_form_main',
  115. 'page arguments' => array('container'),
  116. 'access arguments' => array('administer forums'),
  117. 'type' => MENU_LOCAL_ACTION,
  118. 'parent' => 'admin/structure/forum',
  119. 'file' => 'forum.admin.inc',
  120. );
  121. $items['admin/structure/forum/add/forum'] = array(
  122. 'title' => 'Add forum',
  123. 'page callback' => 'forum_form_main',
  124. 'page arguments' => array('forum'),
  125. 'access arguments' => array('administer forums'),
  126. 'type' => MENU_LOCAL_ACTION,
  127. 'parent' => 'admin/structure/forum',
  128. 'file' => 'forum.admin.inc',
  129. );
  130. $items['admin/structure/forum/settings'] = array(
  131. 'title' => 'Settings',
  132. 'page callback' => 'drupal_get_form',
  133. 'page arguments' => array('forum_admin_settings'),
  134. 'access arguments' => array('administer forums'),
  135. 'weight' => 5,
  136. 'type' => MENU_LOCAL_TASK,
  137. 'parent' => 'admin/structure/forum',
  138. 'file' => 'forum.admin.inc',
  139. );
  140. $items['admin/structure/forum/edit/container/%taxonomy_term'] = array(
  141. 'title' => 'Edit container',
  142. 'page callback' => 'forum_form_main',
  143. 'page arguments' => array('container', 5),
  144. 'access arguments' => array('administer forums'),
  145. 'file' => 'forum.admin.inc',
  146. );
  147. $items['admin/structure/forum/edit/forum/%taxonomy_term'] = array(
  148. 'title' => 'Edit forum',
  149. 'page callback' => 'forum_form_main',
  150. 'page arguments' => array('forum', 5),
  151. 'access arguments' => array('administer forums'),
  152. 'file' => 'forum.admin.inc',
  153. );
  154. return $items;
  155. }
  156. /**
  157. * Implements hook_menu_local_tasks_alter().
  158. */
  159. function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  160. global $user;
  161. // Add action link to 'node/add/forum' on 'forum' sub-pages.
  162. if ($root_path == 'forum' || $root_path == 'forum/%') {
  163. $tid = (isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0);
  164. $forum_term = forum_forum_load($tid);
  165. if ($forum_term) {
  166. $links = array();
  167. // Loop through all bundles for forum taxonomy vocabulary field.
  168. $field = field_info_field('taxonomy_forums');
  169. foreach ($field['bundles']['node'] as $type) {
  170. if (node_access('create', $type)) {
  171. $links[$type] = array(
  172. '#theme' => 'menu_local_action',
  173. '#link' => array(
  174. 'title' => t('Add new @node_type', array('@node_type' => node_type_get_name($type))),
  175. 'href' => 'node/add/' . str_replace('_', '-', $type) . '/' . $forum_term->tid,
  176. ),
  177. );
  178. }
  179. }
  180. if (empty($links)) {
  181. // Authenticated user does not have access to create new topics.
  182. if ($user->uid) {
  183. $links['disallowed'] = array(
  184. '#theme' => 'menu_local_action',
  185. '#link' => array(
  186. 'title' => t('You are not allowed to post new content in the forum.'),
  187. ),
  188. );
  189. }
  190. // Anonymous user does not have access to create new topics.
  191. else {
  192. $links['login'] = array(
  193. '#theme' => 'menu_local_action',
  194. '#link' => array(
  195. 'title' => t('<a href="@login">Log in</a> to post new content in the forum.', array(
  196. '@login' => url('user/login', array('query' => drupal_get_destination())),
  197. )),
  198. 'localized_options' => array('html' => TRUE),
  199. ),
  200. );
  201. }
  202. }
  203. $data['actions']['output'] = array_merge($data['actions']['output'], $links);
  204. }
  205. }
  206. }
  207. /**
  208. * Implements hook_entity_info_alter().
  209. */
  210. function forum_entity_info_alter(&$info) {
  211. // Take over URI construction for taxonomy terms that are forums.
  212. if ($vid = variable_get('forum_nav_vocabulary', 0)) {
  213. // Within hook_entity_info(), we can't invoke entity_load() as that would
  214. // cause infinite recursion, so we call taxonomy_vocabulary_get_names()
  215. // instead of taxonomy_vocabulary_load(). All we need is the machine name
  216. // of $vid, so retrieving and iterating all the vocabulary names is somewhat
  217. // inefficient, but entity info is cached across page requests, and an
  218. // iteration of all vocabularies once per cache clearing isn't a big deal,
  219. // and is done as part of taxonomy_entity_info() anyway.
  220. foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
  221. if ($vid == $vocabulary->vid) {
  222. $info['taxonomy_term']['bundles'][$machine_name]['uri callback'] = 'forum_uri';
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * Implements callback_entity_info_uri().
  229. *
  230. * Entity URI callback used in forum_entity_info_alter().
  231. */
  232. function forum_uri($forum) {
  233. return array(
  234. 'path' => 'forum/' . $forum->tid,
  235. );
  236. }
  237. /**
  238. * Checks whether a node can be used in a forum, based on its content type.
  239. *
  240. * @param $node
  241. * A node object.
  242. *
  243. * @return
  244. * Boolean indicating if the node can be assigned to a forum.
  245. */
  246. function _forum_node_check_node_type($node) {
  247. // Fetch information about the forum field.
  248. $field = field_info_instance('node', 'taxonomy_forums', $node->type);
  249. return is_array($field);
  250. }
  251. /**
  252. * Implements hook_node_view().
  253. */
  254. function forum_node_view($node, $view_mode) {
  255. $vid = variable_get('forum_nav_vocabulary', 0);
  256. $vocabulary = taxonomy_vocabulary_load($vid);
  257. if (_forum_node_check_node_type($node)) {
  258. if ($view_mode == 'full' && node_is_page($node)) {
  259. // Breadcrumb navigation
  260. $breadcrumb[] = l(t('Home'), NULL);
  261. $breadcrumb[] = l($vocabulary->name, 'forum');
  262. if ($parents = taxonomy_get_parents_all($node->forum_tid)) {
  263. $parents = array_reverse($parents);
  264. foreach ($parents as $parent) {
  265. $breadcrumb[] = l($parent->name, 'forum/' . $parent->tid);
  266. }
  267. }
  268. drupal_set_breadcrumb($breadcrumb);
  269. }
  270. }
  271. }
  272. /**
  273. * Implements hook_node_validate().
  274. *
  275. * Checks in particular that the node is assigned only a "leaf" term in the
  276. * forum taxonomy.
  277. */
  278. function forum_node_validate($node, $form) {
  279. if (_forum_node_check_node_type($node)) {
  280. $langcode = $form['taxonomy_forums']['#language'];
  281. // vocabulary is selected, not a "container" term.
  282. if (!empty($node->taxonomy_forums[$langcode])) {
  283. // Extract the node's proper topic ID.
  284. $containers = variable_get('forum_containers', array());
  285. foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
  286. // If no term was selected (e.g. when no terms exist yet), remove the
  287. // item.
  288. if (empty($item['tid'])) {
  289. unset($node->taxonomy_forums[$langcode][$delta]);
  290. continue;
  291. }
  292. $term = taxonomy_term_load($item['tid']);
  293. if (!$term) {
  294. form_set_error('taxonomy_forums', t('Select a forum.'));
  295. continue;
  296. }
  297. $used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid',0 , 1, array(
  298. ':tid' => $term->tid,
  299. ':vid' => $term->vid,
  300. ))->fetchField();
  301. if ($used && in_array($term->tid, $containers)) {
  302. form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->name)));
  303. }
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Implements hook_node_presave().
  310. *
  311. * Assigns the forum taxonomy when adding a topic from within a forum.
  312. */
  313. function forum_node_presave($node) {
  314. if (_forum_node_check_node_type($node)) {
  315. // Make sure all fields are set properly:
  316. $node->icon = !empty($node->icon) ? $node->icon : '';
  317. reset($node->taxonomy_forums);
  318. $langcode = key($node->taxonomy_forums);
  319. if (!empty($node->taxonomy_forums[$langcode])) {
  320. $node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid'];
  321. if (isset($node->nid)) {
  322. $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField();
  323. if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) {
  324. // A shadow copy needs to be created. Retain new term and add old term.
  325. $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * Implements hook_node_update().
  333. */
  334. function forum_node_update($node) {
  335. if (_forum_node_check_node_type($node)) {
  336. if (empty($node->revision) && db_query('SELECT tid FROM {forum} WHERE nid=:nid', array(':nid' => $node->nid))->fetchField()) {
  337. if (!empty($node->forum_tid)) {
  338. db_update('forum')
  339. ->fields(array('tid' => $node->forum_tid))
  340. ->condition('vid', $node->vid)
  341. ->execute();
  342. }
  343. // The node is removed from the forum.
  344. else {
  345. db_delete('forum')
  346. ->condition('nid', $node->nid)
  347. ->execute();
  348. }
  349. }
  350. else {
  351. if (!empty($node->forum_tid)) {
  352. db_insert('forum')
  353. ->fields(array(
  354. 'tid' => $node->forum_tid,
  355. 'vid' => $node->vid,
  356. 'nid' => $node->nid,
  357. ))
  358. ->execute();
  359. }
  360. }
  361. // If the node has a shadow forum topic, update the record for this
  362. // revision.
  363. if (!empty($node->shadow)) {
  364. db_delete('forum')
  365. ->condition('nid', $node->nid)
  366. ->condition('vid', $node->vid)
  367. ->execute();
  368. db_insert('forum')
  369. ->fields(array(
  370. 'nid' => $node->nid,
  371. 'vid' => $node->vid,
  372. 'tid' => $node->forum_tid,
  373. ))
  374. ->execute();
  375. }
  376. }
  377. }
  378. /**
  379. * Implements hook_node_insert().
  380. */
  381. function forum_node_insert($node) {
  382. if (_forum_node_check_node_type($node)) {
  383. if (!empty($node->forum_tid)) {
  384. $nid = db_insert('forum')
  385. ->fields(array(
  386. 'tid' => $node->forum_tid,
  387. 'vid' => $node->vid,
  388. 'nid' => $node->nid,
  389. ))
  390. ->execute();
  391. }
  392. }
  393. }
  394. /**
  395. * Implements hook_node_delete().
  396. */
  397. function forum_node_delete($node) {
  398. if (_forum_node_check_node_type($node)) {
  399. db_delete('forum')
  400. ->condition('nid', $node->nid)
  401. ->execute();
  402. db_delete('forum_index')
  403. ->condition('nid', $node->nid)
  404. ->execute();
  405. }
  406. }
  407. /**
  408. * Implements hook_node_load().
  409. */
  410. function forum_node_load($nodes) {
  411. $node_vids = array();
  412. foreach ($nodes as $node) {
  413. if (_forum_node_check_node_type($node)) {
  414. $node_vids[] = $node->vid;
  415. }
  416. }
  417. if (!empty($node_vids)) {
  418. $query = db_select('forum', 'f');
  419. $query
  420. ->fields('f', array('nid', 'tid'))
  421. ->condition('f.vid', $node_vids);
  422. $result = $query->execute();
  423. foreach ($result as $record) {
  424. $nodes[$record->nid]->forum_tid = $record->tid;
  425. }
  426. }
  427. }
  428. /**
  429. * Implements hook_node_info().
  430. */
  431. function forum_node_info() {
  432. return array(
  433. 'forum' => array(
  434. 'name' => t('Forum topic'),
  435. 'base' => 'forum',
  436. 'description' => t('A <em>forum topic</em> starts a new discussion thread within a forum.'),
  437. 'title_label' => t('Subject'),
  438. )
  439. );
  440. }
  441. /**
  442. * Implements hook_permission().
  443. */
  444. function forum_permission() {
  445. $perms = array(
  446. 'administer forums' => array(
  447. 'title' => t('Administer forums'),
  448. ),
  449. );
  450. return $perms;
  451. }
  452. /**
  453. * Implements hook_taxonomy_term_delete().
  454. */
  455. function forum_taxonomy_term_delete($term) {
  456. // For containers, remove the tid from the forum_containers variable.
  457. $containers = variable_get('forum_containers', array());
  458. $key = array_search($term->tid, $containers);
  459. if ($key !== FALSE) {
  460. unset($containers[$key]);
  461. }
  462. variable_set('forum_containers', $containers);
  463. }
  464. /**
  465. * Implements hook_comment_publish().
  466. *
  467. * This actually handles the insertion and update of published nodes since
  468. * comment_save() calls hook_comment_publish() for all published comments.
  469. */
  470. function forum_comment_publish($comment) {
  471. _forum_update_forum_index($comment->nid);
  472. }
  473. /**
  474. * Implements hook_comment_update().
  475. *
  476. * The Comment module doesn't call hook_comment_unpublish() when saving
  477. * individual comments, so we need to check for those here.
  478. */
  479. function forum_comment_update($comment) {
  480. // comment_save() calls hook_comment_publish() for all published comments,
  481. // so we need to handle all other values here.
  482. if (!$comment->status) {
  483. _forum_update_forum_index($comment->nid);
  484. }
  485. }
  486. /**
  487. * Implements hook_comment_unpublish().
  488. */
  489. function forum_comment_unpublish($comment) {
  490. _forum_update_forum_index($comment->nid);
  491. }
  492. /**
  493. * Implements hook_comment_delete().
  494. */
  495. function forum_comment_delete($comment) {
  496. _forum_update_forum_index($comment->nid);
  497. }
  498. /**
  499. * Implements hook_field_storage_pre_insert().
  500. */
  501. function forum_field_storage_pre_insert($entity_type, $entity, &$skip_fields) {
  502. if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
  503. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  504. foreach ($entity->taxonomy_forums as $language) {
  505. foreach ($language as $item) {
  506. $query->values(array(
  507. 'nid' => $entity->nid,
  508. 'title' => $entity->title,
  509. 'tid' => $item['tid'],
  510. 'sticky' => $entity->sticky,
  511. 'created' => $entity->created,
  512. 'comment_count' => 0,
  513. 'last_comment_timestamp' => $entity->created,
  514. ));
  515. }
  516. }
  517. $query->execute();
  518. }
  519. }
  520. /**
  521. * Implements hook_field_storage_pre_update().
  522. */
  523. function forum_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
  524. $first_call = &drupal_static(__FUNCTION__, array());
  525. if ($entity_type == 'node' && _forum_node_check_node_type($entity)) {
  526. // If the node is published, update the forum index.
  527. if ($entity->status) {
  528. // We don't maintain data for old revisions, so clear all previous values
  529. // from the table. Since this hook runs once per field, per object, make
  530. // sure we only wipe values once.
  531. if (!isset($first_call[$entity->nid])) {
  532. $first_call[$entity->nid] = FALSE;
  533. db_delete('forum_index')->condition('nid', $entity->nid)->execute();
  534. }
  535. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  536. foreach ($entity->taxonomy_forums as $language) {
  537. foreach ($language as $item) {
  538. $query->values(array(
  539. 'nid' => $entity->nid,
  540. 'title' => $entity->title,
  541. 'tid' => $item['tid'],
  542. 'sticky' => $entity->sticky,
  543. 'created' => $entity->created,
  544. 'comment_count' => 0,
  545. 'last_comment_timestamp' => $entity->created,
  546. ));
  547. }
  548. }
  549. $query->execute();
  550. // The logic for determining last_comment_count is fairly complex, so
  551. // call _forum_update_forum_index() too.
  552. _forum_update_forum_index($entity->nid);
  553. }
  554. // When a forum node is unpublished, remove it from the forum_index table.
  555. else {
  556. db_delete('forum_index')->condition('nid', $entity->nid)->execute();
  557. }
  558. }
  559. }
  560. /**
  561. * Implements hook_form_FORM_ID_alter() for taxonomy_form_vocabulary().
  562. */
  563. function forum_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_id) {
  564. $vid = variable_get('forum_nav_vocabulary', 0);
  565. if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
  566. $form['help_forum_vocab'] = array(
  567. '#markup' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
  568. '#weight' => -1,
  569. );
  570. // Forum's vocabulary always has single hierarchy. Forums and containers
  571. // have only one parent or no parent for root items. By default this value
  572. // is 0.
  573. $form['hierarchy']['#value'] = 1;
  574. // Do not allow to delete forum's vocabulary.
  575. $form['actions']['delete']['#access'] = FALSE;
  576. }
  577. }
  578. /**
  579. * Implements hook_form_FORM_ID_alter() for taxonomy_form_term().
  580. */
  581. function forum_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
  582. $vid = variable_get('forum_nav_vocabulary', 0);
  583. if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
  584. // Hide multiple parents select from forum terms.
  585. $form['relations']['parent']['#access'] = FALSE;
  586. }
  587. }
  588. /**
  589. * Implements hook_form_BASE_FORM_ID_alter() for node_form().
  590. */
  591. function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
  592. if (isset($form['taxonomy_forums'])) {
  593. $langcode = $form['taxonomy_forums']['#language'];
  594. // Make the vocabulary required for 'real' forum-nodes.
  595. $form['taxonomy_forums'][$langcode]['#required'] = TRUE;
  596. $form['taxonomy_forums'][$langcode]['#multiple'] = FALSE;
  597. if (empty($form['taxonomy_forums'][$langcode]['#default_value'])) {
  598. // If there is no default forum already selected, try to get the forum
  599. // ID from the URL (e.g., if we are on a page like node/add/forum/2, we
  600. // expect "2" to be the ID of the forum that was requested).
  601. $requested_forum_id = arg(3);
  602. $form['taxonomy_forums'][$langcode]['#default_value'] = is_numeric($requested_forum_id) ? $requested_forum_id : '';
  603. }
  604. }
  605. }
  606. /**
  607. * Implements hook_block_info().
  608. */
  609. function forum_block_info() {
  610. $blocks['active'] = array(
  611. 'info' => t('Active forum topics'),
  612. 'cache' => DRUPAL_CACHE_CUSTOM,
  613. 'properties' => array('administrative' => TRUE),
  614. );
  615. $blocks['new'] = array(
  616. 'info' => t('New forum topics'),
  617. 'cache' => DRUPAL_CACHE_CUSTOM,
  618. 'properties' => array('administrative' => TRUE),
  619. );
  620. return $blocks;
  621. }
  622. /**
  623. * Implements hook_block_configure().
  624. */
  625. function forum_block_configure($delta = '') {
  626. $form['forum_block_num_' . $delta] = array(
  627. '#type' => 'select',
  628. '#title' => t('Number of topics'),
  629. '#default_value' => variable_get('forum_block_num_' . $delta, '5'),
  630. '#options' => drupal_map_assoc(range(2, 20))
  631. );
  632. return $form;
  633. }
  634. /**
  635. * Implements hook_block_save().
  636. */
  637. function forum_block_save($delta = '', $edit = array()) {
  638. variable_set('forum_block_num_' . $delta, $edit['forum_block_num_' . $delta]);
  639. }
  640. /**
  641. * Implements hook_block_view().
  642. *
  643. * Generates a block containing the currently active forum topics and the most
  644. * recently added forum topics.
  645. */
  646. function forum_block_view($delta = '') {
  647. $query = db_select('forum_index', 'f')
  648. ->fields('f')
  649. ->addTag('node_access');
  650. switch ($delta) {
  651. case 'active':
  652. $title = t('Active forum topics');
  653. $query
  654. ->orderBy('f.last_comment_timestamp', 'DESC')
  655. ->range(0, variable_get('forum_block_num_active', '5'));
  656. break;
  657. case 'new':
  658. $title = t('New forum topics');
  659. $query
  660. ->orderBy('f.created', 'DESC')
  661. ->range(0, variable_get('forum_block_num_new', '5'));
  662. break;
  663. }
  664. $block['subject'] = $title;
  665. // Cache based on the altered query. Enables us to cache with node access enabled.
  666. $block['content'] = drupal_render_cache_by_query($query, 'forum_block_view');
  667. $block['content']['#access'] = user_access('access content');
  668. return $block;
  669. }
  670. /**
  671. * Render API callback: Lists nodes based on the element's #query property.
  672. *
  673. * This function can be used as a #pre_render callback.
  674. *
  675. * @see forum_block_view()
  676. */
  677. function forum_block_view_pre_render($elements) {
  678. $result = $elements['#query']->execute();
  679. if ($node_title_list = node_title_list($result)) {
  680. $elements['forum_list'] = $node_title_list;
  681. $elements['forum_more'] = array('#theme' => 'more_link', '#url' => 'forum', '#title' => t('Read the latest forum topics.'));
  682. }
  683. return $elements;
  684. }
  685. /**
  686. * Implements hook_form().
  687. */
  688. function forum_form($node, $form_state) {
  689. $type = node_type_get_type($node);
  690. $form['title'] = array(
  691. '#type' => 'textfield',
  692. '#title' => check_plain($type->title_label),
  693. '#default_value' => !empty($node->title) ? $node->title : '',
  694. '#required' => TRUE, '#weight' => -5
  695. );
  696. if (!empty($node->nid)) {
  697. $forum_terms = $node->taxonomy_forums;
  698. // If editing, give option to leave shadows.
  699. $shadow = (count($forum_terms) > 1);
  700. $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.'));
  701. $form['forum_tid'] = array('#type' => 'value', '#value' => $node->forum_tid);
  702. }
  703. return $form;
  704. }
  705. /**
  706. * Returns a tree of all forums for a given taxonomy term ID.
  707. *
  708. * @param $tid
  709. * (optional) Taxonomy term ID of the forum. If not given all forums will be
  710. * returned.
  711. *
  712. * @return
  713. * A tree of taxonomy objects, with the following additional properties:
  714. * - num_topics: Number of topics in the forum.
  715. * - num_posts: Total number of posts in all topics.
  716. * - last_post: Most recent post for the forum.
  717. * - forums: An array of child forums.
  718. */
  719. function forum_forum_load($tid = NULL) {
  720. $cache = &drupal_static(__FUNCTION__, array());
  721. // Return a cached forum tree if available.
  722. if (!isset($tid)) {
  723. $tid = 0;
  724. }
  725. if (isset($cache[$tid])) {
  726. return $cache[$tid];
  727. }
  728. $vid = variable_get('forum_nav_vocabulary', 0);
  729. // Load and validate the parent term.
  730. if ($tid) {
  731. $forum_term = taxonomy_term_load($tid);
  732. if (!$forum_term || ($forum_term->vid != $vid)) {
  733. return $cache[$tid] = FALSE;
  734. }
  735. }
  736. // If $tid is 0, create an empty object to hold the child terms.
  737. elseif ($tid === 0) {
  738. $forum_term = (object) array(
  739. 'tid' => 0,
  740. );
  741. }
  742. // Determine if the requested term is a container.
  743. if (!$forum_term->tid || in_array($forum_term->tid, variable_get('forum_containers', array()))) {
  744. $forum_term->container = 1;
  745. }
  746. // Load parent terms.
  747. $forum_term->parents = taxonomy_get_parents_all($forum_term->tid);
  748. // Load the tree below.
  749. $forums = array();
  750. $_forums = taxonomy_get_tree($vid, $tid);
  751. if (count($_forums)) {
  752. $query = db_select('node', 'n');
  753. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  754. $query->join('forum', 'f', 'n.vid = f.vid');
  755. $query->addExpression('COUNT(n.nid)', 'topic_count');
  756. $query->addExpression('SUM(ncs.comment_count)', 'comment_count');
  757. $counts = $query
  758. ->fields('f', array('tid'))
  759. ->condition('n.status', 1)
  760. ->groupBy('tid')
  761. ->addTag('node_access')
  762. ->execute()
  763. ->fetchAllAssoc('tid');
  764. }
  765. foreach ($_forums as $forum) {
  766. // Determine if the child term is a container.
  767. if (in_array($forum->tid, variable_get('forum_containers', array()))) {
  768. $forum->container = 1;
  769. }
  770. // Merge in the topic and post counters.
  771. if (!empty($counts[$forum->tid])) {
  772. $forum->num_topics = $counts[$forum->tid]->topic_count;
  773. $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
  774. }
  775. else {
  776. $forum->num_topics = 0;
  777. $forum->num_posts = 0;
  778. }
  779. // Query "Last Post" information for this forum.
  780. $query = db_select('node', 'n');
  781. $query->join('users', 'u1', 'n.uid = u1.uid');
  782. $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $forum->tid));
  783. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  784. $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');
  785. $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');
  786. $topic = $query
  787. ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
  788. ->condition('n.status', 1)
  789. ->orderBy('last_comment_timestamp', 'DESC')
  790. ->range(0, 1)
  791. ->addTag('node_access')
  792. ->execute()
  793. ->fetchObject();
  794. // Merge in the "Last Post" information.
  795. $last_post = new stdClass();
  796. if (!empty($topic->last_comment_timestamp)) {
  797. $last_post->created = $topic->last_comment_timestamp;
  798. $last_post->name = $topic->last_comment_name;
  799. $last_post->uid = $topic->last_comment_uid;
  800. }
  801. $forum->last_post = $last_post;
  802. $forums[$forum->tid] = $forum;
  803. }
  804. // Cache the result, and return the tree.
  805. $forum_term->forums = $forums;
  806. $cache[$tid] = $forum_term;
  807. return $forum_term;
  808. }
  809. /**
  810. * Calculates the number of new posts in a forum that the user has not yet read.
  811. *
  812. * Nodes are new if they are newer than NODE_NEW_LIMIT.
  813. *
  814. * @param $term
  815. * The term ID of the forum.
  816. * @param $uid
  817. * The user ID.
  818. *
  819. * @return
  820. * The number of new posts in the forum that have not been read by the user.
  821. */
  822. function _forum_topics_unread($term, $uid) {
  823. $query = db_select('node', 'n');
  824. $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $term));
  825. $query->leftJoin('history', 'h', 'n.nid = h.nid AND h.uid = :uid', array(':uid' => $uid));
  826. $query->addExpression('COUNT(n.nid)', 'count');
  827. return $query
  828. ->condition('status', 1)
  829. ->condition('n.created', NODE_NEW_LIMIT, '>')
  830. ->isNull('h.nid')
  831. ->addTag('node_access')
  832. ->execute()
  833. ->fetchField();
  834. }
  835. /**
  836. * Gets all the topics in a forum.
  837. *
  838. * @param $tid
  839. * The term ID of the forum.
  840. * @param $sortby
  841. * One of the following integers indicating the sort criteria:
  842. * - 1: Date - newest first.
  843. * - 2: Date - oldest first.
  844. * - 3: Posts with the most comments first.
  845. * - 4: Posts with the least comments first.
  846. * @param $forum_per_page
  847. * The maximum number of topics to display per page.
  848. *
  849. * @return
  850. * A list of all the topics in a forum.
  851. */
  852. function forum_get_topics($tid, $sortby, $forum_per_page) {
  853. global $user, $forum_topic_list_header;
  854. $forum_topic_list_header = array(
  855. NULL,
  856. array('data' => t('Topic'), 'field' => 'f.title'),
  857. array('data' => t('Replies'), 'field' => 'f.comment_count'),
  858. array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'),
  859. );
  860. $order = _forum_get_topic_order($sortby);
  861. for ($i = 0; $i < count($forum_topic_list_header); $i++) {
  862. if ($forum_topic_list_header[$i]['field'] == $order['field']) {
  863. $forum_topic_list_header[$i]['sort'] = $order['sort'];
  864. }
  865. }
  866. $query = db_select('forum_index', 'f')->extend('PagerDefault')->extend('TableSort');
  867. $query->fields('f');
  868. $query
  869. ->condition('f.tid', $tid)
  870. ->addTag('node_access')
  871. ->orderBy('f.sticky', 'DESC')
  872. ->orderByHeader($forum_topic_list_header)
  873. ->limit($forum_per_page);
  874. $count_query = db_select('forum_index', 'f');
  875. $count_query->condition('f.tid', $tid);
  876. $count_query->addExpression('COUNT(*)');
  877. $count_query->addTag('node_access');
  878. $query->setCountQuery($count_query);
  879. $result = $query->execute();
  880. $nids = array();
  881. foreach ($result as $record) {
  882. $nids[] = $record->nid;
  883. }
  884. if ($nids) {
  885. $query = db_select('node', 'n')->extend('TableSort');
  886. $query->fields('n', array('title', 'nid', 'type', 'sticky', 'created', 'uid'));
  887. $query->addField('n', 'comment', 'comment_mode');
  888. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  889. $query->fields('ncs', array('cid', 'last_comment_uid', 'last_comment_timestamp', 'comment_count'));
  890. $query->join('forum_index', 'f', 'f.nid = ncs.nid');
  891. $query->addField('f', 'tid', 'forum_tid');
  892. $query->join('users', 'u', 'n.uid = u.uid');
  893. $query->addField('u', 'name');
  894. $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');
  895. $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');
  896. $query
  897. ->orderBy('f.sticky', 'DESC')
  898. ->orderByHeader($forum_topic_list_header)
  899. ->condition('n.nid', $nids);
  900. $result = $query->execute();
  901. }
  902. else {
  903. $result = array();
  904. }
  905. $topics = array();
  906. $first_new_found = FALSE;
  907. foreach ($result as $topic) {
  908. if ($user->uid) {
  909. // A forum is new if the topic is new, or if there are new comments since
  910. // the user's last visit.
  911. if ($topic->forum_tid != $tid) {
  912. $topic->new = 0;
  913. }
  914. else {
  915. $history = _forum_user_last_visit($topic->nid);
  916. $topic->new_replies = comment_num_new($topic->nid, $history);
  917. $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
  918. }
  919. }
  920. else {
  921. // Do not track "new replies" status for topics if the user is anonymous.
  922. $topic->new_replies = 0;
  923. $topic->new = 0;
  924. }
  925. // Make sure only one topic is indicated as the first new topic.
  926. $topic->first_new = FALSE;
  927. if ($topic->new != 0 && !$first_new_found) {
  928. $topic->first_new = TRUE;
  929. $first_new_found = TRUE;
  930. }
  931. if ($topic->comment_count > 0) {
  932. $last_reply = new stdClass();
  933. $last_reply->created = $topic->last_comment_timestamp;
  934. $last_reply->name = $topic->last_comment_name;
  935. $last_reply->uid = $topic->last_comment_uid;
  936. $topic->last_reply = $last_reply;
  937. }
  938. $topics[] = $topic;
  939. }
  940. return $topics;
  941. }
  942. /**
  943. * Preprocesses variables for forums.tpl.php.
  944. *
  945. * @param $variables
  946. * An array containing the following elements:
  947. * - forums: An array of all forum objects to display for the given taxonomy
  948. * term ID. If tid = 0 then all the top-level forums are displayed.
  949. * - topics: An array of all the topics in the current forum.
  950. * - parents: An array of taxonomy term objects that are ancestors of the
  951. * current term ID.
  952. * - tid: Taxonomy term ID of the current forum.
  953. * - sortby: One of the following integers indicating the sort criteria:
  954. * - 1: Date - newest first.
  955. * - 2: Date - oldest first.
  956. * - 3: Posts with the most comments first.
  957. * - 4: Posts with the least comments first.
  958. * - forum_per_page: The maximum number of topics to display per page.
  959. *
  960. * @see forums.tpl.php
  961. */
  962. function template_preprocess_forums(&$variables) {
  963. global $user;
  964. $vid = variable_get('forum_nav_vocabulary', 0);
  965. $vocabulary = taxonomy_vocabulary_load($vid);
  966. $title = !empty($vocabulary->name) ? $vocabulary->name : '';
  967. // Breadcrumb navigation:
  968. $breadcrumb[] = l(t('Home'), NULL);
  969. if ($variables['tid']) {
  970. $breadcrumb[] = l($vocabulary->name, 'forum');
  971. }
  972. if ($variables['parents']) {
  973. $variables['parents'] = array_reverse($variables['parents']);
  974. foreach ($variables['parents'] as $p) {
  975. if ($p->tid == $variables['tid']) {
  976. $title = $p->name;
  977. }
  978. else {
  979. $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
  980. }
  981. }
  982. }
  983. drupal_set_breadcrumb($breadcrumb);
  984. drupal_set_title($title);
  985. if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {
  986. if (!empty($variables['forums'])) {
  987. $variables['forums'] = theme('forum_list', $variables);
  988. }
  989. else {
  990. $variables['forums'] = '';
  991. }
  992. if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
  993. $variables['topics'] = theme('forum_topic_list', $variables);
  994. drupal_add_feed('taxonomy/term/' . $variables['tid'] . '/feed', 'RSS - ' . $title);
  995. }
  996. else {
  997. $variables['topics'] = '';
  998. }
  999. // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
  1000. // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
  1001. if ($variables['forums'] && !$variables['topics']) {
  1002. $variables['theme_hook_suggestions'][] = 'forums__containers';
  1003. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  1004. $variables['theme_hook_suggestions'][] = 'forums__containers__' . $variables['tid'];
  1005. }
  1006. elseif (!$variables['forums'] && $variables['topics']) {
  1007. $variables['theme_hook_suggestions'][] = 'forums__topics';
  1008. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  1009. $variables['theme_hook_suggestions'][] = 'forums__topics__' . $variables['tid'];
  1010. }
  1011. else {
  1012. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  1013. }
  1014. }
  1015. else {
  1016. drupal_set_title(t('No forums defined'));
  1017. $variables['forums'] = '';
  1018. $variables['topics'] = '';
  1019. }
  1020. }
  1021. /**
  1022. * Preprocesses variables for forum-list.tpl.php.
  1023. *
  1024. * @param $variables
  1025. * An array containing the following elements:
  1026. * - forums: An array of all forum objects to display for the given taxonomy
  1027. * term ID. If tid = 0 then all the top-level forums are displayed.
  1028. * - parents: An array of taxonomy term objects that are ancestors of the
  1029. * current term ID.
  1030. * - tid: Taxonomy term ID of the current forum.
  1031. *
  1032. * @see forum-list.tpl.php
  1033. * @see theme_forum_list()
  1034. */
  1035. function template_preprocess_forum_list(&$variables) {
  1036. global $user;
  1037. $row = 0;
  1038. // Sanitize each forum so that the template can safely print the data.
  1039. foreach ($variables['forums'] as $id => $forum) {
  1040. $variables['forums'][$id]->description = !empty($forum->description) ? filter_xss_admin($forum->description) : '';
  1041. $variables['forums'][$id]->link = url("forum/$forum->tid");
  1042. $variables['forums'][$id]->name = check_plain($forum->name);
  1043. $variables['forums'][$id]->is_container = !empty($forum->container);
  1044. $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
  1045. $row++;
  1046. $variables['forums'][$id]->new_text = '';
  1047. $variables['forums'][$id]->new_url = '';
  1048. $variables['forums'][$id]->new_topics = 0;
  1049. $variables['forums'][$id]->old_topics = $forum->num_topics;
  1050. $variables['forums'][$id]->icon_class = 'default';
  1051. $variables['forums'][$id]->icon_title = t('No new posts');
  1052. if ($user->uid) {
  1053. $variables['forums'][$id]->new_topics = _forum_topics_unread($forum->tid, $user->uid);
  1054. if ($variables['forums'][$id]->new_topics) {
  1055. $variables['forums'][$id]->new_text = format_plural($variables['forums'][$id]->new_topics, '1 new', '@count new');
  1056. $variables['forums'][$id]->new_url = url("forum/$forum->tid", array('fragment' => 'new'));
  1057. $variables['forums'][$id]->icon_class = 'new';
  1058. $variables['forums'][$id]->icon_title = t('New posts');
  1059. }
  1060. $variables['forums'][$id]->old_topics = $forum->num_topics - $variables['forums'][$id]->new_topics;
  1061. }
  1062. $variables['forums'][$id]->last_reply = theme('forum_submitted', array('topic' => $forum->last_post));
  1063. }
  1064. // Give meaning to $tid for themers. $tid actually stands for term id.
  1065. $variables['forum_id'] = $variables['tid'];
  1066. unset($variables['tid']);
  1067. }
  1068. /**
  1069. * Preprocesses variables for forum-topic-list.tpl.php.
  1070. *
  1071. * @param $variables
  1072. * An array containing the following elements:
  1073. * - tid: Taxonomy term ID of the current forum.
  1074. * - topics: An array of all the topics in the current forum.
  1075. * - forum_per_page: The maximum number of topics to display per page.
  1076. *
  1077. * @see forum-topic-list.tpl.php
  1078. * @see theme_forum_topic_list()
  1079. */
  1080. function template_preprocess_forum_topic_list(&$variables) {
  1081. global $forum_topic_list_header;
  1082. // Create the tablesorting header.
  1083. $ts = tablesort_init($forum_topic_list_header);
  1084. $header = '';
  1085. foreach ($forum_topic_list_header as $cell) {
  1086. $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
  1087. $header .= _theme_table_cell($cell, TRUE);
  1088. }
  1089. $variables['header'] = $header;
  1090. if (!empty($variables['topics'])) {
  1091. $row = 0;
  1092. foreach ($variables['topics'] as $id => $topic) {
  1093. $variables['topics'][$id]->icon = theme('forum_icon', array('new_posts' => $topic->new, 'num_posts' => $topic->comment_count, 'comment_mode' => $topic->comment_mode, 'sticky' => $topic->sticky, 'first_new' => $topic->first_new));
  1094. $variables['topics'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
  1095. $row++;
  1096. // We keep the actual tid in forum table, if it's different from the
  1097. // current tid then it means the topic appears in two forums, one of
  1098. // them is a shadow copy.
  1099. if ($variables['tid'] != $topic->forum_tid) {
  1100. $variables['topics'][$id]->moved = TRUE;
  1101. $variables['topics'][$id]->title = check_plain($topic->title);
  1102. $variables['topics'][$id]->message = l(t('This topic has been moved'), "forum/$topic->forum_tid");
  1103. }
  1104. else {
  1105. $variables['topics'][$id]->moved = FALSE;
  1106. $variables['topics'][$id]->title = l($topic->title, "node/$topic->nid");
  1107. $variables['topics'][$id]->message = '';
  1108. }
  1109. $variables['topics'][$id]->created = theme('forum_submitted', array('topic' => $topic));
  1110. $variables['topics'][$id]->last_reply = theme('forum_submitted', array('topic' => isset($topic->last_reply) ? $topic->last_reply : NULL));
  1111. $variables['topics'][$id]->new_text = '';
  1112. $variables['topics'][$id]->new_url = '';
  1113. if ($topic->new_replies) {
  1114. $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new', '@count new');
  1115. $variables['topics'][$id]->new_url = url("node/$topic->nid", array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic), 'fragment' => 'new'));
  1116. }
  1117. }
  1118. }
  1119. else {
  1120. // Make this safe for the template.
  1121. $variables['topics'] = array();
  1122. }
  1123. // Give meaning to $tid for themers. $tid actually stands for term id.
  1124. $variables['topic_id'] = $variables['tid'];
  1125. unset($variables['tid']);
  1126. $variables['pager'] = theme('pager');
  1127. }
  1128. /**
  1129. * Preprocesses variables for forum-icon.tpl.php.
  1130. *
  1131. * @param $variables
  1132. * An array containing the following elements:
  1133. * - new_posts: Indicates whether or not the topic contains new posts.
  1134. * - num_posts: The total number of posts in all topics.
  1135. * - comment_mode: An integer indicating whether comments are open, closed,
  1136. * or hidden.
  1137. * - sticky: Indicates whether the topic is sticky.
  1138. * - first_new: Indicates whether this is the first topic with new posts.
  1139. *
  1140. * @see forum-icon.tpl.php
  1141. * @see theme_forum_icon()
  1142. */
  1143. function template_preprocess_forum_icon(&$variables) {
  1144. $variables['hot_threshold'] = variable_get('forum_hot_topic', 15);
  1145. if ($variables['num_posts'] > $variables['hot_threshold']) {
  1146. $variables['icon_class'] = $variables['new_posts'] ? 'hot-new' : 'hot';
  1147. $variables['icon_title'] = $variables['new_posts'] ? t('Hot topic, new comments') : t('Hot topic');
  1148. }
  1149. else {
  1150. $variables['icon_class'] = $variables['new_posts'] ? 'new' : 'default';
  1151. $variables['icon_title'] = $variables['new_posts'] ? t('New comments') : t('Normal topic');
  1152. }
  1153. if ($variables['comment_mode'] == COMMENT_NODE_CLOSED || $variables['comment_mode'] == COMMENT_NODE_HIDDEN) {
  1154. $variables['icon_class'] = 'closed';
  1155. $variables['icon_title'] = t('Closed topic');
  1156. }
  1157. if ($variables['sticky'] == 1) {
  1158. $variables['icon_class'] = 'sticky';
  1159. $variables['icon_title'] = t('Sticky topic');
  1160. }
  1161. }
  1162. /**
  1163. * Preprocesses variables for forum-submitted.tpl.php.
  1164. *
  1165. * The submission information will be displayed in the forum list and topic
  1166. * list.
  1167. *
  1168. * @param $variables
  1169. * An array containing the following elements:
  1170. * - topic: The topic object.
  1171. *
  1172. * @see forum-submitted.tpl.php
  1173. * @see theme_forum_submitted()
  1174. */
  1175. function template_preprocess_forum_submitted(&$variables) {
  1176. $variables['author'] = isset($variables['topic']->uid) ? theme('username', array('account' => $variables['topic'])) : '';
  1177. $variables['time'] = isset($variables['topic']->created) ? format_interval(REQUEST_TIME - $variables['topic']->created) : '';
  1178. }
  1179. /**
  1180. * Gets the last time the user viewed a node.
  1181. *
  1182. * @param $nid
  1183. * The node ID.
  1184. *
  1185. * @return
  1186. * The timestamp when the user last viewed this node, if the user has
  1187. * previously viewed the node; otherwise NODE_NEW_LIMIT.
  1188. */
  1189. function _forum_user_last_visit($nid) {
  1190. global $user;
  1191. $history = &drupal_static(__FUNCTION__, array());
  1192. if (empty($history)) {
  1193. $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid', array(':uid' => $user->uid));
  1194. foreach ($result as $t) {
  1195. $history[$t->nid] = $t->timestamp > NODE_NEW_LIMIT ? $t->timestamp : NODE_NEW_LIMIT;
  1196. }
  1197. }
  1198. return isset($history[$nid]) ? $history[$nid] : NODE_NEW_LIMIT;
  1199. }
  1200. /**
  1201. * Gets topic sorting information based on an integer code.
  1202. *
  1203. * @param $sortby
  1204. * One of the following integers indicating the sort criteria:
  1205. * - 1: Date - newest first.
  1206. * - 2: Date - oldest first.
  1207. * - 3: Posts with the most comments first.
  1208. * - 4: Posts with the least comments first.
  1209. *
  1210. * @return
  1211. * An array with the following values:
  1212. * - field: A field for an SQL query.
  1213. * - sort: 'asc' or 'desc'.
  1214. */
  1215. function _forum_get_topic_order($sortby) {
  1216. switch ($sortby) {
  1217. case 1:
  1218. return array('field' => 'f.last_comment_timestamp', 'sort' => 'desc');
  1219. break;
  1220. case 2:
  1221. return array('field' => 'f.last_comment_timestamp', 'sort' => 'asc');
  1222. break;
  1223. case 3:
  1224. return array('field' => 'f.comment_count', 'sort' => 'desc');
  1225. break;
  1226. case 4:
  1227. return array('field' => 'f.comment_count', 'sort' => 'asc');
  1228. break;
  1229. }
  1230. }
  1231. /**
  1232. * Updates the taxonomy index for a given node.
  1233. *
  1234. * @param $nid
  1235. * The ID of the node to update.
  1236. */
  1237. function _forum_update_forum_index($nid) {
  1238. $count = db_query('SELECT COUNT(cid) FROM {comment} c INNER JOIN {forum_index} i ON c.nid = i.nid WHERE c.nid = :nid AND c.status = :status', array(
  1239. ':nid' => $nid,
  1240. ':status' => COMMENT_PUBLISHED,
  1241. ))->fetchField();
  1242. if ($count > 0) {
  1243. // Comments exist.
  1244. $last_reply = db_query_range('SELECT cid, name, created, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
  1245. ':nid' => $nid,
  1246. ':status' => COMMENT_PUBLISHED,
  1247. ))->fetchObject();
  1248. db_update('forum_index')
  1249. ->fields( array(
  1250. 'comment_count' => $count,
  1251. 'last_comment_timestamp' => $last_reply->created,
  1252. ))
  1253. ->condition('nid', $nid)
  1254. ->execute();
  1255. }
  1256. else {
  1257. // Comments do not exist.
  1258. $node = db_query('SELECT uid, created FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  1259. db_update('forum_index')
  1260. ->fields( array(
  1261. 'comment_count' => 0,
  1262. 'last_comment_timestamp' => $node->created,
  1263. ))
  1264. ->condition('nid', $nid)
  1265. ->execute();
  1266. }
  1267. }
  1268. /**
  1269. * Implements hook_rdf_mapping().
  1270. */
  1271. function forum_rdf_mapping() {
  1272. return array(
  1273. array(
  1274. 'type' => 'node',
  1275. 'bundle' => 'forum',
  1276. 'mapping' => array(
  1277. 'rdftype' => array('sioc:Post', 'sioct:BoardPost'),
  1278. 'taxonomy_forums' => array(
  1279. 'predicates' => array('sioc:has_container'),
  1280. 'type' => 'rel',
  1281. ),
  1282. ),
  1283. ),
  1284. array(
  1285. 'type' => 'taxonomy_term',
  1286. 'bundle' => 'forums',
  1287. 'mapping' => array(
  1288. 'rdftype' => array('sioc:Container', 'sioc:Forum'),
  1289. ),
  1290. ),
  1291. );
  1292. }