1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* User page callbacks for the forum module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Menu callback; prints a forum listing.
|
10 |
|
|
*/
|
11 |
|
|
function advanced_forum_page($forum_term = NULL) {
|
12 |
|
|
if (!isset($forum_term)) {
|
13 |
|
|
// On the main page, display all the top-level forums.
|
14 |
|
|
$forum_term = advanced_forum_forum_load(0);
|
15 |
|
|
}
|
16 |
|
|
|
17 |
13c3c9b4
|
Assos Assos
|
// Set tid for <root> container.
|
18 |
85ad3d82
|
Assos Assos
|
if (!isset($forum_term->tid)) {
|
19 |
|
|
$forum_term->tid = 0;
|
20 |
|
|
}
|
21 |
|
|
_advanced_forum_add_files();
|
22 |
|
|
|
23 |
|
|
$forum_per_page = variable_get('forum_per_page', 25);
|
24 |
|
|
$sortby = variable_get('forum_order', 1);
|
25 |
|
|
|
26 |
|
|
if (empty($forum_term->container)) {
|
27 |
|
|
$topics = advanced_forum_get_topics($forum_term->tid, $sortby, $forum_per_page);
|
28 |
|
|
}
|
29 |
|
|
else {
|
30 |
|
|
$topics = '';
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
$vid = variable_get('forum_nav_vocabulary', 0);
|
34 |
|
|
$vocabulary = taxonomy_vocabulary_load($vid);
|
35 |
|
|
|
36 |
|
|
// Breadcrumb navigation:
|
37 |
|
|
$breadcrumb[] = l(t('Home'), NULL);
|
38 |
|
|
if ($forum_term->tid) {
|
39 |
|
|
$breadcrumb[] = l($vocabulary->name, 'forum');
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
if ($forum_term->parents) {
|
43 |
|
|
foreach (array_reverse($forum_term->parents) as $p) {
|
44 |
|
|
if ($p->tid != $forum_term->tid) {
|
45 |
|
|
$breadcrumb[] = l($p->name, 'forum/' . $p->tid);
|
46 |
|
|
}
|
47 |
|
|
else {
|
48 |
|
|
$title = $p->name;
|
49 |
|
|
}
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
if (empty($title)) {
|
54 |
|
|
$title = $vocabulary->name;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
if (!variable_get('advanced_forum_disable_breadcrumbs', FALSE)) {
|
58 |
|
|
drupal_set_breadcrumb($breadcrumb);
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
drupal_set_title($title);
|
62 |
|
|
|
63 |
|
|
return theme('forums', array(
|
64 |
|
|
'forums' => $forum_term->forums,
|
65 |
|
|
'topics' => $topics,
|
66 |
|
|
'parents' => $forum_term->parents,
|
67 |
|
|
'tid' => $forum_term->tid,
|
68 |
|
|
'sortby' => $sortby,
|
69 |
13c3c9b4
|
Assos Assos
|
'forums_per_page' => $forum_per_page,
|
70 |
85ad3d82
|
Assos Assos
|
));
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Returns a tree of all forums for a given taxonomy term ID.
|
75 |
|
|
*
|
76 |
|
|
* This is copied from the forum module and adapted.
|
77 |
|
|
*
|
78 |
13c3c9b4
|
Assos Assos
|
* @param int|null $tid
|
79 |
|
|
* (optional) Taxonomy ID of the forum, if not givin all forums will be returned.
|
80 |
|
|
*
|
81 |
|
|
* @return object
|
82 |
85ad3d82
|
Assos Assos
|
* A tree of taxonomy objects, with the following additional properties:
|
83 |
|
|
* - 'num_topics': Number of topics in the forum
|
84 |
|
|
* - 'num_posts': Total number of posts in all topics
|
85 |
|
|
* - 'last_post': Most recent post for the forum
|
86 |
|
|
* - 'forums': An array of child forums
|
87 |
|
|
*/
|
88 |
|
|
function advanced_forum_forum_load($tid = NULL) {
|
89 |
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
90 |
|
|
|
91 |
13c3c9b4
|
Assos Assos
|
global $language;
|
92 |
85ad3d82
|
Assos Assos
|
|
93 |
|
|
// Return a cached forum tree if available.
|
94 |
c169e7c4
|
Assos Assos
|
if (empty($tid)) {
|
95 |
85ad3d82
|
Assos Assos
|
$tid = 0;
|
96 |
|
|
}
|
97 |
|
|
if (isset($cache[$tid])) {
|
98 |
|
|
return $cache[$tid];
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
// Find out from the style's .info how many posts per forum to collect.
|
102 |
|
|
$info = advanced_forum_style_info();
|
103 |
|
|
$post_count = isset($info['forum list post count']) ? intval($info['forum list post count']) : 1;
|
104 |
|
|
|
105 |
|
|
$vid = variable_get('forum_nav_vocabulary', 0);
|
106 |
|
|
|
107 |
|
|
// Load and validate the parent term.
|
108 |
|
|
if ($tid) {
|
109 |
|
|
$forum_term = taxonomy_term_load($tid);
|
110 |
|
|
|
111 |
|
|
if (module_exists('i18n_taxonomy')) {
|
112 |
|
|
$forum_term = i18n_taxonomy_localize_terms($forum_term);
|
113 |
|
|
}
|
114 |
|
|
if (!$forum_term || ($forum_term->vid != $vid)) {
|
115 |
|
|
return $cache[$tid] = FALSE;
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
// If $tid is 0, create an empty object to hold the child terms.
|
119 |
|
|
elseif ($tid === 0) {
|
120 |
|
|
$forum_term = (object) array(
|
121 |
13c3c9b4
|
Assos Assos
|
'tid' => 0,
|
122 |
85ad3d82
|
Assos Assos
|
);
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
// Determine if the requested term is a container.
|
126 |
|
|
if (!$forum_term->tid || in_array($forum_term->tid, variable_get('forum_containers', array()))) {
|
127 |
|
|
$forum_term->container = 1;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
// Load parent terms.
|
131 |
|
|
$forum_term->parents = taxonomy_get_parents_all($forum_term->tid);
|
132 |
|
|
|
133 |
|
|
// Load the tree below.
|
134 |
|
|
$forums = array();
|
135 |
|
|
$_forums = taxonomy_get_tree($vid, $tid);
|
136 |
|
|
|
137 |
|
|
if (module_exists('i18n_taxonomy')) {
|
138 |
|
|
$_forums = i18n_taxonomy_localize_terms($_forums);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
13c3c9b4
|
Assos Assos
|
if ($cached = cache_get('adv_forum_counts' . $tid . ':' . $language->language, 'cache')) {
|
142 |
c169e7c4
|
Assos Assos
|
if ($cached->expire > time()) {
|
143 |
|
|
$counts = $cached->data;
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
if (count($_forums) && empty($counts)) {
|
147 |
85ad3d82
|
Assos Assos
|
$query = db_select('node', 'n');
|
148 |
|
|
$query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
|
149 |
|
|
$query->join('forum_index', 'f', 'n.nid = f.nid');
|
150 |
|
|
$query->addExpression('COUNT(DISTINCT(f.nid))', 'topic_count');
|
151 |
|
|
$query->addExpression('SUM(f.comment_count)', 'comment_count');
|
152 |
ba09eb79
|
Assos Assos
|
$counts = $query
|
153 |
|
|
->fields('f', array('tid'))
|
154 |
|
|
->condition('status', 1)
|
155 |
|
|
->groupBy('tid')
|
156 |
|
|
->addTag('node_access')
|
157 |
|
|
->execute()
|
158 |
|
|
->fetchAllAssoc('tid');
|
159 |
85ad3d82
|
Assos Assos
|
|
160 |
|
|
// Limit the query to only node types that can actually be associated with
|
161 |
|
|
// the forum vocabulary.
|
162 |
|
|
if ($vid) {
|
163 |
|
|
$vocabulary = taxonomy_vocabulary_load($vid);
|
164 |
|
|
$vocabulary_fields = field_read_fields(array('module' => 'taxonomy'));
|
165 |
|
|
$node_types = array();
|
166 |
|
|
foreach ($vocabulary_fields as $field_name => $vocabulary_field) {
|
167 |
|
|
foreach ($vocabulary_field['settings']['allowed_values'] as $key => $allowed_value) {
|
168 |
|
|
if ($allowed_value['vocabulary'] == $vocabulary->machine_name) {
|
169 |
|
|
$field_info = field_info_field($field_name);
|
170 |
|
|
if (isset($field_info['bundles']['node'])) {
|
171 |
|
|
$node_types += $field_info['bundles']['node'];
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
if (!empty($node_types)) {
|
178 |
|
|
$query->condition('type', $node_types, 'IN');
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
|
182 |
ba09eb79
|
Assos Assos
|
|
183 |
|
|
cache_set('adv_forum_counts' . $tid, $counts, 'cache', time() + 60*60);
|
184 |
85ad3d82
|
Assos Assos
|
}
|
185 |
|
|
|
186 |
13c3c9b4
|
Assos Assos
|
// Get last nodes with comments for subquery SELECT optimization.
|
187 |
c169e7c4
|
Assos Assos
|
$subquery = db_select('node_comment_statistics', 'lnsc')
|
188 |
|
|
->fields('lnsc', array('nid'))
|
189 |
|
|
->distinct()
|
190 |
|
|
->orderBy('last_comment_timestamp', 'DESC')
|
191 |
ba09eb79
|
Assos Assos
|
->range(0, variable_get('advanced_forum_last_post_query', 10000));
|
192 |
13c3c9b4
|
Assos Assos
|
// Should be moved to options or additionally calculated elsewhere.
|
193 |
85ad3d82
|
Assos Assos
|
foreach ($_forums as $forum) {
|
194 |
|
|
// Determine if the child term is a container.
|
195 |
|
|
if (in_array($forum->tid, variable_get('forum_containers', array()))) {
|
196 |
|
|
$forum->container = 1;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// Merge in the topic and post counters.
|
200 |
|
|
if (!empty($counts[$forum->tid])) {
|
201 |
|
|
$forum->num_topics = $counts[$forum->tid]->topic_count;
|
202 |
|
|
$forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
|
203 |
|
|
}
|
204 |
|
|
else {
|
205 |
|
|
$forum->num_topics = 0;
|
206 |
|
|
$forum->num_posts = 0;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
// Query "Last Post" information for this forum.
|
210 |
|
|
$query = db_select('node', 'n');
|
211 |
c169e7c4
|
Assos Assos
|
$query->join($subquery, 'lastnodes', 'n.nid = lastnodes.nid');
|
212 |
85ad3d82
|
Assos Assos
|
$query->join('forum_index', 'f', 'n.nid = f.nid AND f.tid = :tid', array(':tid' => $forum->tid));
|
213 |
|
|
$query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
|
214 |
|
|
$query->join('users', 'u', 'ncs.last_comment_uid = u.uid');
|
215 |
|
|
$query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u.name END', 'last_comment_name');
|
216 |
|
|
|
217 |
|
|
$topics = $query
|
218 |
|
|
->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
|
219 |
|
|
->fields('n', array('nid', 'title', 'type'))
|
220 |
|
|
->condition('n.status', 1)
|
221 |
|
|
->orderBy('last_comment_timestamp', 'DESC')
|
222 |
|
|
->range(0, $post_count)
|
223 |
|
|
->addTag('node_access')
|
224 |
|
|
->execute();
|
225 |
|
|
|
226 |
|
|
while ($topic = $topics->fetchObject()) {
|
227 |
|
|
// Merge in the "Last Post" information.
|
228 |
|
|
$last_post = new stdClass();
|
229 |
|
|
if (!empty($topic->last_comment_timestamp)) {
|
230 |
|
|
$last_post->nid = $topic->nid;
|
231 |
|
|
$last_post->node_title = $topic->title;
|
232 |
|
|
$last_post->type = $topic->type;
|
233 |
|
|
$last_post->created = $topic->last_comment_timestamp;
|
234 |
|
|
$last_post->name = $topic->last_comment_name;
|
235 |
|
|
$last_post->uid = $topic->last_comment_uid;
|
236 |
|
|
}
|
237 |
|
|
if ($post_count > 1) {
|
238 |
|
|
$forum->last_post[] = $last_post;
|
239 |
|
|
}
|
240 |
|
|
else {
|
241 |
|
|
$forum->last_post = $last_post;
|
242 |
|
|
}
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
$forums[$forum->tid] = $forum;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
// Cache the result, and return the tree.
|
249 |
|
|
$forum_term->forums = $forums;
|
250 |
|
|
$cache[$tid] = $forum_term;
|
251 |
|
|
return $forum_term;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
13c3c9b4
|
Assos Assos
|
/**
|
255 |
|
|
* Update parent post count.
|
256 |
|
|
*/
|
257 |
85ad3d82
|
Assos Assos
|
function _advanced_forum_update_parent_post_count(&$forums, $forum) {
|
258 |
|
|
foreach ($forum->parents as $parent_tid) {
|
259 |
|
|
if (!empty($forums[$parent_tid])) {
|
260 |
|
|
$forums[$parent_tid]->num_topics += $forum->num_topics;
|
261 |
|
|
$forums[$parent_tid]->num_posts += $forum->num_posts;
|
262 |
|
|
|
263 |
13c3c9b4
|
Assos Assos
|
// Recursive loop to update all parents/
|
264 |
85ad3d82
|
Assos Assos
|
if (!empty($forums[$parent_tid]->parents)) {
|
265 |
|
|
_advanced_forum_update_parent_post_count($forums, $forums[$parent_tid]);
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
}
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
/**
|
272 |
|
|
* This is copied from the forum module and adapted.
|
273 |
|
|
*/
|
274 |
|
|
function advanced_forum_get_topics($tid, $sortby, $forum_per_page, $sort_form = TRUE) {
|
275 |
|
|
$term = taxonomy_term_load($tid);
|
276 |
|
|
drupal_add_feed('taxonomy/term/' . $tid . '/feed', 'RSS - ' . check_plain($term->name));
|
277 |
|
|
|
278 |
13c3c9b4
|
Assos Assos
|
// Views handles this page.
|
279 |
85ad3d82
|
Assos Assos
|
$view = views_get_view('advanced_forum_topic_list');
|
280 |
|
|
$view->set_items_per_page($forum_per_page);
|
281 |
|
|
$view->sort_form = $sort_form;
|
282 |
|
|
|
283 |
|
|
return $view->preview('default', array($tid));
|
284 |
|
|
} |