[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_templates/src/Model/ -> StylesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_templates
   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\Templates\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Database\ParameterType;
  18  use Joomla\String\StringHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Methods supporting a list of template style records.
  26   *
  27   * @since  1.6
  28   */
  29  class StylesModel extends ListModel
  30  {
  31      /**
  32       * Constructor.
  33       *
  34       * @param   array                $config   An optional associative array of configuration settings.
  35       * @param   MVCFactoryInterface  $factory  The factory.
  36       *
  37       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  38       * @since   3.2
  39       */
  40      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  41      {
  42          if (empty($config['filter_fields'])) {
  43              $config['filter_fields'] = array(
  44                  'id', 'a.id',
  45                  'title', 'a.title',
  46                  'template', 'a.template',
  47                  'home', 'a.home',
  48                  'menuitem',
  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.template', $direction = 'asc')
  68      {
  69          $app = Factory::getApplication();
  70  
  71          if (!$app->isClient('api')) {
  72              // Load the filter state.
  73              $this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
  74              $this->setState('filter.template', $this->getUserStateFromRequest($this->context . '.filter.template', 'filter_template', '', 'string'));
  75              $this->setState('filter.menuitem', $this->getUserStateFromRequest($this->context . '.filter.menuitem', 'filter_menuitem', '', 'cmd'));
  76  
  77              // Special case for the client id.
  78              $clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
  79              $clientId = !in_array($clientId, [0, 1]) ? 0 : $clientId;
  80              $this->setState('client_id', $clientId);
  81          }
  82  
  83          // Load the parameters.
  84          $params = ComponentHelper::getParams('com_templates');
  85          $this->setState('params', $params);
  86  
  87          // List state information.
  88          parent::populateState($ordering, $direction);
  89      }
  90  
  91      /**
  92       * Method to get a store id based on model configuration state.
  93       *
  94       * This is necessary because the model is used by the component and
  95       * different modules that might need different sets of data or different
  96       * ordering requirements.
  97       *
  98       * @param   string  $id  A prefix for the store id.
  99       *
 100       * @return  string  A store id.
 101       */
 102      protected function getStoreId($id = '')
 103      {
 104          // Compile the store id.
 105          $id .= ':' . $this->getState('client_id');
 106          $id .= ':' . $this->getState('filter.search');
 107          $id .= ':' . $this->getState('filter.template');
 108          $id .= ':' . $this->getState('filter.menuitem');
 109  
 110          return parent::getStoreId($id);
 111      }
 112  
 113      /**
 114       * Build an SQL query to load the list data.
 115       *
 116       * @return  \Joomla\Database\DatabaseQuery
 117       */
 118      protected function getListQuery()
 119      {
 120          $clientId = (int) $this->getState('client_id');
 121  
 122          // Create a new query object.
 123          $db = $this->getDatabase();
 124          $query = $db->getQuery(true);
 125  
 126          // Select the required fields from the table.
 127          $query->select(
 128              $this->getState(
 129                  'list.select',
 130                  [
 131                      $db->quoteName('a.id'),
 132                      $db->quoteName('a.template'),
 133                      $db->quoteName('a.title'),
 134                      $db->quoteName('a.home'),
 135                      $db->quoteName('a.client_id'),
 136                      $db->quoteName('l.title', 'language_title'),
 137                      $db->quoteName('l.image'),
 138                      $db->quoteName('l.sef', 'language_sef'),
 139                  ]
 140              )
 141          )
 142              ->select(
 143                  [
 144                      'COUNT(' . $db->quoteName('m.template_style_id') . ') AS assigned',
 145                      $db->quoteName('extension_id', 'e_id'),
 146                  ]
 147              )
 148              ->from($db->quoteName('#__template_styles', 'a'))
 149              ->where($db->quoteName('a.client_id') . ' = :clientid')
 150              ->bind(':clientid', $clientId, ParameterType::INTEGER);
 151  
 152          // Join on menus.
 153          $query->join('LEFT', $db->quoteName('#__menu', 'm'), $db->quoteName('m.template_style_id') . ' = ' . $db->quoteName('a.id'))
 154              ->group(
 155                  [
 156                      $db->quoteName('a.id'),
 157                      $db->quoteName('a.template'),
 158                      $db->quoteName('a.title'),
 159                      $db->quoteName('a.home'),
 160                      $db->quoteName('a.client_id'),
 161                      $db->quoteName('l.title'),
 162                      $db->quoteName('l.image'),
 163                      $db->quoteName('l.sef'),
 164                      $db->quoteName('e.extension_id'),
 165                  ]
 166              );
 167  
 168          // Join over the language.
 169          $query->join('LEFT', $db->quoteName('#__languages', 'l'), $db->quoteName('l.lang_code') . ' = ' . $db->quoteName('a.home'));
 170  
 171          // Filter by extension enabled.
 172          $query->join(
 173              'LEFT',
 174              $db->quoteName('#__extensions', 'e'),
 175              $db->quoteName('e.element') . ' = ' . $db->quoteName('a.template')
 176                  . ' AND ' . $db->quoteName('e.client_id') . ' = ' . $db->quoteName('a.client_id')
 177          )
 178              ->where(
 179                  [
 180                      $db->quoteName('e.enabled') . ' = 1',
 181                      $db->quoteName('e.type') . ' = ' . $db->quote('template'),
 182                  ]
 183              );
 184  
 185          // Filter by template.
 186          if ($template = $this->getState('filter.template')) {
 187              $query->where($db->quoteName('a.template') . ' = :template')
 188                  ->bind(':template', $template);
 189          }
 190  
 191          // Filter by menuitem.
 192          $menuItemId = $this->getState('filter.menuitem');
 193  
 194          if ($clientId === 0 && is_numeric($menuItemId)) {
 195              // If user selected the templates styles that are not assigned to any page.
 196              if ((int) $menuItemId === -1) {
 197                  // Only custom template styles overrides not assigned to any menu item.
 198                  $query->where(
 199                      [
 200                          $db->quoteName('a.home') . ' = ' . $db->quote('0'),
 201                          $db->quoteName('m.id') . ' IS NULL',
 202                      ]
 203                  );
 204              } else {
 205                  // If user selected the templates styles assigned to particular pages.
 206                  // Subquery to get the language of the selected menu item.
 207                  $menuItemId = (int) $menuItemId;
 208                  $menuItemLanguageSubQuery = $db->getQuery(true);
 209                  $menuItemLanguageSubQuery->select($db->quoteName('language'))
 210                      ->from($db->quoteName('#__menu'))
 211                      ->where($db->quoteName('id') . ' = :menuitemid');
 212                  $query->bind(':menuitemid', $menuItemId, ParameterType::INTEGER);
 213  
 214                  // Subquery to get the language of the selected menu item.
 215                  $templateStylesMenuItemsSubQuery = $db->getQuery(true);
 216                  $templateStylesMenuItemsSubQuery->select($db->quoteName('id'))
 217                      ->from($db->quoteName('#__menu'))
 218                      ->where($db->quoteName('template_style_id') . ' = ' . $db->quoteName('a.id'));
 219  
 220                  // Main query where clause.
 221                  $query->where('(' .
 222                      // Default template style (fallback template style to all menu items).
 223                      $db->quoteName('a.home') . ' = ' . $db->quote('1') . ' OR ' .
 224                      // Default template style for specific language (fallback template style to the selected menu item language).
 225                      $db->quoteName('a.home') . ' IN (' . $menuItemLanguageSubQuery . ') OR ' .
 226                      // Custom template styles override (only if assigned to the selected menu item).
 227                      '(' . $db->quoteName('a.home') . ' = ' . $db->quote('0') . ' AND ' . $menuItemId . ' IN (' . $templateStylesMenuItemsSubQuery . '))' .
 228                      ')');
 229              }
 230          }
 231  
 232          // Filter by search in title.
 233          if ($search = $this->getState('filter.search')) {
 234              if (stripos($search, 'id:') === 0) {
 235                  $ids = (int) substr($search, 3);
 236                  $query->where($db->quoteName('a.id') . ' = :id');
 237                  $query->bind(':id', $ids, ParameterType::INTEGER);
 238              } else {
 239                  $search = '%' . StringHelper::strtolower($search) . '%';
 240                  $query->extendWhere(
 241                      'AND',
 242                      [
 243                          'LOWER(' . $db->quoteName('a.template') . ') LIKE :template',
 244                          'LOWER(' . $db->quoteName('a.title') . ') LIKE :title',
 245                      ],
 246                      'OR'
 247                  )
 248                      ->bind(':template', $search)
 249                      ->bind(':title', $search);
 250              }
 251          }
 252  
 253          // Add the list ordering clause.
 254          $query->order($db->escape($this->getState('list.ordering', 'a.template')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 255  
 256          return $query;
 257      }
 258  }


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