forum.module 48 KB

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