[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_modules/src/Model/ -> PositionsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_modules
   6   *
   7   * @copyright   (C) 2010 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\Modules\Administrator\Model;
  12  
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Filesystem\Path;
  17  use Joomla\CMS\MVC\Model\ListModel;
  18  use Joomla\Component\Modules\Administrator\Helper\ModulesHelper;
  19  use Joomla\Database\ParameterType;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Modules Component Positions Model
  27   *
  28   * @since  1.6
  29   */
  30  class PositionsModel extends ListModel
  31  {
  32      /**
  33       * Constructor.
  34       *
  35       * @param   array  $config  An optional associative array of configuration settings.
  36       *
  37       * @see     \JController
  38       * @since   1.6
  39       */
  40      public function __construct($config = array())
  41      {
  42          if (empty($config['filter_fields'])) {
  43              $config['filter_fields'] = array(
  44                  'value',
  45                  'templates',
  46              );
  47          }
  48  
  49          parent::__construct($config);
  50      }
  51  
  52      /**
  53       * Method to auto-populate the model state.
  54       *
  55       * Note. Calling getState in this method will result in recursion.
  56       *
  57       * @param   string  $ordering   An optional ordering field.
  58       * @param   string  $direction  An optional direction (asc|desc).
  59       *
  60       * @return  void
  61       *
  62       * @since   1.6
  63       */
  64      protected function populateState($ordering = 'ordering', $direction = 'asc')
  65      {
  66          // Load the filter state.
  67          $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  68          $this->setState('filter.search', $search);
  69  
  70          $state = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_state', '', 'string');
  71          $this->setState('filter.state', $state);
  72  
  73          $template = $this->getUserStateFromRequest($this->context . '.filter.template', 'filter_template', '', 'string');
  74          $this->setState('filter.template', $template);
  75  
  76          $type = $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string');
  77          $this->setState('filter.type', $type);
  78  
  79          // Special case for the client id.
  80          $clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
  81          $clientId = (!in_array((int) $clientId, array (0, 1))) ? 0 : (int) $clientId;
  82          $this->setState('client_id', $clientId);
  83  
  84          // Load the parameters.
  85          $params = ComponentHelper::getParams('com_modules');
  86          $this->setState('params', $params);
  87  
  88          // List state information.
  89          parent::populateState($ordering, $direction);
  90      }
  91  
  92      /**
  93       * Method to get an array of data items.
  94       *
  95       * @return  mixed  An array of data items on success, false on failure.
  96       *
  97       * @since   1.6
  98       */
  99      public function getItems()
 100      {
 101          if (!isset($this->items)) {
 102              $lang            = Factory::getLanguage();
 103              $search          = $this->getState('filter.search');
 104              $state           = $this->getState('filter.state');
 105              $clientId        = $this->getState('client_id');
 106              $filter_template = $this->getState('filter.template');
 107              $type            = $this->getState('filter.type');
 108              $ordering        = $this->getState('list.ordering');
 109              $direction       = $this->getState('list.direction');
 110              $limitstart      = $this->getState('list.start');
 111              $limit           = $this->getState('list.limit');
 112              $client          = ApplicationHelper::getClientInfo($clientId);
 113  
 114              if ($type != 'template') {
 115                  $clientId = (int) $clientId;
 116  
 117                  // Get the database object and a new query object.
 118                  $db  = $this->getDatabase();
 119                  $query = $db->getQuery(true)
 120                      ->select('DISTINCT ' . $db->quoteName('position', 'value'))
 121                      ->from($db->quoteName('#__modules'))
 122                      ->where($db->quoteName('client_id') . ' = :clientid')
 123                      ->bind(':clientid', $clientId, ParameterType::INTEGER);
 124  
 125                  if ($search) {
 126                      $search = '%' . str_replace(' ', '%', trim($search), true) . '%';
 127                      $query->where($db->quoteName('position') . ' LIKE :position')
 128                          ->bind(':position', $search);
 129                  }
 130  
 131                  $db->setQuery($query);
 132  
 133                  try {
 134                      $positions = $db->loadObjectList('value');
 135                  } catch (\RuntimeException $e) {
 136                      $this->setError($e->getMessage());
 137  
 138                      return false;
 139                  }
 140  
 141                  foreach ($positions as $value => $position) {
 142                      $positions[$value] = array();
 143                  }
 144              } else {
 145                  $positions = array();
 146              }
 147  
 148              // Load the positions from the installed templates.
 149              foreach (ModulesHelper::getTemplates($clientId) as $template) {
 150                  $path = Path::clean($client->path . '/templates/' . $template->element . '/templateDetails.xml');
 151  
 152                  if (file_exists($path)) {
 153                      $xml = simplexml_load_file($path);
 154  
 155                      if (isset($xml->positions[0])) {
 156                          $lang->load('tpl_' . $template->element . '.sys', $client->path)
 157                          || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element);
 158  
 159                          foreach ($xml->positions[0] as $position) {
 160                              $value = (string) $position['value'];
 161                              $label = (string) $position;
 162  
 163                              if (!$value) {
 164                                  $value = $label;
 165                                  $label = preg_replace('/[^a-zA-Z0-9_\-]/', '_', 'TPL_' . $template->element . '_POSITION_' . $value);
 166                                  $altlabel = preg_replace('/[^a-zA-Z0-9_\-]/', '_', 'COM_MODULES_POSITION_' . $value);
 167  
 168                                  if (!$lang->hasKey($label) && $lang->hasKey($altlabel)) {
 169                                      $label = $altlabel;
 170                                  }
 171                              }
 172  
 173                              if ($type == 'user' || ($state != '' && $state != $template->enabled)) {
 174                                  unset($positions[$value]);
 175                              } elseif (preg_match(chr(1) . $search . chr(1) . 'i', $value) && ($filter_template == '' || $filter_template == $template->element)) {
 176                                  if (!isset($positions[$value])) {
 177                                      $positions[$value] = array();
 178                                  }
 179  
 180                                  $positions[$value][$template->name] = $label;
 181                              }
 182                          }
 183                      }
 184                  }
 185              }
 186  
 187              $this->total = count($positions);
 188  
 189              if ($limitstart >= $this->total) {
 190                  $limitstart = $limitstart < $limit ? 0 : $limitstart - $limit;
 191                  $this->setState('list.start', $limitstart);
 192              }
 193  
 194              if ($ordering == 'value') {
 195                  if ($direction == 'asc') {
 196                      ksort($positions);
 197                  } else {
 198                      krsort($positions);
 199                  }
 200              } else {
 201                  if ($direction == 'asc') {
 202                      asort($positions);
 203                  } else {
 204                      arsort($positions);
 205                  }
 206              }
 207  
 208              $this->items = array_slice($positions, $limitstart, $limit ?: null);
 209          }
 210  
 211          return $this->items;
 212      }
 213  
 214      /**
 215       * Method to get the total number of items.
 216       *
 217       * @return  integer  The total number of items.
 218       *
 219       * @since   1.6
 220       */
 221      public function getTotal()
 222      {
 223          if (!isset($this->total)) {
 224              $this->getItems();
 225          }
 226  
 227          return $this->total;
 228      }
 229  }


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