Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

GroupPermForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 4
1
<?php
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 or later (http://www.gnu.org/licenses/gpl-2.0.html)
18
 * @package
19
 * @since
20
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
21
 */
22
23
/**
24
 * Module: SmartFAQ
25
 * Author: The SmartFactory <www.smartfactory.ca>
26
 */
27
28
use XoopsModules\Smartfaq;
29
30
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
31
32
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
33
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
34
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
35
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
36
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
37
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
38
39
/**
40
 * Renders a form for setting module specific group permissions
41
 *
42
 * @author      Kazumi Ono  <[email protected]>
43
 * @copyright   copyright (c) 2000-2003 XOOPS.org
44
 *
45
 * @package     kernel
46
 * @subpackage  form
47
 */
48
class GroupPermForm extends \XoopsForm
49
{
50
    /**
51
     * Module ID
52
     * @var int
53
     */
54
    public $_modid;
55
    /**
56
     * Tree structure of items
57
     * @var array
58
     */
59
    public $_itemTree;
60
    /**
61
     * Name of permission
62
     * @var string
63
     */
64
    public $_permName;
65
    /**
66
     * Description of permission
67
     * @var string
68
     */
69
    public $_permDesc;
70
    /**
71
     * Appendix
72
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
73
     */
74
    public $_appendix = [];
75
76
    /**
77
     * Constructor
78
     * @param string $title
79
     * @param string $modid
80
     * @param string $permname
81
     * @param string $permdesc
82
     */
83
    public function __construct($title, $modid, $permname, $permdesc)
84
    {
85
        //      $this->XoopsForm($title, 'groupperm_form', XOOPS_URL.'/modules/system/admin/groupperm.php', 'post'); GIJ
86
        parent::__construct($title, 'groupperm_form', '', 'post');
87
        $this->_modid    = (int)$modid;
88
        $this->_permName = $permname;
89
        $this->_permDesc = $permdesc;
90
        $this->addElement(new \XoopsFormHidden('modid', $this->_modid));
91
        $this->addElement(new \XoopsFormHiddenToken($permname));
92
    }
93
94
    /**
95
     * Adds an item to which permission will be assigned
96
     *
97
     * @param string $itemName
98
     * @param int    $itemId
99
     * @param int    $itemParent
100
     * @access public
101
     */
102
    public function addItem($itemId, $itemName, $itemParent = 0)
103
    {
104
        $this->_itemTree[$itemParent]['children'][] = $itemId;
105
        $this->_itemTree[$itemId]['parent']         = $itemParent;
106
        $this->_itemTree[$itemId]['name']           = $itemName;
107
        $this->_itemTree[$itemId]['id']             = $itemId;
108
    }
109
110
    /**
111
     * Add appendix
112
     *
113
     * @access public
114
     * @param $permName
115
     * @param $itemId
116
     * @param $itemName
117
     */
118
    public function addAppendix($permName, $itemId, $itemName)
119
    {
120
        $this->_appendix[] = [
121
            'permname' => $permName,
122
            'itemid'   => $itemId,
123
            'itemname' => $itemName,
124
            'selected' => false,
125
        ];
126
    }
127
128
    /**
129
     * Loads all child ids for an item to be used in javascript
130
     *
131
     * @param int   $itemId
132
     * @param array $childIds
133
     * @access private
134
     */
135
    public function _loadAllChildItemIds($itemId, &$childIds)
136
    {
137
        if (!empty($this->_itemTree[$itemId]['children'])) {
138
            $first_child = $this->_itemTree[$itemId]['children'];
139
            foreach ($first_child as $fcid) {
140
                array_push($childIds, $fcid);
141
                if (!empty($this->_itemTree[$fcid]['children'])) {
142
                    foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
143
                        array_push($childIds, $_fcid);
144
                        $this->_loadAllChildItemIds($_fcid, $childIds);
145
                    }
146
                }
147
            }
148
        }
149
    }
150
151
    /**
152
     * Renders the form
153
     *
154
     * @return string
155
     * @access public
156
     */
157
    public function render()
158
    {
159
        // load all child ids for javascript codes
160
        foreach (array_keys($this->_itemTree) as $item_id) {
161
            $this->_itemTree[$item_id]['allchild'] = [];
162
            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
163
        }
164
        $grouppermHandler = xoops_getHandler('groupperm');
165
        $memberHandler    = xoops_getHandler('member');
166
        $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

166
        /** @scrutinizer ignore-call */ 
167
        $glist            = $memberHandler->getGroupList();
Loading history...
167
        foreach (array_keys($glist) as $i) {
168
            // get selected item id(s) for each group
169
            $selected = $grouppermHandler->getItemIds($this->_permName, $i, $this->_modid);
0 ignored issues
show
Bug introduced by
The method getItemIds() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
            /** @scrutinizer ignore-call */ 
170
            $selected = $grouppermHandler->getItemIds($this->_permName, $i, $this->_modid);
Loading history...
170
            $ele      = new GroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
171
            $ele->setOptionTree($this->_itemTree);
172
173
            foreach ($this->_appendix as $key => $append) {
174
                $this->_appendix[$key]['selected'] = $grouppermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
0 ignored issues
show
Bug introduced by
The method checkRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
                /** @scrutinizer ignore-call */ 
175
                $this->_appendix[$key]['selected'] = $grouppermHandler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid);
Loading history...
175
            }
176
            $ele->setAppendix($this->_appendix);
177
            $this->addElement($ele);
178
            unset($ele);
179
        }
180
181
        // GIJ start
182
        $jstray          = new \XoopsFormElementTray(' &nbsp; ');
183
        $jsuncheckbutton = new \XoopsFormButton('', 'none', _NONE, 'button');
184
        $jsuncheckbutton->setExtra("onclick=\"with(document.groupperm_form){for (i=0;i<length;i++) {if (elements[i].type=='checkbox') {elements[i].checked=false;}}}\"");
185
        $jscheckbutton = new \XoopsFormButton('', 'all', _ALL, 'button');
186
        $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;}}}\"");
187
        $jstray->addElement($jsuncheckbutton);
188
        $jstray->addElement($jscheckbutton);
189
        $this->addElement($jstray);
190
        // GIJ end
191
192
        $tray = new \XoopsFormElementTray('');
193
        $tray->addElement(new \XoopsFormButton('', 'reset', _CANCEL, 'reset'));
194
        $tray->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
195
        $this->addElement($tray);
196
197
        $ret      = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br>';
198
        $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";
199
        $elements = &$this->getElements();
200
        foreach (array_keys($elements) as $i) {
201
            if (!is_object($elements[$i])) {
202
                $ret .= $elements[$i];
203
            } elseif (!$elements[$i]->isHidden()) {
204
                $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
205
                if ('' != $elements[$i]->getDescription()) {
206
                    $ret .= '<br><br><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
207
                }
208
                $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...
209
            } else {
210
                $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...
211
            }
212
        }
213
        $ret .= '</table></form>';
214
215
        return $ret;
216
    }
217
}
218