[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Component/Router/Rules/ -> MenuRules.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2016 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\Component\Router\Rules;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Component\Router\RouterView;
  14  use Joomla\CMS\Language\Multilanguage;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Rule to identify the right Itemid for a view in a component
  22   *
  23   * @since  3.4
  24   */
  25  class MenuRules implements RulesInterface
  26  {
  27      /**
  28       * Router this rule belongs to
  29       *
  30       * @var   RouterView
  31       * @since 3.4
  32       */
  33      protected $router;
  34  
  35      /**
  36       * Lookup array of the menu items
  37       *
  38       * @var   array
  39       * @since 3.4
  40       */
  41      protected $lookup = array();
  42  
  43      /**
  44       * Class constructor.
  45       *
  46       * @param   RouterView  $router  Router this rule belongs to
  47       *
  48       * @since   3.4
  49       */
  50      public function __construct(RouterView $router)
  51      {
  52          $this->router = $router;
  53  
  54          $this->buildLookup();
  55      }
  56  
  57      /**
  58       * Finds the right Itemid for this query
  59       *
  60       * @param   array  &$query  The query array to process
  61       *
  62       * @return  void
  63       *
  64       * @since   3.4
  65       */
  66      public function preprocess(&$query)
  67      {
  68          $active = $this->router->menu->getActive();
  69  
  70          /**
  71           * If the active item id is not the same as the supplied item id or we have a supplied item id and no active
  72           * menu item then we just use the supplied menu item and continue
  73           */
  74          if (isset($query['Itemid']) && ($active === null || $query['Itemid'] != $active->id)) {
  75              return;
  76          }
  77  
  78          // Get query language
  79          $language = isset($query['lang']) ? $query['lang'] : '*';
  80  
  81          // Set the language to the current one when multilang is enabled and item is tagged to ALL
  82          if (Multilanguage::isEnabled() && $language === '*') {
  83              $language = $this->router->app->get('language');
  84          }
  85  
  86          if (!isset($this->lookup[$language])) {
  87              $this->buildLookup($language);
  88          }
  89  
  90          // Check if the active menu item matches the requested query
  91          if ($active !== null && isset($query['Itemid'])) {
  92              // Check if active->query and supplied query are the same
  93              $match = true;
  94  
  95              foreach ($active->query as $k => $v) {
  96                  if (isset($query[$k]) && $v !== $query[$k]) {
  97                      // Compare again without alias
  98                      if (\is_string($v) && $v == current(explode(':', $query[$k], 2))) {
  99                          continue;
 100                      }
 101  
 102                      $match = false;
 103                      break;
 104                  }
 105              }
 106  
 107              if ($match) {
 108                  // Just use the supplied menu item
 109                  return;
 110              }
 111          }
 112  
 113          $needles = $this->router->getPath($query);
 114  
 115          $layout = isset($query['layout']) && $query['layout'] !== 'default' ? ':' . $query['layout'] : '';
 116  
 117          if ($needles) {
 118              foreach ($needles as $view => $ids) {
 119                  $viewLayout = $view . $layout;
 120  
 121                  if ($layout && isset($this->lookup[$language][$viewLayout])) {
 122                      if (\is_bool($ids)) {
 123                          $query['Itemid'] = $this->lookup[$language][$viewLayout];
 124  
 125                          return;
 126                      }
 127  
 128                      foreach ($ids as $id => $segment) {
 129                          if (isset($this->lookup[$language][$viewLayout][(int) $id])) {
 130                              $query['Itemid'] = $this->lookup[$language][$viewLayout][(int) $id];
 131  
 132                              return;
 133                          }
 134                      }
 135                  }
 136  
 137                  if (isset($this->lookup[$language][$view])) {
 138                      if (\is_bool($ids)) {
 139                          $query['Itemid'] = $this->lookup[$language][$view];
 140  
 141                          return;
 142                      }
 143  
 144                      foreach ($ids as $id => $segment) {
 145                          if (isset($this->lookup[$language][$view][(int) $id])) {
 146                              $query['Itemid'] = $this->lookup[$language][$view][(int) $id];
 147  
 148                              return;
 149                          }
 150                      }
 151                  }
 152              }
 153          }
 154  
 155          // Check if the active menuitem matches the requested language
 156          if (
 157              $active && $active->component === 'com_' . $this->router->getName()
 158              && ($language === '*' || \in_array($active->language, array('*', $language)) || !Multilanguage::isEnabled())
 159          ) {
 160              $query['Itemid'] = $active->id;
 161  
 162              return;
 163          }
 164  
 165          // If not found, return language specific home link
 166          $default = $this->router->menu->getDefault($language);
 167  
 168          if (!empty($default->id)) {
 169              $query['Itemid'] = $default->id;
 170          }
 171      }
 172  
 173      /**
 174       * Method to build the lookup array
 175       *
 176       * @param   string  $language  The language that the lookup should be built up for
 177       *
 178       * @return  void
 179       *
 180       * @since   3.4
 181       */
 182      protected function buildLookup($language = '*')
 183      {
 184          // Prepare the reverse lookup array.
 185          if (!isset($this->lookup[$language])) {
 186              $this->lookup[$language] = array();
 187  
 188              $component  = ComponentHelper::getComponent('com_' . $this->router->getName());
 189              $views = $this->router->getViews();
 190  
 191              $attributes = array('component_id');
 192              $values     = array((int) $component->id);
 193  
 194              $attributes[] = 'language';
 195              $values[]     = array($language, '*');
 196  
 197              $items = $this->router->menu->getItems($attributes, $values);
 198  
 199              foreach ($items as $item) {
 200                  if (isset($item->query['view'], $views[$item->query['view']])) {
 201                      $view = $item->query['view'];
 202  
 203                      $layout = '';
 204  
 205                      if (isset($item->query['layout'])) {
 206                          $layout = ':' . $item->query['layout'];
 207                      }
 208  
 209                      if ($views[$view]->key) {
 210                          if (!isset($this->lookup[$language][$view . $layout])) {
 211                              $this->lookup[$language][$view . $layout] = array();
 212                          }
 213  
 214                          if (!isset($this->lookup[$language][$view])) {
 215                              $this->lookup[$language][$view] = array();
 216                          }
 217  
 218                          // If menuitem has no key set, we assume 0.
 219                          if (!isset($item->query[$views[$view]->key])) {
 220                              $item->query[$views[$view]->key] = 0;
 221                          }
 222  
 223                          /**
 224                           * Here it will become a bit tricky
 225                           * language != * can override existing entries
 226                           * language == * cannot override existing entries
 227                           */
 228                          if (!isset($this->lookup[$language][$view . $layout][$item->query[$views[$view]->key]]) || $item->language !== '*') {
 229                              $this->lookup[$language][$view . $layout][$item->query[$views[$view]->key]] = $item->id;
 230                              $this->lookup[$language][$view][$item->query[$views[$view]->key]] = $item->id;
 231                          }
 232                      } else {
 233                          /**
 234                           * Here it will become a bit tricky
 235                           * language != * can override existing entries
 236                           * language == * cannot override existing entries
 237                           */
 238                          if (!isset($this->lookup[$language][$view . $layout]) || $item->language !== '*') {
 239                              $this->lookup[$language][$view . $layout] = $item->id;
 240                          }
 241                      }
 242                  }
 243              }
 244          }
 245      }
 246  
 247      /**
 248       * Dummy method to fulfil the interface requirements
 249       *
 250       * @param   array  &$segments  The URL segments to parse
 251       * @param   array  &$vars      The vars that result from the segments
 252       *
 253       * @return  void
 254       *
 255       * @since   3.4
 256       * @codeCoverageIgnore
 257       */
 258      public function parse(&$segments, &$vars)
 259      {
 260      }
 261  
 262      /**
 263       * Dummy method to fulfil the interface requirements
 264       *
 265       * @param   array  &$query     The vars that should be converted
 266       * @param   array  &$segments  The URL segments to create
 267       *
 268       * @return  void
 269       *
 270       * @since   3.4
 271       * @codeCoverageIgnore
 272       */
 273      public function build(&$query, &$segments)
 274      {
 275      }
 276  }


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