[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/View/ -> CategoryView.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\MVC\View;
  11  
  12  use Joomla\CMS\Categories\CategoryNode;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Helper\TagsHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Plugin\PluginHelper;
  17  use Joomla\CMS\Router\Route;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Base HTML View class for the a Category list
  25   *
  26   * @since  3.2
  27   */
  28  class CategoryView extends HtmlView
  29  {
  30      /**
  31       * State data
  32       *
  33       * @var    \Joomla\Registry\Registry
  34       * @since  3.2
  35       */
  36      protected $state;
  37  
  38      /**
  39       * Category items data
  40       *
  41       * @var    array
  42       * @since  3.2
  43       */
  44      protected $items;
  45  
  46      /**
  47       * The category model object for this category
  48       *
  49       * @var    CategoryNode
  50       * @since  3.2
  51       */
  52      protected $category;
  53  
  54      /**
  55       * The list of other categories for this extension.
  56       *
  57       * @var    array
  58       * @since  3.2
  59       */
  60      protected $categories;
  61  
  62      /**
  63       * Pagination object
  64       *
  65       * @var    \Joomla\CMS\Pagination\Pagination
  66       * @since  3.2
  67       */
  68      protected $pagination;
  69  
  70      /**
  71       * Child objects
  72       *
  73       * @var    array
  74       * @since  3.2
  75       */
  76      protected $children;
  77  
  78      /**
  79       * The name of the extension for the category
  80       *
  81       * @var    string
  82       * @since  3.2
  83       */
  84      protected $extension;
  85  
  86      /**
  87       * The name of the view to link individual items to
  88       *
  89       * @var    string
  90       * @since  3.2
  91       */
  92      protected $viewName;
  93  
  94      /**
  95       * Default title to use for page title
  96       *
  97       * @var    string
  98       * @since  3.2
  99       */
 100      protected $defaultPageTitle;
 101  
 102      /**
 103       * Whether to run the standard Joomla plugin events.
 104       * Off by default for b/c
 105       *
 106       * @var    boolean
 107       * @since  3.5
 108       */
 109      protected $runPlugins = false;
 110  
 111      /**
 112       * The flag to mark if the active menu item is linked to the category being displayed
 113       *
 114       * @var bool
 115       * @since 4.0.0
 116       */
 117      protected $menuItemMatchCategory = false;
 118  
 119      /**
 120       * Method with common display elements used in category list displays
 121       *
 122       * @return  void
 123       *
 124       * @since   3.2
 125       */
 126      public function commonCategoryDisplay()
 127      {
 128          $app    = Factory::getApplication();
 129          $user   = Factory::getUser();
 130          $params = $app->getParams();
 131  
 132          // Get some data from the models
 133          $model       = $this->getModel();
 134          $paramsModel = $model->getState('params');
 135  
 136          $paramsModel->set('check_access_rights', 0);
 137          $model->setState('params', $paramsModel);
 138  
 139          $state       = $this->get('State');
 140          $category    = $this->get('Category');
 141          $children    = $this->get('Children');
 142          $parent      = $this->get('Parent');
 143  
 144          if ($category == false) {
 145              throw new \InvalidArgumentException(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 404);
 146          }
 147  
 148          if ($parent == false) {
 149              throw new \InvalidArgumentException(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 404);
 150          }
 151  
 152          // Check whether category access level allows access.
 153          $groups = $user->getAuthorisedViewLevels();
 154  
 155          if (!\in_array($category->access, $groups)) {
 156              throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403);
 157          }
 158  
 159          $items      = $this->get('Items');
 160          $pagination = $this->get('Pagination');
 161  
 162          // Check for errors.
 163          if (\count($errors = $this->get('Errors'))) {
 164              throw new GenericDataException(implode("\n", $errors), 500);
 165          }
 166  
 167          // Setup the category parameters.
 168          $cparams          = $category->getParams();
 169          $category->params = clone $params;
 170          $category->params->merge($cparams);
 171  
 172          $children = array($category->id => $children);
 173  
 174          // Escape strings for HTML output
 175          $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx', ''));
 176  
 177          if ($this->runPlugins) {
 178              PluginHelper::importPlugin('content');
 179  
 180              foreach ($items as $itemElement) {
 181                  $itemElement = (object) $itemElement;
 182                  $itemElement->event = new \stdClass();
 183  
 184                  // For some plugins.
 185                  !empty($itemElement->description) ? $itemElement->text = $itemElement->description : $itemElement->text = '';
 186  
 187                  Factory::getApplication()->triggerEvent('onContentPrepare', [$this->extension . '.category', &$itemElement, &$itemElement->params, 0]);
 188  
 189                  $results = Factory::getApplication()->triggerEvent(
 190                      'onContentAfterTitle',
 191                      [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0]
 192                  );
 193                  $itemElement->event->afterDisplayTitle = trim(implode("\n", $results));
 194  
 195                  $results = Factory::getApplication()->triggerEvent(
 196                      'onContentBeforeDisplay',
 197                      [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0]
 198                  );
 199                  $itemElement->event->beforeDisplayContent = trim(implode("\n", $results));
 200  
 201                  $results = Factory::getApplication()->triggerEvent(
 202                      'onContentAfterDisplay',
 203                      [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0]
 204                  );
 205                  $itemElement->event->afterDisplayContent = trim(implode("\n", $results));
 206  
 207                  if ($itemElement->text) {
 208                      $itemElement->description = $itemElement->text;
 209                  }
 210              }
 211          }
 212  
 213          $maxLevel         = $params->get('maxLevel', -1) < 0 ? PHP_INT_MAX : $params->get('maxLevel', PHP_INT_MAX);
 214          $this->maxLevel   = &$maxLevel;
 215          $this->state      = &$state;
 216          $this->items      = &$items;
 217          $this->category   = &$category;
 218          $this->children   = &$children;
 219          $this->params     = &$params;
 220          $this->parent     = &$parent;
 221          $this->pagination = &$pagination;
 222          $this->user       = &$user;
 223  
 224          // Check for layout override only if this is not the active menu item
 225          // If it is the active menu item, then the view and category id will match
 226          $active = $app->getMenu()->getActive();
 227  
 228          if (
 229              $active
 230              && $active->component == $this->extension
 231              && isset($active->query['view'], $active->query['id'])
 232              && $active->query['view'] === 'category'
 233              && $active->query['id'] == $this->category->id
 234          ) {
 235              if (isset($active->query['layout'])) {
 236                  $this->setLayout($active->query['layout']);
 237              }
 238  
 239              $this->menuItemMatchCategory = true;
 240          } elseif ($layout = $category->params->get('category_layout')) {
 241              $this->setLayout($layout);
 242          }
 243  
 244          $this->category->tags = new TagsHelper();
 245          $this->category->tags->getItemTags($this->extension . '.category', $this->category->id);
 246      }
 247  
 248      /**
 249       * Execute and display a template script.
 250       *
 251       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 252       *
 253       * @return  void
 254       *
 255       * @since   3.2
 256       * @throws  \Exception
 257       */
 258      public function display($tpl = null)
 259      {
 260          $this->prepareDocument();
 261  
 262          parent::display($tpl);
 263      }
 264  
 265      /**
 266       * Method to prepares the document
 267       *
 268       * @return  void
 269       *
 270       * @since   3.2
 271       */
 272      protected function prepareDocument()
 273      {
 274          $app           = Factory::getApplication();
 275          $this->pathway = $app->getPathway();
 276  
 277          // Because the application sets a default page title, we need to get it from the menu item itself
 278          $this->menu = $app->getMenu()->getActive();
 279  
 280          if ($this->menu) {
 281              $this->params->def('page_heading', $this->params->get('page_title', $this->menu->title));
 282          } else {
 283              $this->params->def('page_heading', Text::_($this->defaultPageTitle));
 284          }
 285  
 286          $this->setDocumentTitle($this->params->get('page_title', ''));
 287  
 288          if ($this->params->get('menu-meta_description')) {
 289              $this->document->setDescription($this->params->get('menu-meta_description'));
 290          }
 291  
 292          if ($this->params->get('robots')) {
 293              $this->document->setMetaData('robots', $this->params->get('robots'));
 294          }
 295      }
 296  
 297      /**
 298       * Method to add an alternative feed link to a category layout.
 299       *
 300       * @return  void
 301       *
 302       * @since   3.2
 303       */
 304      protected function addFeed()
 305      {
 306          if ($this->params->get('show_feed_link', 1) == 1) {
 307              $link    = '&format=feed&limitstart=';
 308              $attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
 309              $this->document->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs);
 310              $attribs = array('type' => 'application/atom+xml', 'title' => 'Atom 1.0');
 311              $this->document->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs);
 312          }
 313      }
 314  }


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