[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_menu/src/Helper/ -> MenuHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_menu
   6   *
   7   * @copyright   (C) 2009 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\Module\Menu\Site\Helper;
  12  
  13  use Joomla\CMS\Cache\CacheControllerFactoryInterface;
  14  use Joomla\CMS\Cache\Controller\OutputController;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\Multilanguage;
  17  use Joomla\CMS\Router\Route;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Helper for mod_menu
  25   *
  26   * @since  1.5
  27   */
  28  class MenuHelper
  29  {
  30      /**
  31       * Get a list of the menu items.
  32       *
  33       * @param   \Joomla\Registry\Registry  &$params  The module options.
  34       *
  35       * @return  array
  36       *
  37       * @since   1.5
  38       */
  39      public static function getList(&$params)
  40      {
  41          $app   = Factory::getApplication();
  42          $menu  = $app->getMenu();
  43  
  44          // Get active menu item
  45          $base   = self::getBase($params);
  46          $levels = Factory::getUser()->getAuthorisedViewLevels();
  47          asort($levels);
  48  
  49          // Compose cache key
  50          $cacheKey = 'menu_items' . $params . implode(',', $levels) . '.' . $base->id;
  51  
  52          /** @var OutputController $cache */
  53          $cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)
  54              ->createCacheController('output', ['defaultgroup' => 'mod_menu']);
  55  
  56          if ($cache->contains($cacheKey)) {
  57              $items = $cache->get($cacheKey);
  58          } else {
  59              $path           = $base->tree;
  60              $start          = (int) $params->get('startLevel', 1);
  61              $end            = (int) $params->get('endLevel', 0);
  62              $showAll        = $params->get('showAllChildren', 1);
  63              $items          = $menu->getItems('menutype', $params->get('menutype'));
  64              $hidden_parents = array();
  65              $lastitem       = 0;
  66  
  67              if ($items) {
  68                  $inputVars = $app->getInput()->getArray();
  69  
  70                  foreach ($items as $i => $item) {
  71                      $item->parent = false;
  72                      $itemParams   = $item->getParams();
  73  
  74                      if (isset($items[$lastitem]) && $items[$lastitem]->id == $item->parent_id && $itemParams->get('menu_show', 1) == 1) {
  75                          $items[$lastitem]->parent = true;
  76                      }
  77  
  78                      if (
  79                          ($start && $start > $item->level)
  80                          || ($end && $item->level > $end)
  81                          || (!$showAll && $item->level > 1 && !\in_array($item->parent_id, $path))
  82                          || ($start > 1 && !\in_array($item->tree[$start - 2], $path))
  83                      ) {
  84                          unset($items[$i]);
  85                          continue;
  86                      }
  87  
  88                      // Exclude item with menu item option set to exclude from menu modules
  89                      if (($itemParams->get('menu_show', 1) == 0) || \in_array($item->parent_id, $hidden_parents)) {
  90                          $hidden_parents[] = $item->id;
  91                          unset($items[$i]);
  92                          continue;
  93                      }
  94  
  95                      $item->current = true;
  96  
  97                      foreach ($item->query as $key => $value) {
  98                          if (!isset($inputVars[$key]) || $inputVars[$key] !== $value) {
  99                              $item->current = false;
 100                              break;
 101                          }
 102                      }
 103  
 104                      $item->deeper     = false;
 105                      $item->shallower  = false;
 106                      $item->level_diff = 0;
 107  
 108                      if (isset($items[$lastitem])) {
 109                          $items[$lastitem]->deeper     = ($item->level > $items[$lastitem]->level);
 110                          $items[$lastitem]->shallower  = ($item->level < $items[$lastitem]->level);
 111                          $items[$lastitem]->level_diff = ($items[$lastitem]->level - $item->level);
 112                      }
 113  
 114                      $lastitem     = $i;
 115                      $item->active = false;
 116                      $item->flink  = $item->link;
 117  
 118                      // Reverted back for CMS version 2.5.6
 119                      switch ($item->type) {
 120                          case 'separator':
 121                              break;
 122  
 123                          case 'heading':
 124                              // No further action needed.
 125                              break;
 126  
 127                          case 'url':
 128                              if ((strpos($item->link, 'index.php?') === 0) && (strpos($item->link, 'Itemid=') === false)) {
 129                                  // If this is an internal Joomla link, ensure the Itemid is set.
 130                                  $item->flink = $item->link . '&Itemid=' . $item->id;
 131                              }
 132                              break;
 133  
 134                          case 'alias':
 135                              $item->flink = 'index.php?Itemid=' . $itemParams->get('aliasoptions');
 136  
 137                              // Get the language of the target menu item when site is multilingual
 138                              if (Multilanguage::isEnabled()) {
 139                                  $newItem = Factory::getApplication()->getMenu()->getItem((int) $itemParams->get('aliasoptions'));
 140  
 141                                  // Use language code if not set to ALL
 142                                  if ($newItem != null && $newItem->language && $newItem->language !== '*') {
 143                                      $item->flink .= '&lang=' . $newItem->language;
 144                                  }
 145                              }
 146                              break;
 147  
 148                          default:
 149                              $item->flink = 'index.php?Itemid=' . $item->id;
 150                              break;
 151                      }
 152  
 153                      if ((strpos($item->flink, 'index.php?') !== false) && strcasecmp(substr($item->flink, 0, 4), 'http')) {
 154                          $item->flink = Route::_($item->flink, true, $itemParams->get('secure'));
 155                      } else {
 156                          $item->flink = Route::_($item->flink);
 157                      }
 158  
 159                      // We prevent the double encoding because for some reason the $item is shared for menu modules and we get double encoding
 160                      // when the cause of that is found the argument should be removed
 161                      $item->title          = htmlspecialchars($item->title, ENT_COMPAT, 'UTF-8', false);
 162                      $item->menu_icon      = htmlspecialchars($itemParams->get('menu_icon_css', ''), ENT_COMPAT, 'UTF-8', false);
 163                      $item->anchor_css     = htmlspecialchars($itemParams->get('menu-anchor_css', ''), ENT_COMPAT, 'UTF-8', false);
 164                      $item->anchor_title   = htmlspecialchars($itemParams->get('menu-anchor_title', ''), ENT_COMPAT, 'UTF-8', false);
 165                      $item->anchor_rel     = htmlspecialchars($itemParams->get('menu-anchor_rel', ''), ENT_COMPAT, 'UTF-8', false);
 166                      $item->menu_image     = htmlspecialchars($itemParams->get('menu_image', ''), ENT_COMPAT, 'UTF-8', false);
 167                      $item->menu_image_css = htmlspecialchars($itemParams->get('menu_image_css', ''), ENT_COMPAT, 'UTF-8', false);
 168                  }
 169  
 170                  if (isset($items[$lastitem])) {
 171                      $items[$lastitem]->deeper     = (($start ?: 1) > $items[$lastitem]->level);
 172                      $items[$lastitem]->shallower  = (($start ?: 1) < $items[$lastitem]->level);
 173                      $items[$lastitem]->level_diff = ($items[$lastitem]->level - ($start ?: 1));
 174                  }
 175              }
 176  
 177              $cache->store($items, $cacheKey);
 178          }
 179  
 180          return $items;
 181      }
 182  
 183      /**
 184       * Get base menu item.
 185       *
 186       * @param   \Joomla\Registry\Registry  &$params  The module options.
 187       *
 188       * @return  object
 189       *
 190       * @since    3.0.2
 191       */
 192      public static function getBase(&$params)
 193      {
 194          // Get base menu item from parameters
 195          if ($params->get('base')) {
 196              $base = Factory::getApplication()->getMenu()->getItem($params->get('base'));
 197          } else {
 198              $base = false;
 199          }
 200  
 201          // Use active menu item if no base found
 202          if (!$base) {
 203              $base = self::getActive($params);
 204          }
 205  
 206          return $base;
 207      }
 208  
 209      /**
 210       * Get active menu item.
 211       *
 212       * @param   \Joomla\Registry\Registry  &$params  The module options.
 213       *
 214       * @return  object
 215       *
 216       * @since    3.0.2
 217       */
 218      public static function getActive(&$params)
 219      {
 220          $menu = Factory::getApplication()->getMenu();
 221  
 222          return $menu->getActive() ?: self::getDefault();
 223      }
 224  
 225      /**
 226       * Get default menu item (home page) for current language.
 227       *
 228       * @return  object
 229       */
 230      public static function getDefault()
 231      {
 232          $menu = Factory::getApplication()->getMenu();
 233  
 234          // Look for the home menu
 235          if (Multilanguage::isEnabled()) {
 236              return $menu->getDefault(Factory::getLanguage()->getTag());
 237          }
 238  
 239          return $menu->getDefault();
 240      }
 241  }


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