Passed
Pull Request — master (#9)
by Michael
03:24
created

GroupPermForm::addItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace XoopsModules\Mylinks;
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    XOOPS Project https://xoops.org/
17
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @package
19
 * @since
20
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
21
 */
22
23
24
25
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
26
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
27
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
28
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
29
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
30
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
31
32
/**
33
 * Renders a form for setting module specific group permissions
34
 *
35
 * @author       Kazumi Ono    <[email protected]>
36
 * @copyright    copyright (c) 2000-2003 XOOPS.org
37
 *
38
 * @package      kernel
39
 * @subpackage   form
40
 */
41
class GroupPermForm extends \XoopsForm
42
{
43
    /**
44
     * Module ID
45
     *
46
     * @var int
47
     */
48
    public $_modid;
49
    /**
50
     * Tree structure of items
51
     *
52
     * @var array
53
     */
54
    public $_itemTree = [];
55
    /**
56
     * Name of permission
57
     *
58
     * @var string
59
     */
60
    public $_permName;
61
    /**
62
     * Description of permission
63
     *
64
     * @var string
65
     */
66
    public $_permDesc;
67
    /**
68
     * Appendix
69
     *
70
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
71
     */
72
    public $_appendix = [];
73
74
    /**
75
     * Constructor
76
     * @param string $title
77
     * @param string $modid
78
     * @param string $permname
79
     * @param string $permdesc
80
     */
81
    public function __construct($title, $modid, $permname, $permdesc)
82
    {
83
        //        $this->XoopsForm($title, 'groupperm_form', XOOPS_URL.'/modules/system/admin/groupperm.php', 'post'); GIJ
84
        parent::__construct($title, 'groupperm_form', '', 'post');
85
        $this->_modid    = (int)$modid;
86
        $this->_permName = $permname;
87
        $this->_permDesc = $permdesc;
88
        $this->addElement(new \XoopsFormHidden('modid', $this->_modid));
89
        $this->addElement(new \XoopsFormHiddenToken($permname));
90
    }
91
92
    /**
93
     * Adds an item to which permission will be assigned
94
     *
95
     * @param string $itemName
96
     * @param int    $itemId
97
     * @param int    $itemParent
98
     *
99
     * @access public
100
     */
101
    public function addItem($itemId, $itemName, $itemParent = 0)
102
    {
103
        $this->_itemTree[$itemParent]['children'][] = $itemId;
104
        $this->_itemTree[$itemId]['parent']         = $itemParent;
105
        $this->_itemTree[$itemId]['name']           = $itemName;
106
        $this->_itemTree[$itemId]['id']             = $itemId;
107
    }
108
109
    /**
110
     * Add appendix
111
     *
112
     * @access public
113
     * @param $permName
114
     * @param $itemId
115
     * @param $itemName
116
     */
117
    public function addAppendix($permName, $itemId, $itemName)
118
    {
119
        $this->_appendix[] = [
120
            'permname' => $permName,
121
            'itemid'   => $itemId,
122
            'itemname' => $itemName,
123
            'selected' => false,
124
        ];
125
    }
126
127
    /**
128
     * Loads all child ids for an item to be used in javascript
129
     *
130
     * @param int   $itemId
131
     * @param array $childIds
132
     *
133
     * @access private
134
     */
135
    public function _loadAllChildItemIds($itemId, &$childIds)
136
    {
137
        if (!empty($this->_itemTree[$itemId]['children'])) {
138
            $first_child = $this->_itemTree[$itemId]['children'];
139
            foreach ($first_child as $fcid) {
140
                array_push($childIds, $fcid);
141
                if (!empty($this->_itemTree[$fcid]['children'])) {
142
                    foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
143
                        array_push($childIds, $_fcid);
144
                        $this->_loadAllChildItemIds($_fcid, $childIds);
145
                    }
146
                }
147
            }
148
        }
149
    }
150
151
    /**
152
     * Renders the form
153
     *
154
     * @return string
155
     * @access public
156
     */
157
    public function render()
158
    {
159
        // load all child ids for javascript codes
160
        foreach (array_keys($this->_itemTree) as $item_id) {
161
            $this->_itemTree[$item_id]['allchild'] = [];
162
            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
163
        }
164
        $grouppermHandler = xoops_getHandler('groupperm');
165
        $memberHandler    = xoops_getHandler('member');
166
        $glist            = $memberHandler->getGroupList();
0 ignored issues
show
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

166
        /** @scrutinizer ignore-call */ 
167
        $glist            = $memberHandler->getGroupList();
Loading history...
167
        foreach (array_keys($glist) as $i) {
168
            // get selected item id(s) for each group
169
            $selected = $grouppermHandler->getItemIds($this->_permName, $i, $this->_modid);
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

169
            /** @scrutinizer ignore-call */ 
170
            $selected = $grouppermHandler->getItemIds($this->_permName, $i, $this->_modid);
Loading history...
170
            $ele      = new MyXoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
171
            $ele->setOptionTree($this->_itemTree);
172
173
            foreach ($this->_appendix as $key => $append) {
174
                $this->_appendix[$key]['selected'] = $grouppermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
0 ignored issues
show
Bug introduced by
The method checkRight() 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

174
                /** @scrutinizer ignore-call */ 
175
                $this->_appendix[$key]['selected'] = $grouppermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
Loading history...
175
            }
176
            $ele->setAppendix($this->_appendix);
177
            $this->addElement($ele);
178
            unset($ele);
179
        }
180
181
        // GIJ start
182
        $jstray          = new \XoopsFormElementTray(' &nbsp; ');
183
        $jsuncheckbutton = new \XoopsFormButton('', 'none', _NONE, 'button');
184
        $jsuncheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if (elements[i].type=='checkbox') {elements[i].checked=false;}}}\"");
185
        $jscheckbutton = new \XoopsFormButton('', 'all', _ALL, 'button');
186
        $jscheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if(elements[i].type=='checkbox' && (elements[i].name.indexOf('module_admin')<0 || elements[i].name.indexOf('[groups][1]')>=0)) {elements[i].checked=true;}}}\"");
187
        $jstray->addElement($jsuncheckbutton);
188
        $jstray->addElement($jscheckbutton);
189
        $this->addElement($jstray);
190
        // GIJ end
191
192
        $tray = new \XoopsFormElementTray('');
193
        $tray->addElement(new \XoopsFormButton('', 'reset', _CANCEL, 'reset'));
194
        $tray->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
195
        $this->addElement($tray);
196
197
        $ret      = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br>';
198
        $ret      .= "<form name='" . $this->getName() . "' id='" . $this->getName() . "' action='" . $this->getAction() . "' method='" . $this->getMethod() . "'" . $this->getExtra() . ">\n<table style='width: 100% margin: 1px;' class='outer'>\n";
199
        $elements = &$this->getElements();
200
        foreach (array_keys($elements) as $i) {
201
            if (!is_object($elements[$i])) {
202
                $ret .= $elements[$i];
203
            } elseif (!$elements[$i]->isHidden()) {
204
                $ret .= "<tr style='vertical-align: top; text-align: left;'><td class='head'>" . $elements[$i]->getCaption();
205
                if ('' != $elements[$i]->getDescription()) {
206
                    $ret .= '<br><br><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
207
                }
208
                $ret .= "</td>\n<td class='even'>\n" . $elements[$i]->render() . "\n</td></tr>\n";
0 ignored issues
show
Bug introduced by
Are you sure the usage of $elements[$i]->render() targeting XoopsFormElement::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
209
            } else {
210
                $ret .= $elements[$i]->render();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $elements[$i]->render() targeting XoopsFormElement::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
211
            }
212
        }
213
        $ret .= '</table>' . $GLOBALS['xoopsSecurity']->getTokenHTML() . '</form>';
214
215
        return $ret;
216
    }
217
}
218
219
/**
220
 * Renders checkbox options for a group permission form
221
 *
222
 * @author       Kazumi Ono    <[email protected]>
223
 * @copyright    copyright (c) 2000-2003 XOOPS.org
224
 *
225
 * @package      kernel
226
 * @subpackage   form
227
 */
228
class MyXoopsGroupFormCheckBox extends \XoopsFormElement
229
{
230
    /**
231
     * Pre-selected value(s)
232
     *
233
     * @var array;
234
     */
235
    public $_value;
236
    /**
237
     * Group ID
238
     *
239
     * @var int
240
     */
241
    public $_groupId;
242
    /**
243
     * Option tree
244
     *
245
     * @var array
246
     */
247
    public $_optionTree;
248
    /**
249
     * Appendix
250
     *
251
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
252
     */
253
    public $_appendix = [];
254
255
    /**
256
     * Constructor
257
     * @param      $caption
258
     * @param      $name
259
     * @param      $groupId
260
     * @param null $values
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $values is correct as it would always require null to be passed?
Loading history...
261
     */
262
    public function __construct($caption, $name, $groupId, $values = null)
263
    {
264
        $this->setCaption($caption);
265
        $this->setName($name);
266
        if (isset($values)) {
267
            $this->setValue($values);
268
        }
269
        $this->_groupId = $groupId;
270
    }
271
272
    /**
273
     * Sets pre-selected values
274
     *
275
     * @param mixed $value A group ID or an array of group IDs
276
     *
277
     * @access public
278
     */
279
    public function setValue($value)
280
    {
281
        if (is_array($value)) {
282
            foreach ($value as $v) {
283
                $this->setValue($v);
284
            }
285
        } else {
286
            $this->_value[] = $value;
287
        }
288
    }
289
290
    /**
291
     * Sets the tree structure of items
292
     *
293
     * @param array $optionTree
294
     *
295
     * @access public
296
     */
297
    public function setOptionTree(&$optionTree)
298
    {
299
        $this->_optionTree = &$optionTree;
300
    }
301
302
    /**
303
     * Sets appendix of checkboxes
304
     *
305
     * @access public
306
     * @param $appendix
307
     */
308
    public function setAppendix($appendix)
309
    {
310
        $this->_appendix = $appendix;
311
    }
312
313
    /**
314
     * Renders checkbox options for this group
315
     *
316
     * @return string
317
     * @access public
318
     */
319
    public function render()
320
    {
321
        $ret = '';
322
323
        if (count($this->_appendix) > 0) {
324
            $ret  .= '<table class="outer"><tr>';
325
            $cols = 1;
326
            foreach ($this->_appendix as $append) {
327
                if ($cols > 4) {
328
                    $ret  .= '</tr><tr>';
329
                    $cols = 1;
330
                }
331
                $checked = $append['selected'] ? 'checked' : '';
332
                $name    = 'perms[' . $append['permname'] . ']';
333
                $itemid  = $append['itemid'];
0 ignored issues
show
Unused Code introduced by
The assignment to $itemid is dead and can be removed.
Loading history...
334
                $itemid  = $append['itemid'];
335
                $ret     .= "<td class=\"odd\"><input type=\"checkbox\" name=\"{$name}[groups][$this->_groupId][$itemid]\" id=\"{$name}[groups][$this->_groupId][$itemid]\" value=\"1\" $checked>{$append['itemname']}<input type=\"hidden\" name=\"{$name}[parents][$itemid]\" value=\"\"><input type=\"hidden\" name=\"{$name}[itemname][$itemid]\" value=\"{$append['itemname']}\"><br></td>";
336
                ++$cols;
337
            }
338
            $ret .= '</tr></table>';
339
        }
340
341
        $ret  .= '<table class="outer"><tr>';
342
        $cols = 1;
343
        if (!empty($this->_optionTree[0]['children'])) {
344
            foreach ($this->_optionTree[0]['children'] as $topitem) {
345
                if ($cols > 4) {
346
                    $ret  .= '</tr><tr>';
347
                    $cols = 1;
348
                }
349
                $tree   = '<td class="odd">';
350
                $prefix = '';
351
                $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
352
                $ret .= "{$tree}</td>";
353
                ++$cols;
354
            }
355
        }
356
        $ret .= '</tr></table>';
357
358
        return $ret;
359
    }
360
361
    /**
362
     * Renders checkbox options for an item tree
363
     *
364
     * @param string $tree
365
     * @param array  $option
366
     * @param string $prefix
367
     * @param array  $parentIds
368
     *
369
     * @access private
370
     */
371
    public function _renderOptionTree(&$tree, $option, $prefix, $parentIds = [])
372
    {
373
        $tree .= $prefix . '<input type="checkbox" name="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" id="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" onclick="';
374
        // If there are parent elements, add javascript that will
375
        // make them selecteded when this element is checked to make
376
        // sure permissions to parent items are added as well.
377
        foreach ($parentIds as $pid) {
378
            $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
379
            $tree       .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked !== true) {ele.checked = this.checked;}";
380
        }
381
        // If there are child elements, add javascript that will
382
        // make them unchecked when this element is unchecked to make
383
        // sure permissions to child items are not added when there
384
        // is no permission to this item.
385
        foreach ($option['allchild'] as $cid) {
386
            $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
387
            $tree      .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked !== true) {ele.checked = false;}";
388
        }
389
        $tree .= '" value="1"';
390
        if (isset($this->_value) && in_array($option['id'], $this->_value)) {
391
            $tree .= ' checked';
392
        }
393
        $tree .= '>' . $option['name'] . '<input type="hidden" name="' . $this->getName() . '[parents][' . $option['id'] . ']" value="' . implode(':', $parentIds) . '"><input type="hidden" name="' . $this->getName() . '[itemname][' . $option['id'] . ']" value="' . htmlspecialchars(
394
                $option['name'],
395
                ENT_QUOTES | ENT_HTML5
396
            ) . "\"><br>\n";
397
        if (isset($option['children'])) {
398
            foreach ($option['children'] as $child) {
399
                array_push($parentIds, $option['id']);
400
                $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
401
            }
402
        }
403
    }
404
}
405