[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/Model/ -> GroupsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Users\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Helper\UserGroupsHelper;
  15  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Database\DatabaseQuery;
  18  use Joomla\Database\ParameterType;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Methods supporting a list of user group records.
  26   *
  27   * @since  1.6
  28   */
  29  class GroupsModel extends ListModel
  30  {
  31      /**
  32       * Override parent constructor.
  33       *
  34       * @param   array                $config   An optional associative array of configuration settings.
  35       * @param   MVCFactoryInterface  $factory  The factory.
  36       *
  37       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  38       * @since   3.2
  39       */
  40      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  41      {
  42          if (empty($config['filter_fields'])) {
  43              $config['filter_fields'] = array(
  44                  'id', 'a.id',
  45                  'parent_id', 'a.parent_id',
  46                  'title', 'a.title',
  47                  'lft', 'a.lft',
  48                  'rgt', 'a.rgt',
  49              );
  50          }
  51  
  52          parent::__construct($config, $factory);
  53      }
  54  
  55      /**
  56       * Method to auto-populate the model state.
  57       *
  58       * Note. Calling getState in this method will result in recursion.
  59       *
  60       * @param   string  $ordering   An optional ordering field.
  61       * @param   string  $direction  An optional direction (asc|desc).
  62       *
  63       * @return  void
  64       *
  65       * @since   1.6
  66       */
  67      protected function populateState($ordering = 'a.lft', $direction = 'asc')
  68      {
  69          // Load the parameters.
  70          $params = ComponentHelper::getParams('com_users');
  71          $this->setState('params', $params);
  72  
  73          // List state information.
  74          parent::populateState($ordering, $direction);
  75      }
  76  
  77      /**
  78       * Method to get a store id based on model configuration state.
  79       *
  80       * This is necessary because the model is used by the component and
  81       * different modules that might need different sets of data or different
  82       * ordering requirements.
  83       *
  84       * @param   string  $id  A prefix for the store id.
  85       *
  86       * @return  string  A store id.
  87       */
  88      protected function getStoreId($id = '')
  89      {
  90          // Compile the store id.
  91          $id .= ':' . $this->getState('filter.search');
  92  
  93          return parent::getStoreId($id);
  94      }
  95  
  96      /**
  97       * Gets the list of groups and adds expensive joins to the result set.
  98       *
  99       * @return  mixed  An array of data items on success, false on failure.
 100       *
 101       * @since   1.6
 102       */
 103      public function getItems()
 104      {
 105          // Get a storage key.
 106          $store = $this->getStoreId();
 107  
 108          // Try to load the data from internal storage.
 109          if (empty($this->cache[$store])) {
 110              $items = parent::getItems();
 111  
 112              // Bail out on an error or empty list.
 113              if (empty($items)) {
 114                  $this->cache[$store] = $items;
 115  
 116                  return $items;
 117              }
 118  
 119              try {
 120                  $items = $this->populateExtraData($items);
 121              } catch (\RuntimeException $e) {
 122                  $this->setError($e->getMessage());
 123  
 124                  return false;
 125              }
 126  
 127              // Add the items to the internal cache.
 128              $this->cache[$store] = $items;
 129          }
 130  
 131          return $this->cache[$store];
 132      }
 133  
 134      /**
 135       * Build an SQL query to load the list data.
 136       *
 137       * @return  DatabaseQuery
 138       */
 139      protected function getListQuery()
 140      {
 141          // Create a new query object.
 142          $db = $this->getDatabase();
 143          $query = $db->getQuery(true);
 144  
 145          // Select the required fields from the table.
 146          $query->select(
 147              $this->getState(
 148                  'list.select',
 149                  'a.*'
 150              )
 151          );
 152          $query->from($db->quoteName('#__usergroups') . ' AS a');
 153  
 154          // Filter the comments over the search string if set.
 155          $search = $this->getState('filter.search');
 156  
 157          if (!empty($search)) {
 158              if (stripos($search, 'id:') === 0) {
 159                  $ids = (int) substr($search, 3);
 160                  $query->where($db->quoteName('a.id') . ' = :id');
 161                  $query->bind(':id', $ids, ParameterType::INTEGER);
 162              } else {
 163                  $search = '%' . trim($search) . '%';
 164                  $query->where($db->quoteName('a.title') . ' LIKE :title');
 165                  $query->bind(':title', $search);
 166              }
 167          }
 168  
 169          // Add the list ordering clause.
 170          $query->order($db->escape($this->getState('list.ordering', 'a.lft')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 171  
 172          return $query;
 173      }
 174  
 175      /**
 176       * Populate level & path for items.
 177       *
 178       * @param   array  $items  Array of \stdClass objects
 179       *
 180       * @return  array
 181       *
 182       * @since   3.6.3
 183       */
 184      private function populateExtraData(array $items)
 185      {
 186          // First pass: get list of the group ids and reset the counts.
 187          $groupsByKey = array();
 188  
 189          foreach ($items as $item) {
 190              $groupsByKey[(int) $item->id] = $item;
 191          }
 192  
 193          $groupIds = array_keys($groupsByKey);
 194  
 195          $db = $this->getDatabase();
 196  
 197          // Get total enabled users in group.
 198          $query = $db->getQuery(true);
 199  
 200          // Count the objects in the user group.
 201          $query->select('map.group_id, COUNT(DISTINCT map.user_id) AS user_count')
 202              ->from($db->quoteName('#__user_usergroup_map', 'map'))
 203              ->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('map.user_id'))
 204              ->whereIn($db->quoteName('map.group_id'), $groupIds)
 205              ->where($db->quoteName('u.block') . ' = 0')
 206              ->group($db->quoteName('map.group_id'));
 207          $db->setQuery($query);
 208  
 209          try {
 210              $countEnabled = $db->loadAssocList('group_id', 'count_enabled');
 211          } catch (\RuntimeException $e) {
 212              $this->setError($e->getMessage());
 213  
 214              return false;
 215          }
 216  
 217          // Get total disabled users in group.
 218          $query->clear();
 219          $query->select('map.group_id, COUNT(DISTINCT map.user_id) AS user_count')
 220              ->from($db->quoteName('#__user_usergroup_map', 'map'))
 221              ->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('map.user_id'))
 222              ->whereIn($db->quoteName('map.group_id'), $groupIds)
 223              ->where($db->quoteName('u.block') . ' = 1')
 224              ->group($db->quoteName('map.group_id'));
 225          $db->setQuery($query);
 226  
 227          try {
 228              $countDisabled = $db->loadAssocList('group_id', 'count_disabled');
 229          } catch (\RuntimeException $e) {
 230              $this->setError($e->getMessage());
 231  
 232              return false;
 233          }
 234  
 235          // Inject the values back into the array.
 236          foreach ($groupsByKey as &$item) {
 237              $item->count_enabled   = isset($countEnabled[$item->id]) ? (int) $countEnabled[$item->id]['user_count'] : 0;
 238              $item->count_disabled  = isset($countDisabled[$item->id]) ? (int) $countDisabled[$item->id]['user_count'] : 0;
 239              $item->user_count      = $item->count_enabled + $item->count_disabled;
 240          }
 241  
 242          $groups = new UserGroupsHelper($groupsByKey);
 243  
 244          return array_values($groups->getAll());
 245      }
 246  }


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