XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
grouppermform.php
1<?php
13
14
15if (!defined('XOOPS_ROOT_PATH')) {
16 exit();
17}
18require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
19
20
21class XoopsGroupPermForm extends XoopsForm
22{
28 public $_modid;
34 public $_itemTree = [];
40 public $_permName;
46 public $_permDesc;
47
56 public function __construct($title, $modid, $permname, $permdesc, $url = '')
57 {
58 $this->XoopsForm($title, 'groupperm_form', XOOPS_URL . '/modules/legacy/include/groupperm.php', 'post');
59 $this->_modid = (int)$modid;
60 $this->_permName = $permname;
61 $this->_permDesc = $permdesc;
62 $this->addElement(new XoopsFormHidden('modid', $this->_modid));
63 if ('' !== $url) {
64 $this->addElement(new XoopsFormHidden('redirect_url', $url));
65 }
66 }
67
76 public function addItem($itemId, $itemName, $itemParent = 0)
77 {
78 $this->_itemTree[$itemParent]['children'][] = $itemId;
79 $this->_itemTree[$itemId]['parent'] = $itemParent;
80 $this->_itemTree[$itemId]['name'] = $itemName;
81 $this->_itemTree[$itemId]['id'] = $itemId;
82 }
83
91 public function _loadAllChildItemIds($itemId, &$childIds)
92 {
93 if (!empty($this->_itemTree[$itemId]['children'])) {
94 $first_child = $this->_itemTree[$itemId]['children'];
95 foreach ($first_child as $fcid) {
96 $childIds[] = $fcid;
97 if (!empty($this->_itemTree[$fcid]['children'])) {
98 foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
99 $childIds[] = $_fcid;
100 $this->_loadAllChildItemIds($_fcid, $childIds);
101 }
102 }
103 }
104 }
105 }
106
113 public function render()
114 {
115 // load all child ids for javascript codes
116 foreach (array_keys($this->_itemTree)as $item_id) {
117 $this->_itemTree[$item_id]['allchild'] = [];
118 $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
119 }
120 $gperm_handler =& xoops_gethandler('groupperm');
121 $member_handler =& xoops_gethandler('member');
122 $glist =& $member_handler->getGroupList();
123 foreach (array_keys($glist) as $i) {
124 // get selected item id(s) for each group
125 $selected = $gperm_handler->getItemIds($this->_permName, $i, $this->_modid);
126 $ele = new XoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
127 $ele->setOptionTree($this->_itemTree);
128 $this->addElement($ele);
129 unset($ele);
130 }
131 $tray = new XoopsFormElementTray('');
132 $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
133 $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
134 $this->addElement($tray);
135
136 $root =& XCube_Root::getSingleton();
137 $renderSystem =& $root->getRenderSystem(XOOPSFORM_DEPENDENCE_RENDER_SYSTEM);
138
139 $renderTarget =& $renderSystem->createRenderTarget('main');
140
141 $renderTarget->setAttribute('legacy_module', 'legacy');
142 $renderTarget->setTemplateName('legacy_xoopsform_grouppermform.html');
143
144 $renderTarget->setAttribute('form', $this);
145
146 $renderSystem->render($renderTarget);
147
148 return $renderTarget->getResult();
149 }
150}
151
155class XoopsGroupFormCheckBox extends XoopsFormElement
156{
162 public $_value = [];
168 public $_groupId;
174 public $_optionTree = [];
175
183 public function __construct($caption, $name, $groupId, $values = null)
184 {
185 $this->setCaption($caption);
186 $this->setName($name);
187 if (isset($values)) {
188 $this->setValue($values);
189 }
190 $this->_groupId = $groupId;
191 }
192
199 public function setValue($value)
200 {
201 if (is_array($value)) {
202 foreach ($value as $v) {
203 $this->setValue($v);
204 }
205 } else {
206 $this->_value[] = $value;
207 }
208 }
209
216 public function setOptionTree(&$optionTree)
217 {
218 $this->_optionTree =& $optionTree;
219 }
220
227 public function render()
228 {
229 $ret = '<table class="outer"><tr><td><table><tr>';
230 $cols = 1;
231
232 if ($this->_hasChildren()) {
233 foreach ($this->_optionTree[0]['children'] as $topitem) {
234 if ($cols > 4) {
235 $ret .= '</tr><tr>';
236 $cols = 1;
237 }
238 $tree = '<td>';
239 $prefix = '';
240 $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
241 $ret .= $tree.'</td>';
242 $cols++;
243 }
244 }
245 $ret .= '</tr></table></td><td>';
246 $option_ids = [];
247 foreach (array_keys($this->_optionTree) as $id) {
248 if (!empty($id)) {
249 $option_ids[] = "'".$this->getName().'[groups]['.$this->_groupId.']['.$id.']'."'";
250 }
251 }
252 $checkallbtn_id = $this->getName().'[checkallbtn]['.$this->_groupId.']';
253 $checkallbtn_id = str_replace(['[', ']'], ['_', ''], $checkallbtn_id); // Remove injury characters for ID
254
255 $option_ids_str = implode(', ', $option_ids);
256 $option_ids_str = str_replace(['[', ']'], ['_', ''], $option_ids_str); // Remove injury characters for ID
257
258
259 $ret .= _ALL . ' <input id="' . $checkallbtn_id . '" type="checkbox" value="" onclick="var optionids = new Array(' . $option_ids_str . "); xoopsCheckAllElements(optionids, '" . $checkallbtn_id . "');\" />";
260 $ret .= '</td></tr></table>';
261 return $ret;
262 }
263
273 public function _renderOptionTree(&$tree, $option, $prefix, $parentIds = [])
274 {
275 // Remove injury characters for ID
276 $tree .= $prefix . '<input type="checkbox" name="' . $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']" id="' .
277 str_replace(['[', ']'], ['_', ''], $this->getName() . '[groups][' . $this->_groupId . '][' . $option['id'] . ']') . '" onclick="';
278
279 // If there are parent elements, add javascript that will
280 // make them selecteded when this element is checked to make
281 // sure permissions to parent items are added as well.
282 foreach ($parentIds as $pid) {
283 $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
284 $parent_ele = str_replace(['[', ']'], ['_', ''], $parent_ele); // Remove injury characters for ID
285 $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
286 }
287 // If there are child elements, add javascript that will
288 // make them unchecked when this element is unchecked to make
289 // sure permissions to child items are not added when there
290 // is no permission to this item.
291 foreach ($option['allchild'] as $cid) {
292 $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
293 $child_ele = str_replace(['[', ']'], ['_', ''], $child_ele); // Remove injury characters for ID
294 $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
295 }
296 $tree .= '" value="1"';
297 if (in_array($option['id'], $this->_value, true)) {
298 $tree .= ' checked="checked"';
299 }
300 $tree .= '>'
301 . $option['name'] . '<input type="hidden" name="'
302 . $this->getName() . '[parents]['
303 . $option['id'] . ']" value="'
304 . implode(':', $parentIds) . '" /><input type="hidden" name="'
305 . $this->getName() . '[itemname]['
306 . $option['id'] . ']" value="'
307 . htmlspecialchars($option['name']) . "\" /><br>\n";
308 if (isset($option['children'])) {
309 foreach ($option['children'] as $child) {
310 $parentIds[] = $option['id'];
311 $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
312 }
313 }
314 }
315
320 public function _hasChildren()
321 {
322 return isset($this->_optionTree[0]) && is_array($this->_optionTree[0]['children']);
323 }
324}
setCaption($caption)
getName($encode=true)
addElement(&$formElement, $required=false)
Definition form.php:133
setOptionTree(&$optionTree)
__construct($caption, $name, $groupId, $values=null)
_renderOptionTree(&$tree, $option, $prefix, $parentIds=[])
__construct($title, $modid, $permname, $permdesc, $url='')
_loadAllChildItemIds($itemId, &$childIds)
addItem($itemId, $itemName, $itemParent=0)