MyXoopsGroupFormCheckBox   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 178
rs 10
c 0
b 0
f 0
wmc 22

6 Methods

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

156
        /** @scrutinizer ignore-call */ 
157
        $glist            = $memberHandler->getGroupList();
Loading history...
157
        foreach (array_keys($glist) as $i) {
158
            // get selected item id(s) for each group
159
            $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

159
            /** @scrutinizer ignore-call */ 
160
            $selected = $grouppermHandler->getItemIds($this->_permName, $i, $this->_modid);
Loading history...
160
            $ele      = new MyXoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
161
            $ele->setOptionTree($this->_itemTree);
162
163
            foreach ($this->_appendix as $key => $append) {
164
                $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

164
                /** @scrutinizer ignore-call */ 
165
                $this->_appendix[$key]['selected'] = $grouppermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
Loading history...
165
            }
166
            $ele->setAppendix($this->_appendix);
167
            $this->addElement($ele);
168
            unset($ele);
169
        }
170
171
        // GIJ start
172
        $jstray          = new \XoopsFormElementTray(' &nbsp; ');
173
        $jsuncheckbutton = new \XoopsFormButton('', 'none', _NONE, 'button');
174
        $jsuncheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if (elements[i].type=='checkbox') {elements[i].checked=false;}}}\"");
175
        $jscheckbutton = new \XoopsFormButton('', 'all', _ALL, 'button');
176
        $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;}}}\"");
177
        $jstray->addElement($jsuncheckbutton);
178
        $jstray->addElement($jscheckbutton);
179
        $this->addElement($jstray);
180
        // GIJ end
181
182
        $tray = new \XoopsFormElementTray('');
183
        $tray->addElement(new \XoopsFormButton('', 'reset', _CANCEL, 'reset'));
184
        $tray->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
185
        $this->addElement($tray);
186
187
        $ret      = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br>';
188
        $ret      .= "<form name='" . $this->getName() . "' id='" . $this->getName() . "' action='" . $this->getAction() . "' method='" . $this->getMethod() . "'" . $this->getExtra() . ">\n<table width='100%' class='outer' cellspacing='1'>\n";
189
        $elements = $this->getElements();
190
        foreach (array_keys($elements) as $i) {
191
            if (!is_object($elements[$i])) {
192
                $ret .= $elements[$i];
193
            } elseif (!$elements[$i]->isHidden()) {
194
                $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
195
                if ('' !== $elements[$i]->getDescription()) {
196
                    $ret .= '<br><br><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
197
                }
198
                $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...
199
            } else {
200
                $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...
201
            }
202
        }
203
        $ret .= '</table>' . $GLOBALS['xoopsSecurity']->getTokenHTML('myblocksadmin') . '</form>';
204
205
        return $ret;
206
    }
207
}
208
209
/**
210
 * Renders checkbox options for a group permission form
211
 *
212
 * @author       Kazumi Ono    <[email protected]>
213
 * @copyright    copyright (c) 2000-2003 XOOPS.org
214
 *
215
 * @package      kernel
216
 * @subpackage   form
217
 */
218
class MyXoopsGroupFormCheckBox extends \XoopsFormElement
219
{
220
    /**
221
     * Pre-selected value(s)
222
     * @var array;
223
     */
224
    public $_value;
225
    /**
226
     * Group ID
227
     * @var int
228
     */
229
    public $_groupId;
230
    /**
231
     * Option tree
232
     * @var array
233
     */
234
    public $_optionTree;
235
    /**
236
     * Appendix
237
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
238
     */
239
    public $_appendix = [];
240
241
    /**
242
     * Constructor
243
     * @param      $caption
244
     * @param      $name
245
     * @param      $groupId
246
     * @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...
247
     */
248
    public function __construct($caption, $name, $groupId, $values = null)
249
    {
250
        $this->setCaption($caption);
251
        $this->setName($name);
252
        if (isset($values)) {
253
            $this->setValue($values);
254
        }
255
        $this->_groupId = $groupId;
256
    }
257
258
    /**
259
     * Sets pre-selected values
260
     *
261
     * @param mixed $value A group ID or an array of group IDs
262
     * @access public
263
     */
264
    public function setValue($value)
265
    {
266
        if (is_array($value)) {
267
            foreach ($value as $v) {
268
                $this->setValue($v);
269
            }
270
        } else {
271
            $this->_value[] = $value;
272
        }
273
    }
274
275
    /**
276
     * Sets the tree structure of items
277
     *
278
     * @param array $optionTree
279
     * @access public
280
     */
281
    public function setOptionTree($optionTree)
282
    {
283
        $this->_optionTree = $optionTree;
284
    }
285
286
    /**
287
     * Sets appendix of checkboxes
288
     *
289
     * @access public
290
     * @param $appendix
291
     */
292
    public function setAppendix($appendix)
293
    {
294
        $this->_appendix = $appendix;
295
    }
296
297
    /**
298
     * Renders checkbox options for this group
299
     *
300
     * @return string
301
     * @access public
302
     */
303
    public function render()
304
    {
305
        $ret = '';
306
307
        if (count($this->_appendix) > 0) {
308
            $ret  .= '<table class="outer"><tr>';
309
            $cols = 1;
310
            foreach ($this->_appendix as $append) {
311
                if ($cols > 4) {
312
                    $ret  .= '</tr><tr>';
313
                    $cols = 1;
314
                }
315
                $checked = $append['selected'] ? 'checked' : '';
316
                $name    = 'perms[' . $append['permname'] . ']';
317
                $itemid  = $append['itemid'];
0 ignored issues
show
Unused Code introduced by
The assignment to $itemid is dead and can be removed.
Loading history...
318
                $itemid  = $append['itemid'];
319
                $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>";
320
                ++$cols;
321
            }
322
            $ret .= '</tr></table>';
323
        }
324
325
        $ret  .= '<table class="outer"><tr>';
326
        $cols = 1;
327
        if (!empty($this->_optionTree[0]['children'])) {
328
            foreach ($this->_optionTree[0]['children'] as $topitem) {
329
                if ($cols > 4) {
330
                    $ret  .= '</tr><tr>';
331
                    $cols = 1;
332
                }
333
                $tree   = '<td class="odd">';
334
                $prefix = '';
335
                $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
336
                $ret .= $tree . '</td>';
337
                ++$cols;
338
            }
339
        }
340
        $ret .= '</tr></table>';
341
342
        return $ret;
343
    }
344
345
    /**
346
     * Renders checkbox options for an item tree
347
     *
348
     * @param string $tree
349
     * @param array  $option
350
     * @param string $prefix
351
     * @param array  $parentIds
352
     * @access private
353
     */
354
    public function _renderOptionTree($tree, $option, $prefix, $parentIds = [])
355
    {
356
        $tree .= $prefix . '<input type="checkbox" name="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" id="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" onclick="';
357
        // If there are parent elements, add javascript that will
358
        // make them selecteded when this element is checked to make
359
        // sure permissions to parent items are added as well.
360
        foreach ($parentIds as $pid) {
361
            $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
362
            $tree       .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked !== true) {ele.checked = this.checked;}";
363
        }
364
        // If there are child elements, add javascript that will
365
        // make them unchecked when this element is unchecked to make
366
        // sure permissions to child items are not added when there
367
        // is no permission to this item.
368
        foreach ($option['allchild'] as $cid) {
369
            $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
370
            $tree      .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked !== true) {ele.checked = false;}";
371
        }
372
        $tree .= '" value="1"';
373
        if (isset($this->_value) && in_array($option['id'], $this->_value, true)) {
374
            $tree .= ' checked';
375
        }
376
        $tree .= '>'
377
                 . $option['name']
378
                 . '<input type="hidden" name="'
379
                 . $this->getName()
380
                 . '[parents]['
381
                 . $option['id']
382
                 . ']" value="'
383
                 . implode(':', $parentIds)
384
                 . '"><input type="hidden" name="'
385
                 . $this->getName()
386
                 . '[itemname]['
387
                 . $option['id']
388
                 . ']" value="'
389
                 . htmlspecialchars($option['name'], ENT_QUOTES
390
                                                     | ENT_HTML5)
391
                 . "\"><br>\n";
392
        if (isset($option['children'])) {
393
            foreach ($option['children'] as $child) {
394
                $parentIds[] = $option['id'];
395
                $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
396
            }
397
        }
398
    }
399
}
400