[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_plugins/src/Model/ -> PluginsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_plugins
   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\Plugins\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\Database\ParameterType;
  19  use Joomla\Utilities\ArrayHelper;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Methods supporting a list of plugin records.
  27   *
  28   * @since  1.6
  29   */
  30  class PluginsModel extends ListModel
  31  {
  32      /**
  33       * Constructor.
  34       *
  35       * @param   array                $config   An optional associative array of configuration settings.
  36       * @param   MVCFactoryInterface  $factory  The factory.
  37       *
  38       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  39       * @since   3.2
  40       */
  41      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  42      {
  43          if (empty($config['filter_fields'])) {
  44              $config['filter_fields'] = array(
  45                  'extension_id', 'a.extension_id',
  46                  'name', 'a.name',
  47                  'folder', 'a.folder',
  48                  'element', 'a.element',
  49                  'checked_out', 'a.checked_out',
  50                  'checked_out_time', 'a.checked_out_time',
  51                  'state', 'a.state',
  52                  'enabled', 'a.enabled',
  53                  'access', 'a.access', 'access_level',
  54                  'ordering', 'a.ordering',
  55                  'client_id', 'a.client_id',
  56              );
  57          }
  58  
  59          parent::__construct($config, $factory);
  60      }
  61  
  62      /**
  63       * Method to auto-populate the model state.
  64       *
  65       * Note. Calling getState in this method will result in recursion.
  66       *
  67       * @param   string  $ordering   An optional ordering field.
  68       * @param   string  $direction  An optional direction (asc|desc).
  69       *
  70       * @return  void
  71       *
  72       * @since   1.6
  73       */
  74      protected function populateState($ordering = 'folder', $direction = 'asc')
  75      {
  76          // Load the parameters.
  77          $params = ComponentHelper::getParams('com_plugins');
  78          $this->setState('params', $params);
  79  
  80          // List state information.
  81          parent::populateState($ordering, $direction);
  82      }
  83  
  84      /**
  85       * Method to get a store id based on model configuration state.
  86       *
  87       * This is necessary because the model is used by the component and
  88       * different modules that might need different sets of data or different
  89       * ordering requirements.
  90       *
  91       * @param   string  $id  A prefix for the store id.
  92       *
  93       * @return  string       A store id.
  94       */
  95      protected function getStoreId($id = '')
  96      {
  97          // Compile the store id.
  98          $id .= ':' . $this->getState('filter.search');
  99          $id .= ':' . $this->getState('filter.access');
 100          $id .= ':' . $this->getState('filter.enabled');
 101          $id .= ':' . $this->getState('filter.folder');
 102          $id .= ':' . $this->getState('filter.element');
 103  
 104          return parent::getStoreId($id);
 105      }
 106  
 107      /**
 108       * Returns an object list.
 109       *
 110       * @param   \Joomla\Database\DatabaseQuery  $query       A database query object.
 111       * @param   integer                         $limitstart  Offset.
 112       * @param   integer                         $limit       The number of records.
 113       *
 114       * @return  array
 115       */
 116      protected function _getList($query, $limitstart = 0, $limit = 0)
 117      {
 118          $search = $this->getState('filter.search');
 119          $ordering = $this->getState('list.ordering', 'ordering');
 120  
 121          // If "Sort Table By:" is not set, set ordering to name
 122          if ($ordering == '') {
 123              $ordering = 'name';
 124          }
 125  
 126          $db = $this->getDatabase();
 127  
 128          if ($ordering == 'name' || (!empty($search) && stripos($search, 'id:') !== 0)) {
 129              $db->setQuery($query);
 130              $result = $db->loadObjectList();
 131              $this->translate($result);
 132  
 133              if (!empty($search)) {
 134                  $escapedSearchString = $this->refineSearchStringToRegex($search, '/');
 135  
 136                  foreach ($result as $i => $item) {
 137                      if (!preg_match("/$escapedSearchString/i", $item->name)) {
 138                          unset($result[$i]);
 139                      }
 140                  }
 141              }
 142  
 143              $orderingDirection = strtolower($this->getState('list.direction'));
 144              $direction         = ($orderingDirection == 'desc') ? -1 : 1;
 145              $result = ArrayHelper::sortObjects($result, $ordering, $direction, true, true);
 146  
 147              $total = count($result);
 148              $this->cache[$this->getStoreId('getTotal')] = $total;
 149  
 150              if ($total < $limitstart) {
 151                  $limitstart = 0;
 152              }
 153  
 154              $this->cache[$this->getStoreId('getStart')] = $limitstart;
 155  
 156              return array_slice($result, $limitstart, $limit ?: null);
 157          } else {
 158              if ($ordering == 'ordering') {
 159                  $query->order('a.folder ASC');
 160                  $ordering = 'a.ordering';
 161              }
 162  
 163              $query->order($db->quoteName($ordering) . ' ' . $this->getState('list.direction'));
 164  
 165              if ($ordering == 'folder') {
 166                  $query->order('a.ordering ASC');
 167              }
 168  
 169              $result = parent::_getList($query, $limitstart, $limit);
 170              $this->translate($result);
 171  
 172              return $result;
 173          }
 174      }
 175  
 176      /**
 177       * Translate a list of objects.
 178       *
 179       * @param   array  &$items  The array of objects.
 180       *
 181       * @return  array The array of translated objects.
 182       */
 183      protected function translate(&$items)
 184      {
 185          $lang = Factory::getLanguage();
 186  
 187          foreach ($items as &$item) {
 188              $source = JPATH_PLUGINS . '/' . $item->folder . '/' . $item->element;
 189              $extension = 'plg_' . $item->folder . '_' . $item->element;
 190              $lang->load($extension . '.sys', JPATH_ADMINISTRATOR)
 191                  || $lang->load($extension . '.sys', $source);
 192              $item->name = Text::_($item->name);
 193          }
 194      }
 195  
 196      /**
 197       * Build an SQL query to load the list data.
 198       *
 199       * @return  \Joomla\Database\DatabaseQuery
 200       */
 201      protected function getListQuery()
 202      {
 203          // Create a new query object.
 204          $db = $this->getDatabase();
 205          $query = $db->getQuery(true);
 206  
 207          // Select the required fields from the table.
 208          $query->select(
 209              $this->getState(
 210                  'list.select',
 211                  'a.extension_id , a.name, a.element, a.folder, a.checked_out, a.checked_out_time,' .
 212                      ' a.enabled, a.access, a.ordering, a.note'
 213              )
 214          )
 215              ->from($db->quoteName('#__extensions') . ' AS a')
 216              ->where($db->quoteName('type') . ' = ' . $db->quote('plugin'));
 217  
 218          // Join over the users for the checked out user.
 219          $query->select('uc.name AS editor')
 220              ->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
 221  
 222          // Join over the asset groups.
 223          $query->select('ag.title AS access_level')
 224              ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
 225  
 226          // Filter by access level.
 227          if ($access = $this->getState('filter.access')) {
 228              $access = (int) $access;
 229              $query->where($db->quoteName('a.access') . ' = :access')
 230                  ->bind(':access', $access, ParameterType::INTEGER);
 231          }
 232  
 233          // Filter by published state.
 234          $published = (string) $this->getState('filter.enabled');
 235  
 236          if (is_numeric($published)) {
 237              $published = (int) $published;
 238              $query->where($db->quoteName('a.enabled') . ' = :published')
 239                  ->bind(':published', $published, ParameterType::INTEGER);
 240          } elseif ($published === '') {
 241              $query->whereIn($db->quoteName('a.enabled'), [0, 1]);
 242          }
 243  
 244          // Filter by state.
 245          $query->where('a.state >= 0');
 246  
 247          // Filter by folder.
 248          if ($folder = $this->getState('filter.folder')) {
 249              $query->where($db->quoteName('a.folder') . ' = :folder')
 250                  ->bind(':folder', $folder);
 251          }
 252  
 253          // Filter by element.
 254          if ($element = $this->getState('filter.element')) {
 255              $query->where($db->quoteName('a.element') . ' = :element')
 256                  ->bind(':element', $element);
 257          }
 258  
 259          // Filter by search in name or id.
 260          $search = $this->getState('filter.search');
 261  
 262          if (!empty($search)) {
 263              if (stripos($search, 'id:') === 0) {
 264                  $ids = (int) substr($search, 3);
 265                  $query->where($db->quoteName('a.extension_id') . ' = :id');
 266                  $query->bind(':id', $ids, ParameterType::INTEGER);
 267              }
 268          }
 269  
 270          return $query;
 271      }
 272  
 273      /**
 274       * Method to get the data that should be injected in the form.
 275       *
 276       * @return  mixed   The data for the form.
 277       *
 278       * @since   3.5
 279       */
 280      protected function loadFormData()
 281      {
 282          $data = parent::loadFormData();
 283  
 284          // Set the selected filter values for pages that use the Layouts for filtering
 285          $data->list['sortTable'] = $this->state->get('list.ordering');
 286          $data->list['directionTable'] = $this->state->get('list.direction');
 287  
 288          return $data;
 289      }
 290  }


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