[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_newsfeeds/src/Model/ -> NewsfeedsModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_newsfeeds
   6   *
   7   * @copyright   (C) 2008 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\Newsfeeds\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Associations;
  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 newsfeed records.
  27   *
  28   * @since  1.6
  29   */
  30  class NewsfeedsModel 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                  'id', 'a.id',
  46                  'name', 'a.name',
  47                  'alias', 'a.alias',
  48                  'checked_out', 'a.checked_out',
  49                  'checked_out_time', 'a.checked_out_time',
  50                  'catid', 'a.catid', 'category_id', 'category_title',
  51                  'published', 'a.published',
  52                  'access', 'a.access', 'access_level',
  53                  'created', 'a.created',
  54                  'created_by', 'a.created_by',
  55                  'ordering', 'a.ordering',
  56                  'language', 'a.language', 'language_title',
  57                  'publish_up', 'a.publish_up',
  58                  'publish_down', 'a.publish_down',
  59                  'cache_time', 'a.cache_time',
  60                  'numarticles',
  61                  'tag',
  62                  'level', 'c.level',
  63                  'tag',
  64              );
  65  
  66              if (Associations::isEnabled()) {
  67                  $config['filter_fields'][] = 'association';
  68              }
  69          }
  70  
  71          parent::__construct($config, $factory);
  72      }
  73  
  74      /**
  75       * Method to auto-populate the model state.
  76       *
  77       * Note. Calling getState in this method will result in recursion.
  78       *
  79       * @param   string  $ordering   An optional ordering field.
  80       * @param   string  $direction  An optional direction (asc|desc).
  81       *
  82       * @return  void
  83       *
  84       * @since   1.6
  85       */
  86      protected function populateState($ordering = 'a.name', $direction = 'asc')
  87      {
  88          $app = Factory::getApplication();
  89  
  90          $forcedLanguage = $app->input->get('forcedLanguage', '', 'cmd');
  91  
  92          // Adjust the context to support modal layouts.
  93          if ($layout = $app->input->get('layout')) {
  94              $this->context .= '.' . $layout;
  95          }
  96  
  97          // Adjust the context to support forced languages.
  98          if ($forcedLanguage) {
  99              $this->context .= '.' . $forcedLanguage;
 100          }
 101  
 102          // Load the parameters.
 103          $params = ComponentHelper::getParams('com_newsfeeds');
 104          $this->setState('params', $params);
 105  
 106          // List state information.
 107          parent::populateState($ordering, $direction);
 108  
 109          // Force a language.
 110          if (!empty($forcedLanguage)) {
 111              $this->setState('filter.language', $forcedLanguage);
 112          }
 113      }
 114  
 115      /**
 116       * Method to get a store id based on model configuration state.
 117       *
 118       * This is necessary because the model is used by the component and
 119       * different modules that might need different sets of data or different
 120       * ordering requirements.
 121       *
 122       * @param   string  $id  A prefix for the store id.
 123       *
 124       * @return  string  A store id.
 125       */
 126      protected function getStoreId($id = '')
 127      {
 128          // Compile the store id.
 129          $id .= ':' . $this->getState('filter.search');
 130          $id .= ':' . $this->getState('filter.published');
 131          $id .= ':' . $this->getState('filter.category_id');
 132          $id .= ':' . $this->getState('filter.access');
 133          $id .= ':' . $this->getState('filter.language');
 134          $id .= ':' . $this->getState('filter.level');
 135          $id .= ':' . serialize($this->getState('filter.tag'));
 136  
 137          return parent::getStoreId($id);
 138      }
 139  
 140      /**
 141       * Build an SQL query to load the list data.
 142       *
 143       * @return  \Joomla\Database\DatabaseQuery
 144       */
 145      protected function getListQuery()
 146      {
 147          // Create a new query object.
 148          $db    = $this->getDatabase();
 149          $query = $db->getQuery(true);
 150          $user  = Factory::getUser();
 151  
 152          // Select the required fields from the table.
 153          $query->select(
 154              $this->getState(
 155                  'list.select',
 156                  [
 157                      $db->quoteName('a.id'),
 158                      $db->quoteName('a.name'),
 159                      $db->quoteName('a.alias'),
 160                      $db->quoteName('a.checked_out'),
 161                      $db->quoteName('a.checked_out_time'),
 162                      $db->quoteName('a.catid'),
 163                      $db->quoteName('a.numarticles'),
 164                      $db->quoteName('a.cache_time'),
 165                      $db->quoteName('a.created_by'),
 166                      $db->quoteName('a.published'),
 167                      $db->quoteName('a.access'),
 168                      $db->quoteName('a.ordering'),
 169                      $db->quoteName('a.language'),
 170                      $db->quoteName('a.publish_up'),
 171                      $db->quoteName('a.publish_down'),
 172                  ]
 173              )
 174          )
 175              ->select(
 176                  [
 177                      $db->quoteName('l.title', 'language_title'),
 178                      $db->quoteName('l.image', 'language_image'),
 179                      $db->quoteName('uc.name', 'editor'),
 180                      $db->quoteName('ag.title', 'access_level'),
 181                      $db->quoteName('c.title', 'category_title'),
 182                  ]
 183              )
 184              ->from($db->quoteName('#__newsfeeds', 'a'))
 185              ->join('LEFT', $db->quoteName('#__languages', 'l'), $db->quoteName('l.lang_code') . ' = ' . $db->quoteName('a.language'))
 186              ->join('LEFT', $db->quoteName('#__users', 'uc'), $db->quoteName('uc.id') . ' = ' . $db->quoteName('a.checked_out'))
 187              ->join('LEFT', $db->quoteName('#__viewlevels', 'ag'), $db->quoteName('ag.id') . ' = ' . $db->quoteName('a.access'))
 188              ->join('LEFT', $db->quoteName('#__categories', 'c'), $db->quoteName('c.id') . ' = ' . $db->quoteName('a.catid'));
 189  
 190          // Join over the associations.
 191          if (Associations::isEnabled()) {
 192              $subQuery = $db->getQuery(true)
 193                  ->select('COUNT(' . $db->quoteName('asso1.id') . ') > 1')
 194                  ->from($db->quoteName('#__associations', 'asso1'))
 195                  ->join('INNER', $db->quoteName('#__associations', 'asso2'), $db->quoteName('asso1.key') . ' = ' . $db->quoteName('asso2.key'))
 196                  ->where(
 197                      [
 198                          $db->quoteName('asso1.id') . ' = ' . $db->quoteName('a.id'),
 199                          $db->quoteName('asso1.context') . ' = ' . $db->quote('com_newsfeeds.item'),
 200                      ]
 201                  );
 202  
 203              $query->select('(' . $subQuery . ') AS ' . $db->quoteName('association'));
 204          }
 205  
 206          // Filter by access level.
 207          if ($access = (int) $this->getState('filter.access')) {
 208              $query->where($db->quoteName('a.access') . ' = :access')
 209                  ->bind(':access', $access, ParameterType::INTEGER);
 210          }
 211  
 212          // Implement View Level Access
 213          if (!$user->authorise('core.admin')) {
 214              $query->whereIn($db->quoteName('a.access'), $user->getAuthorisedViewLevels());
 215          }
 216  
 217          // Filter by published state.
 218          $published = (string) $this->getState('filter.published');
 219  
 220          if (is_numeric($published)) {
 221              $published = (int) $published;
 222              $query->where($db->quoteName('a.published') . ' = :published')
 223                  ->bind(':published', $published, ParameterType::INTEGER);
 224          } elseif ($published === '') {
 225              $query->where($db->quoteName('a.published') . ' IN (0, 1)');
 226          }
 227  
 228          // Filter by category.
 229          $categoryId = $this->getState('filter.category_id');
 230  
 231          if (is_numeric($categoryId)) {
 232              $categoryId = (int) $categoryId;
 233              $query->where($db->quoteName('a.catid') . ' = :categoryId')
 234                  ->bind(':categoryId', $categoryId, ParameterType::INTEGER);
 235          }
 236  
 237          // Filter on the level.
 238          if ($level = (int) $this->getState('filter.level')) {
 239              $query->where($db->quoteName('c.level') . ' <= :level')
 240                  ->bind(':level', $level, ParameterType::INTEGER);
 241          }
 242  
 243          // Filter by search in title
 244          if ($search = $this->getState('filter.search')) {
 245              if (stripos($search, 'id:') === 0) {
 246                  $search = (int) substr($search, 3);
 247                  $query->where($db->quoteName('a.id') . ' = :search')
 248                      ->bind(':search', $search, ParameterType::INTEGER);
 249              } else {
 250                  $search = '%' . str_replace(' ', '%', trim($search)) . '%';
 251                  $query->where('(' . $db->quoteName('a.name') . ' LIKE :search1 OR ' . $db->quoteName('a.alias') . ' LIKE :search2)')
 252                      ->bind([':search1', ':search2'], $search);
 253              }
 254          }
 255  
 256          // Filter on the language.
 257          if ($language = $this->getState('filter.language')) {
 258              $query->where($db->quoteName('a.language') . ' = :language')
 259                  ->bind(':language', $language);
 260          }
 261  
 262          // Filter by a single or group of tags.
 263          $tag = $this->getState('filter.tag');
 264  
 265          // Run simplified query when filtering by one tag.
 266          if (\is_array($tag) && \count($tag) === 1) {
 267              $tag = $tag[0];
 268          }
 269  
 270          if ($tag && \is_array($tag)) {
 271              $tag = ArrayHelper::toInteger($tag);
 272  
 273              $subQuery = $db->getQuery(true)
 274                  ->select('DISTINCT ' . $db->quoteName('content_item_id'))
 275                  ->from($db->quoteName('#__contentitem_tag_map'))
 276                  ->where(
 277                      [
 278                          $db->quoteName('tag_id') . ' IN (' . implode(',', $query->bindArray($tag)) . ')',
 279                          $db->quoteName('type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed'),
 280                      ]
 281                  );
 282  
 283              $query->join(
 284                  'INNER',
 285                  '(' . $subQuery . ') AS ' . $db->quoteName('tagmap'),
 286                  $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
 287              );
 288          } elseif ($tag = (int) $tag) {
 289              $query->join(
 290                  'INNER',
 291                  $db->quoteName('#__contentitem_tag_map', 'tagmap'),
 292                  $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
 293              )
 294                  ->where(
 295                      [
 296                          $db->quoteName('tagmap.tag_id') . ' = :tag',
 297                          $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed'),
 298                      ]
 299                  )
 300                  ->bind(':tag', $tag, ParameterType::INTEGER);
 301          }
 302  
 303          // Add the list ordering clause.
 304          $orderCol  = $this->state->get('list.ordering', 'a.name');
 305          $orderDirn = $this->state->get('list.direction', 'ASC');
 306  
 307          if ($orderCol == 'a.ordering' || $orderCol == 'category_title') {
 308              $ordering = [
 309                  $db->quoteName('c.title') . ' ' . $db->escape($orderDirn),
 310                  $db->quoteName('a.ordering') . ' ' . $db->escape($orderDirn),
 311              ];
 312          } else {
 313              $ordering = $db->escape($orderCol) . ' ' . $db->escape($orderDirn);
 314          }
 315  
 316          $query->order($ordering);
 317  
 318          return $query;
 319      }
 320  }


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