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

GroupFormCheckBox::setAppendix()   A

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 checkbox options for a group permission form
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 GroupFormCheckBox extends \XoopsFormElement
49
{
50
    /**
51
     * Pre-selected value(s)
52
     * @var array;
53
     */
54
    public $_value;
55
    /**
56
     * Group ID
57
     * @var int
58
     */
59
    public $_groupId;
60
    /**
61
     * Option tree
62
     * @var array
63
     */
64
    public $_optionTree;
65
    /**
66
     * Appendix
67
     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
68
     */
69
    public $_appendix = [];
70
71
    /**
72
     * Constructor
73
     * @param      $caption
74
     * @param      $name
75
     * @param      $groupId
76
     * @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...
77
     */
78
    public function __construct($caption, $name, $groupId, $values = null)
79
    {
80
        $this->setCaption($caption);
81
        $this->setName($name);
82
        if (null !== $values) {
0 ignored issues
show
introduced by
The condition null !== $values is always false.
Loading history...
83
            $this->setValue($values);
84
        }
85
        $this->_groupId = $groupId;
86
    }
87
88
    /**
89
     * Sets pre-selected values
90
     *
91
     * @param mixed $value A group ID or an array of group IDs
92
     * @access public
93
     */
94
    public function setValue($value)
95
    {
96
        if (is_array($value)) {
97
            foreach ($value as $v) {
98
                $this->setValue($v);
99
            }
100
        } else {
101
            $this->_value[] = $value;
102
        }
103
    }
104
105
    /**
106
     * Sets the tree structure of items
107
     *
108
     * @param array $optionTree
109
     * @access public
110
     */
111
    public function setOptionTree(&$optionTree)
112
    {
113
        $this->_optionTree = &$optionTree;
114
    }
115
116
    /**
117
     * Sets appendix of checkboxes
118
     *
119
     * @access public
120
     * @param $appendix
121
     */
122
    public function setAppendix($appendix)
123
    {
124
        $this->_appendix = $appendix;
125
    }
126
127
    /**
128
     * Renders checkbox options for this group
129
     *
130
     * @return string
131
     * @access public
132
     */
133
    public function render()
134
    {
135
        $ret = '';
136
137
        if (count($this->_appendix) > 0) {
138
            $ret  .= '<table class="outer"><tr>';
139
            $cols = 1;
140
            foreach ($this->_appendix as $append) {
141
                if ($cols > 4) {
142
                    $ret  .= '</tr><tr>';
143
                    $cols = 1;
144
                }
145
                $checked = $append['selected'] ? 'checked' : '';
146
                $name    = 'perms[' . $append['permname'] . ']';
147
                $itemid  = $append['itemid'];
0 ignored issues
show
Unused Code introduced by
The assignment to $itemid is dead and can be removed.
Loading history...
148
                $itemid  = $append['itemid'];
149
                $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>";
150
                ++$cols;
151
            }
152
            $ret .= '</tr></table>';
153
        }
154
155
        $ret  .= '<table class="outer"><tr>';
156
        $cols = 1;
157
        foreach ($this->_optionTree[0]['children'] as $topitem) {
158
            if ($cols > 4) {
159
                $ret  .= '</tr><tr>';
160
                $cols = 1;
161
            }
162
            $tree   = '<td class="odd">';
163
            $prefix = '';
164
            $this->renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
165
            $ret .= $tree . '</td>';
166
            ++$cols;
167
        }
168
        $ret .= '</tr></table>';
169
170
        return $ret;
171
    }
172
173
    /**
174
     * Renders checkbox options for an item tree
175
     *
176
     * @param string $tree
177
     * @param array  $option
178
     * @param string $prefix
179
     * @param array  $parentIds
180
     * @access private
181
     */
182
    private function renderOptionTree(&$tree, $option, $prefix, $parentIds = [])
183
    {
184
        $tree .= $prefix . '<input type="checkbox" name="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" id="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" onclick="';
185
        // If there are parent elements, add javascript that will
186
        // make them selecteded when this element is checked to make
187
        // sure permissions to parent items are added as well.
188
        foreach ($parentIds as $pid) {
189
            $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
190
            $tree       .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if (ele.checked !== true) {ele.checked = this.checked;}";
191
        }
192
        // If there are child elements, add javascript that will
193
        // make them unchecked when this element is unchecked to make
194
        // sure permissions to child items are not added when there
195
        // is no permission to this item.
196
        foreach ($option['allchild'] as $cid) {
197
            $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
198
            $tree      .= "var ele = xoopsGetElementById('" . $child_ele . "'); if (this.checked !== true) {ele.checked = false;}";
199
        }
200
        $tree .= '" value="1"';
201
        if (null !== $this->_value && in_array($option['id'], $this->_value)) {
202
            $tree .= ' checked';
203
        }
204
        $tree .= '>'
205
                 . $option['name']
206
                 . '<input type="hidden" name="'
207
                 . $this->getName()
208
                 . '[parents]['
209
                 . $option['id']
210
                 . ']" value="'
211
                 . implode(':', $parentIds)
212
                 . '"><input type="hidden" name="'
213
                 . $this->getName()
214
                 . '[itemname]['
215
                 . $option['id']
216
                 . ']" value="'
217
                 . htmlspecialchars($option['name'], ENT_QUOTES)
218
                 . "\"><br>\n";
219
        if (isset($option['children'])) {
220
            foreach ($option['children'] as $child) {
221
                array_push($parentIds, $option['id']);
222
                $this->renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
223
            }
224
        }
225
    }
226
}
227