[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Helper/ -> UserGroupsHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2016 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\Helper;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\Database\ParameterType;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Helper to deal with user groups.
  21   *
  22   * @since  3.6.3
  23   */
  24  final class UserGroupsHelper
  25  {
  26      /**
  27       * Indicates the current helper instance is the singleton instance.
  28       *
  29       * @var    integer
  30       * @since  3.6.3
  31       */
  32      public const MODE_SINGLETON = 1;
  33  
  34      /**
  35       * Indicates the current helper instance is a standalone class instance.
  36       *
  37       * @var    integer
  38       * @since  3.6.3
  39       */
  40      public const MODE_INSTANCE = 2;
  41  
  42      /**
  43       * Singleton instance.
  44       *
  45       * @var    array
  46       * @since  3.6.3
  47       */
  48      private static $instance;
  49  
  50      /**
  51       * Available user groups
  52       *
  53       * @var    array
  54       * @since  3.6.3
  55       */
  56      private $groups = array();
  57  
  58      /**
  59       * Mode this class is working: singleton or std instance
  60       *
  61       * @var    integer
  62       * @since  3.6.3
  63       */
  64      private $mode;
  65  
  66      /**
  67       * Total available groups
  68       *
  69       * @var    integer
  70       * @since  3.6.3
  71       */
  72      private $total;
  73  
  74      /**
  75       * Constructor
  76       *
  77       * @param   array    $groups  Array of groups
  78       * @param   integer  $mode    Working mode for this class
  79       *
  80       * @since   3.6.3
  81       */
  82      public function __construct(array $groups = array(), $mode = self::MODE_INSTANCE)
  83      {
  84          $this->mode = (int) $mode;
  85  
  86          if ($groups) {
  87              $this->setGroups($groups);
  88          }
  89      }
  90  
  91      /**
  92       * Count loaded user groups.
  93       *
  94       * @return  integer
  95       *
  96       * @since   3.6.3
  97       */
  98      public function count()
  99      {
 100          return \count($this->groups);
 101      }
 102  
 103      /**
 104       * Get the helper instance.
 105       *
 106       * @return  self
 107       *
 108       * @since   3.6.3
 109       */
 110      public static function getInstance()
 111      {
 112          if (static::$instance === null) {
 113              // Only here to avoid code style issues...
 114              $groups = array();
 115  
 116              static::$instance = new static($groups, static::MODE_SINGLETON);
 117          }
 118  
 119          return static::$instance;
 120      }
 121  
 122      /**
 123       * Get a user group by its id.
 124       *
 125       * @param   integer  $id  Group identifier
 126       *
 127       * @return  mixed  stdClass on success. False otherwise
 128       *
 129       * @since   3.6.3
 130       */
 131      public function get($id)
 132      {
 133          if ($this->has($id)) {
 134              return $this->groups[$id];
 135          }
 136  
 137          // Singleton will load groups as they are requested
 138          if ($this->isSingleton()) {
 139              $this->groups[$id] = $this->load($id);
 140  
 141              return $this->groups[$id];
 142          }
 143  
 144          return false;
 145      }
 146  
 147      /**
 148       * Get the list of existing user groups.
 149       *
 150       * @return  array
 151       *
 152       * @since   3.6.3
 153       */
 154      public function getAll()
 155      {
 156          if ($this->isSingleton() && $this->total() !== $this->count()) {
 157              $this->loadAll();
 158          }
 159  
 160          return $this->groups;
 161      }
 162  
 163      /**
 164       * Check if a group is in the list.
 165       *
 166       * @param   integer  $id  Group identifier
 167       *
 168       * @return  boolean
 169       *
 170       * @since   3.6.3
 171       */
 172      public function has($id)
 173      {
 174          return (\array_key_exists($id, $this->groups) && $this->groups[$id] !== false);
 175      }
 176  
 177      /**
 178       * Check if this instance is a singleton.
 179       *
 180       * @return  boolean
 181       *
 182       * @since   3.6.3
 183       */
 184      private function isSingleton()
 185      {
 186          return $this->mode === static::MODE_SINGLETON;
 187      }
 188  
 189      /**
 190       * Get total available user groups in database.
 191       *
 192       * @return  integer
 193       *
 194       * @since   3.6.3
 195       */
 196      public function total()
 197      {
 198          if ($this->total === null) {
 199              $db = Factory::getDbo();
 200  
 201              $query = $db->getQuery(true)
 202                  ->select('COUNT(' . $db->quoteName('id') . ')')
 203                  ->from($db->quoteName('#__usergroups'));
 204  
 205              $db->setQuery($query);
 206  
 207              $this->total = (int) $db->loadResult();
 208          }
 209  
 210          return $this->total;
 211      }
 212  
 213      /**
 214       * Load a group from database.
 215       *
 216       * @param   integer  $id  Group identifier
 217       *
 218       * @return  mixed
 219       *
 220       * @since   3.6.3
 221       */
 222      public function load($id)
 223      {
 224          // Cast as integer until method is typehinted.
 225          $id = (int) $id;
 226  
 227          $db = Factory::getDbo();
 228  
 229          $query = $db->getQuery(true)
 230              ->select('*')
 231              ->from($db->quoteName('#__usergroups'))
 232              ->where($db->quoteName('id') . ' = :id')
 233              ->bind(':id', $id, ParameterType::INTEGER);
 234  
 235          $db->setQuery($query);
 236  
 237          $group = $db->loadObject();
 238  
 239          if (!$group) {
 240              return false;
 241          }
 242  
 243          return $this->populateGroupData($group);
 244      }
 245  
 246      /**
 247       * Load all user groups from the database.
 248       *
 249       * @return  self
 250       *
 251       * @since   3.6.3
 252       */
 253      public function loadAll()
 254      {
 255          $this->groups = array();
 256  
 257          $db = Factory::getDbo();
 258  
 259          $query = $db->getQuery(true)
 260              ->select('*')
 261              ->from($db->quoteName('#__usergroups'))
 262              ->order($db->quoteName('lft') . ' ASC');
 263  
 264          $db->setQuery($query);
 265  
 266          $groups = $db->loadObjectList('id');
 267  
 268          $this->groups = $groups ?: array();
 269          $this->populateGroupsData();
 270  
 271          return $this;
 272      }
 273  
 274      /**
 275       * Populates extra information for groups.
 276       *
 277       * @return  array
 278       *
 279       * @since   3.6.3
 280       */
 281      private function populateGroupsData()
 282      {
 283          foreach ($this->groups as $group) {
 284              $this->populateGroupData($group);
 285          }
 286  
 287          return $this->groups;
 288      }
 289  
 290      /**
 291       * Populate data for a specific user group.
 292       *
 293       * @param   \stdClass  $group  Group
 294       *
 295       * @return  \stdClass
 296       *
 297       * @since   3.6.3
 298       */
 299      public function populateGroupData($group)
 300      {
 301          if (!$group || property_exists($group, 'path')) {
 302              return $group;
 303          }
 304  
 305          $parentId = (int) $group->parent_id;
 306  
 307          if ($parentId === 0) {
 308              $group->path = array($group->id);
 309              $group->level = 0;
 310  
 311              return $group;
 312          }
 313  
 314          $parentGroup = $this->has($parentId) ? $this->get($parentId) : $this->load($parentId);
 315  
 316          if (!property_exists($parentGroup, 'path')) {
 317              $parentGroup = $this->populateGroupData($parentGroup);
 318          }
 319  
 320          $group->path = array_merge($parentGroup->path, array($group->id));
 321          $group->level = \count($group->path) - 1;
 322  
 323          return $group;
 324      }
 325  
 326      /**
 327       * Set the groups to be used as source.
 328       *
 329       * @param   array  $groups  Array of user groups.
 330       *
 331       * @return  self
 332       *
 333       * @since   3.6.3
 334       */
 335      public function setGroups(array $groups)
 336      {
 337          $this->groups = $groups;
 338          $this->populateGroupsData();
 339          $this->total  = \count($groups);
 340  
 341          return $this;
 342      }
 343  }


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