1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Form;
13:
14: use Xoops\Core\FixedGroups;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class GroupPermissionForm extends Form
27: {
28: 29: 30: 31: 32:
33: private $modid;
34:
35: 36: 37: 38: 39:
40: private $itemTree = array();
41:
42: 43: 44: 45: 46:
47: private $permName;
48:
49: 50: 51: 52: 53:
54: private $permDesc;
55:
56: 57: 58: 59: 60:
61: private $showAnonymous;
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72:
73: public function __construct($title, $modid, $permname, $permdesc, $url = '', $anonymous = true)
74: {
75: parent::__construct($title, 'groupperm_form', XOOPS_URL . '/modules/system/admin/groupperm.php', 'post');
76: $this->modid = (int)($modid);
77: $this->permName = $permname;
78: $this->permDesc = $permdesc;
79: $this->addElement(new Hidden('modid', $this->modid));
80: $this->addElement(new Token($permname));
81: if ($url != "") {
82: $this->addElement(new Hidden('redirect_url', $url));
83: }
84: $this->showAnonymous = $anonymous;
85: }
86:
87: 88: 89: 90: 91: 92: 93: 94: 95:
96: public function addItem($itemId, $itemName, $itemParent = 0)
97: {
98: $this->itemTree[$itemParent]['children'][] = $itemId;
99: $this->itemTree[$itemId]['parent'] = $itemParent;
100: $this->itemTree[$itemId]['name'] = $itemName;
101: $this->itemTree[$itemId]['id'] = $itemId;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: private function loadAllChildItemIds($itemId, &$childIds)
113: {
114: if (!empty($this->itemTree[$itemId]['children'])) {
115: $children = $this->itemTree[$itemId]['children'];
116: foreach ($children as $fcid) {
117: array_push($childIds, $fcid);
118: $this->loadAllChildItemIds($fcid, $childIds);
119: }
120: }
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function render()
130: {
131: $xoops = \Xoops::getInstance();
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->getHandlerGroupPermission();
138: $member_handler = $xoops->getHandlerMember();
139: $glist = $member_handler->getGroupList();
140: foreach (array_keys($glist) as $i) {
141: if ($i == FixedGroups::ANONYMOUS && !$this->showAnonymous) {
142: continue;
143: }
144:
145: $selected = $gperm_handler->getItemIds($this->permName, $i, $this->modid);
146: $ele = new GroupFormCheckbox($glist[$i], 'perms[' . $this->permName . ']', $i, $selected);
147: $ele->setOptionTree($this->itemTree);
148: $this->addElement($ele);
149: unset($ele);
150: }
151: $tray = new ElementTray('');
152: $tray->addElement(new Button('', 'submit', \XoopsLocale::A_SUBMIT, 'submit'));
153: $tray->addElement(new Button('', 'reset', \XoopsLocale::A_CANCEL, 'reset'));
154: $this->addElement($tray);
155:
156: $ret = '<h4>' . $this->getTitle() . '</h4>';
157: if ($this->permDesc) {
158: $ret .= $this->permDesc . '<br /><br />';
159: }
160: $ret .= '<form title="' . str_replace('"', '', $this->getTitle()) . '" name="'
161: . $this->getName() . '" id="' . $this->getName() . '" action="' . $this->getAction()
162: . '" method="' . $this->getMethod() . '"' . $this->getExtra() . '>'
163: . '<table width="100%" class="outer" cellspacing="1" valign="top">';
164: $elements = $this->getElements();
165: $hidden = '';
166: foreach (array_keys($elements) as $i) {
167: if ($elements[$i] instanceof Raw) {
168: $ret .= $elements[$i]->render();
169: } elseif (!$elements[$i]->isHidden()) {
170: $ret .= '<tr valign="top" align="left"><td class="head">' . $elements[$i]->getCaption();
171: if ($elements[$i]->getDescription() != "") {
172: $ret .= "<br /><br /><span style='font-weight: normal;'>"
173: . $elements[$i]->getDescription() . "</span>";
174: }
175: $ret .= '</td>' . '<td class="even">' . $elements[$i]->render() . '</td></tr>' . '';
176: } else {
177: $hidden .= $elements[$i]->render();
178: }
179: }
180: $ret .= '</table>' . $hidden . '</form>';
181: $ret .= $this->renderValidationJS(true);
182: return $ret;
183: }
184: }
185: