newbbCreateSubForumList()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * NewBB,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 */
11
12
use Xmf\Module\Helper\Cache;
13
use XoopsModules\Newbb\{
14
    CategoryHandler,
15
    ForumHandler,
16
    Helper,
17
    ObjectTree
18
};
19
20
/** @var Helper $helper */
21
/** @var CategoryHandler $categoryHandler */
22
/** @var ForumHandler $forumHandler */
23
defined('NEWBB_FUNCTIONS_INI') || require __DIR__ . '/functions.ini.php';
24
define('NEWBB_FUNCTIONS_FORUM_LOADED', true);
25
26
if (!defined('NEWBB_FUNCTIONS_FORUM')) {
27
    define('NEWBB_FUNCTIONS_FORUM', 1);
28
29
    /**
30
     * @param array|int|null $value             selected forum id
31
     * @param string     $permission        permission (access, all, etc.)
32
     * @param bool       $categoryDelimiter show delimiter between categories
33
     * @param bool       $see
34
     * @return string
35
     */
36
    function newbbForumSelectBox($value = null, string $permission = 'access', bool $categoryDelimiter = true, bool $see = false): string
37
    {
38
        global $xoopsUser;
39
        $categoryHandler = Helper::getInstance()->getHandler('Category');
40
        $categories      = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false);
41
42
        $cacheHelper = new Cache('newbb');
43
44
        $groups = [XOOPS_GROUP_ANONYMOUS];
45
        if (is_object($xoopsUser)) {
46
            $groups = $xoopsUser->getGroups();
47
        }
48
        sort($groups);
49
        $groupKey = 'forumselect_' . $permission . '_' . md5(implode(',', $groups));
50
        $forums   = $cacheHelper->cacheRead(
51
            $groupKey,
52
            static function () use ($categories, $permission) {
0 ignored issues
show
Unused Code introduced by
The import $categories is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
53
                $categoryHandler = Helper::getInstance()->getHandler('Category');
54
                $categories      = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false);
55
                $forumHandler    = Helper::getInstance()->getHandler('Forum');
56
                $forums          = $forumHandler->getTree(array_keys($categories), 0, 'all');
57
58
                return $forums;
59
            },
60
            300
61
        );
62
63
        $value = is_array($value) ? $value : [$value];
64
        //$see = is_array($see) ? $see : array($see);
65
        $box = '';
66
        if (is_iterable($forums)) {
67
            foreach (array_keys($categories) as $key) {
68
                if ($categoryDelimiter) {
69
                    $box .= "<option value=0>&nbsp;</option>\n";
70
                }
71
                $box .= "<option value='" . (-1 * $key) . "'>[" . $categories[$key]['cat_title'] . "]</option>\n";
72
                if (empty($forums[$key])) {
73
                    continue;
74
                }
75
                foreach ($forums[$key] as $f => $forum) {
76
                    if ($see && in_array($f, $value, true)) {
77
                        continue;
78
                    }
79
                    $box .= "<option value='{$f}' " . (in_array($f, $value, true) ? ' selected' : '') . '>' . $forum['prefix'] . $forum['forum_name'] . "</option>\n";
80
                }
81
            }
82
        } else {
83
            $box .= '<option value=0>' . _MD_NEWBB_NOFORUMINDB . "</option>\n";
84
        }
85
        unset($forums, $categories);
86
87
        return $box;
88
    }
89
90
    /**
91
     * @param int $forum_id
92
     * @return string
93
     */
94
    function newbbMakeJumpbox(int $forum_id = 0): string
95
    {
96
        $box = '<form name="forum_jumpbox" method="get" action="' . XOOPS_URL . '/modules/newbb/viewforum.php" onsubmit="javascript: if (document.forum_jumpbox.forum.value &lt; 1) {return false;}">';
97
        $box .= '<select class="select" name="forum" onchange="if (this.options[this.selectedIndex].value >0) { document.forms.forum_jumpbox.submit();}">';
98
        $box .= '<option value=0>-- ' . _MD_NEWBB_SELFORUM . ' --</option>';
99
        $box .= newbbForumSelectBox($forum_id);
100
        $box .= "</select> <input type='submit' class='button' value='" . _GO . "' ></form>";
101
        //        unset($forums, $categories);
102
103
        return $box;
104
    }
105
106
    /**
107
     * Get structured forums
108
     *
109
     * This is a temporary solution
110
     * To be substituted with a new tree handler
111
     *
112
     * @int integer    $pid    parent forum ID
113
     *
114
     * @param int  $pid
115
     * @param bool $refresh
116
     * @return array
117
     */
118
    function newbbGetSubForum(int $pid = 0, bool $refresh = false): array
119
    {
120
        static $list;
121
        if (null === $list) {
122
            $cacheHelper = new Cache('newbb');
123
            $list        = $cacheHelper->read('forum_sub');
124
        }
125
126
        if (!is_array($list) || $refresh) {
127
            $list = newbbCreateSubForumList();
128
        }
129
        if (0 === $pid) {
130
            return $list;
131
        }
132
133
        return @$list[$pid];
134
    }
135
136
    /**
137
     * @return array[]
138
     *
139
     * @psalm-return array<non-empty-list<empty>>
140
     */
141
    function newbbCreateSubForumList(): array
142
    {
143
        $forumHandler = Helper::getInstance()->getHandler('Forum');
144
        $criteria     = new \CriteriaCompo(null, 1);
145
        $criteria->setSort('cat_id ASC, parent_forum ASC, forum_order');
146
        $criteria->setOrder('ASC');
147
        $forumsObject = $forumHandler->getObjects($criteria);
148
        //        require_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php');
149
        $tree        = new ObjectTree($forumsObject, 'forum_id', 'parent_forum');
150
        $forum_array = [];
151
        foreach (array_keys($forumsObject) as $key) {
152
            if (!$child = array_keys($tree->getAllChild($forumsObject[$key]->getVar('forum_id')))) {
153
                continue;
154
            }
155
            $forum_array[$forumsObject[$key]->getVar('forum_id')] = $child;
156
        }
157
        unset($forumsObject, $tree, $criteria);
158
159
        $cacheHelper = new Cache('newbb');
160
        $cacheHelper->write('forum_sub', $forum_array);
161
162
        return $forum_array;
163
    }
164
165
    /**
166
     * @param int  $forum_id
167
     * @param bool $refresh
168
     * @return array|mixed|null
169
     */
170
    function newbbGetParentForum(int $forum_id = 0, bool $refresh = false)
171
    {
172
        static $list = null;
173
174
        if (null === $list) {
175
            $cacheHelper = new Cache('newbb');
176
            $list        = $cacheHelper->read('forum_parent');
177
        }
178
        if (!is_array($list) || $refresh) {
179
            $list = newbbCreateParentForumList();
180
        }
181
        if (0 === $forum_id) {
182
            return $list;
183
        }
184
185
        return @$list[$forum_id];
186
    }
187
188
    /**
189
     * @return array[]
190
     *
191
     * @psalm-return array<array>
192
     */
193
    function newbbCreateParentForumList(): array
194
    {
195
        $forumHandler = Helper::getInstance()->getHandler('Forum');
196
        $criteria     = new \Criteria('forum_id');
197
        $criteria->setSort('parent_forum');
198
        $criteria->setOrder('ASC');
199
        $forumsObject = $forumHandler->getObjects($criteria);
200
        //        require_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php');
201
        $tree        = new ObjectTree($forumsObject, 'forum_id', 'parent_forum');
202
        $forum_array = [];
203
        foreach (array_keys($forumsObject) as $key) {
204
            $parent_forum = $forumsObject[$key]->getVar('parent_forum');
205
            if (!$parent_forum) {
206
                continue;
207
            }
208
            if (isset($forum_array[$parent_forum])) {
209
                $forum_array[$forumsObject[$key]->getVar('forum_id')]   = $forum_array[$parent_forum];
210
                $forum_array[$forumsObject[$key]->getVar('forum_id')][] = $parent_forum;
211
            } else {
212
                $forum_array[$forumsObject[$key]->getVar('forum_id')] = $tree->getParentForums($forumsObject[$key]->getVar('forum_id'));
213
            }
214
        }
215
        unset($forumsObject, $tree, $criteria);
216
217
        $cacheHelper = new Cache('newbb');
218
        $cacheHelper->write('forum_parent', $forum_array);
219
220
        return $forum_array;
221
    }
222
}
223