b_sitemap_newbb()   C
last analyzed

Complexity

Conditions 13
Paths 72

Size

Total Lines 87
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 57
nc 72
nop 0
dl 0
loc 87
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//
3
// FILE        ::    newbb.php
4
// AUTHOR    ::    Ryuji AMANO <[email protected]>
5
// WEB        ::    Ryu's Planning <http://ryus.biz>
6
7
// NewBB plugin: D.J., https://xoops.org.cn
8
9
use XoopsModules\Newbb;
10
11
/**
12
 * @return array
13
 */
14
function b_sitemap_newbb()
15
{
16
    global $sitemap_configs;
17
    $sitemap = [];
18
19
    /** @var Newbb\ForumHandler $forumHandler */
20
    $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
21
    /* Allowed forums */
22
    $forums_allowed = $forumHandler->getIdsByPermission();
23
24
    /* fetch top forums */
25
    $forums_top_id = [];
26
    if (!empty($forums_allowed)) {
27
        $crit_top = new \CriteriaCompo(new \Criteria('parent_forum', 0));
28
        //$crit_top->add(new \Criteria("cat_id", "(".implode(", ", array_keys($categories)).")", "IN"));
29
        $crit_top->add(new \Criteria('forum_id', '(' . implode(', ', $forums_allowed) . ')', 'IN'));
30
        $forums_top_id = $forumHandler->getIds($crit_top);
31
    }
32
33
    $forums_sub_id = [];
34
    if ((bool)$forums_top_id && $sitemap_configs['show_subcategoris']) {
35
        $crit_sub = new \CriteriaCompo(new \Criteria('parent_forum', '(' . implode(', ', $forums_top_id) . ')', 'IN'));
36
        $crit_sub->add(new \Criteria('forum_id', '(' . implode(', ', $forums_allowed) . ')', 'IN'));
37
        $forums_sub_id = $forumHandler->getIds($crit_sub);
38
    }
39
40
    /* Fetch forum data */
41
    $forums_available = array_merge($forums_top_id, $forums_sub_id);
42
    $forums_array     = [];
43
    if ((bool)$forums_available) {
44
        $crit_forum = new \Criteria('forum_id', '(' . implode(', ', $forums_available) . ')', 'IN');
45
        $crit_forum->setSort('cat_id ASC, parent_forum ASC, forum_order');
46
        $crit_forum->setOrder('ASC');
47
        $forums_array = $forumHandler->getAll($crit_forum, ['forum_name', 'parent_forum', 'cat_id'], false);
48
    }
49
50
    $forums = [];
51
    foreach ($forums_array as $forumid => $forum) {
52
        if ((bool)$forum['parent_forum']) {
53
            $forums[$forum['parent_forum']]['fchild'][$forumid] = [
54
                'id'    => $forumid,
55
                'url'   => 'viewforum.php?forum=' . $forumid,
56
                'title' => $forum['forum_name'],
57
            ];
58
        } else {
59
            $forums[$forumid] = [
60
                'id'    => $forumid,
61
                'cid'   => $forum['cat_id'],
62
                'url'   => 'viewforum.php?forum=' . $forumid,
63
                'title' => $forum['forum_name'],
64
            ];
65
        }
66
    }
67
68
    if ($sitemap_configs['show_subcategoris']) {
69
        /** @var Newbb\CategoryHandler $categoryHandler */
70
        $categoryHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Category');
71
        $categories      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
72
        $categories      = $categoryHandler->getByPermission('access', ['cat_id', 'cat_title'], false);
73
74
        foreach ($categories as $key => $category) {
75
            $cat_id                         = $category['cat_id'];
76
            $i                              = $cat_id;
77
            $sitemap['parent'][$i]['id']    = $cat_id;
78
            $sitemap['parent'][$i]['title'] = $category['cat_title'];
79
            $sitemap['parent'][$i]['url']   = XOOPS_URL . '/modules/newbb/index.php?cat=' . $cat_id;
80
        }
81
        foreach ($forums as $id => $forum) {
82
            $cid                                            = $forum['cid'];
83
            $sitemap['parent'][$cid]['child'][$id]          = $forum;
84
            $sitemap['parent'][$cid]['child'][$id]['image'] = 2;
85
            if (empty($forum['fchild'])) {
86
                continue;
87
            }
88
89
            foreach ($forum['fchild'] as $_id => $_forum) {
90
                $sitemap['parent'][$cid]['child'][$_id]          = $_forum;
91
                $sitemap['parent'][$cid]['child'][$_id]['image'] = 3;
92
            }
93
        }
94
    } else {
95
        foreach ($forums as $id => $forum) {
96
            $sitemap['parent'][$id] = $forum;
97
        }
98
    }
99
100
    return $sitemap;
101
}
102