Passed
Push — master ( 1dbb73...f7ea8a )
by Michael
05:10
created

CategoryHandler::deleteCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace XoopsModules\Extcal;
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
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @package      extcal
19
 * @since
20
 * @author       XOOPS Development Team,
21
 */
22
23
use XoopsModules\Extcal\{
24
    Helper,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Extcal\Helper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
25
    Perm,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Extcal\Perm. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
    Time
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Extcal\Time. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
};
28
29
30
/**
31
 * Class CategoryHandler.
32
 */
33
class CategoryHandler extends ExtcalPersistableObjectHandler
34
{
35
    public $extcalPerm;
36
37
    /**
38
     * @param \XoopsDatabase|null $db
39
     */
40
    public function __construct(\XoopsDatabase $db = null)
41
    {
42
43
        $this->extcalPerm = Perm::getHandler();
44
        //        parent::__construct($db, 'extcal_cat', _EXTCAL_CLN_CAT, 'cat_id');
45
        parent::__construct($db, 'extcal_cat', Category::class, 'cat_id');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Extcal\Extc...tHandler::__construct() does only seem to accept XoopsDatabase, 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

45
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'extcal_cat', Category::class, 'cat_id');
Loading history...
46
    }
47
48
    /**
49
     * @param $data
50
     *
51
     * @return bool
52
     */
53
    public function createCategory($data)
54
    {
55
        $cat = $this->create();
56
        $cat->setVars($data);
57
        $this->insert($cat);
58
59
        $catId = $this->getInsertId();
0 ignored issues
show
Unused Code introduced by
The assignment to $catId is dead and can be removed.
Loading history...
60
61
        // Retriving permission mask
62
        /** @var \XoopsGroupPermHandler $grouppermHandler */
63
        $grouppermHandler = \xoops_getHandler('groupperm');
64
        $moduleId               = $GLOBALS['xoopsModule']->getVar('mid');
65
66
        $criteria = new \CriteriaCompo();
67
        $criteria->add(new \Criteria('gperm_name', 'extcal_perm_mask'));
68
        $criteria->add(new \Criteria('gperm_modid', $moduleId));
69
        $permMask = $grouppermHandler->getObjects($criteria);
70
71
        // Retriving group list
72
        $memberHandler = \xoops_getHandler('member');
73
        $glist         = $memberHandler->getGroupList();
0 ignored issues
show
Unused Code introduced by
The assignment to $glist is dead and can be removed.
Loading history...
Bug introduced by
The method getGroupList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

73
        /** @scrutinizer ignore-call */ 
74
        $glist         = $memberHandler->getGroupList();
Loading history...
74
75
        // Applying permission mask
76
        foreach ($permMask as $perm) {
77
            if (1 == $perm->getVar('gperm_itemid')) {
78
                $grouppermHandler->addRight('extcal_cat_view', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
79
            }
80
            if (2 == $perm->getVar('gperm_itemid')) {
81
                $grouppermHandler->addRight('extcal_cat_submit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
82
            }
83
            if (4 == $perm->getVar('gperm_itemid')) {
84
                $grouppermHandler->addRight('extcal_cat_autoapprove', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
85
            }
86
            if (8 == $perm->getVar('gperm_itemid')) {
87
                $grouppermHandler->addRight('extcal_cat_edit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
88
            }
89
        }
90
91
        return true;
92
    }
93
94
    /**
95
     * @param $catId
96
     * @param $data
97
     *
98
     * @return bool
99
     */
100
    public function modifyCat($catId, $data)
101
    {
102
        $cat = $this->get($catId);
103
        $cat->setVars($data);
104
105
        return $this->insert($cat);
106
    }
107
108
    /**
109
     * @param $catId
110
     */
111
    public function deleteCategory($catId)
112
    {
113
        /* TODO :
114
           - Delete all events in this category
115
          */
116
        $this->deleteById($catId);
117
    }
118
119
    // Return one cat selected by his id
120
121
    /**
122
     * @param      $catId
123
     * @param bool $skipPerm
124
     *
125
     * @return bool
126
     */
127
    public function getCat($catId, $skipPerm = false)
128
    {
129
        $criteriaCompo = new \CriteriaCompo();
130
        $criteriaCompo->add(new \Criteria('cat_id', $catId));
131
        if (!$skipPerm) {
132
            $this->addCatPermCriteria($criteriaCompo, $GLOBALS['xoopsUser']);
133
        }
134
        $ret = $this->getObjects($criteriaCompo);
135
        if (isset($ret[0])) {
136
            return $ret[0];
137
        }
138
139
        return false;
140
    }
141
142
    /**
143
     * @param        $user
144
     * @param string $perm
145
     *
146
     * @return array
147
     */
148
    public function getAllCat($user, $perm = 'extcal_cat_view')
149
    {
150
        $criteriaCompo = new \CriteriaCompo();
151
        if ('all' !== $perm) {
152
            $this->addCatPermCriteria($criteriaCompo, $user, $perm);
153
        }
154
155
        return $this->getObjects($criteriaCompo);
156
    }
157
158
    /**
159
     * @param        $user
160
     * @param string $perm
161
     *
162
     * @return array
163
     */
164
    public function getAllCatById($user, $perm = 'all')
165
    {
166
        $criteriaCompo = new \CriteriaCompo();
167
        if ('all' !== $perm) {
168
            $this->addCatPermCriteria($criteriaCompo, $user, $perm);
169
        }
170
171
        $t = $this->objectToArray($this->getObjects($criteriaCompo));
172
        $r = [];
173
        //        while (list($k, $v) = each($t)) {
174
        foreach ($t as $k => $v) {
175
            $r[$v['cat_id']] = $v;
176
        }
177
178
        return $r;
179
    }
180
181
    /**
182
     * @param \CriteriaElement   $criteria
183
     * @param                    $user
184
     * @param string             $perm
185
     */
186
    public function addCatPermCriteria(\CriteriaElement $criteria, $user, $perm = 'extcal_cat_view')
0 ignored issues
show
Unused Code introduced by
The parameter $perm 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

186
    public function addCatPermCriteria(\CriteriaElement $criteria, $user, /** @scrutinizer ignore-unused */ $perm = 'extcal_cat_view')

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...
187
    {
188
        $authorizedAccessCats = $this->extcalPerm->getAuthorizedCat($user, 'extcal_cat_view');
189
        $count                = \count($authorizedAccessCats);
0 ignored issues
show
Bug introduced by
$authorizedAccessCats of type boolean is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

189
        $count                = \count(/** @scrutinizer ignore-type */ $authorizedAccessCats);
Loading history...
190
        if ($count > 0) {
191
            $in = '(' . $authorizedAccessCats[0];
192
            \array_shift($authorizedAccessCats);
0 ignored issues
show
Bug introduced by
$authorizedAccessCats of type boolean is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

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

192
            \array_shift(/** @scrutinizer ignore-type */ $authorizedAccessCats);
Loading history...
193
            foreach ($authorizedAccessCats as $authorizedAccessCat) {
194
                $in .= ',' . $authorizedAccessCat;
195
            }
196
            $in .= ')';
197
            $criteria->add(new \Criteria('cat_id', $in, 'IN'));
0 ignored issues
show
Bug introduced by
The method add() does not exist on CriteriaElement. It seems like you code against a sub-type of CriteriaElement such as CriteriaCompo. ( Ignorable by Annotation )

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

197
            $criteria->/** @scrutinizer ignore-call */ 
198
                       add(new \Criteria('cat_id', $in, 'IN'));
Loading history...
198
        } else {
199
            $criteria->add(new \Criteria('cat_id', '(0)', 'IN'));
200
        }
201
    }
202
203
    /**
204
     * @param \XoopsUser|string $xoopsUser
205
     *
206
     * @return bool
207
     */
208
    public function haveSubmitRight($xoopsUser)
209
    {
210
        return \count($this->extcalPerm->getAuthorizedCat($xoopsUser, 'extcal_cat_submit')) > 0;
0 ignored issues
show
Bug introduced by
$this->extcalPerm->getAu...r, 'extcal_cat_submit') of type boolean is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

210
        return \count(/** @scrutinizer ignore-type */ $this->extcalPerm->getAuthorizedCat($xoopsUser, 'extcal_cat_submit')) > 0;
Loading history...
211
    }
212
}
213