1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* NewBB 5.0x, the forum module for XOOPS project |
4
|
|
|
* |
5
|
|
|
* @copyright XOOPS Project (http://xoops.org) |
6
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
7
|
|
|
* @author Taiwen Jiang (phppp or D.J.) <[email protected]> |
8
|
|
|
* @since 4.00 |
9
|
|
|
* @package module::newbb |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined'); |
|
|
|
|
13
|
|
|
|
14
|
|
|
defined('NEWBB_FUNCTIONS_INI') || include __DIR__ . '/functions.ini.php'; |
15
|
|
|
define('NEWBB_FUNCTIONS_FORUM_LOADED', true); |
16
|
|
|
|
17
|
|
|
if (!defined('NEWBB_FUNCTIONS_FORUM')) { |
18
|
|
|
define('NEWBB_FUNCTIONS_FORUM', 1); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param null|array $value selected forum id |
22
|
|
|
* @param string $permission permission (access, all, etc.) |
23
|
|
|
* @param bool $categoryDelimiter show delimiter between categories |
24
|
|
|
* @param bool $see |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
function newbbForumSelectBox($value = null, $permission = 'access', $categoryDelimiter = true, $see = false) |
28
|
|
|
{ |
29
|
|
|
global $xoopsUser; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** @var \NewbbCategoryHandler $categoryHandler */ |
32
|
|
|
$categoryHandler = xoops_getModuleHandler('category', 'newbb'); |
33
|
|
|
$categories = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false); |
34
|
|
|
|
35
|
|
|
$cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
36
|
|
|
|
37
|
|
|
$groups = [XOOPS_GROUP_ANONYMOUS]; |
38
|
|
|
if (is_object($xoopsUser)) { |
39
|
|
|
$groups = $xoopsUser->getGroups(); |
40
|
|
|
} |
41
|
|
|
sort($groups); |
42
|
|
|
$groupKey = 'forumselect_' . $permission . '_' . md5(implode(',', $groups)); |
43
|
|
|
$forums = $cacheHelper->cacheRead($groupKey, function () use ($categories, $permission) { |
44
|
|
|
/** @var \NewbbCategoryHandler $categoryHandler */ |
45
|
|
|
$categoryHandler = xoops_getModuleHandler('category', 'newbb'); |
46
|
|
|
$categories = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false); |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** @var \NewbbForumHandler $forumHandler */ |
49
|
|
|
$forumHandler = xoops_getModuleHandler('forum', 'newbb'); |
50
|
|
|
$forums = $forumHandler->getTree(array_keys($categories), 0, 'all'); |
51
|
|
|
|
52
|
|
|
return $forums; |
53
|
|
|
}, 300); |
54
|
|
|
|
55
|
|
|
$value = is_array($value) ? $value : [$value]; |
56
|
|
|
//$see = is_array($see) ? $see : array($see); |
|
|
|
|
57
|
|
|
$box = ''; |
58
|
|
|
if (count($forums) > 0) { |
59
|
|
|
foreach (array_keys($categories) as $key) { |
60
|
|
|
if ($categoryDelimiter) { |
61
|
|
|
$box .= "<option value=0> </option>\n"; |
62
|
|
|
} |
63
|
|
|
$box .= "<option value='" . (-1 * $key) . "'>[" . $categories[$key]['cat_title'] . "]</option>\n"; |
64
|
|
|
if (empty($forums[$key])) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
foreach ($forums[$key] as $f => $forum) { |
68
|
|
|
if ($see && in_array($f, $value)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
$box .= "<option value='{$f}' " . (in_array($f, $value) ? ' selected' : '') . '>' . $forum['prefix'] . $forum['forum_name'] . "</option>\n"; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$box .= '<option value=0>' . _MD_NEWBB_NOFORUMINDB . "</option>\n"; |
76
|
|
|
} |
77
|
|
|
unset($forums, $categories); |
78
|
|
|
|
79
|
|
|
return $box; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $forum_id |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
function newbbMakeJumpbox($forum_id = 0) |
87
|
|
|
{ |
88
|
|
|
$box = '<form name="forum_jumpbox" method="get" action="' . XOOPS_URL . '/modules/newbb/viewforum.php" onsubmit="javascript: if (document.forum_jumpbox.forum.value < 1) {return false;}">'; |
89
|
|
|
$box .= '<select class="select" name="forum" onchange="if (this.options[this.selectedIndex].value >0) { document.forms.forum_jumpbox.submit();}">'; |
90
|
|
|
$box .= '<option value=0>-- ' . _MD_NEWBB_SELFORUM . ' --</option>'; |
91
|
|
|
$box .= newbbForumSelectBox($forum_id); |
|
|
|
|
92
|
|
|
$box .= "</select> <input type='submit' class='button' value='" . _GO . "' /></form>"; |
93
|
|
|
unset($forums, $categories); |
94
|
|
|
|
95
|
|
|
return $box; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get structured forums |
100
|
|
|
* |
101
|
|
|
* This is a temporary solution |
102
|
|
|
* To be substituted with a new tree handler |
103
|
|
|
* |
104
|
|
|
* @int integer $pid parent forum ID |
105
|
|
|
* |
106
|
|
|
* @param int $pid |
107
|
|
|
* @param bool $refresh |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
function newbbGetSubForum($pid = 0, $refresh = false) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
static $list; |
113
|
|
|
if (!isset($list)) { |
114
|
|
|
$cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
115
|
|
|
$list = $cacheHelper->read('forum_sub'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!is_array($list) || $refresh) { |
119
|
|
|
$list = newbbCreateSubForumList(); |
120
|
|
|
} |
121
|
|
|
if ($pid == 0) { |
122
|
|
|
return $list; |
123
|
|
|
} else { |
124
|
|
|
return @$list[$pid]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
function newbbCreateSubForumList() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
/** @var \NewbbForumHandler $forumHandler */ |
134
|
|
|
$forumHandler = xoops_getModuleHandler('forum', 'newbb'); |
135
|
|
|
$criteria = new CriteriaCompo(null, 1); |
136
|
|
|
$criteria->setSort('cat_id ASC, parent_forum ASC, forum_order'); |
137
|
|
|
$criteria->setOrder('ASC'); |
138
|
|
|
$forumsObject = $forumHandler->getObjects($criteria); |
139
|
|
|
require_once $GLOBALS['xoops']->path('modules/newbb/class/tree.php'); |
140
|
|
|
$tree = new NewbbObjectTree($forumsObject, 'forum_id', 'parent_forum'); |
141
|
|
|
$forum_array = []; |
142
|
|
|
foreach (array_keys($forumsObject) as $key) { |
143
|
|
|
if (!$child = array_keys($tree->getAllChild($forumsObject[$key]->getVar('forum_id')))) { |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
$forum_array[$forumsObject[$key]->getVar('forum_id')] = $child; |
147
|
|
|
} |
148
|
|
|
unset($forumsObject, $tree, $criteria); |
149
|
|
|
|
150
|
|
|
$cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
151
|
|
|
$cacheHelper->write('forum_sub', $forum_array); |
152
|
|
|
|
153
|
|
|
return $forum_array; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $forum_id |
158
|
|
|
* @param bool $refresh |
159
|
|
|
* @return array|mixed|null |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
function newbbGetParentForum($forum_id = 0, $refresh = false) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
static $list = null; |
164
|
|
|
|
165
|
|
|
if (!isset($list)) { |
166
|
|
|
$cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
167
|
|
|
$list = $cacheHelper->read('forum_parent'); |
168
|
|
|
} |
169
|
|
|
if (!is_array($list) || $refresh) { |
170
|
|
|
$list = newbbCreateParentForumList(); |
171
|
|
|
} |
172
|
|
|
if ($forum_id == 0) { |
173
|
|
|
return $list; |
174
|
|
|
} else { |
175
|
|
|
return @$list[$forum_id]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
function newbbCreateParentForumList() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
/** @var \NewbbForumHandler $forumHandler */ |
185
|
|
|
$forumHandler = xoops_getModuleHandler('forum', 'newbb'); |
186
|
|
|
$criteria = new Criteria('1', 1); |
187
|
|
|
$criteria->setSort('parent_forum'); |
188
|
|
|
$criteria->setOrder('ASC'); |
189
|
|
|
$forumsObject = $forumHandler->getObjects($criteria); |
190
|
|
|
require_once $GLOBALS['xoops']->path('modules/newbb/class/tree.php'); |
191
|
|
|
$tree = new NewbbObjectTree($forumsObject, 'forum_id', 'parent_forum'); |
192
|
|
|
$forum_array = []; |
193
|
|
|
foreach (array_keys($forumsObject) as $key) { |
194
|
|
|
$parent_forum = $forumsObject[$key]->getVar('parent_forum'); |
195
|
|
|
if (!$parent_forum) { |
196
|
|
|
continue; |
197
|
|
|
} |
198
|
|
|
if (isset($forum_array[$parent_forum])) { |
199
|
|
|
$forum_array[$forumsObject[$key]->getVar('forum_id')] = $forum_array[$parent_forum]; |
200
|
|
|
$forum_array[$forumsObject[$key]->getVar('forum_id')][] = $parent_forum; |
201
|
|
|
} else { |
202
|
|
|
$forum_array[$forumsObject[$key]->getVar('forum_id')] = $tree->getParentForums($forumsObject[$key]->getVar('forum_id')); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
unset($forumsObject, $tree, $criteria); |
206
|
|
|
|
207
|
|
|
$cacheHelper = new \Xmf\Module\Helper\Cache('newbb'); |
208
|
|
|
$cacheHelper->write('forum_parent', $forum_array); |
209
|
|
|
|
210
|
|
|
return $forum_array; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.