[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/Service/ -> Router.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\Service;
  12  
  13  use Joomla\CMS\Application\SiteApplication;
  14  use Joomla\CMS\Categories\CategoryFactoryInterface;
  15  use Joomla\CMS\Categories\CategoryInterface;
  16  use Joomla\CMS\Component\ComponentHelper;
  17  use Joomla\CMS\Component\Router\RouterView;
  18  use Joomla\CMS\Component\Router\RouterViewConfiguration;
  19  use Joomla\CMS\Component\Router\Rules\MenuRules;
  20  use Joomla\CMS\Component\Router\Rules\NomenuRules;
  21  use Joomla\CMS\Component\Router\Rules\StandardRules;
  22  use Joomla\CMS\Menu\AbstractMenu;
  23  use Joomla\Database\DatabaseInterface;
  24  use Joomla\Database\ParameterType;
  25  
  26  // phpcs:disable PSR1.Files.SideEffects
  27  \defined('_JEXEC') or die;
  28  // phpcs:enable PSR1.Files.SideEffects
  29  
  30  /**
  31   * Routing class of com_content
  32   *
  33   * @since  3.3
  34   */
  35  class Router extends RouterView
  36  {
  37      /**
  38       * Flag to remove IDs
  39       *
  40       * @var    boolean
  41       */
  42      protected $noIDs = false;
  43  
  44      /**
  45       * The category factory
  46       *
  47       * @var CategoryFactoryInterface
  48       *
  49       * @since  4.0.0
  50       */
  51      private $categoryFactory;
  52  
  53      /**
  54       * The category cache
  55       *
  56       * @var  array
  57       *
  58       * @since  4.0.0
  59       */
  60      private $categoryCache = [];
  61  
  62      /**
  63       * The db
  64       *
  65       * @var DatabaseInterface
  66       *
  67       * @since  4.0.0
  68       */
  69      private $db;
  70  
  71      /**
  72       * Content Component router constructor
  73       *
  74       * @param   SiteApplication           $app              The application object
  75       * @param   AbstractMenu              $menu             The menu object to work with
  76       * @param   CategoryFactoryInterface  $categoryFactory  The category object
  77       * @param   DatabaseInterface         $db               The database object
  78       */
  79      public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db)
  80      {
  81          $this->categoryFactory = $categoryFactory;
  82          $this->db              = $db;
  83  
  84          $params = ComponentHelper::getParams('com_content');
  85          $this->noIDs = (bool) $params->get('sef_ids');
  86          $categories = new RouterViewConfiguration('categories');
  87          $categories->setKey('id');
  88          $this->registerView($categories);
  89          $category = new RouterViewConfiguration('category');
  90          $category->setKey('id')->setParent($categories, 'catid')->setNestable()->addLayout('blog');
  91          $this->registerView($category);
  92          $article = new RouterViewConfiguration('article');
  93          $article->setKey('id')->setParent($category, 'catid');
  94          $this->registerView($article);
  95          $this->registerView(new RouterViewConfiguration('archive'));
  96          $this->registerView(new RouterViewConfiguration('featured'));
  97          $form = new RouterViewConfiguration('form');
  98          $form->setKey('a_id');
  99          $this->registerView($form);
 100  
 101          parent::__construct($app, $menu);
 102  
 103          $this->attachRule(new MenuRules($this));
 104          $this->attachRule(new StandardRules($this));
 105          $this->attachRule(new NomenuRules($this));
 106      }
 107  
 108      /**
 109       * Method to get the segment(s) for a category
 110       *
 111       * @param   string  $id     ID of the category to retrieve the segments for
 112       * @param   array   $query  The request that is built right now
 113       *
 114       * @return  array|string  The segments of this item
 115       */
 116      public function getCategorySegment($id, $query)
 117      {
 118          $category = $this->getCategories(['access' => true])->get($id);
 119  
 120          if ($category) {
 121              $path = array_reverse($category->getPath(), true);
 122              $path[0] = '1:root';
 123  
 124              if ($this->noIDs) {
 125                  foreach ($path as &$segment) {
 126                      list($id, $segment) = explode(':', $segment, 2);
 127                  }
 128              }
 129  
 130              return $path;
 131          }
 132  
 133          return array();
 134      }
 135  
 136      /**
 137       * Method to get the segment(s) for a category
 138       *
 139       * @param   string  $id     ID of the category to retrieve the segments for
 140       * @param   array   $query  The request that is built right now
 141       *
 142       * @return  array|string  The segments of this item
 143       */
 144      public function getCategoriesSegment($id, $query)
 145      {
 146          return $this->getCategorySegment($id, $query);
 147      }
 148  
 149      /**
 150       * Method to get the segment(s) for an article
 151       *
 152       * @param   string  $id     ID of the article to retrieve the segments for
 153       * @param   array   $query  The request that is built right now
 154       *
 155       * @return  array|string  The segments of this item
 156       */
 157      public function getArticleSegment($id, $query)
 158      {
 159          if (!strpos($id, ':')) {
 160              $id      = (int) $id;
 161              $dbquery = $this->db->getQuery(true);
 162              $dbquery->select($this->db->quoteName('alias'))
 163                  ->from($this->db->quoteName('#__content'))
 164                  ->where($this->db->quoteName('id') . ' = :id')
 165                  ->bind(':id', $id, ParameterType::INTEGER);
 166              $this->db->setQuery($dbquery);
 167  
 168              $id .= ':' . $this->db->loadResult();
 169          }
 170  
 171          if ($this->noIDs) {
 172              list($void, $segment) = explode(':', $id, 2);
 173  
 174              return array($void => $segment);
 175          }
 176  
 177          return array((int) $id => $id);
 178      }
 179  
 180      /**
 181       * Method to get the segment(s) for a form
 182       *
 183       * @param   string  $id     ID of the article form to retrieve the segments for
 184       * @param   array   $query  The request that is built right now
 185       *
 186       * @return  array|string  The segments of this item
 187       *
 188       * @since   3.7.3
 189       */
 190      public function getFormSegment($id, $query)
 191      {
 192          return $this->getArticleSegment($id, $query);
 193      }
 194  
 195      /**
 196       * Method to get the id for a category
 197       *
 198       * @param   string  $segment  Segment to retrieve the ID for
 199       * @param   array   $query    The request that is parsed right now
 200       *
 201       * @return  mixed   The id of this item or false
 202       */
 203      public function getCategoryId($segment, $query)
 204      {
 205          if (isset($query['id'])) {
 206              $category = $this->getCategories(['access' => false])->get($query['id']);
 207  
 208              if ($category) {
 209                  foreach ($category->getChildren() as $child) {
 210                      if ($this->noIDs) {
 211                          if ($child->alias == $segment) {
 212                              return $child->id;
 213                          }
 214                      } else {
 215                          if ($child->id == (int) $segment) {
 216                              return $child->id;
 217                          }
 218                      }
 219                  }
 220              }
 221          }
 222  
 223          return false;
 224      }
 225  
 226      /**
 227       * Method to get the segment(s) for a category
 228       *
 229       * @param   string  $segment  Segment to retrieve the ID for
 230       * @param   array   $query    The request that is parsed right now
 231       *
 232       * @return  mixed   The id of this item or false
 233       */
 234      public function getCategoriesId($segment, $query)
 235      {
 236          return $this->getCategoryId($segment, $query);
 237      }
 238  
 239      /**
 240       * Method to get the segment(s) for an article
 241       *
 242       * @param   string  $segment  Segment of the article to retrieve the ID for
 243       * @param   array   $query    The request that is parsed right now
 244       *
 245       * @return  mixed   The id of this item or false
 246       */
 247      public function getArticleId($segment, $query)
 248      {
 249          if ($this->noIDs) {
 250              $dbquery = $this->db->getQuery(true);
 251              $dbquery->select($this->db->quoteName('id'))
 252                  ->from($this->db->quoteName('#__content'))
 253                  ->where(
 254                      [
 255                          $this->db->quoteName('alias') . ' = :alias',
 256                          $this->db->quoteName('catid') . ' = :catid',
 257                      ]
 258                  )
 259                  ->bind(':alias', $segment)
 260                  ->bind(':catid', $query['id'], ParameterType::INTEGER);
 261              $this->db->setQuery($dbquery);
 262  
 263              return (int) $this->db->loadResult();
 264          }
 265  
 266          return (int) $segment;
 267      }
 268  
 269      /**
 270       * Method to get categories from cache
 271       *
 272       * @param   array  $options   The options for retrieving categories
 273       *
 274       * @return  CategoryInterface  The object containing categories
 275       *
 276       * @since   4.0.0
 277       */
 278      private function getCategories(array $options = []): CategoryInterface
 279      {
 280          $key = serialize($options);
 281  
 282          if (!isset($this->categoryCache[$key])) {
 283              $this->categoryCache[$key] = $this->categoryFactory->createCategory($options);
 284          }
 285  
 286          return $this->categoryCache[$key];
 287      }
 288  }


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