-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubforums.php
More file actions
430 lines (375 loc) · 14.3 KB
/
Copy pathsubforums.php
File metadata and controls
430 lines (375 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
require_once './include/api/forums.php';
/**
* Handle "mark read" functionality for forums that have subforums.
*/
function phorum_mod_subforums_page_index()
{
global $PHORUM;
// Check if we have to handle "mark read".
if (empty($PHORUM['args'][1]) || $PHORUM['args'][1] != 'markread' ||
!$PHORUM['DATA']['LOGGEDIN'] ||
// These shouldn't happen
!isset($PHORUM['forum_id']) || !isset($PHORUM['parent_id'])) return;
// Check if the parent of the required forum_id is a second level
// folder. In our subforums system, second level folders are used
// for grouping a forum and its subforums.
$parent = phorum_api_forums_get($PHORUM['parent_id']);
if (empty($parent) ||
$parent['forum_id'] == $parent['vroot'] ||
$parent['parent_id'] == $parent['vroot']) return;
// Yes, we have a second level folder.
// Mark all forums inside the folder read.
// Retrieve the forums inside the folder.
$forums = phorum_api_forums_get(NULL, $PHORUM['parent_id']);
// Handle marking the contained forums as read.
unset($PHORUM['user']['newinfo']);
foreach ($forums as $forum) {
phorum_db_newflag_allread($forum['forum_id']);
if ($PHORUM['cache_newflags']) {
$newflagkey = $forum['forum_id'].'-'.$PHORUM['user']['user_id'];
phorum_cache_remove('newflags', $newflagkey);
phorum_cache_remove('newflags_index', $newflagkey);
}
}
// No further action. We'll let the regular mark read code
// handle the redirection to a fresh index page.
}
/**
* Handle mark read for subforums on a list page.
*/
function phorum_mod_subforums_page_list()
{
global $PHORUM;
// Check if we have to handle "mark read".
if (empty($PHORUM['args'][1]) || $PHORUM['args'][1] != 'markread' ||
empty($PHORUM['args'][2]) || !is_numeric($PHORUM['args'][2]) ||
!$PHORUM['DATA']['LOGGEDIN'] ||
// These shouldn't happen
!isset($PHORUM['forum_id']) || !isset($PHORUM['parent_id'])) return;
// setting all posts read
unset($PHORUM['user']['newinfo']);
phorum_db_newflag_allread();
if ($PHORUM['cache_newflags']) {
$newflagkey = $PHORUM['forum_id'].'-'.$PHORUM['user']['user_id'];
phorum_cache_remove('newflags',$newflagkey);
phorum_cache_remove('newflags_index',$newflagkey);
}
// Redirect to a fresh list page without markread in the url.
$dest_url = phorum_get_url(PHORUM_LIST_URL, $PHORUM['args'][2]);
phorum_redirect_by_url($dest_url);
exit();
}
/**
* Replace second level folders in the index with forum/subforum structures.
*
* @param array $index
* An array of items to show on the index page.
*
* @return array
* The possibly modified array of index page items.
*/
function phorum_mod_subforums_index($index)
{
global $PHORUM;
$map = array();
$replaced = array();
foreach ($index as $id => $item)
{
// Skip forums.
if (empty($item['folder_flag'])) continue;
// Skip the vroot folder.
if ($item['forum_id'] == $item['vroot']) continue;
// Skip folder if the index view is showing this folder
// (in which case the folder is shown as a category header.)
if ($item['forum_id'] == $PHORUM['forum_id']) continue;
// Skip folders that have the (v)root at their parent. These
// are the category folders.
if ($item['parent_id'] == $item['vroot']) continue;
// When we get here, then we are at a second level folder, for which
// we will modify the index to show the first forum inside as the
// main forum and the other forums inside as the subforums for that
// forum.
// Retrieve the forums inside the folder.
$forums = phorum_api_forums_get(NULL, $item['forum_id']);
// Create a replacement item for the folder item.
unset($newitem); // prevent reusing a copy
foreach ($forums as $forum)
{
// Just in case an admin put an extra level of folders in the
// subforums grouping folder. This is not supported. We silently
// ignore this folder.
if ($forum['folder_flag']) continue;
// Skip the forum if the user is not allowed to read it and
// the hide forums feature is enabled.
if ($PHORUM['hide_forums'] && !phorum_api_user_check_access(
PHORUM_USER_ALLOW_READ, $forum['forum_id'])) continue;
// The first forum in the folder is the main forum.
if (!isset($newitem))
{
$newitem = subforums_format_forum($forum);
$newitem['subforums'] = array();
$map[$forum['forum_id']] =& $newitem;
}
// The rest of the forums are subforums.
else
{
unset($subforum); // prevent reusing a copy
$subforum = subforums_format_forum($forum);
$newitem['subforums'][] =& $subforum;
$map[$forum['forum_id']] =& $subforum;
}
}
// If one or more visible forums were found in the folder, then
// we replace the folder with the main forum.
if (isset($newitem)) {
$index[$id] =& $newitem;
$replaced[] = $id;
}
// No visible forums were found in the folder. We can delete the
// complete folder from the index.
else {
unset($index[$id]);
}
}
// Do checks for new messages.
// In a future version of this module, we will be able to make
// use of the newflags API calls, but currently, these are still
// under development.
if ($PHORUM['DATA']['LOGGEDIN'] && !empty($map))
{
if ($PHORUM['show_new_on_index']==2)
{
$new_checks = phorum_db_newflag_check(array_keys($map));
foreach ($new_checks as $forum_id => $checks) {
if (!empty($checks)) {
$map[$forum_id]['new_message_check'] = TRUE;
} else {
$map[$forum_id]['new_message_check'] = FALSE;
}
}
}
elseif ($PHORUM['show_new_on_index']==1)
{
$new_counts = phorum_db_newflag_count(array_keys($map));
foreach ($new_counts as $forum_id => $counts)
{
$map[$forum_id]['new_messages'] = number_format(
$counts['messages'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
$map[$forum_id]['new_threads'] = number_format(
$counts['threads'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
}
}
}
// Format the subforums. These are added to the description
// of the main forum. The formatted code is based on the module's
// "index" template.
foreach ($replaced as $id)
{
if (!empty($index[$id]['subforums']))
{
$PHORUM['DATA']['SUBFORUMCOUNT'] = count($index[$id]['subforums']);
$PHORUM['DATA']['SUBFORUMS'] = $index[$id]['subforums'];
ob_start();
include phorum_get_template('subforums::index');
$append = ob_get_contents();
ob_end_clean();
$index[$id]['description'] .= $append;
}
}
return $index;
}
/**
* Fix breadcrumbs for the subforums system.
*
* Second level folders should not be showns, because these are only
* in use for grouping a forum and its subforums. This hook replaces
* a second level folder with the main forum from that folder.
*/
function phorum_mod_subforums_start_output()
{
global $PHORUM;
$seen_folder = FALSE;
$fixed_forum_id = NULL;
foreach ($PHORUM['DATA']['BREADCRUMBS'] as $id => $item)
{
if (isset($item['TYPE']) && $item['TYPE'] == 'folder')
{
if ($seen_folder)
{
// We have a second level folder. Fix this item.
$forums = phorum_api_forums_get(NULL, $item['ID']);
if (!empty($forums)) {
$forum = array_shift($forums);
$PHORUM['DATA']['BREADCRUMBS'][$id] = array(
'URL' => phorum_get_url(
PHORUM_LIST_URL, $forum['forum_id']
),
'TEXT' => $forum['name'],
'ID' => $forum['forum_id'],
'TYPE' => 'forum'
);
$fixed_forum_id = $forum['forum_id'];
}
} else {
$seen_folder = TRUE;
}
continue;
} else {
$seen_folder = FALSE;
}
// When we are in a main forum, then the breadcrumbs look
// like Home -> Category -> Forum X -> Forum X
// after fixing a folder item. Here we filter out the
// last "Forum X".
if ($fixed_forum_id !== NULL &&
isset($item['TYPE']) && $item['TYPE'] == 'forum' &&
$item['ID'] == $fixed_forum_id) {
unset($PHORUM['DATA']['BREADCRUMBS'][$id]);
}
}
}
/**
* Display subforums for a forum on the message list page.
*/
function phorum_mod_subforums_after_header()
{
global $PHORUM;
// We only run this hook on the list page.
if (phorum_page != 'list') return;
// Check if the parent of the current forum_id is a second level
// folder. In our subforums system, second level folders are used
// for grouping a forum and its subforums.
$parent = phorum_api_forums_get($PHORUM['parent_id']);
if (empty($parent) ||
$parent['forum_id'] == $parent['vroot'] ||
$parent['parent_id'] == $parent['vroot']) return;
// Yes, we have a second level folder.
// Retrieve the forums inside the folder.
$forums = phorum_api_forums_get(NULL, $PHORUM['parent_id']);
// If the first forum in the list is the current forum, then we
// are in the main forum. In other cases, we are in a subforum, where
// we do not want to show the forum list.
// (as for the crazy construction: array_shift() would be nice, but
// that one would reset the array indices, breaking the code below.)
reset($forums);
list ($id, $forum) = each($forums);
if ($forum['forum_id'] != $PHORUM['forum_id']) return;
unset($forums[$forum['forum_id']]);
reset($forums);
// If we have no forums left by now, then we are done.
if (empty($forums)) return;
// Do checks for new messages.
// In a future version of this module, we will be able to make
// use of the newflags API calls, but currently, these are still
// under development.
if ($PHORUM['DATA']['LOGGEDIN'])
{
if ($PHORUM['show_new_on_index']==2)
{
$new_checks = phorum_db_newflag_check(array_keys($forums));
foreach ($new_checks as $forum_id => $checks) {
if (!empty($checks)) {
$forums[$forum_id]['new_message_check'] = TRUE;
} else {
$forums[$forum_id]['new_message_check'] = FALSE;
}
}
}
elseif ($PHORUM['show_new_on_index']==1)
{
$new_counts = phorum_db_newflag_count(array_keys($forums));
foreach ($new_counts as $forum_id => $counts)
{
$forums[$forum_id]['new_messages'] = number_format(
$counts['messages'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
$forums[$forum_id]['new_threads'] = number_format(
$counts['threads'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
}
}
}
// Format the forums.
foreach ($forums as $id => $forum) {
$forums[$id] = subforums_format_forum($forum);
}
// Add a header item.
array_unshift($forums, array(
'folder_flag' => TRUE,
'name' => count($forums) == 1
? $PHORUM['DATA']['LANG']['mod_subforums']['subforum']
: $PHORUM['DATA']['LANG']['mod_subforums']['subforums'],
'level' => 0,
'URL' => array(
'LIST' => phorum_get_url(
PHORUM_LIST_URL, $PHORUM['forum_id']
)
)
));
// Render the forum list.
$PHORUM['DATA']['FORUMS'] = $forums;
include phorum_get_template('subforums::list');
}
/**
* This function is used for template formatting the data for a single
* forum. In a future version, we can replace this with the API layer
* call phorum_api_format_forum(). For now, we cannot yet use that function,
* since it is only available in the Phorum development tree.
*
* @param array $forum
* An array of forum data.
*
* @return array
* The formatted forum data.
*/
function subforums_format_forum($forum)
{
global $PHORUM;
$forum['URL']['LIST'] = phorum_get_url(PHORUM_LIST_URL, $forum['forum_id']);
if ($PHORUM['DATA']['LOGGEDIN']) {
if (phorum_page == 'list') {
$forum['URL']['MARK_READ'] = phorum_get_url(
PHORUM_LIST_URL, $forum['forum_id'],
'markread', $PHORUM['forum_id']
);
} else {
$forum['URL']['MARK_READ'] = phorum_get_url(
PHORUM_INDEX_URL, $forum['forum_id'],
'markread', $PHORUM['forum_id']
);
}
}
if (!empty($PHORUM['use_rss'])) {
$forum['URL']['FEED'] = phorum_get_url(
PHORUM_FEED_URL, $forum['forum_id'],
'type='.$PHORUM['default_feed']
);
}
if ($forum['message_count'] > 0) {
$forum['last_post'] = phorum_date(
$PHORUM['long_date_time'], $forum['last_post_time']
);
$forum['raw_last_post'] = $forum['last_post_time'];
} else {
$forum['last_post'] = ' ';
}
$forum['raw_message_count'] = $forum['message_count'];
$forum['message_count'] = number_format(
$forum['message_count'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
$forum['raw_thread_count'] = $forum['thread_count'];
$forum['thread_count'] = number_format(
$forum['thread_count'], 0,
$PHORUM['dec_sep'], $PHORUM['thous_sep']
);
return $forum;
}
?>