Completed
Push — master ( 937117...931dfe )
by Michael
05:45 queued 02:42
created

MyXoopsGroupFormCheckBox::_renderOptionTree()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 24
nop 4
dl 0
loc 44
rs 6.7272
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 38 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    XOOPS Project http://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') || exit('XOOPS root path not defined');
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 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
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...
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 = array();
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 = array();
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
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
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[] = array(
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
        global $xoopsGTicket;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

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