[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/Model/ -> LevelsModel.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\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  17  use Joomla\CMS\MVC\Model\ListModel;
  18  use Joomla\CMS\Table\Table;
  19  use Joomla\Database\DatabaseQuery;
  20  use Joomla\Database\ParameterType;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Methods supporting a list of user access level records.
  28   *
  29   * @since  1.6
  30   */
  31  class LevelsModel extends ListModel
  32  {
  33      /**
  34       * Override parent constructor.
  35       *
  36       * @param   array                $config   An optional associative array of configuration settings.
  37       * @param   MVCFactoryInterface  $factory  The factory.
  38       *
  39       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  40       * @since   3.2
  41       */
  42      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  43      {
  44          if (empty($config['filter_fields'])) {
  45              $config['filter_fields'] = array(
  46                  'id', 'a.id',
  47                  'title', 'a.title',
  48                  'ordering', 'a.ordering',
  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.ordering', $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       * Build an SQL query to load the list data.
  98       *
  99       * @return  DatabaseQuery
 100       */
 101      protected function getListQuery()
 102      {
 103          // Create a new query object.
 104          $db = $this->getDatabase();
 105          $query = $db->getQuery(true);
 106  
 107          // Select the required fields from the table.
 108          $query->select(
 109              $this->getState(
 110                  'list.select',
 111                  'a.*'
 112              )
 113          );
 114          $query->from($db->quoteName('#__viewlevels') . ' AS a');
 115  
 116          // Add the level in the tree.
 117          $query->group('a.id, a.title, a.ordering, a.rules');
 118  
 119          // Filter the items over the search string if set.
 120          $search = $this->getState('filter.search');
 121  
 122          if (!empty($search)) {
 123              if (stripos($search, 'id:') === 0) {
 124                  $ids = (int) substr($search, 3);
 125                  $query->where($db->quoteName('a.id') . ' = :id');
 126                  $query->bind(':id', $ids, ParameterType::INTEGER);
 127              } else {
 128                  $search = '%' . trim($search) . '%';
 129                  $query->where('a.title LIKE :title')
 130                      ->bind(':title', $search);
 131              }
 132          }
 133  
 134          $query->group('a.id');
 135  
 136          // Add the list ordering clause.
 137          $query->order($db->escape($this->getState('list.ordering', 'a.ordering')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 138  
 139          return $query;
 140      }
 141  
 142      /**
 143       * Method to adjust the ordering of a row.
 144       *
 145       * @param   integer  $pk         The ID of the primary key to move.
 146       * @param   integer  $direction  Increment, usually +1 or -1
 147       *
 148       * @return  boolean  False on failure or error, true otherwise.
 149       */
 150      public function reorder($pk, $direction = 0)
 151      {
 152          // Sanitize the id and adjustment.
 153          $pk = (!empty($pk)) ? $pk : (int) $this->getState('level.id');
 154          $user = Factory::getUser();
 155  
 156          // Get an instance of the record's table.
 157          $table = Table::getInstance('ViewLevel', 'Joomla\\CMS\Table\\');
 158  
 159          // Load the row.
 160          if (!$table->load($pk)) {
 161              $this->setError($table->getError());
 162  
 163              return false;
 164          }
 165  
 166          // Access checks.
 167          $allow = $user->authorise('core.edit.state', 'com_users');
 168  
 169          if (!$allow) {
 170              $this->setError(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
 171  
 172              return false;
 173          }
 174  
 175          // Move the row.
 176          // @todo: Where clause to restrict category.
 177          $table->move($pk);
 178  
 179          return true;
 180      }
 181  
 182      /**
 183       * Saves the manually set order of records.
 184       *
 185       * @param   array    $pks    An array of primary key ids.
 186       * @param   integer  $order  Order position
 187       *
 188       * @return  boolean  Boolean true on success, boolean false
 189       *
 190       * @throws  \Exception
 191       */
 192      public function saveorder($pks, $order)
 193      {
 194          $table = Table::getInstance('viewlevel', 'Joomla\\CMS\Table\\');
 195          $user = Factory::getUser();
 196          $conditions = array();
 197  
 198          if (empty($pks)) {
 199              Factory::getApplication()->enqueueMessage(Text::_('COM_USERS_ERROR_LEVELS_NOLEVELS_SELECTED'), 'error');
 200  
 201              return false;
 202          }
 203  
 204          // Update ordering values
 205          foreach ($pks as $i => $pk) {
 206              $table->load((int) $pk);
 207  
 208              // Access checks.
 209              $allow = $user->authorise('core.edit.state', 'com_users');
 210  
 211              if (!$allow) {
 212                  // Prune items that you can't change.
 213                  unset($pks[$i]);
 214                  Factory::getApplication()->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), 'error');
 215              } elseif ($table->ordering != $order[$i]) {
 216                  $table->ordering = $order[$i];
 217  
 218                  if (!$table->store()) {
 219                      $this->setError($table->getError());
 220  
 221                      return false;
 222                  }
 223              }
 224          }
 225  
 226          // Execute reorder for each category.
 227          foreach ($conditions as $cond) {
 228              $table->load($cond[0]);
 229              $table->reorder($cond[1]);
 230          }
 231  
 232          return true;
 233      }
 234  }


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