GroupFormCheckBox::setAppendix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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