1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Form;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class GroupFormCheckbox extends Element
25: {
26: 27: 28: 29: 30:
31: private $groupId;
32:
33: 34: 35: 36: 37:
38: private $optionTree = array();
39:
40: 41: 42: 43: 44: 45: 46: 47:
48: public function __construct($caption, $name = null, $groupId = null, $values = null)
49: {
50: if (is_array($caption)) {
51: parent::__construct($caption);
52: } else {
53: parent::__construct([]);
54: $this->setCaption($caption);
55: $this->setName($name);
56: $this->setWithDefaults('value', (array) $values, []);
57: $this->set(':groupid', $groupId);
58: }
59: $this->groupId = $this->get(':groupid', 0);
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function setOptionTree(&$optionTree)
70: {
71: $this->optionTree = $optionTree;
72: }
73:
74: 75: 76: 77: 78:
79: public function render()
80: {
81: $ele_name = $this->getName();
82: $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
83: $cols = 1;
84:
85: foreach ($this->optionTree[0]['children'] as $topitem) {
86: if ($cols > 4) {
87: $ret .= '</tr><tr>';
88: $cols = 1;
89: }
90: $tree = '<td valign="top">';
91: $prefix = '';
92: $this->renderOptionTree($tree, $this->optionTree[$topitem], $prefix);
93: $ret .= $tree . '</td>';
94: ++$cols;
95: }
96: $ret .= '</tr></table></td><td class="even" valign="top">';
97: $option_ids = array();
98: foreach (array_keys($this->optionTree) as $id) {
99: if (!empty($id)) {
100: $option_ids[] = "'" . $ele_name . '[groups][' . $this->groupId . '][' . $id . ']' . "'";
101: }
102: }
103: $checkAllButtonId = $ele_name . '[checkallbtn][' . $this->groupId . ']';
104: $option_ids_str = implode(', ', $option_ids);
105: $ret .= \XoopsLocale::ALL . " <input id=\"" . $checkAllButtonId . "\" type=\"checkbox\" value=\"\" "
106: . "onclick=\"var optionids = new Array(" . $option_ids_str . "); "
107: . "xoopsCheckAllElements(optionids, '" . $checkAllButtonId . "');\" />";
108: $ret .= '</td></tr></table>';
109: return $ret;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: private function renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
123: {
124: $elementName = $this->getName();
125: $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $elementName . "[groups]["
126: . $this->groupId . "][" . $option['id'] . "]\" id=\"" . $elementName
127: . "[groups][" . $this->groupId . "][" . $option['id'] . "]\" onclick=\"";
128:
129:
130:
131: foreach ($parentIds as $pid) {
132: $parent_ele = $elementName . '[groups][' . $this->groupId . '][' . $pid . ']';
133: $tree .= "var ele = xoopsGetElementById('" . $parent_ele
134: . "'); if(ele.checked != true) {ele.checked = this.checked;}";
135: }
136:
137:
138:
139:
140: foreach ($option['allchild'] as $cid) {
141: $child_ele = $elementName . '[groups][' . $this->groupId . '][' . $cid . ']';
142: $tree .= "var ele = xoopsGetElementById('" . $child_ele
143: . "'); if(this.checked != true) {ele.checked = false;}";
144: }
145: $tree .= '" value="1"';
146: if (in_array($option['id'], $this->get('value', []))) {
147: $tree .= ' checked="checked"';
148: }
149: $tree .= " />" . $option['name'] . "<input type=\"hidden\" name=\"" . $elementName . "[parents]["
150: . $option['id'] . "]\" value=\"" . implode(':', $parentIds) . "\" /><input type=\"hidden\" name=\""
151: . $elementName . "[itemname][" . $option['id'] . "]\" value=\""
152: . htmlspecialchars($option['name']) . "\" /><br />\n";
153: if (isset($option['children'])) {
154: foreach ($option['children'] as $child) {
155: array_push($parentIds, $option['id']);
156: $this->renderOptionTree($tree, $this->optionTree[$child], $prefix . ' -', $parentIds);
157: }
158: }
159: }
160: }
161: