home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopsform / grouppermform.php < prev    next >
Encoding:
PHP Script  |  2007-12-08  |  12.2 KB  |  326 lines

  1. <?php 
  2. // $Id: grouppermform.php 1158 2007-12-08 06:24:20Z phppp $
  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. if (!defined('XOOPS_ROOT_PATH')) {
  32.     die("XOOPS root path not defined");
  33. }
  34. require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelement.php';
  35. require_once XOOPS_ROOT_PATH . '/class/xoopsform/formhidden.php';
  36. require_once XOOPS_ROOT_PATH . '/class/xoopsform/formbutton.php';
  37. require_once XOOPS_ROOT_PATH . '/class/xoopsform/formelementtray.php';
  38. require_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 <onokazu@myweb.ne.jp> 
  44.  * @copyright copyright (c) 2000-2003 XOOPS.org
  45.  * @package kernel
  46.  * @subpackage form
  47.  */
  48. class XoopsGroupPermForm extends XoopsForm
  49. {
  50.     /**
  51.      * Module ID
  52.      * 
  53.      * @var int 
  54.      */
  55.     var $_modid;
  56.     /**
  57.      * Tree structure of items
  58.      * 
  59.      * @var array 
  60.      */
  61.     var $_itemTree;
  62.     /**
  63.      * Name of permission
  64.      * 
  65.      * @var string 
  66.      */
  67.     var $_permName;
  68.     /**
  69.      * Description of permission
  70.      * 
  71.      * @var string 
  72.      */
  73.     var $_permDesc;
  74.  
  75.     /**
  76.      * Constructor
  77.      */
  78.     function XoopsGroupPermForm($title, $modid, $permname, $permdesc, $url = "")
  79.     {
  80.         $this->XoopsForm($title, 'groupperm_form', XOOPS_URL . '/modules/system/admin/groupperm.php', 'post');
  81.         $this->_modid = intval($modid);
  82.         $this->_permName = $permname;
  83.         $this->_permDesc = $permdesc;
  84.         $this->addElement(new XoopsFormHidden('modid', $this->_modid));
  85.         if ($url != "") {
  86.             $this->addElement(new XoopsFormHidden('redirect_url', $url));
  87.         }
  88.     } 
  89.  
  90.     /**
  91.      * Adds an item to which permission will be assigned
  92.      * 
  93.      * @param string $itemName 
  94.      * @param int $itemId 
  95.      * @param int $itemParent 
  96.      * @access public 
  97.      */
  98.     function addItem($itemId, $itemName, $itemParent = 0)
  99.     {
  100.         $this->_itemTree[$itemParent]['children'][] = $itemId;
  101.         $this->_itemTree[$itemId]['parent'] = $itemParent;
  102.         $this->_itemTree[$itemId]['name'] = $itemName;
  103.         $this->_itemTree[$itemId]['id'] = $itemId;
  104.     }
  105.  
  106.     /**
  107.      * Loads all child ids for an item to be used in javascript
  108.      * 
  109.      * @param int $itemId 
  110.      * @param array $childIds 
  111.      * @access private 
  112.      */
  113.     function _loadAllChildItemIds($itemId, &$childIds)
  114.     {
  115.         if (!empty($this->_itemTree[$itemId]['children'])) {
  116.             $first_child = $this->_itemTree[$itemId]['children'];
  117.             foreach ($first_child as $fcid) {
  118.                 array_push($childIds, $fcid);
  119.                 if (!empty($this->_itemTree[$fcid]['children'])) {
  120.                     foreach ($this->_itemTree[$fcid]['children'] as $_fcid) {
  121.                         array_push($childIds, $_fcid);
  122.                         $this->_loadAllChildItemIds($_fcid, $childIds);
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * Renders the form
  131.      * 
  132.      * @return string
  133.      * @access public
  134.      */
  135.     function render()
  136.     { 
  137.         // load all child ids for javascript codes
  138.         foreach (array_keys($this->_itemTree)as $item_id) {
  139.             $this->_itemTree[$item_id]['allchild'] = array();
  140.             $this->_loadAllChildItemIds($item_id, $this->_itemTree[$item_id]['allchild']);
  141.         }
  142.         $gperm_handler =& xoops_gethandler('groupperm');
  143.         $member_handler =& xoops_gethandler('member');
  144.         $glist =& $member_handler->getGroupList();
  145.         foreach (array_keys($glist) as $i) {
  146.             // get selected item id(s) for each group
  147.             $selected = $gperm_handler->getItemIds($this->_permName, $i, $this->_modid);
  148.             $ele = new XoopsGroupFormCheckBox($glist[$i], 'perms[' . $this->_permName . ']', $i, $selected);
  149.             $ele->setOptionTree($this->_itemTree);
  150.             $this->addElement($ele);
  151.             unset($ele);
  152.         } 
  153.         $tray = new XoopsFormElementTray('');
  154.         $tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
  155.         $tray->addElement(new XoopsFormButton('', 'reset', _CANCEL, 'reset'));
  156.         $this->addElement($tray);
  157.         $ret = '<h4>' . $this->getTitle() . '</h4>' . $this->_permDesc . '<br />';
  158.         $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";
  159.         $elements = $this->getElements();
  160.         $hidden = '';
  161.         foreach (array_keys($elements) as $i) {
  162.             if (!is_object($elements[$i])) {
  163.                 $ret .= $elements[$i];
  164.             } elseif (!$elements[$i]->isHidden()) {
  165.                 $ret .= "<tr valign='top' align='left'><td class='head'>" . $elements[$i]->getCaption();
  166.                 if ($elements[$i]->getDescription() != '') {
  167.                     $ret .= '<br /><br /><span style="font-weight: normal;">' . $elements[$i]->getDescription() . '</span>';
  168.                 }
  169.                 $ret .= "</td>\n<td class='even'>\n" . $elements[$i]->render() . "\n</td></tr>\n";
  170.             } else {
  171.                 $hidden .= $elements[$i]->render();
  172.             }
  173.         }
  174.         $ret .= "</table>$hidden</form>";
  175.         $ret .= $this->renderValidationJS( true );
  176.         return $ret;
  177.     }
  178. }
  179.  
  180. /**
  181.  * Renders checkbox options for a group permission form
  182.  * 
  183.  * @author Kazumi Ono <onokazu@myweb.ne.jp> 
  184.  * @copyright copyright (c) 2000-2003 XOOPS.org
  185.  * @package kernel
  186.  * @subpackage form
  187.  */
  188. class XoopsGroupFormCheckBox extends XoopsFormElement
  189. {
  190.     /**
  191.      * Pre-selected value(s)
  192.      * 
  193.      * @var array;
  194.      */
  195.     var $_value = array();
  196.     /**
  197.      * Group ID
  198.      * 
  199.      * @var int 
  200.      */
  201.     var $_groupId;
  202.     /**
  203.      * Option tree
  204.      * 
  205.      * @var array 
  206.      */
  207.     var $_optionTree;
  208.  
  209.     /**
  210.      * Constructor
  211.      */
  212.     function XoopsGroupFormCheckBox($caption, $name, $groupId, $values = null)
  213.     {
  214.         $this->setCaption($caption);
  215.         $this->setName($name);
  216.         if (isset($values)) {
  217.             $this->setValue($values);
  218.         }
  219.         $this->_groupId = $groupId;
  220.     }
  221.  
  222.     /**
  223.      * Sets pre-selected values
  224.      * 
  225.      * @param mixed $value A group ID or an array of group IDs
  226.      * @access public 
  227.      */
  228.     function setValue($value)
  229.     {
  230.         if (is_array($value)) {
  231.             foreach ($value as $v) {
  232.                 $this->setValue($v);
  233.             }
  234.         } else {
  235.             $this->_value[] = $value;
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Sets the tree structure of items
  241.      * 
  242.      * @param array $optionTree 
  243.      * @access public 
  244.      */
  245.     function setOptionTree(&$optionTree)
  246.     {
  247.         $this->_optionTree =& $optionTree;
  248.     }
  249.  
  250.     /**
  251.      * Renders checkbox options for this group
  252.      * 
  253.      * @return string 
  254.      * @access public 
  255.      */
  256.     function render()
  257.     {
  258.         $ele_name = $this->getName();
  259.         $ret = '<table class="outer"><tr><td class="odd"><table><tr>';
  260.         $cols = 1;
  261.         foreach ($this->_optionTree[0]['children'] as $topitem) {
  262.             if ($cols > 4) {
  263.                 $ret .= '</tr><tr>';
  264.                 $cols = 1;
  265.             }
  266.             $tree = '<td valign="top">';
  267.             $prefix = '';
  268.             $this->_renderOptionTree($tree, $this->_optionTree[$topitem], $prefix);
  269.             $ret .= $tree.'</td>';
  270.             $cols++;
  271.         }
  272.         $ret .= '</tr></table></td><td class="even" valign="top">';
  273.         foreach (array_keys($this->_optionTree) as $id) {
  274.             if (!empty($id)) {
  275.                 $option_ids[] = "'".$ele_name.'[groups]['.$this->_groupId.']['.$id.']'."'";
  276.             }
  277.         }
  278.         $checkallbtn_id = $ele_name.'[checkallbtn]['.$this->_groupId.']';
  279.         $option_ids_str = implode(', ', $option_ids);
  280.         $ret .= _ALL." <input id=\"".$checkallbtn_id."\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(".$option_ids_str."); xoopsCheckAllElements(optionids, '".$checkallbtn_id."');\" />";
  281.         $ret .= '</td></tr></table>';
  282.         return $ret;
  283.     } 
  284.  
  285.     /**
  286.      * Renders checkbox options for an item tree
  287.      * 
  288.      * @param string $tree 
  289.      * @param array $option 
  290.      * @param string $prefix 
  291.      * @param array $parentIds 
  292.      * @access private 
  293.      */
  294.     function _renderOptionTree(&$tree, $option, $prefix, $parentIds = array())
  295.     {
  296.         $ele_name = $this->getName();
  297.         $tree .= $prefix . "<input type=\"checkbox\" name=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" id=\"" . $ele_name . "[groups][" . $this->_groupId . "][" . $option['id'] . "]\" onclick=\""; 
  298.         // If there are parent elements, add javascript that will
  299.         // make them selecteded when this element is checked to make
  300.         // sure permissions to parent items are added as well.
  301.         foreach ($parentIds as $pid) {
  302.             $parent_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $pid . ']';
  303.             $tree .= "var ele = xoopsGetElementById('" . $parent_ele . "'); if(ele.checked != true) {ele.checked = this.checked;}";
  304.         } 
  305.         // If there are child elements, add javascript that will
  306.         // make them unchecked when this element is unchecked to make
  307.         // sure permissions to child items are not added when there
  308.         // is no permission to this item.
  309.         foreach ($option['allchild'] as $cid) {
  310.             $child_ele = $ele_name . '[groups][' . $this->_groupId . '][' . $cid . ']';
  311.             $tree .= "var ele = xoopsGetElementById('" . $child_ele . "'); if(this.checked != true) {ele.checked = false;}";
  312.         } 
  313.         $tree .= '" value="1"';
  314.         if (in_array($option['id'], $this->_value)) {
  315.             $tree .= ' checked="checked"';
  316.         } 
  317.         $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";
  318.         if (isset($option['children'])) {
  319.             foreach ($option['children'] as $child) {
  320.                 array_push($parentIds, $option['id']);
  321.                 $this->_renderOptionTree($tree, $this->_optionTree[$child], $prefix . ' -', $parentIds);
  322.             }
  323.         }
  324.     }
  325. }
  326. ?>