[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_tags/src/Model/ -> TagModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_tags
   6   *
   7   * @copyright   (C) 2013 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\Tags\Site\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Helper\TagsHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\MVC\Model\ListModel;
  19  use Joomla\CMS\Object\CMSObject;
  20  use Joomla\Component\Tags\Site\Helper\RouteHelper;
  21  use Joomla\Utilities\ArrayHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Tags Component Tag Model
  29   *
  30   * @since  3.1
  31   */
  32  class TagModel extends ListModel
  33  {
  34      /**
  35       * The tags that apply.
  36       *
  37       * @var    object
  38       * @since  3.1
  39       */
  40      protected $tag = null;
  41  
  42      /**
  43       * The list of items associated with the tags.
  44       *
  45       * @var    array
  46       * @since  3.1
  47       */
  48      protected $items = null;
  49  
  50      /**
  51       * Constructor.
  52       *
  53       * @param   array                $config   An optional associative array of configuration settings.
  54       * @param   MVCFactoryInterface  $factory  The factory.
  55       *
  56       * @since   1.6
  57       */
  58      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  59      {
  60          if (empty($config['filter_fields'])) {
  61              $config['filter_fields'] = array(
  62                  'core_content_id', 'c.core_content_id',
  63                  'core_title', 'c.core_title',
  64                  'core_type_alias', 'c.core_type_alias',
  65                  'core_checked_out_user_id', 'c.core_checked_out_user_id',
  66                  'core_checked_out_time', 'c.core_checked_out_time',
  67                  'core_catid', 'c.core_catid',
  68                  'core_state', 'c.core_state',
  69                  'core_access', 'c.core_access',
  70                  'core_created_user_id', 'c.core_created_user_id',
  71                  'core_created_time', 'c.core_created_time',
  72                  'core_modified_time', 'c.core_modified_time',
  73                  'core_ordering', 'c.core_ordering',
  74                  'core_featured', 'c.core_featured',
  75                  'core_language', 'c.core_language',
  76                  'core_hits', 'c.core_hits',
  77                  'core_publish_up', 'c.core_publish_up',
  78                  'core_publish_down', 'c.core_publish_down',
  79                  'core_images', 'c.core_images',
  80                  'core_urls', 'c.core_urls',
  81                  'match_count',
  82              );
  83          }
  84  
  85          parent::__construct($config, $factory);
  86      }
  87  
  88      /**
  89       * Method to get a list of items for a list of tags.
  90       *
  91       * @return  mixed  An array of objects on success, false on failure.
  92       *
  93       * @since   3.1
  94       */
  95      public function getItems()
  96      {
  97          // Invoke the parent getItems method to get the main list
  98          $items = parent::getItems();
  99  
 100          if (!empty($items)) {
 101              foreach ($items as $item) {
 102                  $item->link = RouteHelper::getItemRoute(
 103                      $item->content_item_id,
 104                      $item->core_alias,
 105                      $item->core_catid,
 106                      $item->core_language,
 107                      $item->type_alias,
 108                      $item->router
 109                  );
 110  
 111                  // Get display date
 112                  switch ($this->state->params->get('tag_list_show_date')) {
 113                      case 'modified':
 114                          $item->displayDate = $item->core_modified_time;
 115                          break;
 116  
 117                      case 'created':
 118                          $item->displayDate = $item->core_created_time;
 119                          break;
 120  
 121                      default:
 122                          $item->displayDate = ($item->core_publish_up == 0) ? $item->core_created_time : $item->core_publish_up;
 123                          break;
 124                  }
 125              }
 126          }
 127  
 128          return $items;
 129      }
 130  
 131      /**
 132       * Method to build an SQL query to load the list data of all items with a given tag.
 133       *
 134       * @return  string  An SQL query
 135       *
 136       * @since   3.1
 137       */
 138      protected function getListQuery()
 139      {
 140          $tagId  = $this->getState('tag.id') ? : '';
 141  
 142          $typesr = $this->getState('tag.typesr');
 143          $orderByOption = $this->getState('list.ordering', 'c.core_title');
 144          $includeChildren = $this->state->params->get('include_children', 0);
 145          $orderDir = $this->getState('list.direction', 'ASC');
 146          $matchAll = $this->getState('params')->get('return_any_or_all', 1);
 147          $language = $this->getState('tag.language');
 148          $stateFilter = $this->getState('tag.state');
 149  
 150          // Optionally filter on language
 151          if (empty($language)) {
 152              $language = ComponentHelper::getParams('com_tags')->get('tag_list_language_filter', 'all');
 153          }
 154  
 155          $query = (new TagsHelper())->getTagItemsQuery($tagId, $typesr, $includeChildren, $orderByOption, $orderDir, $matchAll, $language, $stateFilter);
 156  
 157          if ($this->state->get('list.filter')) {
 158              $db = $this->getDatabase();
 159              $query->where($db->quoteName('c.core_title') . ' LIKE ' . $db->quote('%' . $this->state->get('list.filter') . '%'));
 160          }
 161  
 162          return $query;
 163      }
 164  
 165      /**
 166       * Method to auto-populate the model state.
 167       *
 168       * Note. Calling getState in this method will result in recursion.
 169       *
 170       * @param   string  $ordering   An optional ordering field.
 171       * @param   string  $direction  An optional direction (asc|desc).
 172       *
 173       * @return  void
 174       *
 175       * @since   3.1
 176       */
 177      protected function populateState($ordering = 'c.core_title', $direction = 'ASC')
 178      {
 179          $app = Factory::getApplication();
 180  
 181          // Load the parameters.
 182          $params = $app->isClient('administrator') ? ComponentHelper::getParams('com_tags') : $app->getParams();
 183  
 184          $this->setState('params', $params);
 185  
 186          // Load state from the request.
 187          $ids = (array) $app->input->get('id', array(), 'string');
 188  
 189          if (count($ids) == 1) {
 190              $ids = explode(',', $ids[0]);
 191          }
 192  
 193          $ids = ArrayHelper::toInteger($ids);
 194  
 195          // Remove zero values resulting from bad input
 196          $ids = array_filter($ids);
 197  
 198          $pkString = implode(',', $ids);
 199  
 200          $this->setState('tag.id', $pkString);
 201  
 202          // Get the selected list of types from the request. If none are specified all are used.
 203          $typesr = $app->input->get('types', array(), 'array');
 204  
 205          if ($typesr) {
 206              // Implode is needed because the array can contain a string with a coma separated list of ids
 207              $typesr = implode(',', $typesr);
 208  
 209              // Sanitise
 210              $typesr = explode(',', $typesr);
 211              $typesr = ArrayHelper::toInteger($typesr);
 212  
 213              $this->setState('tag.typesr', $typesr);
 214          }
 215  
 216          $language = $app->input->getString('tag_list_language_filter');
 217          $this->setState('tag.language', $language);
 218  
 219          // List state information
 220          $format = $app->input->getWord('format');
 221  
 222          if ($format === 'feed') {
 223              $limit = $app->get('feed_limit');
 224          } else {
 225              $limit = $params->get('display_num', $app->get('list_limit', 20));
 226              $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $limit, 'uint');
 227          }
 228  
 229          $this->setState('list.limit', $limit);
 230  
 231          $offset = $app->input->get('limitstart', 0, 'uint');
 232          $this->setState('list.start', $offset);
 233  
 234          $itemid = $pkString . ':' . $app->input->get('Itemid', 0, 'int');
 235          $orderCol = $app->getUserStateFromRequest('com_tags.tag.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
 236          $orderCol = !$orderCol ? $this->state->params->get('tag_list_orderby', 'c.core_title') : $orderCol;
 237  
 238          if (!in_array($orderCol, $this->filter_fields)) {
 239              $orderCol = 'c.core_title';
 240          }
 241  
 242          $this->setState('list.ordering', $orderCol);
 243  
 244          $listOrder = $app->getUserStateFromRequest('com_tags.tag.list.' . $itemid . '.filter_order_direction', 'filter_order_Dir', '', 'string');
 245          $listOrder = !$listOrder ? $this->state->params->get('tag_list_orderby_direction', 'ASC') : $listOrder;
 246  
 247          if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
 248              $listOrder = 'ASC';
 249          }
 250  
 251          $this->setState('list.direction', $listOrder);
 252  
 253          $this->setState('tag.state', 1);
 254  
 255          // Optional filter text
 256          $filterSearch = $app->getUserStateFromRequest('com_tags.tag.list.' . $itemid . '.filter_search', 'filter-search', '', 'string');
 257          $this->setState('list.filter', $filterSearch);
 258      }
 259  
 260      /**
 261       * Method to get tag data for the current tag or tags
 262       *
 263       * @param   integer  $pk  An optional ID
 264       *
 265       * @return  array
 266       *
 267       * @since   3.1
 268       */
 269      public function getItem($pk = null)
 270      {
 271          if (!isset($this->item)) {
 272              $this->item = [];
 273  
 274              if (empty($pk)) {
 275                  $pk = $this->getState('tag.id');
 276              }
 277  
 278              // Get a level row instance.
 279              /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 280              $table = $this->getTable();
 281  
 282              $idsArray = explode(',', $pk);
 283  
 284              // Attempt to load the rows into an array.
 285              foreach ($idsArray as $id) {
 286                  try {
 287                      $table->load($id);
 288  
 289                      // Check published state.
 290                      if ($published = $this->getState('tag.state')) {
 291                          if ($table->published != $published) {
 292                              continue;
 293                          }
 294                      }
 295  
 296                      if (!in_array($table->access, Factory::getUser()->getAuthorisedViewLevels())) {
 297                          continue;
 298                      }
 299  
 300                      // Convert the Table to a clean CMSObject.
 301                      $properties = $table->getProperties(1);
 302                      $this->item[] = ArrayHelper::toObject($properties, CMSObject::class);
 303                  } catch (\RuntimeException $e) {
 304                      $this->setError($e->getMessage());
 305  
 306                      return false;
 307                  }
 308              }
 309          }
 310  
 311          if (!$this->item) {
 312              throw new \Exception(Text::_('COM_TAGS_TAG_NOT_FOUND'), 404);
 313          }
 314  
 315          return $this->item;
 316      }
 317  
 318      /**
 319       * Increment the hit counter.
 320       *
 321       * @param   integer  $pk  Optional primary key of the article to increment.
 322       *
 323       * @return  boolean  True if successful; false otherwise and internal error set.
 324       *
 325       * @since   3.2
 326       */
 327      public function hit($pk = 0)
 328      {
 329          $input    = Factory::getApplication()->input;
 330          $hitcount = $input->getInt('hitcount', 1);
 331  
 332          if ($hitcount) {
 333              $pk    = (!empty($pk)) ? $pk : (int) $this->getState('tag.id');
 334  
 335              /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 336              $table = $this->getTable();
 337              $table->hit($pk);
 338  
 339              // Load the table data for later
 340              $table->load($pk);
 341  
 342              if (!$table->hasPrimaryKey()) {
 343                  throw new \Exception(Text::_('COM_TAGS_TAG_NOT_FOUND'), 404);
 344              }
 345          }
 346  
 347          return true;
 348      }
 349  }


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