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