PermissionCategoryHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setCategoryPermission() 0 27 6
A getValidItems() 0 10 2
A __construct() 0 4 1
A deleteByCategory() 0 13 2
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB 5.0x,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 * @package        module::newbb
13
 */
14
15
use XoopsModules\Newbb;
16
17
18
19
//defined("NEWBB_HANDLER_PERMISSION") || require_once __DIR__  .'/permission.php';
20
//define("NEWBB_HANDLER_PERMISSION_CATEGORY", 1);
21
22
/**
23
 * Class PermissionCategoryHandler
24
 * @package XoopsModules\Newbb
25
 */
26
class PermissionCategoryHandler extends Newbb\PermissionHandler
27
{
28
    /**
29
     * @param \XoopsDatabase|null $db
30
     */
31
    public function __construct(\XoopsDatabase $db = null)
32
    {
33
        //        $this->PermissionHandler($db);
34
        parent::__construct($db);
35
    }
36
37
    /**
38
     * @param        $mid
39
     * @param int    $id
40
     * @return array
41
     */
42
    public function getValidItems($mid, $id = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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

42
    public function getValidItems($mid, /** @scrutinizer ignore-unused */ $id = 0)

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...
43
    {
44
        $full_items = [];
45
        if (empty($mid)) {
46
            return $full_items;
47
        }
48
49
        $full_items[] = "'category_access'";
50
51
        return $full_items;
52
    }
53
54
    /**
55
     * @param $cat_id
56
     * @return bool
57
     */
58
    public function deleteByCategory($cat_id)
59
    {
60
        $cat_id = (int)$cat_id;
61
        if (empty($cat_id)) {
62
            return false;
63
        }
64
        /** @var \XoopsGroupPermHandler $grouppermHandler */
65
        $grouppermHandler = \xoops_getHandler('groupperm');
66
        $criteria         = new \CriteriaCompo(new \Criteria('gperm_modid', $GLOBALS['xoopsModule']->getVar('mid')));
67
        $criteria->add(new \Criteria('gperm_name', 'category_access'));
68
        $criteria->add(new \Criteria('gperm_itemid', $cat_id));
69
70
        return $grouppermHandler->deleteAll($criteria);
71
    }
72
73
    /**
74
     * @param        $category
75
     * @param array  $groups
76
     * @return bool
77
     */
78
    public function setCategoryPermission($category, array $groups = [])
79
    {
80
        if (\is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
81
            $mid = $GLOBALS['xoopsModule']->getVar('mid');
82
        } else {
83
            /** @var \XoopsModuleHandler $moduleHandler */
84
            $moduleHandler = \xoops_getHandler('module');
85
            $newbb         = $moduleHandler->getByDirname('newbb');
86
            $mid           = $newbb->getVar('mid');
87
        }
88
        if (empty($groups)) {
89
            /** @var \XoopsMemberHandler $memberHandler */
90
            $memberHandler = \xoops_getHandler('member');
91
            $glist         = $memberHandler->getGroupList();
92
            $groups        = \array_keys($glist);
93
        }
94
        $ids     = $this->getGroupIds('category_access', $category, $mid);
95
        $ids_add = \array_diff($groups, $ids);
96
        $ids_rmv = \array_diff($ids, $groups);
97
        foreach ($ids_add as $group) {
98
            $this->addRight('category_access', $category, $group, $mid);
99
        }
100
        foreach ($ids_rmv as $group) {
101
            $this->deleteRight('category_access', $category, $group, $mid);
102
        }
103
104
        return true;
105
    }
106
}
107