[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> Access.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer