[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_users/models/ -> groups.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  defined('_JEXEC') or die;
   8  
   9  jimport('joomla.application.component.modellist');
  10  
  11  /**
  12   * Methods supporting a list of user group records.
  13   *
  14   * @package        Joomla.Administrator
  15   * @subpackage    com_users
  16   * @since        1.6
  17   */
  18  class UsersModelGroups extends JModelList
  19  {
  20      /**
  21       * Constructor.
  22       *
  23       * @param    array    An optional associative array of configuration settings.
  24       * @see        JController
  25       * @since    1.6
  26       */
  27  	public function __construct($config = array())
  28      {
  29          if (empty($config['filter_fields'])) {
  30              $config['filter_fields'] = array(
  31                  'id', 'a.id',
  32                  'parent_id', 'a.parent_id',
  33                  'title', 'a.title',
  34                  'lft', 'a.lft',
  35                  'rgt', 'a.rgt',
  36              );
  37          }
  38  
  39          parent::__construct($config);
  40      }
  41  
  42      /**
  43       * Method to auto-populate the model state.
  44       *
  45       * Note. Calling getState in this method will result in recursion.
  46       *
  47       * @since    1.6
  48       */
  49  	protected function populateState($ordering = null, $direction = null)
  50      {
  51          // Initialise variables.
  52          $app = JFactory::getApplication('administrator');
  53  
  54          // Load the filter state.
  55          $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
  56          $this->setState('filter.search', $search);
  57  
  58          // Load the parameters.
  59          $params = JComponentHelper::getParams('com_users');
  60          $this->setState('params', $params);
  61  
  62          // List state information.
  63          parent::populateState('a.lft', 'asc');
  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              $items = parent::getItems();
 100  
 101  
 102              // Bail out on an error or empty list.
 103              if (empty($items)) {
 104                  $this->cache[$store] = $items;
 105  
 106                  return $items;
 107              }
 108  
 109              // First pass: get list of the group id's and reset the counts.
 110              $groupIds = array();
 111              foreach ($items as $item)
 112              {
 113                  $groupIds[] = (int) $item->id;
 114                  $item->user_count = 0;
 115              }
 116  
 117              // Get the counts from the database only for the users in the list.
 118              $query    = $db->getQuery(true);
 119  
 120              // Count the objects in the user group.
 121              $query->select('map.group_id, COUNT(DISTINCT map.user_id) AS user_count')
 122                  ->from($db->quoteName('#__user_usergroup_map').' AS map')
 123                  ->where('map.group_id IN ('.implode(',', $groupIds).')')
 124                  ->group('map.group_id');
 125  
 126              $db->setQuery($query);
 127  
 128              // Load the counts into an array indexed on the user id field.
 129              $users = $db->loadObjectList('group_id');
 130  
 131              $error = $db->getErrorMsg();
 132              if ($error) {
 133                  $this->setError($error);
 134  
 135                  return false;
 136              }
 137  
 138              // Second pass: collect the group counts into the master items array.
 139              foreach ($items as &$item)
 140              {
 141                  if (isset($users[$item->id])) {
 142                      $item->user_count = $users[$item->id]->user_count;
 143                  }
 144              }
 145  
 146              // Add the items to the internal cache.
 147              $this->cache[$store] = $items;
 148          }
 149  
 150          return $this->cache[$store];
 151      }
 152  
 153      /**
 154       * Build an SQL query to load the list data.
 155       *
 156       * @return    JDatabaseQuery
 157       */
 158  	protected function getListQuery()
 159      {
 160          // Create a new query object.
 161          $db        = $this->getDbo();
 162          $query    = $db->getQuery(true);
 163  
 164          // Select the required fields from the table.
 165          $query->select(
 166              $this->getState(
 167                  'list.select',
 168                  'a.*'
 169              )
 170          );
 171          $query->from($db->quoteName('#__usergroups').' AS a');
 172  
 173          // Add the level in the tree.
 174          $query->select('COUNT(DISTINCT c2.id) AS level');
 175          $query->join('LEFT OUTER', $db->quoteName('#__usergroups').' AS c2 ON a.lft > c2.lft AND a.rgt < c2.rgt');
 176          $query->group('a.id, a.lft, a.rgt, a.parent_id, a.title');
 177  
 178          // Filter the comments over the search string if set.
 179          $search = $this->getState('filter.search');
 180          if (!empty($search)) {
 181              if (stripos($search, 'id:') === 0) {
 182                  $query->where('a.id = '.(int) substr($search, 3));
 183              } else {
 184                  $search = $db->Quote('%'.$db->escape($search, true).'%');
 185                  $query->where('a.title LIKE '.$search);
 186              }
 187          }
 188  
 189          // Add the list ordering clause.
 190          $query->order($db->escape($this->getState('list.ordering', 'a.lft')).' '.$db->escape($this->getState('list.direction', 'ASC')));
 191  
 192          //echo nl2br(str_replace('#__','jos_',$query));
 193          return $query;
 194      }
 195  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1