1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if (!defined('XOOPS_ROOT_PATH')) exit();
33: require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: class XoopsGroupPermForm extends XoopsForm
44: {
45: 46: 47: 48: 49:
50: var $_modid;
51: 52: 53: 54: 55:
56: var $_itemTree = array();
57: 58: 59: 60: 61:
62: var $_permName;
63: 64: 65: 66: 67:
68: var $_permDesc;
69:
70: 71: 72:
73: function XoopsGroupPermForm($title, $modid, $permname, $permdesc, $url = "")
74: {
75: $this->XoopsForm($title, 'groupperm_form', XOOPS_URL . '/modules/legacy/include/groupperm.php', 'post');
76: $this->_modid = intval($modid);
77: $this->_permName = $permname;
78: $this->_permDesc = $permdesc;
79: $this->addElement(new XoopsFormHidden('modid', $this->_modid));
80: if ($url != "") {
81: $this->addElement(new XoopsFormHidden('redirect_url', $url));
82: }
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: function addItem($itemId, $itemName, $itemParent = 0)
94: {
95: $this->_itemTree[$itemParent]['children'][] = $itemId;
96: $this->_itemTree[$itemId]['parent'] = $itemParent;
97: $this->_itemTree[$itemId]['name'] = $itemName;
98: $this->_itemTree[$itemId]['id'] = $itemId;
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: function _loadAllChildItemIds($itemId, &$childIds)
109: {
110: if (!empty($this->_itemTree[$itemId]['children'])) {
111: $first_child = $this->_itemTree[$itemId]['children'];
112: foreach ($first_child as $fcid) {
113: array_push($childIds, $fcid);
114: if (!empty($this->_itemTree[$fcid]['children'])) {
115: foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
116: array_push($childIds, $_fcid);
117: $this->_loadAllChildItemIds($_fcid, $childIds);
118: }
119: }
120: }
121: }
122: }
123:
124: 125: 126: 127: 128: 129:
130: function render()
131: {
132:
133: foreach (array_keys($this->_itemTree)as $item_id) {
134: $this->_itemTree[$item_id]['allchild'] = array();
135: $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
136: }
137: $gperm_handler =& xoops_gethandler('groupperm');
138: $member_handler =& xoops_gethandler('member');
139: $glist =& $member_handler->getGroupList();
140: foreach (array_keys($glist) as $i) {
141:
142: $selected = $gperm_handler->getItemIds($this->_permName, $i, $this->_modid);
143: $ele = new XoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
144: $ele->setOptionTree($this->_itemTree);
145: $this->addElement($ele);
146: unset($ele);
147: }
148: $tray = new XoopsFormElementTray('');
149: $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
150: $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
151: $this->addElement($tray);
152:
153: $root =& XCube_Root::getSingleton();
154: $renderSystem =& $root->getRenderSystem(XOOPSFORM_DEPENDENCE_RENDER_SYSTEM);
155:
156: $renderTarget =& $renderSystem->createRenderTarget('main');
157:
158: $renderTarget->setAttribute('legacy_module', 'legacy');
159: $renderTarget->setTemplateName("legacy_xoopsform_grouppermform.html");
160:
161: $renderTarget->setAttribute("form", $this);
162:
163: $renderSystem->render($renderTarget);
164:
165: return $renderTarget->getResult();
166: }
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: class XoopsGroupFormCheckBox extends XoopsFormElement
178: {
179: 180: 181: 182: 183:
184: var $_value = array();
185: 186: 187: 188: 189:
190: var $_groupId;
191: 192: 193: 194: 195:
196: var $_optionTree = array();
197:
198: 199: 200:
201: function XoopsGroupFormCheckBox($caption, $name, $groupId, $values = null)
202: {
203: $this->setCaption($caption);
204: $this->setName($name);
205: if (isset($values)) {
206: $this->setValue($values);
207: }
208: $this->_groupId = $groupId;
209: }
210:
211: 212: 213: 214: 215: 216:
217: function setValue($value)
218: {
219: if (is_array($value)) {
220: foreach ($value as $v) {
221: $this->setValue($v);
222: }
223: } else {
224: $this->_value[] = $value;
225: }
226: }
227:
228: 229: 230: 231: 232: 233:
234: function setOptionTree(&$optionTree)
235: {
236: $this->_optionTree =& $optionTree;
237: }
238:
239: 240: 241: 242: 243: 244:
245: function render()
246: {
247: $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
248: $cols = 1;
249:
250: if ($this->_hasChildren())
251: foreach ($this->_optionTree[0]['children'] as $topitem) {
252: if ($cols > 4) {
253: $ret .= '</tr><tr>';
254: $cols = 1;
255: }
256: $tree = '<td>';
257: $prefix = '';
258: $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
259: $ret .= $tree.'</td>';
260: $cols++;
261: }
262: $ret .= '</tr></table></td><td class="even">';
263: $option_ids = array();
264: foreach (array_keys($this->_optionTree) as $id) {
265: if (!empty($id)) {
266: $option_ids[] = "'".$this->getName().'[groups]['.$this->_groupId.']['.$id.']'."'";
267: }
268: }
269: $checkallbtn_id = $this->getName().'[checkallbtn]['.$this->_groupId.']';
270: $checkallbtn_id = str_replace(array('[', ']'), array('_', ''), $checkallbtn_id);
271:
272: $option_ids_str = implode(', ', $option_ids);
273: $option_ids_str = str_replace(array('[', ']'), array('_', ''), $option_ids_str);
274:
275:
276: $ret .= _ALL." <input id=\"".$checkallbtn_id."\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(".$option_ids_str."); xoopsCheckAllElements(optionids, '".$checkallbtn_id."');\" />";
277: $ret .= '</td></tr></table>';
278: return $ret;
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288: 289:
290: function _renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
291: {
292:
293: $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $this->getName() .
294: "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" id=\"" .
295: str_replace(array('[', ']'), array('_', ''), $this->getName() . "[groups][" . $this->_groupId . "][" . $option['id'] . "]") .
296: "\" onclick=\"";
297:
298:
299:
300:
301: foreach ($parentIds as $pid) {
302: $parent_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $pid . ']';
303: $parent_ele = str_replace(array('[', ']'), array('_', ''), $parent_ele);
304: $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
305: }
306:
307:
308:
309:
310: foreach ($option['allchild'] as $cid) {
311: $child_ele = $this->getName() . '[groups][' . $this->_groupId . '][' . $cid . ']';
312: $child_ele = str_replace(array('[', ']'), array('_', ''), $child_ele);
313: $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
314: }
315: $tree .= '" value="1"';
316: if (in_array($option['id'], $this->_value)) {
317: $tree .= ' checked="checked"';
318: }
319: $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($option['name']). "\" /><br />\n";
320: if (isset($option['children'])) {
321: foreach ($option['children'] as $child) {
322: array_push($parentIds, $option['id']);
323: $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . ' -', $parentIds);
324: }
325: }
326: }
327:
328: 329: 330: 331:
332: function _hasChildren()
333: {
334: return isset($this->_optionTree[0]) && is_array($this->_optionTree[0]['children']);
335: }
336: }
337: ?>