[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

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


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