Completed
Branch master (c92e39)
by Michael
02:32
created

MyXoopsGroupPermForm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 181
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A addItem() 0 7 1
A addAppendix() 0 9 1
B _loadAllChildItemIds() 0 15 5
C render() 0 70 8
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 43 and the first side effect is on line 27.

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
/**
21
 * Module: SmartFAQ
22
 * Author: The SmartFactory <www.smartfactory.ca>
23
 */
24
25
// 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...
26
27
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
28
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
29
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
30
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
31
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
32
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
33
34
/**
35
 * Renders a form for setting module specific group permissions
36
 *
37
 * @author      Kazumi Ono  <[email protected]>
38
 * @copyright   copyright (c) 2000-2003 XOOPS.org
39
 *
40
 * @package     kernel
41
 * @subpackage  form
42
 */
43
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...
44
{
45
46
    /**
47
     * Module ID
48
     * @var int
49
     */
50
    public $_modid;
51
    /**
52
     * Tree structure of items
53
     * @var array
54
     */
55
    public $_itemTree;
56
    /**
57
     * Name of permission
58
     * @var string
59
     */
60
    public $_permName;
61
    /**
62
     * Description of permission
63
     * @var string
64
     */
65
    public $_permDesc;
66
    /**
67
     * Appendix
68
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
69
     */
70
    public $_appendix = array();
71
72
    /**
73
     * Constructor
74
     * @param string $title
75
     * @param string $modid
76
     * @param string $permname
77
     * @param string $permdesc
78
     */
79
    public function __construct($title, $modid, $permname, $permdesc)
80
    {
81
        //      $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...
82
        parent::__construct($title, 'groupperm_form', '', 'post');
83
        $this->_modid    = (int)$modid;
84
        $this->_permName = $permname;
85
        $this->_permDesc = $permdesc;
86
        $this->addElement(new XoopsFormHidden('modid', $this->_modid));
87
        $this->addElement(new XoopsFormHiddenToken($permname));
88
    }
89
90
    /**
91
     * Adds an item to which permission will be assigned
92
     *
93
     * @param string $itemName
94
     * @param int    $itemId
95
     * @param int    $itemParent
96
     * @access public
97
     */
98
    public function addItem($itemId, $itemName, $itemParent = 0)
99
    {
100
        $this->_itemTree[$itemParent]['children'][] = $itemId;
101
        $this->_itemTree[$itemId]['parent']         = $itemParent;
102
        $this->_itemTree[$itemId]['name']           = $itemName;
103
        $this->_itemTree[$itemId]['id']             = $itemId;
104
    }
105
106
    /**
107
     * Add appendix
108
     *
109
     * @access public
110
     * @param $permName
111
     * @param $itemId
112
     * @param $itemName
113
     */
114
    public function addAppendix($permName, $itemId, $itemName)
115
    {
116
        $this->_appendix[] = array(
117
            'permname' => $permName,
118
            'itemid'   => $itemId,
119
            'itemname' => $itemName,
120
            'selected' => false
121
        );
122
    }
123
124
    /**
125
     * Loads all child ids for an item to be used in javascript
126
     *
127
     * @param int   $itemId
128
     * @param array $childIds
129
     * @access private
130
     */
131
    public function _loadAllChildItemIds($itemId, &$childIds)
132
    {
133
        if (!empty($this->_itemTree[$itemId]['children'])) {
134
            $first_child = $this->_itemTree[$itemId]['children'];
135
            foreach ($first_child as $fcid) {
136
                array_push($childIds, $fcid);
137
                if (!empty($this->_itemTree[$fcid]['children'])) {
138
                    foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
139
                        array_push($childIds, $_fcid);
140
                        $this->_loadAllChildItemIds($_fcid, $childIds);
141
                    }
142
                }
143
            }
144
        }
145
    }
146
147
    /**
148
     * Renders the form
149
     *
150
     * @return string
151
     * @access public
152
     */
153
    public function render()
154
    {
155
        // load all child ids for javascript codes
156
        foreach (array_keys($this->_itemTree) as $item_id) {
157
            $this->_itemTree[$item_id]['allchild'] = array();
158
            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
159
        }
160
        $gpermHandler  = xoops_getHandler('groupperm');
161
        $memberHandler = xoops_getHandler('member');
162
        $glist         =& $memberHandler->getGroupList();
163
        foreach (array_keys($glist) as $i) {
164
            // get selected item id(s) for each group
165
            $selected = $gpermHandler->getItemIds($this->_permName, $i, $this->_modid);
166
            $ele      = new MyXoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
167
            $ele->setOptionTree($this->_itemTree);
168
169
            foreach ($this->_appendix as $key => $append) {
170
                $this->_appendix[$key]['selected'] = $gpermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
171
            }
172
            $ele->setAppendix($this->_appendix);
173
            $this->addElement($ele);
174
            unset($ele);
175
        }
176
177
        // GIJ start
178
        $jstray          = new XoopsFormElementTray(' &nbsp; ');
179
        $jsuncheckbutton = new XoopsFormButton('', 'none', _NONE, 'button');
180
        $jsuncheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if (elements[i].type=='checkbox') {elements[i].checked=false;}}}\"");
181
        $jscheckbutton = new XoopsFormButton('', 'all', _ALL, 'button');
182
        $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;}}}\"");
183
        $jstray->addElement($jsuncheckbutton);
184
        $jstray->addElement($jscheckbutton);
185
        $this->addElement($jstray);
186
        // GIJ end
187
188
        $tray = new XoopsFormElementTray('');
189
        $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
190
        $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
191
        $this->addElement($tray);
192
193
        $ret      = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br>';
194
        $ret      .= "<form name='"
195
                     . $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...
196
                     . "' id='"
197
                     . $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...
198
                     . "' action='"
199
                     . $this->getAction()
200
                     . "' method='"
201
                     . $this->getMethod()
202
                     . "'"
203
                     . $this->getExtra()
204
                     . ">\n<table width='100%' class='outer' cellspacing='1'>\n";
205
        $elements =& $this->getElements();
206
        foreach (array_keys($elements) as $i) {
207
            if (!is_object($elements[$i])) {
208
                $ret .= $elements[$i];
209
            } elseif (!$elements[$i]->isHidden()) {
210
                $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
211
                if ($elements[$i]->getDescription() != '') {
212
                    $ret .= '<br><br><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
213
                }
214
                $ret .= "</td>\n<td class='even'>\n" . $elements[$i]->render() . "\n</td></tr>\n";
215
            } else {
216
                $ret .= $elements[$i]->render();
217
            }
218
        }
219
        $ret .= '</table></form>';
220
221
        return $ret;
222
    }
223
}
224
225
/**
226
 * Renders checkbox options for a group permission form
227
 *
228
 * @author      Kazumi Ono  <[email protected]>
229
 * @copyright   copyright (c) 2000-2003 XOOPS.org
230
 *
231
 * @package     kernel
232
 * @subpackage  form
233
 */
234
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...
235
{
236
237
    /**
238
     * Pre-selected value(s)
239
     * @var array;
240
     */
241
    public $_value;
242
    /**
243
     * Group ID
244
     * @var int
245
     */
246
    public $_groupId;
247
    /**
248
     * Option tree
249
     * @var array
250
     */
251
    public $_optionTree;
252
    /**
253
     * Appendix
254
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
255
     */
256
    public $_appendix = array();
257
258
    /**
259
     * Constructor
260
     * @param      $caption
261
     * @param      $name
262
     * @param      $groupId
263
     * @param null $values
264
     */
265
    public function __construct($caption, $name, $groupId, $values = null)
266
    {
267
        $this->setCaption($caption);
268
        $this->setName($name);
269
        if (isset($values)) {
270
            $this->setValue($values);
271
        }
272
        $this->_groupId = $groupId;
273
    }
274
275
    /**
276
     * Sets pre-selected values
277
     *
278
     * @param mixed $value A group ID or an array of group IDs
279
     * @access public
280
     */
281
    public function setValue($value)
282
    {
283
        if (is_array($value)) {
284
            foreach ($value as $v) {
285
                $this->setValue($v);
286
            }
287
        } else {
288
            $this->_value[] = $value;
289
        }
290
    }
291
292
    /**
293
     * Sets the tree structure of items
294
     *
295
     * @param array $optionTree
296
     * @access public
297
     */
298
    public function setOptionTree(&$optionTree)
299
    {
300
        $this->_optionTree =& $optionTree;
301
    }
302
303
    /**
304
     * Sets appendix of checkboxes
305
     *
306
     * @access public
307
     * @param $appendix
308
     */
309
    public function setAppendix($appendix)
310
    {
311
        $this->_appendix = $appendix;
312
    }
313
314
    /**
315
     * Renders checkbox options for this group
316
     *
317
     * @return string
318
     * @access public
319
     */
320
    public function render()
321
    {
322
        $ret = '';
323
324
        if (count($this->_appendix) > 0) {
325
            $ret  .= '<table class="outer"><tr>';
326
            $cols = 1;
327
            foreach ($this->_appendix as $append) {
328
                if ($cols > 4) {
329
                    $ret  .= '</tr><tr>';
330
                    $cols = 1;
331
                }
332
                $checked = $append['selected'] ? 'checked' : '';
333
                $name    = 'perms[' . $append['permname'] . ']';
334
                $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...
335
                $itemid  = $append['itemid'];
336
                $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>";
337
                ++$cols;
338
            }
339
            $ret .= '</tr></table>';
340
        }
341
342
        $ret  .= '<table class="outer"><tr>';
343
        $cols = 1;
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
        $ret .= '</tr></table>';
356
357
        return $ret;
358
    }
359
360
    /**
361
     * Renders checkbox options for an item tree
362
     *
363
     * @param string $tree
364
     * @param array  $option
365
     * @param string $prefix
366
     * @param array  $parentIds
367
     * @access private
368
     */
369
    private function renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
370
    {
371
        $tree .= $prefix
372
                 . '<input type="checkbox" name="'
373
                 . $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...
374
                 . '[groups]['
375
                 . $this->_groupId
376
                 . ']['
377
                 . $option['id']
378
                 . ']" id="'
379
                 . $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...
380
                 . '[groups]['
381
                 . $this->_groupId
382
                 . ']['
383
                 . $option['id']
384
                 . ']" onclick="';
385
        // If there are parent elements, add javascript that will
386
        // make them selecteded when this element is checked to make
387
        // sure permissions to parent items are added as well.
388
        foreach ($parentIds as $pid) {
389
            $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...
390
            $tree       .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked !== true) {ele.checked = this.checked;}";
391
        }
392
        // If there are child elements, add javascript that will
393
        // make them unchecked when this element is unchecked to make
394
        // sure permissions to child items are not added when there
395
        // is no permission to this item.
396
        foreach ($option['allchild'] as $cid) {
397
            $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...
398
            $tree      .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked !== true) {ele.checked = false;}";
399
        }
400
        $tree .= '" value="1"';
401
        if (isset($this->_value) && in_array($option['id'], $this->_value)) {
402
            $tree .= ' checked';
403
        }
404
        $tree .= ' />'
405
                 . $option['name']
406
                 . '<input type="hidden" name="'
407
                 . $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...
408
                 . '[parents]['
409
                 . $option['id']
410
                 . ']" value="'
411
                 . implode(':', $parentIds)
412
                 . '" /><input type="hidden" name="'
413
                 . $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...
414
                 . '[itemname]['
415
                 . $option['id']
416
                 . ']" value="'
417
                 . htmlspecialchars($option['name'])
418
                 . "\" /><br>\n";
419
        if (isset($option['children'])) {
420
            foreach ($option['children'] as $child) {
421
                array_push($parentIds, $option['id']);
422
                $this->renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
423
            }
424
        }
425
    }
426
}
427