Source for file access.php

Documentation is available at access.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  HTML
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Extended Utility class for all HTML drawing classes.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  HTML
  17.  * @since       1.6
  18.  */
  19. abstract class JHtmlAccess
  20. {
  21.     /**
  22.      * A cached array of the asset groups
  23.      *
  24.      * @var    array 
  25.      * @since  1.6
  26.      */
  27.     protected static $asset_groups null;
  28.  
  29.     /**
  30.      * Displays a list of the available access view levels
  31.      *
  32.      * @param   string  $name      The form field name.
  33.      * @param   string  $selected  The name of the selected section.
  34.      * @param   string  $attribs   Additional attributes to add to the select field.
  35.      * @param   mixed   $params    True to add "All Sections" option or and array of options
  36.      * @param   string  $id        The form field id
  37.      *
  38.      * @return  string  The required HTML for the SELECT tag.
  39.      *
  40.      * @see    JFormFieldAccessLevel
  41.      * @since  1.6
  42.      */
  43.     public static function level($name$selected$attribs ''$params true$id false)
  44.     {
  45.         $db JFactory::getDbo();
  46.         $query $db->getQuery(true)
  47.             ->select('a.id AS value, a.title AS text')
  48.             ->from('#__viewlevels AS a')
  49.             ->group('a.id, a.title, a.ordering')
  50.             ->order('a.ordering ASC')
  51.             ->order($db->quoteName('title'' ASC');
  52.  
  53.         // Get the options.
  54.         $db->setQuery($query);
  55.         $options $db->loadObjectList();
  56.  
  57.         // If params is an array, push these options to the array
  58.         if (is_array($params))
  59.         {
  60.             $options array_merge($params$options);
  61.         }
  62.         // If all levels is allowed, push it into the array.
  63.         elseif ($params)
  64.         {
  65.             array_unshift($optionsJHtml::_('select.option'''JText::_('JOPTION_ACCESS_SHOW_ALL_LEVELS')));
  66.         }
  67.  
  68.         return JHtml::_(
  69.             'select.genericlist',
  70.             $options,
  71.             $name,
  72.             array(
  73.                 'list.attr' => $attribs,
  74.                 'list.select' => $selected,
  75.                 'id' => $id
  76.             )
  77.         );
  78.     }
  79.  
  80.     /**
  81.      * Displays a list of the available user groups.
  82.      *
  83.      * @param   string   $name      The form field name.
  84.      * @param   string   $selected  The name of the selected section.
  85.      * @param   string   $attribs   Additional attributes to add to the select field.
  86.      * @param   boolean  $allowAll  True to add "All Groups" option.
  87.      *
  88.      * @return  string   The required HTML for the SELECT tag.
  89.      *
  90.      * @see     JFormFieldUsergroup
  91.      * @since   1.6
  92.      */
  93.     public static function usergroup($name$selected$attribs ''$allowAll true)
  94.     {
  95.         $db JFactory::getDbo();
  96.         $query $db->getQuery(true)
  97.             ->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level')
  98.             ->from($db->quoteName('#__usergroups'' AS a')
  99.             ->join('LEFT'$db->quoteName('#__usergroups'' AS b ON a.lft > b.lft AND a.rgt < b.rgt')
  100.             ->group('a.id, a.title, a.lft, a.rgt')
  101.             ->order('a.lft ASC');
  102.         $db->setQuery($query);
  103.         $options $db->loadObjectList();
  104.  
  105.         for ($i 0$n count($options)$i $n$i++)
  106.         {
  107.             $options[$i]->text str_repeat('- '$options[$i]->level$options[$i]->text;
  108.         }
  109.  
  110.         // If all usergroups is allowed, push it into the array.
  111.         if ($allowAll)
  112.         {
  113.             array_unshift($optionsJHtml::_('select.option'''JText::_('JOPTION_ACCESS_SHOW_ALL_GROUPS')));
  114.         }
  115.  
  116.         return JHtml::_('select.genericlist'$options$namearray('list.attr' => $attribs'list.select' => $selected));
  117.     }
  118.  
  119.     /**
  120.      * Returns a UL list of user groups with check boxes
  121.      *
  122.      * @param   string   $name             The name of the checkbox controls array
  123.      * @param   array    $selected         An array of the checked boxes
  124.      * @param   boolean  $checkSuperAdmin  If false only super admins can add to super admin groups
  125.      *
  126.      * @return  string 
  127.      *
  128.      * @since   1.6
  129.      */
  130.     public static function usergroups($name$selected$checkSuperAdmin false)
  131.     {
  132.         static $count;
  133.  
  134.         $count++;
  135.  
  136.         $isSuperAdmin JFactory::getUser()->authorise('core.admin');
  137.  
  138.         $db JFactory::getDbo();
  139.         $query $db->getQuery(true)
  140.             ->select('a.*, COUNT(DISTINCT b.id) AS level')
  141.             ->from($db->quoteName('#__usergroups'' AS a')
  142.             ->join('LEFT'$db->quoteName('#__usergroups'' AS b ON a.lft > b.lft AND a.rgt < b.rgt')
  143.             ->group('a.id, a.title, a.lft, a.rgt, a.parent_id')
  144.             ->order('a.lft ASC');
  145.         $db->setQuery($query);
  146.         $groups $db->loadObjectList();
  147.  
  148.         $html array();
  149.  
  150.         for ($i 0$n count($groups)$i $n$i++)
  151.         {
  152.             $item &$groups[$i];
  153.  
  154.             // If checkSuperAdmin is true, only add item if the user is superadmin or the group is not super admin
  155.             if ((!$checkSuperAdmin|| $isSuperAdmin || (!JAccess::checkGroup($item->id'core.admin')))
  156.             {
  157.                 // Setup  the variable attributes.
  158.                 $eid $count 'group_' $item->id;
  159.  
  160.                 // Don't call in_array unless something is selected
  161.                 $checked '';
  162.  
  163.                 if ($selected)
  164.                 {
  165.                     $checked in_array($item->id$selected' checked="checked"' '';
  166.                 }
  167.                 $rel ($item->parent_id 0' rel="' $count 'group_' $item->parent_id '"' '';
  168.  
  169.                 // Build the HTML for the item.
  170.                 $html['    <div class="control-group">';
  171.                 $html['        <div class="controls">';
  172.                 $html['            <label class="checkbox" for="' $eid '">';
  173.                 $html['            <input type="checkbox" name="' $name '[]" value="' $item->id '" id="' $eid '"';
  174.                 $html['                    ' $checked $rel ' />';
  175.                 $html['            ' str_repeat('<span class="gi">|&mdash;</span>'$item->level$item->title;
  176.                 $html['            </label>';
  177.                 $html['        </div>';
  178.                 $html['    </div>';
  179.             }
  180.         }
  181.  
  182.         return implode("\n"$html);
  183.     }
  184.  
  185.     /**
  186.      * Returns a UL list of actions with check boxes
  187.      *
  188.      * @param   string  $name       The name of the checkbox controls array
  189.      * @param   array   $selected   An array of the checked boxes
  190.      * @param   string  $component  The component the permissions apply to
  191.      * @param   string  $section    The section (within a component) the permissions apply to
  192.      *
  193.      * @return  string 
  194.      *
  195.      * @see     JAccess
  196.      * @since   1.6
  197.      */
  198.     public static function actions($name$selected$component$section 'global')
  199.     {
  200.         static $count;
  201.  
  202.         $count++;
  203.  
  204.         $actions JAccess::getActionsFromFile(
  205.             JPATH_ADMINISTRATOR '/components/' $component '/access.xml',
  206.             "/access/section[@name='" $section "']/"
  207.         );
  208.  
  209.         $html array();
  210.         $html['<ul class="checklist access-actions">';
  211.  
  212.         for ($i 0$n count($actions)$i $n$i++)
  213.         {
  214.             $item &$actions[$i];
  215.  
  216.             // Setup  the variable attributes.
  217.             $eid $count 'action_' $item->id;
  218.             $checked in_array($item->id$selected' checked="checked"' '';
  219.  
  220.             // Build the HTML for the item.
  221.             $html['    <li>';
  222.             $html['        <input type="checkbox" name="' $name '[]" value="' $item->id '" id="' $eid '"';
  223.             $html['            ' $checked ' />';
  224.             $html['        <label for="' $eid '">';
  225.             $html['            ' JText::_($item->title);
  226.             $html['        </label>';
  227.             $html['    </li>';
  228.         }
  229.  
  230.         $html['</ul>';
  231.  
  232.         return implode("\n"$html);
  233.     }
  234.  
  235.     /**
  236.      * Gets a list of the asset groups as an array of JHtml compatible options.
  237.      *
  238.      * @return  mixed  An array or false if an error occurs
  239.      *
  240.      * @since   1.6
  241.      */
  242.     public static function assetgroups()
  243.     {
  244.         if (empty(static::$asset_groups))
  245.         {
  246.             $db JFactory::getDbo();
  247.             $query $db->getQuery(true)
  248.                 ->select('a.id AS value, a.title AS text')
  249.                 ->from($db->quoteName('#__viewlevels'' AS a')
  250.                 ->group('a.id, a.title, a.ordering')
  251.                 ->order('a.ordering ASC');
  252.  
  253.             $db->setQuery($query);
  254.             static::$asset_groups $db->loadObjectList();
  255.         }
  256.  
  257.         return static::$asset_groups;
  258.     }
  259.  
  260.     /**
  261.      * Displays a Select list of the available asset groups
  262.      *
  263.      * @param   string  $name      The name of the select element
  264.      * @param   mixed   $selected  The selected asset group id
  265.      * @param   string  $attribs   Optional attributes for the select field
  266.      * @param   array   $config    An array of options for the control
  267.      *
  268.      * @return  mixed  An HTML string or null if an error occurs
  269.      *
  270.      * @since   1.6
  271.      */
  272.     public static function assetgrouplist($name$selected$attribs null$config array())
  273.     {
  274.         static $count;
  275.  
  276.         $options static::assetgroups();
  277.  
  278.         if (isset($config['title']))
  279.         {
  280.             array_unshift($optionsJHtml::_('select.option'''$config['title']));
  281.         }
  282.  
  283.         return JHtml::_(
  284.             'select.genericlist',
  285.             $options,
  286.             $name,
  287.             array(
  288.                 'id' => isset($config['id']$config['id''assetgroups_' (++$count),
  289.                 'list.attr' => (is_null($attribs'class="inputbox" size="3"' $attribs),
  290.                 'list.select' => (int) $selected
  291.             )
  292.         );
  293.     }
  294. }

Documentation generated on Tue, 19 Nov 2013 14:53:16 +0100 by phpDocumentor 1.4.3