- md5
- af954934259c4bc402ce5b6eb3612d6d
- sha1
- fec290fbae9df0fbf8e54dd957549c242f75cdf2
- sha256
- adc0eb0e98480e6a128b6e2cf86d179d491c528acde15a2cefdf5e0ba4bc96c0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Helper;
defined('JPATH_PLATFORM') or die;
/**
* Helper to deal with user groups.
*
* @since 3.6.3
*/
final class UserGroupsHelper
{
/**
* Indicates the current helper instance is the singleton instance.
*
* @var integer
* @since 3.6.3
*/
const MODE_SINGLETON = 1;
/**
* Indicates the current helper instance is a standalone class instance.
*
* @var integer
* @since 3.6.3
*/
const MODE_INSTANCE = 2;
/**
* Singleton instance.
*
* @var array
* @since 3.6.3
*/
private static $instance;
/**
* Available user groups
*
* @var array
* @since 3.6.3
*/
private $groups = array();
/**
* Mode this class is working: singleton or std instance
*
* @var integer
* @since 3.6.3
*/
private $mode;
/**
* Total available groups
*
* @var integer
* @since 3.6.3
*/
private $total;
/**
* Constructor
*
* @param array $groups Array of groups
* @param integer $mode Working mode for this class
*
* @since 3.6.3
*/
public function __construct(array $groups = array(), $mode = self::MODE_INSTANCE)
{
$this->mode = (int) $mode;
if ($groups)
{
$this->setGroups($groups);
}
}
/**
* Count loaded user groups.
*
* @return integer
*
* @since 3.6.3
*/
public function count()
{
return count($this->groups);
}
/**
* Get the helper instance.
*
* @return self
*
* @since 3.6.3
*/
public static function getInstance()
{
if (static::$instance === null)
{
// Only here to avoid code style issues...
$groups = array();
static::$instance = new static($groups, static::MODE_SINGLETON);
}
return static::$instance;
}
/**
* Get a user group by its id.
*
* @param integer $id Group identifier
*
* @return mixed stdClass on success. False otherwise
*
* @since 3.6.3
*/
public function get($id)
{
if ($this->has($id))
{
return $this->groups[$id];
}
// Singleton will load groups as they are requested
if ($this->isSingleton())
{
$this->groups[$id] = $this->load($id);
return $this->groups[$id];
}
return false;
}
/**
* Get the list of existing user groups.
*
* @return array
*
* @since 3.6.3
*/
public function getAll()
{
if ($this->isSingleton() && $this->total() !== $this->count())
{
$this->loadAll();
}
return $this->groups;
}
/**
* Check if a group is in the list.
*
* @param integer $id Group identifier
*
* @return boolean
*
* @since 3.6.3
*/
public function has($id)
{
return (array_key_exists($id, $this->groups) && $this->groups[$id] !== false);
}
/**
* Check if this instance is a singleton.
*
* @return boolean
*
* @since 3.6.3
*/
private function isSingleton()
{
return $this->mode === static::MODE_SINGLETON;
}
/**
* Get total available user groups in database.
*
* @return integer
*
* @since 3.6.3
*/
public function total()
{
if ($this->total === null)
{
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('count(id)')
->from('#__usergroups');
$db->setQuery($query);
$this->total = (int) $db->loadResult();
}
return $this->total;
}
/**
* Load a group from database.
*
* @param integer $id Group identifier
*
* @return mixed
*
* @since 3.6.3
*/
public function load($id)
{
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__usergroups')
->where('id = ' . (int) $id);
$db->setQuery($query);
$group = $db->loadObject();
if (!$group)
{
return false;
}
return $this->populateGroupData($group);
}
/**
* Load all user groups from the database.
*
* @return self
*
* @since 3.6.3
*/
public function loadAll()
{
$this->groups = array();
$db = \JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__usergroups')
->order('lft ASC');
$db->setQuery($query);
$groups = $db->loadObjectList('id');
$this->groups = $groups ?: array();
$this->populateGroupsData();
return $this;
}
/**
* Populates extra information for groups.
*
* @return array
*
* @since 3.6.3
*/
private function populateGroupsData()
{
foreach ($this->groups as $group)
{
$this->populateGroupData($group);
}
return $this->groups;
}
/**
* Populate data for a specific user group.
*
* @param \stdClass $group Group
*
* @return \stdClass
*
* @since 3.6.3
*/
public function populateGroupData($group)
{
if (!$group || property_exists($group, 'path'))
{
return $group;
}
$parentId = (int) $group->parent_id;
if ($parentId === 0)
{
$group->path = array($group->id);
$group->level = 0;
return $group;
}
$parentGroup = $this->has($parentId) ? $this->get($parentId) : $this->load($parentId);
if (!property_exists($parentGroup, 'path'))
{
$parentGroup = $this->populateGroupData($parentGroup);
}
$group->path = array_merge($parentGroup->path, array($group->id));
$group->level = count($group->path) - 1;
return $group;
}
/**
* Set the groups to be used as source.
*
* @param array $groups Array of user groups.
*
* @return self
*
* @since 3.6.3
*/
public function setGroups(array $groups)
{
$this->groups = $groups;
$this->populateGroupsData();
$this->total = count($groups);
return $this;
}
}
The same content is in 30 releases, Joomla 3.9.15-rc to 3.10.0-alpha1. See them all, or every version of this file.