CategoryHandler::getAllSubcatsTopParentCid()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 0
dl 0
loc 24
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Wfdownloads module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
require_once \dirname(__DIR__) . '/include/common.php';
26
27
/**
28
 * Class CategoryHandler
29
 */
30
class CategoryHandler extends \XoopsPersistableObjectHandler
31
{
32
    /**
33
     * @access public
34
     */
35
    public $helper;
36
    public $allCategories = false;
37
    public $topCategories = false;
38
39
    /**
40
     * @param null|\XoopsDatabase $db
41
     */
42
    public function __construct(\XoopsDatabase $db = null)
43
    {
44
        parent::__construct($db, 'wfdownloads_cat', Category::class, 'cid', 'title');
45
        /** @var Helper $this ->helper */
46
        $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Wfdownloads\Helper.
Loading history...
47
    }
48
49
    /**
50
     * @param        $cid
51
     * @param string $root_filename
52
     * @param string $item_filename
53
     *
54
     * @return mixed|string
55
     */
56
    public function getNicePath($cid, $root_filename = 'index.php', $item_filename = 'viewcat.php?op=')
0 ignored issues
show
Unused Code introduced by
The parameter $root_filename is not used and could be removed. ( Ignorable by Annotation )

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

56
    public function getNicePath($cid, /** @scrutinizer ignore-unused */ $root_filename = 'index.php', $item_filename = 'viewcat.php?op=')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $mytree     = new XoopsTree($this->table, $this->keyName, 'pid');
59
        $pathString = $mytree->getNicePathFromId($cid, $this->identifierName, $item_filename);
60
61
        /**
62
         * Replacing the " with ">" and deleteing the last ">" at the end
63
         */
64
        $pathString = \trim($pathString);
65
        $pathString = \str_replace(':', '>', $pathString);
66
67
        //      $pathString = substr($pathString, 0, strlen($pathString) - 13); // not needed now with fixed icms core! but required for XOOPS
68
        return $pathString;
69
    }
70
71
    /**
72
     * Get categories that the current user has permissions for
73
     *
74
     * @param bool $id_as_key
75
     * @param bool $as_object
76
     *
77
     * @return array
78
     */
79
    public function getUserCategories($id_as_key = false, $as_object = true)
80
    {
81
        $grouppermHandler = \xoops_getHandler('groupperm');
82
83
        $groups                   = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
84
        $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $this->helper->getModule()->mid());
0 ignored issues
show
Bug introduced by
The method getItemIds() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

84
        /** @scrutinizer ignore-call */ 
85
        $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $this->helper->getModule()->mid());
Loading history...
85
86
        return $this->getObjects(new \Criteria('cid', '(' . \implode(',', $allowedDownCategoriesIds) . ')', 'IN'), $id_as_key, $as_object);
87
    }
88
89
    /**
90
     * Get categories that the current user has permissions for
91
     *
92
     * @param bool $id_as_key
93
     * @param bool $as_object
94
     *
95
     * @return array
96
     */
97
    public function getUserDownCategories($id_as_key = false, $as_object = true)
98
    {
99
        $grouppermHandler = \xoops_getHandler('groupperm');
100
101
        $groups                   = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
102
        $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $this->helper->getModule()->mid());
103
104
        return $this->getObjects(new \Criteria('cid', '(' . \implode(',', $allowedDownCategoriesIds) . ')', 'IN'), $id_as_key, $as_object);
105
    }
106
107
    /**
108
     * @param bool $id_as_key
109
     * @param bool $as_object
110
     *
111
     * @return array
112
     */
113
    public function getUserUpCategories($id_as_key = false, $as_object = true)
114
    {
115
        $grouppermHandler = \xoops_getHandler('groupperm');
116
117
        $groups                 = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
118
        $allowedUpCategoriesIds = $grouppermHandler->getItemIds('WFUpCatPerm', $groups, $this->helper->getModule()->mid());
119
120
        return $this->getObjects(new \Criteria('cid', '(' . \implode(',', $allowedUpCategoriesIds) . ')', 'IN'), $id_as_key, $as_object);
121
    }
122
123
    /**
124
     * @param $category
125
     *
126
     * @return array
127
     */
128
    public function getChildCats($category)
129
    {
130
        $categoryObjs = $this->getObjects();
131
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
132
        $categoryObjsTree = new ObjectTree($categoryObjs, $this->keyName, 'pid');
133
134
        return $categoryObjsTree->getAllChild($category->getVar($this->keyName));
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getAllSubcatsTopParentCid()
141
    {
142
        if (!$this->allCategories) {
143
            $this->allCategories = $this->getObjects(null, true);
144
        }
145
146
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
147
        $categoryObjsTree = new ObjectTree($this->allCategories, $this->keyName, 'pid');
0 ignored issues
show
Bug introduced by
It seems like $this->allCategories can also be of type true; however, parameter $objectArr of XoopsModules\Wfdownloads\ObjectTree::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

147
        $categoryObjsTree = new ObjectTree(/** @scrutinizer ignore-type */ $this->allCategories, $this->keyName, 'pid');
Loading history...
148
149
        $allsubcats_linked_totop = [];
150
        foreach ($this->allCategories as $cid => $category) {
151
            $parentCategoryObjs = $categoryObjsTree->getAllParent($cid);
152
            if (0 == \count($parentCategoryObjs)) {
153
                // is a top category
154
                $allsubcats_linked_totop[$cid] = $cid;
155
            } else {
156
                // is not a top category
157
                $topParentCategoryObj          = \end($parentCategoryObjs);
158
                $allsubcats_linked_totop[$cid] = $topParentCategoryObj->getVar($cid);
159
            }
160
            unset($parentCategoryObjs);
161
        }
162
163
        return $allsubcats_linked_totop;
164
        /*
165
                $categoryObjsTreeNodes = $categoryObjsTree->getTree();
166
167
                // Let's create an array where key will be cid of the top categories and
168
                // value will be an array containing all the cid of its subcategories
169
                // If value = 0, then this topcat does not have any subcats
170
                $topCategories = [];
171
                foreach ($categoryObjsTreeNodes[0]['child'] as $topCategory_cid) {
172
                    if (!isset($this->topCategories[$topCategory_cid])) {
173
                        $this->topCategories[$topCategory_cid] = $topCategory_cid;
174
                    }
175
                    foreach ($categoryObjsTree->getAllChild($topCategory_cid) as $key => $childCategory) {
176
                        $childCategory_cids[] = $childCategory->getVar('cid');
177
                    }
178
                    $childCategory_cids = isset($childCategory_cids) ? $childCategory_cids : 0;
179
                    $topCategories[$topCategory_cid] = $childCategory_cids;
180
                    unset($childCategory_cids);
181
                }
182
183
                // Now we need to create another array where key will be all subcategories cid and
184
                // value will be the cid of its top most category
185
                $allsubcats_linked_totop = [];
186
187
                foreach ($topCategories as $topCategory_cid => $childCategory_cids) {
188
                    if ($childCategory_cids == 0) {
189
                        $allsubcats_linked_totop[$topCategory_cid] = $topCategory_cid;
190
                    } else {
191
                        foreach ($childCategory_cids as $childCategory_cid) {
192
                            $allsubcats_linked_totop[$childCategory_cid] = $topCategory_cid;
193
                        }
194
                    }
195
                }
196
197
                // Finally, let's finish by adding to this array, all the top categories which values
198
                // will be their cid
199
                  foreach ($topCategories as $topCategory_cid) {
200
                    $allsubcats_linked_totop[$topCategory_cid] = $topCategory_cid;
201
                }
202
203
                return $allsubcats_linked_totop;
204
        */
205
    }
206
}
207