newbbCreateParentForumList()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 0
dl 0
loc 29
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * NewBB 5.0x,  the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2 or later (https://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
use XoopsModules\Newbb;
13
14
15
16
defined('NEWBB_FUNCTIONS_INI') || require __DIR__ . '/functions.ini.php';
17
define('NEWBB_FUNCTIONS_FORUM_LOADED', true);
18
19
if (!defined('NEWBB_FUNCTIONS_FORUM')) {
20
    define('NEWBB_FUNCTIONS_FORUM', 1);
21
22
    /**
23
     * @param null|array $value             selected forum id
24
     * @param string     $permission        permission (access, all, etc.)
25
     * @param bool       $categoryDelimiter show delimiter between categories
26
     * @param bool       $see
27
     * @return string
28
     */
29
    function newbbForumSelectBox($value = null, $permission = 'access', $categoryDelimiter = true, $see = false)
30
    {
31
        global $xoopsUser;
32
        /** @var Newbb\CategoryHandler $categoryHandler */
33
        $categoryHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Category');
34
        $categories      = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false);
35
36
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
37
38
        $groups = [XOOPS_GROUP_ANONYMOUS];
39
        if (is_object($xoopsUser)) {
40
            $groups = $xoopsUser->getGroups();
41
        }
42
        sort($groups);
43
        $groupKey = 'forumselect_' . $permission . '_' . md5(implode(',', $groups));
44
        $forums   = $cacheHelper->cacheRead(
45
            $groupKey,
46
            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...
47
                /** @var Newbb\CategoryHandler $categoryHandler */
48
                $categoryHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Category');
49
                $categories      = $categoryHandler->getByPermission($permission, ['cat_id', 'cat_order', 'cat_title'], false);
50
51
                /** @var Newbb\ForumHandler $forumHandler */
52
                $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
53
                $forums       = $forumHandler->getTree(array_keys($categories), 0, 'all');
0 ignored issues
show
Bug introduced by
array_keys($categories) of type array is incompatible with the type integer expected by parameter $cat_id of XoopsModules\Newbb\ForumHandler::getTree(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                $forums       = $forumHandler->getTree(/** @scrutinizer ignore-type */ array_keys($categories), 0, 'all');
Loading history...
54
55
                return $forums;
56
            },
57
            300
58
        );
59
60
        $value = is_array($value) ? $value : [$value];
61
        //$see = is_array($see) ? $see : array($see);
62
        $box = '';
63
        if (count($forums) > 0) {
64
            foreach (array_keys($categories) as $key) {
65
                if ($categoryDelimiter) {
66
                    $box .= "<option value=0>&nbsp;</option>\n";
67
                }
68
                $box .= "<option value='" . (-1 * $key) . "'>[" . $categories[$key]['cat_title'] . "]</option>\n";
69
                if (empty($forums[$key])) {
70
                    continue;
71
                }
72
                foreach ($forums[$key] as $f => $forum) {
73
                    if ($see && in_array($f, $value)) {
74
                        continue;
75
                    }
76
                    $box .= "<option value='{$f}' " . (in_array($f, $value) ? ' selected' : '') . '>' . $forum['prefix'] . $forum['forum_name'] . "</option>\n";
77
                }
78
            }
79
        } else {
80
            $box .= '<option value=0>' . _MD_NEWBB_NOFORUMINDB . "</option>\n";
81
        }
82
        unset($forums, $categories);
83
84
        return $box;
85
    }
86
87
    /**
88
     * @param int $forum_id
89
     * @return string
90
     */
91
    function newbbMakeJumpbox($forum_id = 0)
92
    {
93
        $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;}">';
94
        $box .= '<select class="select" name="forum" onchange="if (this.options[this.selectedIndex].value >0) { document.forms.forum_jumpbox.submit();}">';
95
        $box .= '<option value=0>-- ' . _MD_NEWBB_SELFORUM . ' --</option>';
96
        $box .= newbbForumSelectBox($forum_id);
0 ignored issues
show
Bug introduced by
$forum_id of type integer is incompatible with the type array|null expected by parameter $value of newbbForumSelectBox(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $box .= newbbForumSelectBox(/** @scrutinizer ignore-type */ $forum_id);
Loading history...
97
        $box .= "</select> <input type='submit' class='button' value='" . _GO . "' ></form>";
98
        unset($forums, $categories);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $forums does not exist. Did you maybe mean $forum_id?
Loading history...
Comprehensibility Best Practice introduced by
The variable $categories seems to be never defined.
Loading history...
99
100
        return $box;
101
    }
102
103
    /**
104
     * Get structured forums
105
     *
106
     * This is a temporary solution
107
     * To be substituted with a new tree handler
108
     *
109
     * @int integer    $pid    parent forum ID
110
     *
111
     * @param int  $pid
112
     * @param bool $refresh
113
     * @return array
114
     */
115
    function newbbGetSubForum($pid = 0, $refresh = false)
116
    {
117
        static $list;
118
        if (null === $list) {
119
            $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
120
            $list        = $cacheHelper->read('forum_sub');
121
        }
122
123
        if (!is_array($list) || $refresh) {
124
            $list = newbbCreateSubForumList();
125
        }
126
        if (0 == $pid) {
127
            return $list;
128
        }
129
130
        return @$list[$pid];
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    function newbbCreateSubForumList()
137
    {
138
        /** @var Newbb\ForumHandler $forumHandler */
139
        //        $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
140
        $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
141
        $criteria     = new \CriteriaCompo(null, 1);
142
        $criteria->setSort('cat_id ASC, parent_forum ASC, forum_order');
143
        $criteria->setOrder('ASC');
144
        $forumsObject = $forumHandler->getObjects($criteria);
145
        //        require_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php');
146
        $tree        = new Newbb\ObjectTree($forumsObject, 'forum_id', 'parent_forum');
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\ObjectTree::__construct() has too many arguments starting with 'parent_forum'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        $tree        = /** @scrutinizer ignore-call */ new Newbb\ObjectTree($forumsObject, 'forum_id', 'parent_forum');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
147
        $forum_array = [];
148
        foreach (array_keys($forumsObject) as $key) {
149
            if (!$child = array_keys($tree->getAllChild($forumsObject[$key]->getVar('forum_id')))) {
150
                continue;
151
            }
152
            $forum_array[$forumsObject[$key]->getVar('forum_id')] = $child;
153
        }
154
        unset($forumsObject, $tree, $criteria);
155
156
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
157
        $cacheHelper->write('forum_sub', $forum_array);
158
159
        return $forum_array;
160
    }
161
162
    /**
163
     * @param int  $forum_id
164
     * @param bool $refresh
165
     * @return array|mixed|null
166
     */
167
    function newbbGetParentForum($forum_id = 0, $refresh = false)
168
    {
169
        static $list = null;
170
171
        if (null === $list) {
172
            $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
173
            $list        = $cacheHelper->read('forum_parent');
174
        }
175
        if (!is_array($list) || $refresh) {
176
            $list = newbbCreateParentForumList();
177
        }
178
        if (0 == $forum_id) {
179
            return $list;
180
        }
181
182
        return @$list[$forum_id];
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    function newbbCreateParentForumList()
189
    {
190
        /** @var Newbb\ForumHandler $forumHandler */
191
        $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
192
        $criteria     = new \Criteria('forum_id');
193
        $criteria->setSort('parent_forum');
194
        $criteria->setOrder('ASC');
195
        $forumsObject = $forumHandler->getObjects($criteria);
196
        //        require_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php');
197
        $tree        = new Newbb\ObjectTree($forumsObject, 'forum_id', 'parent_forum');
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\ObjectTree::__construct() has too many arguments starting with 'parent_forum'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
        $tree        = /** @scrutinizer ignore-call */ new Newbb\ObjectTree($forumsObject, 'forum_id', 'parent_forum');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
198
        $forum_array = [];
199
        foreach (array_keys($forumsObject) as $key) {
200
            $parent_forum = $forumsObject[$key]->getVar('parent_forum');
201
            if (!$parent_forum) {
202
                continue;
203
            }
204
            if (isset($forum_array[$parent_forum])) {
205
                $forum_array[$forumsObject[$key]->getVar('forum_id')]   = $forum_array[$parent_forum];
206
                $forum_array[$forumsObject[$key]->getVar('forum_id')][] = $parent_forum;
207
            } else {
208
                $forum_array[$forumsObject[$key]->getVar('forum_id')] = $tree->getParentForums($forumsObject[$key]->getVar('forum_id'));
209
            }
210
        }
211
        unset($forumsObject, $tree, $criteria);
212
213
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
214
        $cacheHelper->write('forum_parent', $forum_array);
215
216
        return $forum_array;
217
    }
218
}
219