Completed
Pull Request — master (#588)
by
unknown
14:32
created

GroupFormCheckbox::defaultRender()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0128

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 9
nop 0
dl 0
loc 31
ccs 23
cts 25
cp 0.92
crap 5.0128
rs 9.2248
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * GroupFormCheckbox - checkbox options for a group permission form
16
 *
17
 * @category  Xoops\Form\GroupFormCheckbox
18
 * @package   Xoops\Form
19
 * @author    Xoops Development Team
20
 * @copyright 2001-2015 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      http://xoops.org
23
 */
24
class GroupFormCheckbox extends Element
25
{
26
    /**
27
     * Group ID
28
     *
29
     * @var int
30
     */
31
    private $groupId;
32
33
    /**
34
     * Option tree
35
     *
36
     * @var array
37
     */
38
    private $optionTree = array();
39
40
    /**
41
     * __construct
42
     *
43
     * @param string|array $caption Caption or array of all attributes
44
     * @param string       $name    element name
45
     * @param integer      $groupId group id
46
     * @param mixed        $values  values
47
     */
48 2
    public function __construct($caption, $name = null, $groupId = null, $values = null)
49
    {
50 2
        if (is_array($caption)) {
51
            parent::__construct($caption);
52
        } else {
53 2
            parent::__construct([]);
54 2
            $this->setCaption($caption);
55 2
            $this->setName($name);
56 2
            $this->setWithDefaults('value', (array) $values, []);
57 2
            $this->set(':groupid', $groupId);
58
        }
59 2
        $this->groupId = $this->get(':groupid', 0);
60 2
    }
61
62
    /**
63
     * Sets the tree structure of items
64
     *
65
     * @param array $optionTree options
66
     *
67
     * @return void
68
     */
69 2
    public function setOptionTree(&$optionTree)
70
    {
71 2
        $this->optionTree = $optionTree;
72 2
    }
73
74
    /**
75
     * defaultRender
76
     *
77
     * @return string rendered form element
78
     */
79 2
    public function defaultRender()
80
    {
81 2
        $ele_name = $this->getName();
82 2
        $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
83 2
        $cols = 1;
84
85 2
        foreach ($this->optionTree[0]['children'] as $topitem) {
86 2
            if ($cols > 4) {
87
                $ret .= '</tr><tr>';
88
                $cols = 1;
89
            }
90 2
            $tree = '<td valign="top">';
91 2
            $prefix = '';
92 2
            $this->renderOptionTree($tree, $this->optionTree[$topitem], $prefix);
93 2
            $ret .= $tree . '</td>';
94 2
            ++$cols;
95
        }
96 2
        $ret .= '</tr></table></td><td class="even" valign="top">';
97 2
        $option_ids = array();
98 2
        foreach (array_keys($this->optionTree) as $id) {
99 2
            if (!empty($id)) {
100 2
                $option_ids[] = "'" . $ele_name . '[groups][' . $this->groupId . '][' . $id . ']' . "'";
101
            }
102
        }
103 2
        $checkAllButtonId = $ele_name . '[checkallbtn][' . $this->groupId . ']';
104 2
        $option_ids_str = implode(', ', $option_ids);
105 2
        $ret .= \XoopsLocale::ALL . " <input id=\"" . $checkAllButtonId . "\" type=\"checkbox\" value=\"\" "
106 2
            . "onclick=\"var optionids = new Array(" . $option_ids_str . "); "
107 2
            . "xoopsCheckAllElements(optionids, '" . $checkAllButtonId . "');\" />";
108 2
        $ret .= '</td></tr></table>';
109 2
        return $ret;
110
    }
111
112
    /**
113
     * Renders checkbox options for an item tree
114
     *
115
     * @param string $tree      rendered tree by reference
116
     * @param array  $option    option
117
     * @param string $prefix    prefix
118
     * @param array  $parentIds parent ids
119
     *
120
     * @return void append
121
     */
122 2
    private function renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
123
    {
124 2
        $elementName = $this->getName();
125 2
        $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $elementName . "[groups]["
126 2
            . $this->groupId . "][" . $option['id'] . "]\" id=\"" . $elementName
127 2
            . "[groups][" . $this->groupId . "][" . $option['id'] . "]\" onclick=\"";
128
        // If there are parent elements, add javascript that will
129
        // make them selected when this element is checked to make
130
        // sure permissions to parent items are added as well.
131 2
        foreach ($parentIds as $pid) {
132 2
            $parent_ele = $elementName . '[groups][' . $this->groupId . '][' . $pid . ']';
133 2
            $tree .= "var ele = xoopsGetElementById('" . $parent_ele
134 2
                . "'); if(ele.checked != true) {ele.checked = this.checked;}";
135
        }
136
        // If there are child elements, add javascript that will
137
        // make them unchecked when this element is unchecked to make
138
        // sure permissions to child items are not added when there
139
        // is no permission to this item.
140 2
        foreach ($option['allchild'] as $cid) {
141 2
            $child_ele = $elementName . '[groups][' . $this->groupId . '][' . $cid . ']';
142 2
            $tree .= "var ele = xoopsGetElementById('" . $child_ele
143 2
                . "'); if(this.checked != true) {ele.checked = false;}";
144
        }
145 2
        $tree .= '" value="1"';
146 2
        if (in_array($option['id'], $this->get('value', []))) {
147
            $tree .= ' checked="checked"';
148
        }
149 2
        $tree .= " />" . $option['name'] . "<input type=\"hidden\" name=\"" . $elementName . "[parents]["
150 2
            . $option['id'] . "]\" value=\"" . implode(':', $parentIds) . "\" /><input type=\"hidden\" name=\""
151 2
            . $elementName . "[itemname][" . $option['id'] . "]\" value=\""
152 2
            . htmlspecialchars($option['name']) . "\" /><br />\n";
153 2
        if (isset($option['children'])) {
154 2
            foreach ($option['children'] as $child) {
155 2
                array_push($parentIds, $option['id']);
156 2
                $this->renderOptionTree($tree, $this->optionTree[$child], $prefix . '&nbsp;-', $parentIds);
157
            }
158
        }
159 2
    }
160
}
161