[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_contact
   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\Contact\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\Model\ListModel;
  20  use Joomla\CMS\Table\Table;
  21  use Joomla\Database\ParameterType;
  22  use Joomla\Registry\Registry;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Single item model for a contact
  30   *
  31   * @package     Joomla.Site
  32   * @subpackage  com_contact
  33   * @since       1.5
  34   */
  35  class CategoryModel extends ListModel
  36  {
  37      /**
  38       * Category item data
  39       *
  40       * @var    CategoryNode
  41       */
  42      protected $_item;
  43  
  44      /**
  45       * Array of contacts in the category
  46       *
  47       * @var    \stdClass[]
  48       */
  49      protected $_articles;
  50  
  51      /**
  52       * Category left and right of this one
  53       *
  54       * @var    CategoryNode[]|null
  55       */
  56      protected $_siblings;
  57  
  58      /**
  59       * Array of child-categories
  60       *
  61       * @var    CategoryNode[]|null
  62       */
  63      protected $_children;
  64  
  65      /**
  66       * Parent category of the current one
  67       *
  68       * @var    CategoryNode|null
  69       */
  70      protected $_parent;
  71  
  72      /**
  73       * The category that applies.
  74       *
  75       * @var    object
  76       */
  77      protected $_category;
  78  
  79      /**
  80       * The list of other contact categories.
  81       *
  82       * @var    array
  83       */
  84      protected $_categories;
  85  
  86      /**
  87       * Constructor.
  88       *
  89       * @param   array  $config  An optional associative array of configuration settings.
  90       *
  91       * @since   1.6
  92       */
  93      public function __construct($config = array())
  94      {
  95          if (empty($config['filter_fields'])) {
  96              $config['filter_fields'] = array(
  97                  'id', 'a.id',
  98                  'name', 'a.name',
  99                  'con_position', 'a.con_position',
 100                  'suburb', 'a.suburb',
 101                  'state', 'a.state',
 102                  'country', 'a.country',
 103                  'ordering', 'a.ordering',
 104                  'sortname',
 105                  'sortname1', 'a.sortname1',
 106                  'sortname2', 'a.sortname2',
 107                  'sortname3', 'a.sortname3',
 108                  'featuredordering', 'a.featured'
 109              );
 110          }
 111  
 112          parent::__construct($config);
 113      }
 114  
 115      /**
 116       * Method to get a list of items.
 117       *
 118       * @return  mixed  An array of objects on success, false on failure.
 119       */
 120      public function getItems()
 121      {
 122          // Invoke the parent getItems method to get the main list
 123          $items = parent::getItems();
 124  
 125          if ($items === false) {
 126              return false;
 127          }
 128  
 129          $taggedItems = [];
 130  
 131          // Convert the params field into an object, saving original in _params
 132          foreach ($items as $item) {
 133              if (!isset($this->_params)) {
 134                  $item->params = new Registry($item->params);
 135              }
 136  
 137              // Some contexts may not use tags data at all, so we allow callers to disable loading tag data
 138              if ($this->getState('load_tags', true)) {
 139                  $item->tags = new TagsHelper();
 140                  $taggedItems[$item->id] = $item;
 141              }
 142          }
 143  
 144          // Load tags of all items.
 145          if ($taggedItems) {
 146              $tagsHelper = new TagsHelper();
 147              $itemIds = \array_keys($taggedItems);
 148  
 149              foreach ($tagsHelper->getMultipleItemTags('com_contact.contact', $itemIds) as $id => $tags) {
 150                  $taggedItems[$id]->tags->itemTags = $tags;
 151              }
 152          }
 153  
 154          return $items;
 155      }
 156  
 157      /**
 158       * Method to build an SQL query to load the list data.
 159       *
 160       * @return  \Joomla\Database\DatabaseQuery    An SQL query
 161       *
 162       * @since   1.6
 163       */
 164      protected function getListQuery()
 165      {
 166          $user   = Factory::getUser();
 167          $groups = $user->getAuthorisedViewLevels();
 168  
 169          // Create a new query object.
 170          $db = $this->getDatabase();
 171  
 172          /** @var \Joomla\Database\DatabaseQuery $query */
 173          $query = $db->getQuery(true);
 174  
 175          $query->select($this->getState('list.select', 'a.*'))
 176              ->select($this->getSlugColumn($query, 'a.id', 'a.alias') . ' AS slug')
 177              ->select($this->getSlugColumn($query, 'c.id', 'c.alias') . ' AS catslug')
 178          /**
 179           * @todo: we actually should be doing it but it's wrong this way
 180           *  . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
 181           *  . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END AS catslug ');
 182           */
 183              ->from($db->quoteName('#__contact_details', 'a'))
 184              ->leftJoin($db->quoteName('#__categories', 'c') . ' ON c.id = a.catid')
 185              ->whereIn($db->quoteName('a.access'), $groups);
 186  
 187          // Filter by category.
 188          if ($categoryId = $this->getState('category.id')) {
 189              $query->where($db->quoteName('a.catid') . ' = :acatid')
 190                  ->whereIn($db->quoteName('c.access'), $groups);
 191              $query->bind(':acatid', $categoryId, ParameterType::INTEGER);
 192          }
 193  
 194          // Join over the users for the author and modified_by names.
 195          $query->select("CASE WHEN a.created_by_alias > ' ' THEN a.created_by_alias ELSE ua.name END AS author")
 196              ->select('ua.email AS author_email')
 197              ->leftJoin($db->quoteName('#__users', 'ua') . ' ON ua.id = a.created_by')
 198              ->leftJoin($db->quoteName('#__users', 'uam') . ' ON uam.id = a.modified_by');
 199  
 200          // Filter by state
 201          $state = $this->getState('filter.published');
 202  
 203          if (is_numeric($state)) {
 204              $query->where($db->quoteName('a.published') . ' = :published');
 205              $query->bind(':published', $state, ParameterType::INTEGER);
 206          } else {
 207              $query->whereIn($db->quoteName('c.published'), [0,1,2]);
 208          }
 209  
 210          // Filter by start and end dates.
 211          $nowDate = Factory::getDate()->toSql();
 212  
 213          if ($this->getState('filter.publish_date')) {
 214              $query->where('(' . $db->quoteName('a.publish_up')
 215                  . ' IS NULL OR ' . $db->quoteName('a.publish_up') . ' <= :publish_up)')
 216                  ->where('(' . $db->quoteName('a.publish_down')
 217                      . ' IS NULL OR ' . $db->quoteName('a.publish_down') . ' >= :publish_down)')
 218                  ->bind(':publish_up', $nowDate)
 219                  ->bind(':publish_down', $nowDate);
 220          }
 221  
 222          // Filter by search in title
 223          $search = $this->getState('list.filter');
 224  
 225          if (!empty($search)) {
 226              $search = '%' . trim($search) . '%';
 227              $query->where($db->quoteName('a.name') . ' LIKE :name ');
 228              $query->bind(':name', $search);
 229          }
 230  
 231          // Filter on the language.
 232          if ($this->getState('filter.language')) {
 233              $query->whereIn($db->quoteName('a.language'), [Factory::getApplication()->getLanguage()->getTag(), '*'], ParameterType::STRING);
 234          }
 235  
 236          // Set sortname ordering if selected
 237          if ($this->getState('list.ordering') === 'sortname') {
 238              $query->order($db->escape('a.sortname1') . ' ' . $db->escape($this->getState('list.direction', 'ASC')))
 239                  ->order($db->escape('a.sortname2') . ' ' . $db->escape($this->getState('list.direction', 'ASC')))
 240                  ->order($db->escape('a.sortname3') . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 241          } elseif ($this->getState('list.ordering') === 'featuredordering') {
 242              $query->order($db->escape('a.featured') . ' DESC')
 243                  ->order($db->escape('a.ordering') . ' ASC');
 244          } else {
 245              $query->order($db->escape($this->getState('list.ordering', 'a.ordering')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 246          }
 247  
 248          return $query;
 249      }
 250  
 251      /**
 252       * Method to auto-populate the model state.
 253       *
 254       * Note. Calling getState in this method will result in recursion.
 255       *
 256       * @param   string  $ordering   An optional ordering field.
 257       * @param   string  $direction  An optional direction (asc|desc).
 258       *
 259       * @return  void
 260       *
 261       * @since   1.6
 262       */
 263      protected function populateState($ordering = null, $direction = null)
 264      {
 265          $app = Factory::getApplication();
 266          $params = ComponentHelper::getParams('com_contact');
 267  
 268          // Get list ordering default from the parameters
 269          if ($menu = $app->getMenu()->getActive()) {
 270              $menuParams = $menu->getParams();
 271          } else {
 272              $menuParams = new Registry();
 273          }
 274  
 275          $mergedParams = clone $params;
 276          $mergedParams->merge($menuParams);
 277  
 278          // List state information
 279          $format = $app->input->getWord('format');
 280  
 281          if ($format === 'feed') {
 282              $limit = $app->get('feed_limit');
 283          } else {
 284              $limit = $app->getUserStateFromRequest(
 285                  'com_contact.category.list.limit',
 286                  'limit',
 287                  $mergedParams->get('contacts_display_num', $app->get('list_limit')),
 288                  'uint'
 289              );
 290          }
 291  
 292          $this->setState('list.limit', $limit);
 293  
 294          $limitstart = $app->input->get('limitstart', 0, 'uint');
 295          $this->setState('list.start', $limitstart);
 296  
 297          // Optional filter text
 298          $itemid = $app->input->get('Itemid', 0, 'int');
 299          $search = $app->getUserStateFromRequest('com_contact.category.list.' . $itemid . '.filter-search', 'filter-search', '', 'string');
 300          $this->setState('list.filter', $search);
 301  
 302          $orderCol = $app->input->get('filter_order', $mergedParams->get('initial_sort', 'ordering'));
 303  
 304          if (!in_array($orderCol, $this->filter_fields)) {
 305              $orderCol = 'ordering';
 306          }
 307  
 308          $this->setState('list.ordering', $orderCol);
 309  
 310          $listOrder = $app->input->get('filter_order_Dir', 'ASC');
 311  
 312          if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
 313              $listOrder = 'ASC';
 314          }
 315  
 316          $this->setState('list.direction', $listOrder);
 317  
 318          $id = $app->input->get('id', 0, 'int');
 319          $this->setState('category.id', $id);
 320  
 321          $user = Factory::getUser();
 322  
 323          if ((!$user->authorise('core.edit.state', 'com_contact')) && (!$user->authorise('core.edit', 'com_contact'))) {
 324              // Limit to published for people who can't edit or edit.state.
 325              $this->setState('filter.published', 1);
 326  
 327              // Filter by start and end dates.
 328              $this->setState('filter.publish_date', true);
 329          }
 330  
 331          $this->setState('filter.language', Multilanguage::isEnabled());
 332  
 333          // Load the parameters.
 334          $this->setState('params', $params);
 335      }
 336  
 337      /**
 338       * Method to get category data for the current category
 339       *
 340       * @return  object  The category object
 341       *
 342       * @since   1.5
 343       */
 344      public function getCategory()
 345      {
 346          if (!is_object($this->_item)) {
 347              $app = Factory::getApplication();
 348              $menu = $app->getMenu();
 349              $active = $menu->getActive();
 350  
 351              if ($active) {
 352                  $params = $active->getParams();
 353              } else {
 354                  $params = new Registry();
 355              }
 356  
 357              $options = array();
 358              $options['countItems'] = $params->get('show_cat_items', 1) || $params->get('show_empty_categories', 0);
 359              $categories = Categories::getInstance('Contact', $options);
 360              $this->_item = $categories->get($this->getState('category.id', 'root'));
 361  
 362              if (is_object($this->_item)) {
 363                  $this->_children = $this->_item->getChildren();
 364                  $this->_parent = false;
 365  
 366                  if ($this->_item->getParent()) {
 367                      $this->_parent = $this->_item->getParent();
 368                  }
 369  
 370                  $this->_rightsibling = $this->_item->getSibling();
 371                  $this->_leftsibling = $this->_item->getSibling(false);
 372              } else {
 373                  $this->_children = false;
 374                  $this->_parent = false;
 375              }
 376          }
 377  
 378          return $this->_item;
 379      }
 380  
 381      /**
 382       * Get the parent category.
 383       *
 384       * @return  mixed  An array of categories or false if an error occurs.
 385       */
 386      public function getParent()
 387      {
 388          if (!is_object($this->_item)) {
 389              $this->getCategory();
 390          }
 391  
 392          return $this->_parent;
 393      }
 394  
 395      /**
 396       * Get the sibling (adjacent) categories.
 397       *
 398       * @return  mixed  An array of categories or false if an error occurs.
 399       */
 400      public function &getLeftSibling()
 401      {
 402          if (!is_object($this->_item)) {
 403              $this->getCategory();
 404          }
 405  
 406          return $this->_leftsibling;
 407      }
 408  
 409      /**
 410       * Get the sibling (adjacent) categories.
 411       *
 412       * @return  mixed  An array of categories or false if an error occurs.
 413       */
 414      public function &getRightSibling()
 415      {
 416          if (!is_object($this->_item)) {
 417              $this->getCategory();
 418          }
 419  
 420          return $this->_rightsibling;
 421      }
 422  
 423      /**
 424       * Get the child categories.
 425       *
 426       * @return  mixed  An array of categories or false if an error occurs.
 427       */
 428      public function &getChildren()
 429      {
 430          if (!is_object($this->_item)) {
 431              $this->getCategory();
 432          }
 433  
 434          return $this->_children;
 435      }
 436  
 437      /**
 438       * Generate column expression for slug or catslug.
 439       *
 440       * @param   \Joomla\Database\DatabaseQuery  $query  Current query instance.
 441       * @param   string                          $id     Column id name.
 442       * @param   string                          $alias  Column alias name.
 443       *
 444       * @return  string
 445       *
 446       * @since   4.0.0
 447       */
 448      private function getSlugColumn($query, $id, $alias)
 449      {
 450          return 'CASE WHEN '
 451              . $query->charLength($alias, '!=', '0')
 452              . ' THEN '
 453              . $query->concatenate(array($query->castAsChar($id), $alias), ':')
 454              . ' ELSE '
 455              . $query->castAsChar($id) . ' END';
 456      }
 457  
 458      /**
 459       * Increment the hit counter for the category.
 460       *
 461       * @param   integer  $pk  Optional primary key of the category to increment.
 462       *
 463       * @return  boolean  True if successful; false otherwise and internal error set.
 464       *
 465       * @since   3.2
 466       */
 467      public function hit($pk = 0)
 468      {
 469          $input = Factory::getApplication()->input;
 470          $hitcount = $input->getInt('hitcount', 1);
 471  
 472          if ($hitcount) {
 473              $pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id');
 474  
 475              $table = Table::getInstance('Category');
 476              $table->hit($pk);
 477          }
 478  
 479          return true;
 480      }
 481  }


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