Source for file groups.php

Documentation is available at groups.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_users
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Methods supporting a list of user group records.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_users
  17.  * @since       1.6
  18.  */
  19. class UsersModelGroups extends JModelList
  20. {
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param   array  An optional associative array of configuration settings.
  25.      * @see     JController
  26.      * @since   1.6
  27.      */
  28.     public function __construct($config array())
  29.     {
  30.         if (empty($config['filter_fields']))
  31.         {
  32.             $config['filter_fields'array(
  33.                 'id''a.id',
  34.                 'parent_id''a.parent_id',
  35.                 'title''a.title',
  36.                 'lft''a.lft',
  37.                 'rgt''a.rgt',
  38.             );
  39.         }
  40.  
  41.         parent::__construct($config);
  42.     }
  43.  
  44.     /**
  45.      * Method to auto-populate the model state.
  46.      *
  47.      * Note. Calling getState in this method will result in recursion.
  48.      *
  49.      * @since   1.6
  50.      */
  51.     protected function populateState($ordering null$direction null)
  52.     {
  53.         // Load the filter state.
  54.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  55.         $this->setState('filter.search'$search);
  56.  
  57.         // Load the parameters.
  58.         $params JComponentHelper::getParams('com_users');
  59.         $this->setState('params'$params);
  60.  
  61.         // List state information.
  62.         parent::populateState('a.lft''asc');
  63.     }
  64.  
  65.     /**
  66.      * Method to get a store id based on model configuration state.
  67.      *
  68.      * This is necessary because the model is used by the component and
  69.      * different modules that might need different sets of data or different
  70.      * ordering requirements.
  71.      *
  72.      * @param   string  $id    A prefix for the store id.
  73.      *
  74.      * @return  string  A store id.
  75.      */
  76.     protected function getStoreId($id '')
  77.     {
  78.         // Compile the store id.
  79.         $id .= ':' $this->getState('filter.search');
  80.         $id .= ':' $this->getState('filter.search');
  81.  
  82.         return parent::getStoreId($id);
  83.     }
  84.  
  85.     /**
  86.      * Gets the list of groups and adds expensive joins to the result set.
  87.      *
  88.      * @return  mixed  An array of data items on success, false on failure.
  89.      * @since   1.6
  90.      */
  91.     public function getItems()
  92.     {
  93.         $db $this->getDbo();
  94.         // Get a storage key.
  95.         $store $this->getStoreId();
  96.  
  97.         // Try to load the data from internal storage.
  98.         if (empty($this->cache[$store]))
  99.         {
  100.             $items parent::getItems();
  101.  
  102.             // Bail out on an error or empty list.
  103.             if (empty($items))
  104.             {
  105.                 $this->cache[$store$items;
  106.  
  107.                 return $items;
  108.             }
  109.  
  110.             // First pass: get list of the group id's and reset the counts.
  111.             $groupIds array();
  112.             foreach ($items as $item)
  113.             {
  114.                 $groupIds[= (int) $item->id;
  115.                 $item->user_count 0;
  116.             }
  117.  
  118.             // Get the counts from the database only for the users in the list.
  119.             $query $db->getQuery(true);
  120.  
  121.             // Count the objects in the user group.
  122.             $query->select('map.group_id, COUNT(DISTINCT map.user_id) AS user_count')
  123.                 ->from($db->quoteName('#__user_usergroup_map'' AS map')
  124.                 ->where('map.group_id IN (' implode(','$groupIds')')
  125.                 ->group('map.group_id');
  126.  
  127.             $db->setQuery($query);
  128.  
  129.             // Load the counts into an array indexed on the user id field.
  130.             try
  131.             {
  132.                 $users $db->loadObjectList('group_id');
  133.             }
  134.             catch (RuntimeException $e)
  135.             {
  136.                 $this->setError($e->getMessage);
  137.                 return false;
  138.             }
  139.  
  140.             // Second pass: collect the group counts into the master items array.
  141.             foreach ($items as &$item)
  142.             {
  143.                 if (isset($users[$item->id]))
  144.                 {
  145.                     $item->user_count $users[$item->id]->user_count;
  146.                 }
  147.             }
  148.  
  149.             // Add the items to the internal cache.
  150.             $this->cache[$store$items;
  151.         }
  152.  
  153.         return $this->cache[$store];
  154.     }
  155.  
  156.     /**
  157.      * Build an SQL query to load the list data.
  158.      *
  159.      * @return  JDatabaseQuery 
  160.      */
  161.     protected function getListQuery()
  162.     {
  163.         // Create a new query object.
  164.         $db $this->getDbo();
  165.         $query $db->getQuery(true);
  166.  
  167.         // Select the required fields from the table.
  168.         $query->select(
  169.             $this->getState(
  170.                 'list.select',
  171.                 'a.*'
  172.             )
  173.         );
  174.         $query->from($db->quoteName('#__usergroups'' AS a');
  175.  
  176.         // Add the level in the tree.
  177.         $query->select('COUNT(DISTINCT c2.id) AS level')
  178.             ->join('LEFT OUTER'$db->quoteName('#__usergroups'' AS c2 ON a.lft > c2.lft AND a.rgt < c2.rgt')
  179.             ->group('a.id, a.lft, a.rgt, a.parent_id, a.title');
  180.  
  181.         // Filter the comments over the search string if set.
  182.         $search $this->getState('filter.search');
  183.         if (!empty($search))
  184.         {
  185.             if (stripos($search'id:'=== 0)
  186.             {
  187.                 $query->where('a.id = ' . (int) substr($search3));
  188.             }
  189.             else
  190.             {
  191.                 $search $db->quote('%' $db->escape($searchtrue'%');
  192.                 $query->where('a.title LIKE ' $search);
  193.             }
  194.         }
  195.  
  196.         // Add the list ordering clause.
  197.         $query->order($db->escape($this->getState('list.ordering''a.lft')) ' ' $db->escape($this->getState('list.direction''ASC')));
  198.  
  199.         //echo nl2br(str_replace('#__','jos_',$query));
  200.         return $query;
  201.     }
  202. }

Documentation generated on Tue, 19 Nov 2013 15:04:11 +0100 by phpDocumentor 1.4.3