[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_newsfeeds
   6   *
   7   * @copyright   (C) 2006 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\Site\Model;
  12  
  13  use Joomla\CMS\Categories\Categories;
  14  use Joomla\CMS\Categories\CategoryNode;
  15  use Joomla\CMS\Component\ComponentHelper;
  16  use Joomla\CMS\Factory;
  17  use Joomla\CMS\Helper\TagsHelper;
  18  use Joomla\CMS\Language\Multilanguage;
  19  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  20  use Joomla\CMS\MVC\Model\ListModel;
  21  use Joomla\CMS\Table\Table;
  22  use Joomla\Database\ParameterType;
  23  use Joomla\Registry\Registry;
  24  
  25  // phpcs:disable PSR1.Files.SideEffects
  26  \defined('_JEXEC') or die;
  27  // phpcs:enable PSR1.Files.SideEffects
  28  
  29  /**
  30   * Newsfeeds Component Category Model
  31   *
  32   * @since  1.5
  33   */
  34  class CategoryModel extends ListModel
  35  {
  36      /**
  37       * Category items data
  38       *
  39       * @var array
  40       */
  41      protected $_item;
  42  
  43      /**
  44       * Array of newsfeeds in the category
  45       *
  46       * @var    \stdClass[]
  47       */
  48      protected $_articles;
  49  
  50      /**
  51       * Category left and right of this one
  52       *
  53       * @var    CategoryNode[]|null
  54       */
  55      protected $_siblings;
  56  
  57      /**
  58       * Array of child-categories
  59       *
  60       * @var    CategoryNode[]|null
  61       */
  62      protected $_children;
  63  
  64      /**
  65       * Parent category of the current one
  66       *
  67       * @var    CategoryNode|null
  68       */
  69      protected $_parent;
  70  
  71      /**
  72       * The category that applies.
  73       *
  74       * @var    object
  75       */
  76      protected $_category;
  77  
  78      /**
  79       * The list of other newsfeed categories.
  80       *
  81       * @var    array
  82       */
  83      protected $_categories;
  84  
  85      /**
  86       * Constructor.
  87       *
  88       * @param   array                $config   An optional associative array of configuration settings.
  89       * @param   MVCFactoryInterface  $factory  The factory.
  90       *
  91       * @see    \Joomla\CMS\MVC\Model\BaseDatabaseModel
  92       * @since   3.2
  93       */
  94      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  95      {
  96          if (empty($config['filter_fields'])) {
  97              $config['filter_fields'] = array(
  98                  'id', 'a.id',
  99                  'name', 'a.name',
 100                  'numarticles', 'a.numarticles',
 101                  'link', 'a.link',
 102                  'ordering', 'a.ordering',
 103              );
 104          }
 105  
 106          parent::__construct($config, $factory);
 107      }
 108  
 109      /**
 110       * Method to get a list of items.
 111       *
 112       * @return  mixed  An array of objects on success, false on failure.
 113       */
 114      public function getItems()
 115      {
 116          // Invoke the parent getItems method to get the main list
 117          $items = parent::getItems();
 118  
 119          $taggedItems = [];
 120  
 121          // Convert the params field into an object, saving original in _params
 122          foreach ($items as $item) {
 123              if (!isset($this->_params)) {
 124                  $item->params = new Registry($item->params);
 125              }
 126  
 127              // Some contexts may not use tags data at all, so we allow callers to disable loading tag data
 128              if ($this->getState('load_tags', true)) {
 129                  $item->tags = new TagsHelper();
 130                  $taggedItems[$item->id] = $item;
 131              }
 132          }
 133  
 134          // Load tags of all items.
 135          if ($taggedItems) {
 136              $tagsHelper = new TagsHelper();
 137              $itemIds = \array_keys($taggedItems);
 138  
 139              foreach ($tagsHelper->getMultipleItemTags('com_newsfeeds.newsfeed', $itemIds) as $id => $tags) {
 140                  $taggedItems[$id]->tags->itemTags = $tags;
 141              }
 142          }
 143  
 144          return $items;
 145      }
 146  
 147      /**
 148       * Method to build an SQL query to load the list data.
 149       *
 150       * @return  \Joomla\Database\DatabaseQuery    An SQL query
 151       *
 152       * @since   1.6
 153       */
 154      protected function getListQuery()
 155      {
 156          $user   = Factory::getUser();
 157          $groups = $user->getAuthorisedViewLevels();
 158  
 159          // Create a new query object.
 160          $db = $this->getDatabase();
 161  
 162          /** @var \Joomla\Database\DatabaseQuery $query */
 163          $query = $db->getQuery(true);
 164  
 165          // Select required fields from the categories.
 166          $query->select($this->getState('list.select', $db->quoteName('a') . '.*'))
 167              ->from($db->quoteName('#__newsfeeds', 'a'))
 168              ->whereIn($db->quoteName('a.access'), $groups);
 169  
 170          // Filter by category.
 171          if ($categoryId = (int) $this->getState('category.id')) {
 172              $query->where($db->quoteName('a.catid') . ' = :categoryId')
 173                  ->join('LEFT', $db->quoteName('#__categories', 'c'), $db->quoteName('c.id') . ' = ' . $db->quoteName('a.catid'))
 174                  ->whereIn($db->quoteName('c.access'), $groups)
 175                  ->bind(':categoryId', $categoryId, ParameterType::INTEGER);
 176          }
 177  
 178          // Filter by state
 179          $state = $this->getState('filter.published');
 180  
 181          if (is_numeric($state)) {
 182              $state = (int) $state;
 183              $query->where($db->quoteName('a.published') . ' = :state')
 184                  ->bind(':state', $state, ParameterType::INTEGER);
 185          } else {
 186              $query->where($db->quoteName('a.published') . ' IN (0,1,2)');
 187          }
 188  
 189          // Filter by start and end dates.
 190          if ($this->getState('filter.publish_date')) {
 191              $nowDate = Factory::getDate()->toSql();
 192  
 193              $query->extendWhere(
 194                  'AND',
 195                  [
 196                      $db->quoteName('a.publish_up') . ' IS NULL',
 197                      $db->quoteName('a.publish_up') . ' <= :nowDate1',
 198                  ],
 199                  'OR'
 200              )
 201                  ->extendWhere(
 202                      'AND',
 203                      [
 204                          $db->quoteName('a.publish_down') . ' IS NULL',
 205                          $db->quoteName('a.publish_down') . ' >= :nowDate2',
 206                      ],
 207                      'OR'
 208                  )
 209                  ->bind([':nowDate1', ':nowDate2'], $nowDate);
 210          }
 211  
 212          // Filter by search in title
 213          if ($search = $this->getState('list.filter')) {
 214              $search = '%' . $search . '%';
 215              $query->where($db->quoteName('a.name') . ' LIKE :search')
 216                  ->bind(':search', $search);
 217          }
 218  
 219          // Filter by language
 220          if ($this->getState('filter.language')) {
 221              $query->whereIn($db->quoteName('a.language'), [Factory::getApplication()->getLanguage()->getTag(), '*'], ParameterType::STRING);
 222          }
 223  
 224          // Add the list ordering clause.
 225          $query->order($db->escape($this->getState('list.ordering', 'a.ordering')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 226  
 227          return $query;
 228      }
 229  
 230      /**
 231       * Method to auto-populate the model state.
 232       *
 233       * Note. Calling getState in this method will result in recursion.
 234       *
 235       * @param   string  $ordering   An optional ordering field
 236       * @param   string  $direction  An optional direction [asc|desc]
 237       *
 238       * @return void
 239       *
 240       * @since   1.6
 241       *
 242       * @throws \Exception
 243       */
 244      protected function populateState($ordering = null, $direction = null)
 245      {
 246          $app = Factory::getApplication();
 247          $params = ComponentHelper::getParams('com_newsfeeds');
 248  
 249          // List state information
 250          $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'uint');
 251          $this->setState('list.limit', $limit);
 252  
 253          $limitstart = $app->input->get('limitstart', 0, 'uint');
 254          $this->setState('list.start', $limitstart);
 255  
 256          // Optional filter text
 257          $this->setState('list.filter', $app->input->getString('filter-search'));
 258  
 259          $orderCol = $app->input->get('filter_order', 'ordering');
 260  
 261          if (!in_array($orderCol, $this->filter_fields)) {
 262              $orderCol = 'ordering';
 263          }
 264  
 265          $this->setState('list.ordering', $orderCol);
 266  
 267          $listOrder = $app->input->get('filter_order_Dir', 'ASC');
 268  
 269          if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
 270              $listOrder = 'ASC';
 271          }
 272  
 273          $this->setState('list.direction', $listOrder);
 274  
 275          $id = $app->input->get('id', 0, 'int');
 276          $this->setState('category.id', $id);
 277  
 278          $user = Factory::getUser();
 279  
 280          if ((!$user->authorise('core.edit.state', 'com_newsfeeds')) && (!$user->authorise('core.edit', 'com_newsfeeds'))) {
 281              // Limit to published for people who can't edit or edit.state.
 282              $this->setState('filter.published', 1);
 283  
 284              // Filter by start and end dates.
 285              $this->setState('filter.publish_date', true);
 286          }
 287  
 288          $this->setState('filter.language', Multilanguage::isEnabled());
 289  
 290          // Load the parameters.
 291          $this->setState('params', $params);
 292      }
 293  
 294      /**
 295       * Method to get category data for the current category
 296       *
 297       * @return  object
 298       *
 299       * @since   1.5
 300       */
 301      public function getCategory()
 302      {
 303          if (!is_object($this->_item)) {
 304              $app = Factory::getApplication();
 305              $menu = $app->getMenu();
 306              $active = $menu->getActive();
 307  
 308              if ($active) {
 309                  $params = $active->getParams();
 310              } else {
 311                  $params = new Registry();
 312              }
 313  
 314              $options = array();
 315              $options['countItems'] = $params->get('show_cat_items', 1) || $params->get('show_empty_categories', 0);
 316              $categories = Categories::getInstance('Newsfeeds', $options);
 317              $this->_item = $categories->get($this->getState('category.id', 'root'));
 318  
 319              if (is_object($this->_item)) {
 320                  $this->_children = $this->_item->getChildren();
 321                  $this->_parent = false;
 322  
 323                  if ($this->_item->getParent()) {
 324                      $this->_parent = $this->_item->getParent();
 325                  }
 326  
 327                  $this->_rightsibling = $this->_item->getSibling();
 328                  $this->_leftsibling = $this->_item->getSibling(false);
 329              } else {
 330                  $this->_children = false;
 331                  $this->_parent = false;
 332              }
 333          }
 334  
 335          return $this->_item;
 336      }
 337  
 338      /**
 339       * Get the parent category.
 340       *
 341       * @return  mixed  An array of categories or false if an error occurs.
 342       */
 343      public function getParent()
 344      {
 345          if (!is_object($this->_item)) {
 346              $this->getCategory();
 347          }
 348  
 349          return $this->_parent;
 350      }
 351  
 352      /**
 353       * Get the sibling (adjacent) categories.
 354       *
 355       * @return  mixed  An array of categories or false if an error occurs.
 356       */
 357      public function &getLeftSibling()
 358      {
 359          if (!is_object($this->_item)) {
 360              $this->getCategory();
 361          }
 362  
 363          return $this->_leftsibling;
 364      }
 365  
 366      /**
 367       * Get the sibling (adjacent) categories.
 368       *
 369       * @return  mixed  An array of categories or false if an error occurs.
 370       */
 371      public function &getRightSibling()
 372      {
 373          if (!is_object($this->_item)) {
 374              $this->getCategory();
 375          }
 376  
 377          return $this->_rightsibling;
 378      }
 379  
 380      /**
 381       * Get the child categories.
 382       *
 383       * @return  mixed  An array of categories or false if an error occurs.
 384       */
 385      public function &getChildren()
 386      {
 387          if (!is_object($this->_item)) {
 388              $this->getCategory();
 389          }
 390  
 391          return $this->_children;
 392      }
 393  
 394      /**
 395       * Increment the hit counter for the category.
 396       *
 397       * @param   int  $pk  Optional primary key of the category to increment.
 398       *
 399       * @return  boolean True if successful; false otherwise and internal error set.
 400       */
 401      public function hit($pk = 0)
 402      {
 403          $input    = Factory::getApplication()->input;
 404          $hitcount = $input->getInt('hitcount', 1);
 405  
 406          if ($hitcount) {
 407              $pk    = (!empty($pk)) ? $pk : (int) $this->getState('category.id');
 408              $table = Table::getInstance('Category', 'JTable');
 409              $table->hit($pk);
 410          }
 411  
 412          return true;
 413      }
 414  }


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