XOOPS Docs - kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file grouppermform.php

Documentation is available at grouppermform.php

  1. <?php 
  2. // $Id: grouppermform.php 981 2007-08-11 14:07:17Z 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($glistas $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($elementsas $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->renderValidationJStrue );
  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. {
  189.     /**
  190.      * Pre-selected value(s)
  191.      * 
  192.      * @var array; 
  193.      */
  194.     var $_value = array();
  195.     /**
  196.      * Group ID
  197.      * 
  198.      * @var int 
  199.      */
  200.     var $_groupId;
  201.     /**
  202.      * Option tree
  203.      * 
  204.      * @var array 
  205.      */
  206.     var $_optionTree;
  207.  
  208.     /**
  209.      * Constructor
  210.      */
  211.     function XoopsGroupFormCheckBox($caption$name$groupId$values null)
  212.     {
  213.         $this->setCaption($caption);
  214.         $this->setName($name);
  215.         if (isset($values)) {
  216.             $this->setValue($values);
  217.         }
  218.         $this->_groupId = $groupId;
  219.     }
  220.  
  221.     /**
  222.      * Sets pre-selected values
  223.      * 
  224.      * @param mixed $value A group ID or an array of group IDs
  225.      * @access public
  226.      */
  227.     function setValue($value)
  228.     {
  229.         if (is_array($value)) {
  230.             foreach ($value as $v{
  231.                 $this->setValue($v);
  232.             }
  233.         else {
  234.             $this->_value[$value;
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * Sets the tree structure of items
  240.      * 
  241.      * @param array $optionTree 
  242.      * @access public
  243.      */
  244.     function setOptionTree(&$optionTree)
  245.     {
  246.         $this->_optionTree =$optionTree;
  247.     }
  248.  
  249.     /**
  250.      * Renders checkbox options for this group
  251.      * 
  252.      * @return string 
  253.      * @access public
  254.      */
  255.     function render()
  256.     {
  257.         $ret '<table class="outer"><tr><td class="odd"><table><tr>';
  258.         $cols 1;
  259.         foreach ($this->_optionTree[0]['children'as $topitem{
  260.             if ($cols 4{
  261.                 $ret .= '</tr><tr>';
  262.                 $cols 1;
  263.             }
  264.             $tree '<td valign="top">';
  265.             $prefix '';
  266.             $this->_renderOptionTree($tree$this->_optionTree[$topitem]$prefix);
  267.             $ret .= $tree.'</td>';
  268.             $cols++;
  269.         }
  270.         $ret .= '</tr></table></td><td class="even" valign="top">';
  271.         foreach (array_keys($this->_optionTreeas $id{
  272.             if (!empty($id)) {
  273.                 $option_ids["'".$this->getName().'[groups]['.$this->_groupId.']['.$id.']'."'";
  274.             }
  275.         }
  276.         $checkallbtn_id $this->getName().'[checkallbtn]['.$this->_groupId.']';
  277.         $option_ids_str implode(', '$option_ids);
  278.         $ret .= _ALL." <input id=\"".$checkallbtn_id."\" type=\"checkbox\" value=\"\" onclick=\"var optionids = new Array(".$option_ids_str."); xoopsCheckAllElements(optionids, '".$checkallbtn_id."');\" />";
  279.         $ret .= '</td></tr></table>';
  280.         return $ret;
  281.     
  282.  
  283.     /**
  284.      * Renders checkbox options for an item tree
  285.      * 
  286.      * @param string $tree 
  287.      * @param array $option 
  288.      * @param string $prefix 
  289.      * @param array $parentIds 
  290.      * @access private
  291.      */
  292.     function _renderOptionTree(&$tree$option$prefix$parentIds array())
  293.     {
  294.         $tree .= $prefix "<input type=\"checkbox\" name=\"" $this->getName("[groups][" $this->_groupId . "][" $option['id'"]\" id=\"" $this->getName("[groups][" $this->_groupId . "][" $option['id'"]\" onclick=\""
  295.         // If there are parent elements, add javascript that will
  296.         // make them selecteded when this element is checked to make
  297.         // sure permissions to parent items are added as well.
  298.         foreach ($parentIds as $pid{
  299.             $parent_ele $this->getName('[groups][' $this->_groupId . '][' $pid ']';
  300.             $tree .= "var ele = xoopsGetElementById('" $parent_ele "'); if(ele.checked != true) {ele.checked = this.checked;}";
  301.         
  302.         // If there are child elements, add javascript that will
  303.         // make them unchecked when this element is unchecked to make
  304.         // sure permissions to child items are not added when there
  305.         // is no permission to this item.
  306.         foreach ($option['allchild'as $cid{
  307.             $child_ele $this->getName('[groups][' $this->_groupId . '][' $cid ']';
  308.             $tree .= "var ele = xoopsGetElementById('" $child_ele "'); if(this.checked != true) {ele.checked = false;}";
  309.         
  310.         $tree .= '" value="1"';
  311.         if (in_array($option['id']$this->_value)) {
  312.             $tree .= ' checked="checked"';
  313.         
  314.         $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";
  315.         if (isset($option['children'])) {
  316.             foreach ($option['children'as $child{
  317.                 array_push($parentIds$option['id']);
  318.                 $this->_renderOptionTree($tree$this->_optionTree[$child]$prefix '&nbsp;-'$parentIds);
  319.             }
  320.         }
  321.     }
  322. }
  323. ?>

XOOPS Docs generated by phpDocumentor