XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
grouppermform.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
28 
32 class XoopsGroupPermForm extends XoopsForm
33 {
39  var $_modid;
58 
65 
69  function XoopsGroupPermForm($title, $modid, $permname, $permdesc, $url = '', $anonymous = true)
70  {
71  $this->XoopsForm($title, 'groupperm_form', XOOPS_URL . '/modules/system/admin/groupperm.php', 'post');
72  $this->_modid = intval($modid);
73  $this->_permName = $permname;
74  $this->_permDesc = $permdesc;
75  $this->addElement(new XoopsFormHidden('modid', $this->_modid));
76  $this->addElement(new XoopsFormHiddenToken($permname));
77  if ($url != "") {
78  $this->addElement(new XoopsFormHidden('redirect_url', $url));
79  }
80  $this->_showAnonymous = $anonymous;
81  }
82 
91  function addItem($itemId, $itemName, $itemParent = 0)
92  {
93  $this->_itemTree[$itemParent]['children'][] = $itemId;
94  $this->_itemTree[$itemId]['parent'] = $itemParent;
95  $this->_itemTree[$itemId]['name'] = $itemName;
96  $this->_itemTree[$itemId]['id'] = $itemId;
97  }
98 
106  function _loadAllChildItemIds($itemId, &$childIds)
107  {
108  if (! empty($this->_itemTree[$itemId]['children'])) {
109  $first_child = $this->_itemTree[$itemId]['children'];
110  foreach($first_child as $fcid) {
111  array_push($childIds, $fcid);
112  if (! empty($this->_itemTree[$fcid]['children'])) {
113  foreach($this->_itemTree[$fcid]['children'] as $_fcid) {
114  array_push($childIds, $_fcid);
115  $this->_loadAllChildItemIds($_fcid, $childIds);
116  }
117  }
118  }
119  }
120  }
121 
128  function render()
129  {
130  // load all child ids for javascript codes
131  foreach(array_keys($this->_itemTree) as $item_id) {
132  $this->_itemTree[$item_id]['allchild'] = array();
133  $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
134  }
135  $gperm_handler = &xoops_gethandler('groupperm');
136  $member_handler = &xoops_gethandler('member');
137  $glist = $member_handler->getGroupList();
138  foreach(array_keys($glist) as $i) {
139  if ($i == XOOPS_GROUP_ANONYMOUS && ! $this->_showAnonymous)
140  continue;
141  // get selected item id(s) for each group
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  $ret = '<h4>' . $this->getTitle() . '</h4>';
154  if ($this->_permDesc) {
155  $ret .= $this->_permDesc . '<br /><br />';
156  }
157  $ret .= '<form title="' . str_replace('"', '', $this->getTitle()) . '" name="' . $this->getName() . '" id="' . $this->getName() . '" action="' . $this->getAction() . '" method="' . $this->getMethod() . '"' . $this->getExtra() . '>' . '<table width="100%" class="outer" cellspacing="1" valign="top">' ;
158  $elements = $this->getElements();
159  $hidden = '';
160  foreach(array_keys($elements) as $i) {
161  if (! is_object($elements[$i])) {
162  $ret .= $elements[$i];
163  } elseif (! $elements[$i]->isHidden()) {
164  $ret .= '<tr valign="top" align="left"><td class="head">' . $elements[$i]->getCaption();
165  if ($elements[$i]->getDescription() != "") {
166  $ret .= "<br /><br /><span style='font-weight: normal;'>" . $elements[$i]->getDescription() . "</span>";
167  }
168  $ret .= '</td>' . '<td class="even">' . $elements[$i]->render() . '</td></tr>' . '';
169  } else {
170  $hidden .= $elements[$i]->render();
171  }
172  }
173  $ret .= '</table>' . $hidden . '</form>';
174  $ret .= $this->renderValidationJS(true);
175  return $ret;
176  }
177 }
178 
183 {
189  var $_value = array();
202 
206  function XoopsGroupFormCheckBox($caption, $name, $groupId, $values = null)
207  {
208  $this->setCaption($caption);
209  $this->setName($name);
210  if (isset($values)) {
211  $this->setValue($values);
212  }
213  $this->_groupId = $groupId;
214  }
215 
222  function setValue($value)
223  {
224  if (is_array($value)) {
225  foreach($value as $v) {
226  $this->setValue($v);
227  }
228  } else {
229  $this->_value[] = $value;
230  }
231  }
232 
239  function setOptionTree(&$optionTree)
240  {
241  $this->_optionTree = &$optionTree;
242  }
243 
250  function render()
251  {
252  $ele_name = $this->getName();
253  $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
254  $cols = 1;
255  foreach($this->_optionTree[0]['children'] as $topitem) {
256  if ($cols > 4) {
257  $ret .= '</tr><tr>';
258  $cols = 1;
259  }
260  $tree = '<td valign="top">';
261  $prefix = '';
262  $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
263  $ret .= $tree . '</td>';
264  $cols ++;
265  }
266  $ret .= '</tr></table></td><td class="even" valign="top">';
267  $option_ids = array();
268  foreach(array_keys($this->_optionTree) as $id) {
269  if (! empty($id)) {
270  $option_ids[] = "'" . $ele_name . '[groups][' . $this->_groupId . '][' . $id . ']' . "'";
271  }
272  }
273  $checkallbtn_id = $ele_name . '[checkallbtn][' . $this->_groupId . ']';
274  $option_ids_str = implode(', ', $option_ids);
275  $ret .= _ALL . " <input id=\"" . $checkallbtn_id . "\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(" . $option_ids_str . "); xoopsCheckAllElements(optionids, '" . $checkallbtn_id . "');\" />";
276  $ret .= '</td></tr></table>';
277  return $ret;
278  }
279 
289  function _renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
290  {
291  $ele_name = $this->getName();
292  $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" id=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" onclick=\"";
293  // If there are parent elements, add javascript that will
294  // make them selecteded when this element is checked to make
295  // sure permissions to parent items are added as well.
296  foreach($parentIds as $pid) {
297  $parent_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $pid . ']';
298  $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
299  }
300  // If there are child elements, add javascript that will
301  // make them unchecked when this element is unchecked to make
302  // sure permissions to child items are not added when there
303  // is no permission to this item.
304  foreach($option['allchild'] as $cid) {
305  $child_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $cid . ']';
306  $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
307  }
308  $tree .= '" value="1"';
309  if (in_array($option['id'], $this->_value)) {
310  $tree .= ' checked="checked"';
311  }
312  $tree .= " />" . $option['name'] . "<input type=\"hidden\" name=\"" . $ele_name . "[parents][" . $option['id'] . "]\" value=\"" . implode(':', $parentIds) . "\" /><input type=\"hidden\" name=\"" . $ele_name . "[itemname][" . $option['id'] . "]\" value=\"" . htmlspecialchars($option['name']) . "\" /><br />\n";
313  if (isset($option['children'])) {
314  foreach($option['children'] as $child) {
315  array_push($parentIds, $option['id']);
316  $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . '&nbsp;-', $parentIds);
317  }
318  }
319  }
320 }
321 
322 ?>