[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_newsfeeds/src/Service/ -> Router.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_newsfeeds
   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\Newsfeeds\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 from com_newsfeeds
  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       * Newsfeeds 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_newsfeeds');
  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();
  91          $this->registerView($category);
  92          $newsfeed = new RouterViewConfiguration('newsfeed');
  93          $newsfeed->setKey('id')->setParent($category, 'catid');
  94          $this->registerView($newsfeed);
  95  
  96          parent::__construct($app, $menu);
  97  
  98          $this->attachRule(new MenuRules($this));
  99          $this->attachRule(new StandardRules($this));
 100          $this->attachRule(new NomenuRules($this));
 101      }
 102  
 103      /**
 104       * Method to get the segment(s) for a category
 105       *
 106       * @param   string  $id     ID of the category to retrieve the segments for
 107       * @param   array   $query  The request that is built right now
 108       *
 109       * @return  array|string  The segments of this item
 110       */
 111      public function getCategorySegment($id, $query)
 112      {
 113          $category = $this->getCategories()->get($id);
 114  
 115          if ($category) {
 116              $path = array_reverse($category->getPath(), true);
 117              $path[0] = '1:root';
 118  
 119              if ($this->noIDs) {
 120                  foreach ($path as &$segment) {
 121                      list($id, $segment) = explode(':', $segment, 2);
 122                  }
 123              }
 124  
 125              return $path;
 126          }
 127  
 128          return array();
 129      }
 130  
 131      /**
 132       * Method to get the segment(s) for a category
 133       *
 134       * @param   string  $id     ID of the category to retrieve the segments for
 135       * @param   array   $query  The request that is built right now
 136       *
 137       * @return  array|string  The segments of this item
 138       */
 139      public function getCategoriesSegment($id, $query)
 140      {
 141          return $this->getCategorySegment($id, $query);
 142      }
 143  
 144      /**
 145       * Method to get the segment(s) for a newsfeed
 146       *
 147       * @param   string  $id     ID of the newsfeed to retrieve the segments for
 148       * @param   array   $query  The request that is built right now
 149       *
 150       * @return  array|string  The segments of this item
 151       */
 152      public function getNewsfeedSegment($id, $query)
 153      {
 154          if (!strpos($id, ':')) {
 155              $id      = (int) $id;
 156              $dbquery = $this->db->getQuery(true);
 157              $dbquery->select($this->db->quoteName('alias'))
 158                  ->from($this->db->quoteName('#__newsfeeds'))
 159                  ->where($this->db->quoteName('id') . ' = :id')
 160                  ->bind(':id', $id, ParameterType::INTEGER);
 161              $this->db->setQuery($dbquery);
 162  
 163              $id .= ':' . $this->db->loadResult();
 164          }
 165  
 166          if ($this->noIDs) {
 167              list($void, $segment) = explode(':', $id, 2);
 168  
 169              return array($void => $segment);
 170          }
 171  
 172          return array((int) $id => $id);
 173      }
 174  
 175      /**
 176       * Method to get the id for a category
 177       *
 178       * @param   string  $segment  Segment to retrieve the ID for
 179       * @param   array   $query    The request that is parsed right now
 180       *
 181       * @return  mixed   The id of this item or false
 182       */
 183      public function getCategoryId($segment, $query)
 184      {
 185          if (isset($query['id'])) {
 186              $category = $this->getCategories(['access' => false])->get($query['id']);
 187  
 188              if ($category) {
 189                  foreach ($category->getChildren() as $child) {
 190                      if ($this->noIDs) {
 191                          if ($child->alias === $segment) {
 192                              return $child->id;
 193                          }
 194                      } else {
 195                          if ($child->id == (int) $segment) {
 196                              return $child->id;
 197                          }
 198                      }
 199                  }
 200              }
 201          }
 202  
 203          return false;
 204      }
 205  
 206      /**
 207       * Method to get the segment(s) for a category
 208       *
 209       * @param   string  $segment  Segment to retrieve the ID for
 210       * @param   array   $query    The request that is parsed right now
 211       *
 212       * @return  mixed   The id of this item or false
 213       */
 214      public function getCategoriesId($segment, $query)
 215      {
 216          return $this->getCategoryId($segment, $query);
 217      }
 218  
 219      /**
 220       * Method to get the segment(s) for a newsfeed
 221       *
 222       * @param   string  $segment  Segment of the newsfeed to retrieve the ID for
 223       * @param   array   $query    The request that is parsed right now
 224       *
 225       * @return  mixed   The id of this item or false
 226       */
 227      public function getNewsfeedId($segment, $query)
 228      {
 229          if ($this->noIDs) {
 230              $dbquery = $this->db->getQuery(true);
 231              $dbquery->select($this->db->quoteName('id'))
 232                  ->from($this->db->quoteName('#__newsfeeds'))
 233                  ->where(
 234                      [
 235                          $this->db->quoteName('alias') . ' = :segment',
 236                          $this->db->quoteName('catid') . ' = :id',
 237                      ]
 238                  )
 239                  ->bind(':segment', $segment)
 240                  ->bind(':id', $query['id'], ParameterType::INTEGER);
 241              $this->db->setQuery($dbquery);
 242  
 243              return (int) $this->db->loadResult();
 244          }
 245  
 246          return (int) $segment;
 247      }
 248  
 249      /**
 250       * Method to get categories from cache
 251       *
 252       * @param   array  $options   The options for retrieving categories
 253       *
 254       * @return  CategoryInterface  The object containing categories
 255       *
 256       * @since   4.0.0
 257       */
 258      private function getCategories(array $options = []): CategoryInterface
 259      {
 260          $key = serialize($options);
 261  
 262          if (!isset($this->categoryCache[$key])) {
 263              $this->categoryCache[$key] = $this->categoryFactory->createCategory($options);
 264          }
 265  
 266          return $this->categoryCache[$key];
 267      }
 268  }


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