Completed
Push — master ( 0881bb...3a6ebc )
by Michael
04:47
created

MyXoopsGroupFormCheckBox::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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