00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 if (!defined('XOOPS_ROOT_PATH')) {
00032 die("XOOPS root path not defined");
00033 }
00034 require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
00035 require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
00036 require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
00037 require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
00038 require_once XOOPS_ROOT_PATH . '/class/xoopsform/form.php';
00039
00048 class XoopsGroupPermForm extends XoopsForm
00049 {
00055 var $_modid;
00061 var $_itemTree;
00067 var $_permName;
00073 var $_permDesc;
00074
00078 function XoopsGroupPermForm($title, $modid, $permname, $permdesc, $url = "")
00079 {
00080 $this->XoopsForm($title, 'groupperm_form', XOOPS_URL . '/modules/system/admin/groupperm.php', 'post');
00081 $this->_modid = intval($modid);
00082 $this->_permName = $permname;
00083 $this->_permDesc = $permdesc;
00084 $this->addElement(new XoopsFormHidden('modid', $this->_modid));
00085 if ($url != "") {
00086 $this->addElement(new XoopsFormHidden('redirect_url', $url));
00087 }
00088 }
00089
00098 function addItem($itemId, $itemName, $itemParent = 0)
00099 {
00100 $this->_itemTree[$itemParent]['children'][] = $itemId;
00101 $this->_itemTree[$itemId]['parent'] = $itemParent;
00102 $this->_itemTree[$itemId]['name'] = $itemName;
00103 $this->_itemTree[$itemId]['id'] = $itemId;
00104 }
00105
00113 function _loadAllChildItemIds($itemId, &$childIds)
00114 {
00115 if (!empty($this->_itemTree[$itemId]['children'])) {
00116 $first_child = $this->_itemTree[$itemId]['children'];
00117 foreach ($first_child as $fcid) {
00118 array_push($childIds, $fcid);
00119 if (!empty($this->_itemTree[$fcid]['children'])) {
00120 foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
00121 array_push($childIds, $_fcid);
00122 $this->_loadAllChildItemIds($_fcid, $childIds);
00123 }
00124 }
00125 }
00126 }
00127 }
00128
00135 function render()
00136 {
00137
00138 foreach (array_keys($this->_itemTree)as $item_id) {
00139 $this->_itemTree[$item_id]['allchild'] = array();
00140 $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
00141 }
00142 $gperm_handler =& xoops_gethandler('groupperm');
00143 $member_handler =& xoops_gethandler('member');
00144 $glist =& $member_handler->getGroupList();
00145 foreach (array_keys($glist) as $i) {
00146
00147 $selected = $gperm_handler->getItemIds($this->_permName, $i, $this->_modid);
00148 $ele = new XoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
00149 $ele->setOptionTree($this->_itemTree);
00150 $this->addElement($ele);
00151 unset($ele);
00152 }
00153 $tray = new XoopsFormElementTray('');
00154 $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
00155 $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
00156 $this->addElement($tray);
00157 $ret = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br />';
00158 $ret .= "<form name='" . $this->getName() . "' id='" . $this->getName() . "' action='" . $this->getAction() . "' method='" . $this->getMethod() . "'" . $this->getExtra() . ">\n<table width='100%' class='outer' cellspacing='1' valign='top'>\n";
00159 $elements = $this->getElements();
00160 $hidden = '';
00161 foreach (array_keys($elements) as $i) {
00162 if (!is_object($elements[$i])) {
00163 $ret .= $elements[$i];
00164 } elseif (!$elements[$i]->isHidden()) {
00165 $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
00166 if ($elements[$i]->getDescription() != '') {
00167 $ret .= '<br /><br /><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
00168 }
00169 $ret .= "</td>\n<td class='even'>\n" . $elements[$i]->render() . "\n</td></tr>\n";
00170 } else {
00171 $hidden .= $elements[$i]->render();
00172 }
00173 }
00174 $ret .= "</table>$hidden</form>";
00175 $ret .= $this->renderValidationJS( true );
00176 return $ret;
00177 }
00178 }
00179
00188 class XoopsGroupFormCheckBox extends XoopsFormElement
00189 {
00195 var $_value = array();
00201 var $_groupId;
00207 var $_optionTree;
00208
00212 function XoopsGroupFormCheckBox($caption, $name, $groupId, $values = null)
00213 {
00214 $this->setCaption($caption);
00215 $this->setName($name);
00216 if (isset($values)) {
00217 $this->setValue($values);
00218 }
00219 $this->_groupId = $groupId;
00220 }
00221
00228 function setValue($value)
00229 {
00230 if (is_array($value)) {
00231 foreach ($value as $v) {
00232 $this->setValue($v);
00233 }
00234 } else {
00235 $this->_value[] = $value;
00236 }
00237 }
00238
00245 function setOptionTree(&$optionTree)
00246 {
00247 $this->_optionTree =& $optionTree;
00248 }
00249
00256 function render()
00257 {
00258 $ele_name = $this->getName();
00259 $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
00260 $cols = 1;
00261 foreach ($this->_optionTree[0]['children'] as $topitem) {
00262 if ($cols > 4) {
00263 $ret .= '</tr><tr>';
00264 $cols = 1;
00265 }
00266 $tree = '<td valign="top">';
00267 $prefix = '';
00268 $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
00269 $ret .= $tree.'</td>';
00270 $cols++;
00271 }
00272 $ret .= '</tr></table></td><td class="even" valign="top">';
00273 foreach (array_keys($this->_optionTree) as $id) {
00274 if (!empty($id)) {
00275 $option_ids[] = "'".$ele_name.'[groups]['.$this->_groupId.']['.$id.']'."'";
00276 }
00277 }
00278 $checkallbtn_id = $ele_name.'[checkallbtn]['.$this->_groupId.']';
00279 $option_ids_str = implode(', ', $option_ids);
00280 $ret .= _ALL." <input id=\"".$checkallbtn_id."\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(".$option_ids_str."); xoopsCheckAllElements(optionids, '".$checkallbtn_id."');\" />";
00281 $ret .= '</td></tr></table>';
00282 return $ret;
00283 }
00284
00294 function _renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
00295 {
00296 $ele_name = $this->getName();
00297 $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" id=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" onclick=\"";
00298
00299
00300
00301 foreach ($parentIds as $pid) {
00302 $parent_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $pid . ']';
00303 $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
00304 }
00305
00306
00307
00308
00309 foreach ($option['allchild'] as $cid) {
00310 $child_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $cid . ']';
00311 $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
00312 }
00313 $tree .= '" value="1"';
00314 if (in_array($option['id'], $this->_value)) {
00315 $tree .= ' checked="checked"';
00316 }
00317 $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";
00318 if (isset($option['children'])) {
00319 foreach ($option['children'] as $child) {
00320 array_push($parentIds, $option['id']);
00321 $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . ' -', $parentIds);
00322 }
00323 }
00324 }
00325 }
00326 ?>