GroupPermForm::render()   B
last analyzed

Complexity

Conditions 8
Paths 30

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 60
rs 8.0035
cc 8
nc 30
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Smartfaq;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    XOOPS Project (https://xoops.org)
17
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
19
 */
20
21
/**
22
 * Module: SmartFAQ
23
 * Author: The SmartFactory <www.smartfactory.ca>
24
 */
25
26
use XoopsModules\Smartfaq;
27
28
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
29
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
30
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
31
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
32
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
33
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
34
35
/**
36
 * Renders a form for setting module specific group permissions
37
 *
38
 * @author      Kazumi Ono  <[email protected]>
39
 * @copyright   copyright (c) 2000-2003 XOOPS.org
40
 */
41
class GroupPermForm extends \XoopsForm
42
{
43
    /**
44
     * Module ID
45
     * @var int
46
     */
47
    public $_modid;
48
    /**
49
     * Tree structure of items
50
     * @var array
51
     */
52
    public $_itemTree;
53
    /**
54
     * Name of permission
55
     * @var string
56
     */
57
    public $_permName;
58
    /**
59
     * Description of permission
60
     * @var string
61
     */
62
    public $_permDesc;
63
    /**
64
     * Appendix
65
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
66
     */
67
    public $_appendix = [];
68
69
    /**
70
     * Constructor
71
     * @param string $title
72
     * @param string $modid
73
     * @param string $permname
74
     * @param string $permdesc
75
     */
76
    public function __construct($title, $modid, $permname, $permdesc)
77
    {
78
        //      $this->XoopsForm($title, 'groupperm_form', XOOPS_URL.'/modules/system/admin/groupperm.php', 'post'); GIJ
79
        parent::__construct($title, 'groupperm_form', '', 'post');
80
        $this->_modid    = (int)$modid;
81
        $this->_permName = $permname;
82
        $this->_permDesc = $permdesc;
83
        $this->addElement(new \XoopsFormHidden('modid', $this->_modid));
84
        $this->addElement(new \XoopsFormHiddenToken($permname));
85
    }
86
87
    /**
88
     * Adds an item to which permission will be assigned
89
     *
90
     * @param string $itemName
91
     * @param int    $itemId
92
     * @param int    $itemParent
93
     */
94
    public function addItem($itemId, $itemName, $itemParent = 0): void
95
    {
96
        $this->_itemTree[$itemParent]['children'][] = $itemId;
97
        $this->_itemTree[$itemId]['parent']         = $itemParent;
98
        $this->_itemTree[$itemId]['name']           = $itemName;
99
        $this->_itemTree[$itemId]['id']             = $itemId;
100
    }
101
102
    /**
103
     * Add appendix
104
     *
105
     * @param $permName
106
     * @param $itemId
107
     * @param $itemName
108
     */
109
    public function addAppendix($permName, $itemId, $itemName): void
110
    {
111
        $this->_appendix[] = [
112
            'permname' => $permName,
113
            'itemid'   => $itemId,
114
            'itemname' => $itemName,
115
            'selected' => false,
116
        ];
117
    }
118
119
    /**
120
     * Loads all child ids for an item to be used in javascript
121
     *
122
     * @param int   $itemId
123
     * @param array $childIds
124
     */
125
    public function _loadAllChildItemIds($itemId, &$childIds): void
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
     */
146
    public function render()
147
    {
148
        // load all child ids for javascript codes
149
        foreach (\array_keys($this->_itemTree) as $item_id) {
150
            $this->_itemTree[$item_id]['allchild'] = [];
151
            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
152
        }
153
        /** @var \XoopsGroupPermHandler $grouppermHandler */
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);
160
            $ele      = new GroupFormCheckBox($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);
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 .= $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...
195
            } else {
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";
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></form>';
204
205
        return $ret;
206
    }
207
}
208