Completed
Branch master (1b2f30)
by Michael
06:29 queued 03:22
created

MyXoopsGroupFormCheckBox::renderOptionTree()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 6.7272
cc 7
eloc 16
nc 24
nop 4
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 56 and the first side effect is on line 40.

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
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
// 
10
//  ------------------------------------------------------------------------ //
11
//                XOOPS - PHP Content Management System                      //
12
//                    Copyright (c) 2000-2003 XOOPS.org                           //
13
//                         <http://xoops.org/>                               //
14
//  ------------------------------------------------------------------------ //
15
//  This program is free software; you can redistribute it and/or modify     //
16
//  it under the terms of the GNU General Public License as published by     //
17
//  the Free Software Foundation; either version 2 of the License, or        //
18
//  (at your option) any later version.                                      //
19
//                                                                           //
20
//  You may not change or alter any portion of this comment or credits       //
21
//  of supporting developers from this source code or any supporting         //
22
//  source code which is considered copyrighted (c) material of the          //
23
//  original comment or credit authors.                                      //
24
//                                                                           //
25
//  This program is distributed in the hope that it will be useful,          //
26
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
27
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
28
//  GNU General Public License for more details.                             //
29
//                                                                           //
30
//  You should have received a copy of the GNU General Public License        //
31
//  along with this program; if not, write to the Free Software              //
32
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
33
//  ------------------------------------------------------------------------ //
34
// Author: Kazumi Ono (AKA onokazu)                                          //
35
// URL: http://www.myweb.ne.jp/, http://xoops.org/, http://jp.xoops.org/ //
36
// Project: XOOPS Project                                                    //
37
// ------------------------------------------------------------------------- //
38
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
39
40
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
41
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
42
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
43
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
44
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
45
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
46
47
/**
48
 * Renders a form for setting module specific group permissions
49
 *
50
 * @author      Kazumi Ono  <[email protected]>
51
 * @copyright   copyright (c) 2000-2003 XOOPS.org
52
 *
53
 * @package     kernel
54
 * @subpackage  form
55
 */
56
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...
57
{
58
59
    /**
60
     * Module ID
61
     * @var int
62
     */
63
    public $_modid;
64
    /**
65
     * Tree structure of items
66
     * @var array
67
     */
68
    public $_itemTree;
69
    /**
70
     * Name of permission
71
     * @var string
72
     */
73
    public $_permName;
74
    /**
75
     * Description of permission
76
     * @var string
77
     */
78
    public $_permDesc;
79
    /**
80
     * Appendix
81
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
82
     */
83
    public $_appendix = array();
84
85
    /**
86
     * Constructor
87
     * @param string $title
88
     * @param string $modid
89
     * @param string $permname
90
     * @param string $permdesc
91
     */
92
    public function __construct($title, $modid, $permname, $permdesc)
93
    {
94
        //      $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...
95
        parent::__construct($title, 'groupperm_form', '', 'post');
96
        $this->_modid    = (int)$modid;
97
        $this->_permName = $permname;
98
        $this->_permDesc = $permdesc;
99
        $this->addElement(new XoopsFormHidden('modid', $this->_modid));
100
        $this->addElement(new XoopsFormHiddenToken($permname));
101
    }
102
103
    /**
104
     * Adds an item to which permission will be assigned
105
     *
106
     * @param string $itemName
107
     * @param int    $itemId
108
     * @param int    $itemParent
109
     * @access public
110
     */
111
    public function addItem($itemId, $itemName, $itemParent = 0)
112
    {
113
        $this->_itemTree[$itemParent]['children'][] = $itemId;
114
        $this->_itemTree[$itemId]['parent']         = $itemParent;
115
        $this->_itemTree[$itemId]['name']           = $itemName;
116
        $this->_itemTree[$itemId]['id']             = $itemId;
117
    }
118
119
    /**
120
     * Add appendix
121
     *
122
     * @access public
123
     * @param $permName
124
     * @param $itemId
125
     * @param $itemName
126
     */
127
    public function addAppendix($permName, $itemId, $itemName)
128
    {
129
        $this->_appendix[] = array('permname' => $permName, 'itemid' => $itemId, 'itemname' => $itemName, 'selected' => false);
130
    }
131
132
    /**
133
     * Loads all child ids for an item to be used in javascript
134
     *
135
     * @param int   $itemId
136
     * @param array $childIds
137
     * @access private
138
     */
139
    public function _loadAllChildItemIds($itemId, &$childIds)
140
    {
141
        if (!empty($this->_itemTree[$itemId]['children'])) {
142
            $first_child = $this->_itemTree[$itemId]['children'];
143
            foreach ($first_child as $fcid) {
144
                array_push($childIds, $fcid);
145
                if (!empty($this->_itemTree[$fcid]['children'])) {
146
                    foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
147
                        array_push($childIds, $_fcid);
148
                        $this->_loadAllChildItemIds($_fcid, $childIds);
149
                    }
150
                }
151
            }
152
        }
153
    }
154
155
    /**
156
     * Renders the form
157
     *
158
     * @return string
159
     * @access public
160
     */
161
    public function render()
162
    {
163
        // load all child ids for javascript codes
164
        foreach (array_keys($this->_itemTree) as $item_id) {
165
            $this->_itemTree[$item_id]['allchild'] = array();
166
            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
167
        }
168
        $gpermHandler  = xoops_getHandler('groupperm');
169
        $memberHandler = xoops_getHandler('member');
170
        $glist         =& $memberHandler->getGroupList();
171
        foreach (array_keys($glist) as $i) {
172
            // get selected item id(s) for each group
173
            $selected = $gpermHandler->getItemIds($this->_permName, $i, $this->_modid);
174
            $ele      = new MyXoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
175
            $ele->setOptionTree($this->_itemTree);
176
177
            foreach ($this->_appendix as $key => $append) {
178
                $this->_appendix[$key]['selected'] = $gpermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
179
            }
180
            $ele->setAppendix($this->_appendix);
181
            $this->addElement($ele);
182
            unset($ele);
183
        }
184
185
        // GIJ start
186
        $jstray          = new XoopsFormElementTray(' &nbsp; ');
187
        $jsuncheckbutton = new XoopsFormButton('', 'none', _NONE, 'button');
188
        $jsuncheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if (elements[i].type=='checkbox') {elements[i].checked=false;}}}\"");
189
        $jscheckbutton = new XoopsFormButton('', 'all', _ALL, 'button');
190
        $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;}}}\"");
191
        $jstray->addElement($jsuncheckbutton);
192
        $jstray->addElement($jscheckbutton);
193
        $this->addElement($jstray);
194
        // GIJ end
195
196
        $tray = new XoopsFormElementTray('');
197
        $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
198
        $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
199
        $this->addElement($tray);
200
201
        $ret = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br />';
202
        $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...
203
        $elements =& $this->getElements();
204
        foreach (array_keys($elements) as $i) {
205
            if (!is_object($elements[$i])) {
206
                $ret .= $elements[$i];
207
            } elseif (!$elements[$i]->isHidden()) {
208
                $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
209
                if ($elements[$i]->getDescription() != '') {
210
                    $ret .= '<br /><br /><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
211
                }
212
                $ret .= "</td>\n<td class='even'>\n" . $elements[$i]->render() . "\n</td></tr>\n";
213
            } else {
214
                $ret .= $elements[$i]->render();
215
            }
216
        }
217
        $ret .= '</table></form>';
218
219
        return $ret;
220
    }
221
}
222
223
/**
224
 * Renders checkbox options for a group permission form
225
 *
226
 * @author      Kazumi Ono  <[email protected]>
227
 * @copyright   copyright (c) 2000-2003 XOOPS.org
228
 *
229
 * @package     kernel
230
 * @subpackage  form
231
 */
232
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...
233
{
234
235
    /**
236
     * Pre-selected value(s)
237
     * @var array;
238
     */
239
    public $_value;
240
    /**
241
     * Group ID
242
     * @var int
243
     */
244
    public $_groupId;
245
    /**
246
     * Option tree
247
     * @var array
248
     */
249
    public $_optionTree;
250
    /**
251
     * Appendix
252
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
253
     */
254
    public $_appendix = array();
255
256
    /**
257
     * Constructor
258
     * @param      $caption
259
     * @param      $name
260
     * @param      $groupId
261
     * @param null $values
262
     */
263
    public function __construct($caption, $name, $groupId, $values = null)
264
    {
265
        $this->setCaption($caption);
266
        $this->setName($name);
267
        if (isset($values)) {
268
            $this->setValue($values);
269
        }
270
        $this->_groupId = $groupId;
271
    }
272
273
    /**
274
     * Sets pre-selected values
275
     *
276
     * @param mixed $value A group ID or an array of group IDs
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
     * @access public
295
     */
296
    public function setOptionTree(&$optionTree)
297
    {
298
        $this->_optionTree =& $optionTree;
299
    }
300
301
    /**
302
     * Sets appendix of checkboxes
303
     *
304
     * @access public
305
     * @param $appendix
306
     */
307
    public function setAppendix($appendix)
308
    {
309
        $this->_appendix = $appendix;
310
    }
311
312
    /**
313
     * Renders checkbox options for this group
314
     *
315
     * @return string
316
     * @access public
317
     */
318
    public function render()
319
    {
320
        $ret = '';
321
322
        if (count($this->_appendix) > 0) {
323
            $ret .= '<table class="outer"><tr>';
324
            $cols = 1;
325
            foreach ($this->_appendix as $append) {
326
                if ($cols > 4) {
327
                    $ret .= '</tr><tr>';
328
                    $cols = 1;
329
                }
330
                $checked = $append['selected'] ? 'checked="checked"' : '';
331
                $name    = 'perms[' . $append['permname'] . ']';
332
                $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...
333
                $itemid  = $append['itemid'];
334
                $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>";
335
                ++$cols;
336
            }
337
            $ret .= '</tr></table>';
338
        }
339
340
        $ret .= '<table class="outer"><tr>';
341
        $cols = 1;
342
        foreach ($this->_optionTree[0]['children'] as $topitem) {
343
            if ($cols > 4) {
344
                $ret .= '</tr><tr>';
345
                $cols = 1;
346
            }
347
            $tree   = '<td class="odd">';
348
            $prefix = '';
349
            $this->renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
350
            $ret .= $tree . '</td>';
351
            ++$cols;
352
        }
353
        $ret .= '</tr></table>';
354
355
        return $ret;
356
    }
357
358
    /**
359
     * Renders checkbox options for an item tree
360
     *
361
     * @param string $tree
362
     * @param array  $option
363
     * @param string $prefix
364
     * @param array  $parentIds
365
     * @access private
366
     */
367
    private function renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
368
    {
369
        $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...
370
        // If there are parent elements, add javascript that will
371
        // make them selecteded when this element is checked to make
372
        // sure permissions to parent items are added as well.
373
        foreach ($parentIds as $pid) {
374
            $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...
375
            $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked != true) {ele.checked = this.checked;}";
376
        }
377
        // If there are child elements, add javascript that will
378
        // make them unchecked when this element is unchecked to make
379
        // sure permissions to child items are not added when there
380
        // is no permission to this item.
381
        foreach ($option['allchild'] as $cid) {
382
            $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...
383
            $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked != true) {ele.checked = false;}";
384
        }
385
        $tree .= '" value="1"';
386
        if (isset($this->_value) && in_array($option['id'], $this->_value)) {
387
            $tree .= ' checked="checked"';
388
        }
389
        $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($option['name']) . "\" /><br />\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...
390
        if (isset($option['children'])) {
391
            foreach ($option['children'] as $child) {
392
                array_push($parentIds, $option['id']);
393
                $this->renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
394
            }
395
        }
396
    }
397
}
398