Completed
Push — master ( 68c045...6d06de )
by Michael
02:34
created

ExtcalCatHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 22.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      extcal
16
 * @since
17
 * @author       XOOPS Development Team,
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
22
require_once __DIR__ . '/ExtcalPersistableObjectHandler.php';
23
require_once __DIR__ . '/perm.php';
24
require_once __DIR__ . '/time.php';
25
26
/**
27
 * Class ExtcalCat.
28
 */
29
class ExtcalCat extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    public $externalKey = [];
32
33
    /**
34
     * ExtcalCat constructor.
35
     */
36
    public function __construct()
37
    {
38
        $this->initVar('cat_id', XOBJ_DTYPE_INT, null, false);
39
        $this->initVar('cat_name', XOBJ_DTYPE_TXTBOX, null, true, 255);
40
        $this->initVar('cat_desc', XOBJ_DTYPE_TXTAREA, null, false);
41
        $this->initVar('cat_color', XOBJ_DTYPE_TXTBOX, '000000', false, 255);
42
        $this->initVar('cat_weight', XOBJ_DTYPE_INT, 0, false);
43
        $this->initVar('cat_icone', XOBJ_DTYPE_TXTBOX, '', false, 50);
44
    }
45
}
46
47
/**
48
 * Class ExtcalCatHandler.
49
 */
50
class ExtcalCatHandler extends ExtcalPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
51
{
52
    public $_extcalPerm;
53
54
    /**
55
     * @param $db
56
     */
57
    public function __construct(XoopsDatabase $db)
58
    {
59
        $this->_extcalPerm = ExtcalPerm::getHandler();
60
        parent::__construct($db, 'extcal_cat', _EXTCAL_CLN_CAT, 'cat_id');
61
    }
62
63
    /**
64
     * @param $data
65
     *
66
     * @return bool
67
     */
68
    public function createCat($data)
0 ignored issues
show
Coding Style introduced by
createCat uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
69
    {
70
        $cat = $this->create();
71
        $cat->setVars($data);
72
        $this->insert($cat);
73
74
        $catId = $this->getInsertId();
0 ignored issues
show
Unused Code introduced by
$catId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
76
        // Retriving permission mask
77
        /** @var XoopsGroupPermHandler $groupPermissionHandler */
78
        $groupPermissionHandler = xoops_getHandler('groupperm');
79
        $moduleId               = $GLOBALS['xoopsModule']->getVar('mid');
80
81
        $criteria = new CriteriaCompo();
82
        $criteria->add(new Criteria('gperm_name', 'extcal_perm_mask'));
83
        $criteria->add(new Criteria('gperm_modid', $moduleId));
84
        $permMask = $groupPermissionHandler->getObjects($criteria);
85
86
        // Retriving group list
87
        $memberHandler = xoops_getHandler('member');
88
        $glist         = $memberHandler->getGroupList();
0 ignored issues
show
Unused Code introduced by
$glist is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
90
        // Applying permission mask
91
        foreach ($permMask as $perm) {
92 View Code Duplication
            if (1 == $perm->getVar('gperm_itemid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
                $groupPermissionHandler->addRight('extcal_cat_view', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
94
            }
95 View Code Duplication
            if (2 == $perm->getVar('gperm_itemid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                $groupPermissionHandler->addRight('extcal_cat_submit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
97
            }
98 View Code Duplication
            if (4 == $perm->getVar('gperm_itemid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                $groupPermissionHandler->addRight('extcal_cat_autoapprove', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
100
            }
101 View Code Duplication
            if (8 == $perm->getVar('gperm_itemid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
                $groupPermissionHandler->addRight('extcal_cat_edit', $cat->getVar('cat_id'), $perm->getVar('gperm_groupid'), $moduleId);
103
            }
104
        }
105
106
        return true;
107
    }
108
109
    /**
110
     * @param $catId
111
     * @param $data
112
     *
113
     * @return bool
114
     */
115
    public function modifyCat($catId, $data)
116
    {
117
        $cat = $this->get($catId);
118
        $cat->setVars($data);
119
120
        return $this->insert($cat);
121
    }
122
123
    /**
124
     * @param $catId
125
     */
126
    public function deleteCat($catId)
127
    {
128
        /* TODO :
129
           - Delete all events in this category
130
          */
131
        $this->deleteById($catId);
132
    }
133
134
    // Return one cat selected by his id
135
136
    /**
137
     * @param      $catId
138
     * @param bool $skipPerm
139
     *
140
     * @return bool
141
     */
142
    public function getCat($catId, $skipPerm = false)
0 ignored issues
show
Coding Style introduced by
getCat uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
143
    {
144
        $criteriaCompo = new CriteriaCompo();
145
        $criteriaCompo->add(new Criteria('cat_id', $catId));
146
        if (!$skipPerm) {
147
            $this->_addCatPermCriteria($criteriaCompo, $GLOBALS['xoopsUser']);
148
        }
149
        $ret = $this->getObjects($criteriaCompo);
150
        if (isset($ret[0])) {
151
            return $ret[0];
152
        } else {
153
            return false;
154
        }
155
    }
156
157
    /**
158
     * @param        $user
159
     * @param string $perm
160
     *
161
     * @return array
162
     */
163
    public function getAllCat($user, $perm = 'extcal_cat_view')
164
    {
165
        $criteriaCompo = new CriteriaCompo();
166
        if ('all' !== $perm) {
167
            $this->_addCatPermCriteria($criteriaCompo, $user, $perm);
168
        }
169
170
        return $this->getObjects($criteriaCompo);
171
    }
172
173
    /**
174
     * @param        $user
175
     * @param string $perm
176
     *
177
     * @return array
178
     */
179
    public function getAllCatById($user, $perm = 'all')
180
    {
181
        $criteriaCompo = new CriteriaCompo();
182
        if ('all' !== $perm) {
183
            $this->_addCatPermCriteria($criteriaCompo, $user, $perm);
184
        }
185
186
        $t = $this->objectToArray($this->getObjects($criteriaCompo));
187
        $r = [];
188
        //        while (list($k, $v) = each($t)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189
        foreach ($t as $k => $v) {
190
            $r[$v['cat_id']] = $v;
191
        }
192
193
        return $r;
194
    }
195
196
    /**
197
     * @param        $criteria
198
     * @param        $user
199
     * @param string $perm
200
     */
201 View Code Duplication
    public function _addCatPermCriteria(&$criteria, &$user, $perm = 'extcal_cat_view')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        $authorizedAccessCats = $this->_extcalPerm->getAuthorizedCat($user, 'extcal_cat_view');
204
        $count                = count($authorizedAccessCats);
205
        if ($count > 0) {
206
            $in = '(' . $authorizedAccessCats[0];
207
            array_shift($authorizedAccessCats);
208
            foreach ($authorizedAccessCats as $authorizedAccessCat) {
209
                $in .= ',' . $authorizedAccessCat;
210
            }
211
            $in .= ')';
212
            $criteria->add(new Criteria('cat_id', $in, 'IN'));
213
        } else {
214
            $criteria->add(new Criteria('cat_id', '(0)', 'IN'));
215
        }
216
    }
217
218
    /**
219
     * @param $xoopsUser
220
     *
221
     * @return bool
222
     */
223
    public function haveSubmitRight(&$xoopsUser)
224
    {
225
        return count($this->_extcalPerm->getAuthorizedCat($xoopsUser, 'extcal_cat_submit')) > 0;
226
    }
227
}
228