source: temp/test-xoops.ec-cube.net/html/modules/tinyd0/admin/mygrouppermform.php @ 405

Revision 405, 12.6 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: grouppermform.php,v 1.4 2003/09/29 18:25:27 okazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000-2003 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
33
34require_once XOOPS_ROOT_PATH.'/class/xoopsform/formelement.php';
35require_once XOOPS_ROOT_PATH.'/class/xoopsform/formhidden.php';
36require_once XOOPS_ROOT_PATH.'/class/xoopsform/formbutton.php';
37require_once XOOPS_ROOT_PATH.'/class/xoopsform/formelementtray.php';
38require_once XOOPS_ROOT_PATH.'/class/xoopsform/form.php';
39
40/**
41 * Renders a form for setting module specific group permissions
42 *
43 * @author  Kazumi Ono  <[email protected]>
44 * @copyright   copyright (c) 2000-2003 XOOPS.org
45 *
46 * @package     kernel
47 * @subpackage  form
48 */
49class MyXoopsGroupPermForm extends XoopsForm
50{
51
52    /**
53     * Module ID
54     * @var int
55     */
56    var $_modid;
57    /**
58     * Tree structure of items
59     * @var array
60     */
61    var $_itemTree = array() ;
62    /**
63     * Name of permission
64     * @var string
65     */
66    var $_permName;
67    /**
68     * Description of permission
69     * @var string
70     */
71    var $_permDesc;
72    /**
73     * Appendix
74     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
75     */
76    var $_appendix = array() ;
77
78    /**
79     * Constructor
80     */
81    function MyXoopsGroupPermForm($title, $modid, $permname, $permdesc)
82    {
83//      $this->XoopsForm($title, 'groupperm_form', XOOPS_URL.'/modules/system/admin/groupperm.php', 'post'); GIJ
84        $this->XoopsForm($title, 'groupperm_form', '' , 'post');
85        $this->_modid = intval($modid);
86        $this->_permName = $permname;
87        $this->_permDesc = $permdesc;
88        $this->addElement(new XoopsFormHidden('modid', $this->_modid));
89    }
90
91    /**
92     * Adds an item to which permission will be assigned
93     *
94     * @param string $itemName
95     * @param int $itemId
96     * @param int $itemParent
97     * @access public
98     */
99    function addItem($itemId, $itemName, $itemParent = 0)
100    {
101        $this->_itemTree[$itemParent]['children'][] = $itemId;
102        $this->_itemTree[$itemId]['parent'] = $itemParent;
103        $this->_itemTree[$itemId]['name'] = $itemName;
104        $this->_itemTree[$itemId]['id'] = $itemId;
105    }
106
107    /**
108     * Add appendix
109     *
110     * @access public
111     */
112    function addAppendix($permName,$itemId,$itemName)
113    {
114        $this->_appendix[] = array('permname'=>$permName,'itemid'=>$itemId,'itemname'=>$itemName,'selected'=>false);
115    }
116
117    /**
118     * Loads all child ids for an item to be used in javascript
119     *
120     * @param int $itemId
121     * @param array $childIds
122     * @access private
123     */
124    function _loadAllChildItemIds($itemId, &$childIds)
125    {
126        if (!empty($this->_itemTree[$itemId]['children'])) {
127            $first_child = $this->_itemTree[$itemId]['children'];
128            foreach ($first_child as $fcid) {
129                array_push($childIds, $fcid);
130                if (!empty($this->_itemTree[$fcid]['children'])) {
131                    foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
132                        array_push($childIds, $_fcid);
133                        $this->_loadAllChildItemIds($_fcid, $childIds);
134                    }
135                }
136            }
137        }
138    }
139
140    /**
141     * Renders the form
142     *
143     * @return string
144     * @access public
145     */
146    function render()
147    {
148        global $xoopsGTicket ;
149   
150        // load all child ids for javascript codes
151        foreach (array_keys($this->_itemTree) as $item_id) {
152            $this->_itemTree[$item_id]['allchild'] = array();
153            $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
154        }
155        $gperm_handler =& xoops_gethandler('groupperm');
156        $member_handler =& xoops_gethandler('member');
157        $glist = $member_handler->getGroupList();
158        foreach (array_keys($glist) as $i) {
159            // get selected item id(s) for each group
160            $selected = $gperm_handler->getItemIds($this->_permName, $i, $this->_modid);
161            $ele = new MyXoopsGroupFormCheckBox($glist[$i], 'perms['.$this->_permName.']', $i, $selected);
162            $ele->setOptionTree($this->_itemTree);
163
164            foreach( $this->_appendix as $key => $append ) {
165                $this->_appendix[$key]['selected'] = $gperm_handler->checkRight($append['permname'], $append['itemid'], $i, $this->_modid ) ;
166            }
167            $ele->setAppendix($this->_appendix);
168            $this->addElement($ele);
169            unset($ele);
170        }
171
172        // GIJ start
173        $jstray = new XoopsFormElementTray(' &nbsp; ');
174        $jsuncheckbutton = new XoopsFormButton('', 'none', _NONE, 'button');
175        $jsuncheckbutton->setExtra( "onclick=\"with(document.groupperm_form){for(i=0;i<length;i++){if(elements[i].type=='checkbox'){elements[i].checked=false;}}}\"" ) ;
176        $jscheckbutton = new XoopsFormButton('', 'all', _ALL, 'button');
177        $jscheckbutton->setExtra( "onclick=\"with(document.groupperm_form){for(i=0;i<length;i++){if(elements[i].type=='checkbox' && (elements[i].name.indexOf('module_admin')<0 || elements[i].name.indexOf('[groups][1]')>=0)){elements[i].checked=true;}}}\"" ) ;
178        $jstray->addElement( $jsuncheckbutton ) ;
179        $jstray->addElement( $jscheckbutton ) ;
180        $this->addElement($jstray);
181        // GIJ end
182
183        $tray = new XoopsFormElementTray('');
184        $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
185        $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
186        $this->addElement($tray);
187
188        $ret = '<h4>'.$this->getTitle().'</h4>'.$this->_permDesc.'<br />';
189        $ret .= "<form name='".$this->getName()."' id='".$this->getName()."' action='".$this->getAction()."' method='".$this->getMethod()."'".$this->getExtra().">\n<table width='100%' class='outer' cellspacing='1'>\n";
190        $elements =& $this->getElements();
191        foreach(array_keys($elements) as $i) {
192            if (!is_object($elements[$i])) {
193                $ret .= $elements[$i];
194            } elseif (!$elements[$i]->isHidden()) {
195                $ret .= "<tr valign='top' align='left'><td class='head'>".$elements[$i]->getCaption();
196                if ($elements[$i]->getDescription() != '') {
197                    $ret .= '<br /><br /><span style="font-weight: normal;">'.$elements[$i]->getDescription().'</span>';
198                }
199                $ret .= "</td>\n<td class='even'>\n".$elements[$i]->render()."\n</td></tr>\n";
200            } else {
201                $ret .= $elements[$i]->render();
202            }
203        }
204        $ret .= "</table>".$xoopsGTicket->getTicketHtml(__LINE__ , 1800 , 'myblocksadmin' )."</form>";
205        return $ret;
206    }
207}
208
209/**
210 * Renders checkbox options for a group permission form
211 *
212 * @author  Kazumi Ono  <[email protected]>
213 * @copyright   copyright (c) 2000-2003 XOOPS.org
214 *
215 * @package     kernel
216 * @subpackage  form
217 */
218class MyXoopsGroupFormCheckBox extends XoopsFormElement
219{
220
221    /**
222     * Pre-selected value(s)
223     * @var array;
224     */
225    var $_value;
226    /**
227     * Group ID
228     * @var int
229     */
230    var $_groupId;
231    /**
232     * Option tree
233     * @var array
234     */
235    var $_optionTree;
236    /**
237     * Appendix
238     * @var array ('permname'=>,'itemid'=>,'itemname'=>,'selected'=>)
239     */
240    var $_appendix = array() ;
241
242    /**
243     * Constructor
244     */
245    function MyXoopsGroupFormCheckBox($caption, $name, $groupId, $values = null)
246    {
247        $this->setCaption($caption);
248        $this->setName($name);
249        if (isset($values)) {
250            $this->setValue($values);
251        }
252        $this->_groupId = $groupId;
253    }
254
255    /**
256     * Sets pre-selected values
257     *
258     * @param mixed $value A group ID or an array of group IDs
259     * @access public
260     */
261    function setValue($value)
262    {
263        if (is_array($value)) {
264            foreach ($value as $v) {
265                $this->setValue($v);
266            }
267        } else {
268            $this->_value[] = $value;
269        }
270    }
271
272    /**
273     * Sets the tree structure of items
274     *
275     * @param array $optionTree
276     * @access public
277     */
278    function setOptionTree(&$optionTree)
279    {
280        $this->_optionTree =& $optionTree;
281    }
282
283    /**
284     * Sets appendix of checkboxes
285     *
286     * @access public
287     */
288    function setAppendix($appendix)
289    {
290        $this->_appendix = $appendix ;
291    }
292
293    /**
294     * Renders checkbox options for this group
295     *
296     * @return string
297     * @access public
298     */
299    function render()
300    {
301        $ret = '' ;
302
303        if( sizeof( $this->_appendix ) > 0 ) {
304            $ret .= '<table class="outer"><tr>';
305            $cols = 1;
306            foreach ($this->_appendix as $append) {
307                if ($cols > 4) {
308                    $ret .= '</tr><tr>';
309                    $cols = 1;
310                }
311                $checked = $append['selected'] ? 'checked="checked"' : '' ;
312                $name = 'perms['.$append['permname'].']' ;
313                $itemid = $append['itemid'] ;
314                $itemid = $append['itemid'] ;
315                $ret .= "<td class=\"odd\"><input type=\"checkbox\" name=\"{$name}[groups][$this->_groupId][$itemid]\" id=\"{$name}[groups][$this->_groupId][$itemid]\" value=\"1\" $checked />{$append['itemname']}<input type=\"hidden\" name=\"{$name}[parents][$itemid]\" value=\"\" /><input type=\"hidden\" name=\"{$name}[itemname][$itemid]\" value=\"{$append['itemname']}\" /><br /></td>" ;
316                $cols++;
317            }
318            $ret .= '</tr></table>';
319        }
320
321        $ret .= '<table class="outer"><tr>';
322        $cols = 1;
323        if( ! empty( $this->_optionTree[0]['children'] ) ) {
324            foreach ($this->_optionTree[0]['children'] as $topitem) {
325                if ($cols > 4) {
326                    $ret .= '</tr><tr>';
327                    $cols = 1;
328                }
329                $tree = '<td class="odd">';
330                $prefix = '';
331                $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
332                $ret .= $tree.'</td>';
333                $cols++;
334            }
335        }
336        $ret .= '</tr></table>';
337        return $ret;
338    }
339
340    /**
341     * Renders checkbox options for an item tree
342     *
343     * @param string $tree
344     * @param array $option
345     * @param string $prefix
346     * @param array $parentIds
347     * @access private
348     */
349    function _renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
350    {
351        $tree .= $prefix."<input type=\"checkbox\" name=\"".$this->getName()."[groups][".$this->_groupId."][".$option['id']."]\" id=\"".$this->getName()."[groups][".$this->_groupId."][".$option['id']."]\" onclick=\"";
352        // If there are parent elements, add javascript that will
353        // make them selecteded when this element is checked to make
354        // sure permissions to parent items are added as well.
355        foreach ($parentIds as $pid) {
356            $parent_ele = $this->getName().'[groups]['.$this->_groupId.']['.$pid.']';
357            $tree .= "var ele = xoopsGetElementById('".$parent_ele."'); if(ele.checked != true) {ele.checked = this.checked;}";
358        }
359        // If there are child elements, add javascript that will
360        // make them unchecked when this element is unchecked to make
361        // sure permissions to child items are not added when there
362        // is no permission to this item.
363        foreach ($option['allchild'] as $cid) {
364            $child_ele = $this->getName().'[groups]['.$this->_groupId.']['.$cid.']';
365            $tree .= "var ele = xoopsGetElementById('".$child_ele."'); if(this.checked != true) {ele.checked = false;}";
366        }
367        $tree .= '" value="1"';
368        if ( isset( $this->_value ) && in_array($option['id'], $this->_value)) {
369            $tree .= ' checked="checked"';
370        }
371        $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";
372        if( isset( $option['children'] ) ) foreach ($option['children'] as $child) {
373            array_push($parentIds, $option['id']);
374            $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix.'&nbsp;-', $parentIds);
375        }
376    }
377}
378?>
Note: See TracBrowser for help on using the repository browser.