Source for file levels.php

Documentation is available at levels.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 access level records.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_users
  17.  * @since       1.6
  18.  */
  19. class UsersModelLevels 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.                 'title''a.title',
  35.                 'ordering''a.ordering',
  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.         // Load the filter state.
  52.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  53.         $this->setState('filter.search'$search);
  54.  
  55.         // Load the parameters.
  56.         $params JComponentHelper::getParams('com_users');
  57.         $this->setState('params'$params);
  58.  
  59.         // List state information.
  60.         parent::populateState('a.title''asc');
  61.     }
  62.  
  63.     /**
  64.      * Method to get a store id based on model configuration state.
  65.      *
  66.      * This is necessary because the model is used by the component and
  67.      * different modules that might need different sets of data or different
  68.      * ordering requirements.
  69.      *
  70.      * @param   string  $id    A prefix for the store id.
  71.      *
  72.      * @return  string  A store id.
  73.      */
  74.     protected function getStoreId($id '')
  75.     {
  76.         // Compile the store id.
  77.         $id .= ':' $this->getState('filter.search');
  78.  
  79.         return parent::getStoreId($id);
  80.     }
  81.  
  82.     /**
  83.      * Build an SQL query to load the list data.
  84.      *
  85.      * @return  JDatabaseQuery 
  86.      */
  87.     protected function getListQuery()
  88.     {
  89.         // Create a new query object.
  90.         $db $this->getDbo();
  91.         $query $db->getQuery(true);
  92.  
  93.         // Select the required fields from the table.
  94.         $query->select(
  95.             $this->getState(
  96.                 'list.select',
  97.                 'a.*'
  98.             )
  99.         );
  100.         $query->from($db->quoteName('#__viewlevels'' AS a');
  101.  
  102.         // Add the level in the tree.
  103.         $query->group('a.id, a.title, a.ordering, a.rules');
  104.  
  105.         // Filter the items over the search string if set.
  106.         $search $this->getState('filter.search');
  107.         if (!empty($search))
  108.         {
  109.             if (stripos($search'id:'=== 0)
  110.             {
  111.                 $query->where('a.id = ' . (int) substr($search3));
  112.             }
  113.             else
  114.             {
  115.                 $search $db->quote('%' $db->escape($searchtrue'%');
  116.                 $query->where('a.title LIKE ' $search);
  117.             }
  118.         }
  119.  
  120.         $query->group('a.id');
  121.  
  122.         // Add the list ordering clause.
  123.         $query->order($db->escape($this->getState('list.ordering''a.lft')) ' ' $db->escape($this->getState('list.direction''ASC')));
  124.  
  125.         //echo nl2br(str_replace('#__','jos_',$query));
  126.         return $query;
  127.     }
  128.  
  129.     /**
  130.      * Method to adjust the ordering of a row.
  131.      *
  132.      * @param   integer    The ID of the primary key to move.
  133.      * @param   integer    Increment, usually +1 or -1
  134.      * @return  boolean  False on failure or error, true otherwise.
  135.      */
  136.     public function reorder($pk$direction 0)
  137.     {
  138.         // Sanitize the id and adjustment.
  139.         $pk (!empty($pk)) $pk : (int) $this->getState('level.id');
  140.         $user JFactory::getUser();
  141.  
  142.         // Get an instance of the record's table.
  143.         $table JTable::getInstance('viewlevel');
  144.  
  145.         // Load the row.
  146.         if (!$table->load($pk))
  147.         {
  148.             $this->setError($table->getError());
  149.             return false;
  150.         }
  151.  
  152.         // Access checks.
  153.         $allow $user->authorise('core.edit.state''com_users');
  154.  
  155.         if (!$allow)
  156.         {
  157.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  158.             return false;
  159.         }
  160.  
  161.         // Move the row.
  162.         // TODO: Where clause to restrict category.
  163.         $table->move($pk);
  164.  
  165.         return true;
  166.     }
  167.  
  168.     /**
  169.      * Saves the manually set order of records.
  170.      *
  171.      * @param   array    An array of primary key ids.
  172.      * @param   integer  +/-1
  173.      */
  174.     public function saveorder($pks$order)
  175.     {
  176.         $table JTable::getInstance('viewlevel');
  177.         $user JFactory::getUser();
  178.         $conditions array();
  179.  
  180.         if (empty($pks))
  181.         {
  182.             return JError::raiseWarning(500JText::_('COM_USERS_ERROR_LEVELS_NOLEVELS_SELECTED'));
  183.         }
  184.  
  185.         // update ordering values
  186.         foreach ($pks as $i => $pk)
  187.         {
  188.             $table->load((int) $pk);
  189.  
  190.             // Access checks.
  191.             $allow $user->authorise('core.edit.state''com_users');
  192.  
  193.             if (!$allow)
  194.             {
  195.                 // Prune items that you can't change.
  196.                 unset($pks[$i]);
  197.                 JError::raiseWarning(403JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  198.             }
  199.             elseif ($table->ordering != $order[$i])
  200.             {
  201.                 $table->ordering $order[$i];
  202.                 if (!$table->store())
  203.                 {
  204.                     $this->setError($table->getError());
  205.                     return false;
  206.                 }
  207.             }
  208.         }
  209.  
  210.         // Execute reorder for each category.
  211.         foreach ($conditions as $cond)
  212.         {
  213.             $table->load($cond[0]);
  214.             $table->reorder($cond[1]);
  215.         }
  216.  
  217.         return true;
  218.     }
  219. }

Documentation generated on Tue, 19 Nov 2013 15:07:01 +0100 by phpDocumentor 1.4.3