GroupFormCheckBox   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 63
c 1
b 0
f 0
dl 0
loc 159
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 8 3
B render() 0 38 7
A setOptionTree() 0 3 1
A setAppendix() 0 3 1
A __construct() 0 8 2
B renderOptionTree() 0 30 7
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 XoopsFormElement;
27
use XoopsModules\Smartfaq;
28
29
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
30
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
31
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhiddentoken.php';
32
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
33
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
34
require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
35
36
/**
37
 * Renders checkbox options for a group permission form
38
 *
39
 * @author      Kazumi Ono  <[email protected]>
40
 * @copyright   copyright (c) 2000-2003 XOOPS.org
41
 */
42
class GroupFormCheckBox extends XoopsFormElement
43
{
44
    /**
45
     * Pre-selected value(s)
46
     * @var array;
47
     */
48
    public $_value;
49
    /**
50
     * Group ID
51
     * @var int
52
     */
53
    public $_groupId;
54
    /**
55
     * Option tree
56
     * @var array
57
     */
58
    public $_optionTree;
59
    /**
60
     * Appendix
61
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
62
     */
63
    public $_appendix = [];
64
65
    /**
66
     * Constructor
67
     * @param      $caption
68
     * @param      $name
69
     * @param      $groupId
70
     * @param null $values
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $values is correct as it would always require null to be passed?
Loading history...
71
     */
72
    public function __construct($caption, $name, $groupId, $values = null)
73
    {
74
        $this->setCaption($caption);
75
        $this->setName($name);
76
        if (null !== $values) {
0 ignored issues
show
introduced by
The condition null !== $values is always false.
Loading history...
77
            $this->setValue($values);
78
        }
79
        $this->_groupId = $groupId;
80
    }
81
82
    /**
83
     * Sets pre-selected values
84
     *
85
     * @param mixed $value A group ID or an array of group IDs
86
     */
87
    public function setValue($value): void
88
    {
89
        if (\is_array($value)) {
90
            foreach ($value as $v) {
91
                $this->setValue($v);
92
            }
93
        } else {
94
            $this->_value[] = $value;
95
        }
96
    }
97
98
    /**
99
     * Sets the tree structure of items
100
     *
101
     * @param array $optionTree
102
     */
103
    public function setOptionTree(&$optionTree): void
104
    {
105
        $this->_optionTree = &$optionTree;
106
    }
107
108
    /**
109
     * Sets appendix of checkboxes
110
     *
111
     * @param $appendix
112
     */
113
    public function setAppendix($appendix): void
114
    {
115
        $this->_appendix = $appendix;
116
    }
117
118
    /**
119
     * Renders checkbox options for this group
120
     *
121
     * @return string
122
     */
123
    public function render()
124
    {
125
        $ret = '';
126
127
        if (\count($this->_appendix) > 0) {
128
            $ret  .= '<table class="outer"><tr>';
129
            $cols = 1;
130
            foreach ($this->_appendix as $append) {
131
                if ($cols > 4) {
132
                    $ret  .= '</tr><tr>';
133
                    $cols = 1;
134
                }
135
                $checked = $append['selected'] ? 'checked' : '';
136
                $name    = 'perms[' . $append['permname'] . ']';
137
                $itemid  = $append['itemid'];
0 ignored issues
show
Unused Code introduced by
The assignment to $itemid is dead and can be removed.
Loading history...
138
                $itemid  = $append['itemid'];
139
                $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>";
140
                ++$cols;
141
            }
142
            $ret .= '</tr></table>';
143
        }
144
145
        $ret  .= '<table class="outer"><tr>';
146
        $cols = 1;
147
        foreach ($this->_optionTree[0]['children'] as $topitem) {
148
            if ($cols > 4) {
149
                $ret  .= '</tr><tr>';
150
                $cols = 1;
151
            }
152
            $tree   = '<td class="odd">';
153
            $prefix = '';
154
            $this->renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
155
            $ret .= $tree . '</td>';
156
            ++$cols;
157
        }
158
        $ret .= '</tr></table>';
159
160
        return $ret;
161
    }
162
163
    /**
164
     * Renders checkbox options for an item tree
165
     *
166
     * @param string $tree
167
     * @param array  $option
168
     * @param string $prefix
169
     * @param array  $parentIds
170
     */
171
    private function renderOptionTree(&$tree, $option, $prefix, $parentIds = []): void
172
    {
173
        $tree .= $prefix . '<input type="checkbox" name="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" id="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" onclick="';
174
        // If there are parent elements, add javascript that will
175
        // make them selecteded when this element is checked to make
176
        // sure permissions to parent items are added as well.
177
        foreach ($parentIds as $pid) {
178
            $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
179
            $tree       .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked !== true) {ele.checked = this.checked;}";
180
        }
181
        // If there are child elements, add javascript that will
182
        // make them unchecked when this element is unchecked to make
183
        // sure permissions to child items are not added when there
184
        // is no permission to this item.
185
        foreach ($option['allchild'] as $cid) {
186
            $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
187
            $tree      .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked !== true) {ele.checked = false;}";
188
        }
189
        $tree .= '" value="1"';
190
        if (null !== $this->_value && \in_array($option['id'], $this->_value, true)) {
191
            $tree .= ' checked';
192
        }
193
        $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(
194
                $option['name'],
195
                \ENT_QUOTES
196
            ) . "\"><br>\n";
197
        if (isset($option['children'])) {
198
            foreach ($option['children'] as $child) {
199
                $parentIds[] = $option['id'];
200
                $this->renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
201
            }
202
        }
203
    }
204
}
205