[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/View/Article/ -> HtmlView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_content
   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\Content\Site\View\Article;
  12  
  13  use Joomla\CMS\Categories\Categories;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Helper\TagsHelper;
  16  use Joomla\CMS\Language\Associations;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\Layout\FileLayout;
  19  use Joomla\CMS\MVC\View\GenericDataException;
  20  use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
  21  use Joomla\CMS\Plugin\PluginHelper;
  22  use Joomla\CMS\Router\Route;
  23  use Joomla\CMS\Uri\Uri;
  24  use Joomla\Component\Content\Site\Helper\AssociationHelper;
  25  use Joomla\Component\Content\Site\Helper\RouteHelper;
  26  use Joomla\Event\Event;
  27  
  28  // phpcs:disable PSR1.Files.SideEffects
  29  \defined('_JEXEC') or die;
  30  // phpcs:enable PSR1.Files.SideEffects
  31  
  32  /**
  33   * HTML Article View class for the Content component
  34   *
  35   * @since  1.5
  36   */
  37  class HtmlView extends BaseHtmlView
  38  {
  39      /**
  40       * The article object
  41       *
  42       * @var  \stdClass
  43       */
  44      protected $item;
  45  
  46      /**
  47       * The page parameters
  48       *
  49       * @var    \Joomla\Registry\Registry|null
  50       *
  51       * @since  4.0.0
  52       */
  53      protected $params = null;
  54  
  55      /**
  56       * Should the print button be displayed or not?
  57       *
  58       * @var   boolean
  59       */
  60      protected $print = false;
  61  
  62      /**
  63       * The model state
  64       *
  65       * @var   \Joomla\CMS\Object\CMSObject
  66       */
  67      protected $state;
  68  
  69      /**
  70       * The user object
  71       *
  72       * @var   \Joomla\CMS\User\User|null
  73       */
  74      protected $user = null;
  75  
  76      /**
  77       * The page class suffix
  78       *
  79       * @var    string
  80       *
  81       * @since  4.0.0
  82       */
  83      protected $pageclass_sfx = '';
  84  
  85      /**
  86       * The flag to mark if the active menu item is linked to the being displayed article
  87       *
  88       * @var boolean
  89       */
  90      protected $menuItemMatchArticle = false;
  91  
  92      /**
  93       * Execute and display a template script.
  94       *
  95       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  96       *
  97       * @return  void
  98       */
  99      public function display($tpl = null)
 100      {
 101          if ($this->getLayout() == 'pagebreak') {
 102              parent::display($tpl);
 103  
 104              return;
 105          }
 106  
 107          $app  = Factory::getApplication();
 108          $user = $this->getCurrentUser();
 109  
 110          $this->item  = $this->get('Item');
 111          $this->print = $app->input->getBool('print', false);
 112          $this->state = $this->get('State');
 113          $this->user  = $user;
 114  
 115          // Check for errors.
 116          if (count($errors = $this->get('Errors'))) {
 117              throw new GenericDataException(implode("\n", $errors), 500);
 118          }
 119  
 120          // Create a shortcut for $item.
 121          $item            = $this->item;
 122          $item->tagLayout = new FileLayout('joomla.content.tags');
 123  
 124          // Add router helpers.
 125          $item->slug = $item->alias ? ($item->id . ':' . $item->alias) : $item->id;
 126  
 127          // No link for ROOT category
 128          if ($item->parent_alias === 'root') {
 129              $item->parent_id = null;
 130          }
 131  
 132          // @todo Change based on shownoauth
 133          $item->readmore_link = Route::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
 134  
 135          // Merge article params. If this is single-article view, menu params override article params
 136          // Otherwise, article params override menu item params
 137          $this->params = $this->state->get('params');
 138          $active       = $app->getMenu()->getActive();
 139          $temp         = clone $this->params;
 140  
 141          // Check to see which parameters should take priority. If the active menu item link to the current article, then
 142          // the menu item params take priority
 143          if (
 144              $active
 145              && $active->component == 'com_content'
 146              && isset($active->query['view'], $active->query['id'])
 147              && $active->query['view'] == 'article'
 148              && $active->query['id'] == $item->id
 149          ) {
 150              $this->menuItemMatchArticle = true;
 151  
 152              // Load layout from active query (in case it is an alternative menu item)
 153              if (isset($active->query['layout'])) {
 154                  $this->setLayout($active->query['layout']);
 155              } elseif ($layout = $item->params->get('article_layout')) {
 156                  // Check for alternative layout of article
 157                  $this->setLayout($layout);
 158              }
 159  
 160              // $item->params are the article params, $temp are the menu item params
 161              // Merge so that the menu item params take priority
 162              $item->params->merge($temp);
 163          } else {
 164              // The active menu item is not linked to this article, so the article params take priority here
 165              // Merge the menu item params with the article params so that the article params take priority
 166              $temp->merge($item->params);
 167              $item->params = $temp;
 168  
 169              // Check for alternative layouts (since we are not in a single-article menu item)
 170              // Single-article menu item layout takes priority over alt layout for an article
 171              if ($layout = $item->params->get('article_layout')) {
 172                  $this->setLayout($layout);
 173              }
 174          }
 175  
 176          $offset = $this->state->get('list.offset');
 177  
 178          // Check the view access to the article (the model has already computed the values).
 179          if ($item->params->get('access-view') == false && ($item->params->get('show_noauth', '0') == '0')) {
 180              $app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
 181              $app->setHeader('status', 403, true);
 182  
 183              return;
 184          }
 185  
 186          /**
 187           * Check for no 'access-view' and empty fulltext,
 188           * - Redirect guest users to login
 189           * - Deny access to logged users with 403 code
 190           * NOTE: we do not recheck for no access-view + show_noauth disabled ... since it was checked above
 191           */
 192          if ($item->params->get('access-view') == false && !strlen($item->fulltext)) {
 193              if ($this->user->get('guest')) {
 194                  $return = base64_encode(Uri::getInstance());
 195                  $login_url_with_return = Route::_('index.php?option=com_users&view=login&return=' . $return);
 196                  $app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'notice');
 197                  $app->redirect($login_url_with_return, 403);
 198              } else {
 199                  $app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
 200                  $app->setHeader('status', 403, true);
 201  
 202                  return;
 203              }
 204          }
 205  
 206          /**
 207           * NOTE: The following code (usually) sets the text to contain the fulltext, but it is the
 208           * responsibility of the layout to check 'access-view' and only use "introtext" for guests
 209           */
 210          if ($item->params->get('show_intro', '1') == '1') {
 211              $item->text = $item->introtext . ' ' . $item->fulltext;
 212          } elseif ($item->fulltext) {
 213              $item->text = $item->fulltext;
 214          } else {
 215              $item->text = $item->introtext;
 216          }
 217  
 218          $item->tags = new TagsHelper();
 219          $item->tags->getItemTags('com_content.article', $this->item->id);
 220  
 221          if (Associations::isEnabled() && $item->params->get('show_associations')) {
 222              $item->associations = AssociationHelper::displayAssociations($item->id);
 223          }
 224  
 225          // Process the content plugins.
 226          PluginHelper::importPlugin('content');
 227          $this->dispatchEvent(new Event('onContentPrepare', array('com_content.article', &$item, &$item->params, $offset)));
 228  
 229          $item->event = new \stdClass();
 230          $results = Factory::getApplication()->triggerEvent('onContentAfterTitle', array('com_content.article', &$item, &$item->params, $offset));
 231          $item->event->afterDisplayTitle = trim(implode("\n", $results));
 232  
 233          $results = Factory::getApplication()->triggerEvent('onContentBeforeDisplay', array('com_content.article', &$item, &$item->params, $offset));
 234          $item->event->beforeDisplayContent = trim(implode("\n", $results));
 235  
 236          $results = Factory::getApplication()->triggerEvent('onContentAfterDisplay', array('com_content.article', &$item, &$item->params, $offset));
 237          $item->event->afterDisplayContent = trim(implode("\n", $results));
 238  
 239          // Escape strings for HTML output
 240          $this->pageclass_sfx = htmlspecialchars($this->item->params->get('pageclass_sfx', ''));
 241  
 242          $this->_prepareDocument();
 243  
 244          parent::display($tpl);
 245      }
 246  
 247      /**
 248       * Prepares the document.
 249       *
 250       * @return  void
 251       */
 252      protected function _prepareDocument()
 253      {
 254          $app     = Factory::getApplication();
 255          $pathway = $app->getPathway();
 256  
 257          /**
 258           * Because the application sets a default page title,
 259           * we need to get it from the menu item itself
 260           */
 261          $menu = $app->getMenu()->getActive();
 262  
 263          if ($menu) {
 264              $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
 265          } else {
 266              $this->params->def('page_heading', Text::_('JGLOBAL_ARTICLES'));
 267          }
 268  
 269          $title = $this->params->get('page_title', '');
 270  
 271          // If the menu item is not linked to this article
 272          if (!$this->menuItemMatchArticle) {
 273              // If a browser page title is defined, use that, then fall back to the article title if set, then fall back to the page_title option
 274              $title = $this->item->params->get('article_page_title', $this->item->title ?: $title);
 275  
 276              // Get ID of the category from active menu item
 277              if (
 278                  $menu && $menu->component == 'com_content' && isset($menu->query['view'])
 279                  && in_array($menu->query['view'], ['categories', 'category'])
 280              ) {
 281                  $id = $menu->query['id'];
 282              } else {
 283                  $id = 0;
 284              }
 285  
 286              $path     = array(array('title' => $this->item->title, 'link' => ''));
 287              $category = Categories::getInstance('Content')->get($this->item->catid);
 288  
 289              while ($category !== null && $category->id != $id && $category->id !== 'root') {
 290                  $path[]   = array('title' => $category->title, 'link' => RouteHelper::getCategoryRoute($category->id, $category->language));
 291                  $category = $category->getParent();
 292              }
 293  
 294              $path = array_reverse($path);
 295  
 296              foreach ($path as $item) {
 297                  $pathway->addItem($item['title'], $item['link']);
 298              }
 299          }
 300  
 301          if (empty($title)) {
 302              $title = $this->item->title;
 303          }
 304  
 305          $this->setDocumentTitle($title);
 306  
 307          if ($this->item->metadesc) {
 308              $this->document->setDescription($this->item->metadesc);
 309          } elseif ($this->params->get('menu-meta_description')) {
 310              $this->document->setDescription($this->params->get('menu-meta_description'));
 311          }
 312  
 313          if ($this->params->get('robots')) {
 314              $this->document->setMetaData('robots', $this->params->get('robots'));
 315          }
 316  
 317          if ($app->get('MetaAuthor') == '1') {
 318              $author = $this->item->created_by_alias ?: $this->item->author;
 319              $this->document->setMetaData('author', $author);
 320          }
 321  
 322          $mdata = $this->item->metadata->toArray();
 323  
 324          foreach ($mdata as $k => $v) {
 325              if ($v) {
 326                  $this->document->setMetaData($k, $v);
 327              }
 328          }
 329  
 330          // If there is a pagebreak heading or title, add it to the page title
 331          if (!empty($this->item->page_title)) {
 332              $this->item->title = $this->item->title . ' - ' . $this->item->page_title;
 333              $this->setDocumentTitle(
 334                  $this->item->page_title . ' - ' . Text::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $this->state->get('list.offset') + 1)
 335              );
 336          }
 337  
 338          if ($this->print) {
 339              $this->document->setMetaData('robots', 'noindex, nofollow');
 340          }
 341      }
 342  }


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