[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/modules/mod_submenu/src/Menu/ -> Menu.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  mod_submenu
   6   *
   7   * @copyright   (C) 2019 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\Submenu\Administrator\Menu;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Associations;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Menu\MenuItem;
  18  use Joomla\CMS\Uri\Uri;
  19  use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
  20  use Joomla\Utilities\ArrayHelper;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Helper class to handle permissions in mod_submenu
  28   *
  29   * @since  4.0.0
  30   */
  31  abstract class Menu
  32  {
  33      /**
  34       * Filter and perform other preparatory tasks for loaded menu items based on access rights and module configurations for display
  35       *
  36       * @param   MenuItem  $parent  A menu item to process
  37       *
  38       * @return  void
  39       *
  40       * @since   4.0.0
  41       */
  42      public static function preprocess($parent)
  43      {
  44          $app      = Factory::getApplication();
  45          $user     = $app->getIdentity();
  46          $children = $parent->getChildren();
  47          $language = Factory::getLanguage();
  48  
  49          /**
  50           * Trigger onPreprocessMenuItems for the current level of backend menu items.
  51           * $children is an array of MenuItem objects. A plugin can traverse the whole tree,
  52           * but new nodes will only be run through this method if their parents have not been processed yet.
  53           */
  54          $app->triggerEvent('onPreprocessMenuItems', array('administrator.module.mod_submenu', $children));
  55  
  56          foreach ($children as $item) {
  57              if (substr($item->link, 0, 8) === 'special:') {
  58                  $special = substr($item->link, 8);
  59  
  60                  if ($special === 'language-forum') {
  61                      $item->link = 'index.php?option=com_admin&amp;view=help&amp;layout=langforum';
  62                  }
  63              }
  64  
  65              $uri   = new Uri($item->link);
  66              $query = $uri->getQuery(true);
  67  
  68              /**
  69               * This is needed to populate the element property when the component is no longer
  70               * installed but its core menu items are left behind.
  71               */
  72              if ($option = $uri->getVar('option')) {
  73                  $item->element = $option;
  74              }
  75  
  76              // Exclude item if is not enabled
  77              if ($item->element && !ComponentHelper::isEnabled($item->element)) {
  78                  $parent->removeChild($item);
  79                  continue;
  80              }
  81  
  82              /*
  83               * Multilingual Associations if the site is not set as multilingual and/or Associations is not enabled in
  84               * the Language Filter plugin
  85               */
  86  
  87              if ($item->element === 'com_associations' && !Associations::isEnabled()) {
  88                  $parent->removeChild($item);
  89                  continue;
  90              }
  91  
  92              $itemParams = $item->getParams();
  93  
  94              // Exclude item with menu item option set to exclude from menu modules
  95              if ($itemParams->get('menu-permission')) {
  96                  @list($action, $asset) = explode(';', $itemParams->get('menu-permission'));
  97  
  98                  if (!$user->authorise($action, $asset)) {
  99                      $parent->removeChild($item);
 100                      continue;
 101                  }
 102              }
 103  
 104              // Populate automatic children for container items
 105              if ($item->type === 'container') {
 106                  $exclude    = (array) $itemParams->get('hideitems') ?: array();
 107                  $components = MenusHelper::getMenuItems('main', false, $exclude);
 108  
 109                  // We are adding the nodes first to preprocess them, then sort them and add them again.
 110                  foreach ($components->getChildren() as $c) {
 111                      if (!$c->hasChildren()) {
 112                          $temp = clone $c;
 113                          $c->addChild($temp);
 114                      }
 115  
 116                      $item->addChild($c);
 117                  }
 118  
 119                  self::preprocess($item);
 120                  $children = ArrayHelper::sortObjects($item->getChildren(), 'text', 1, false, true);
 121  
 122                  foreach ($children as $c) {
 123                      $parent->addChild($c);
 124                  }
 125  
 126                  $parent->removeChild($item);
 127                  continue;
 128              }
 129  
 130              // Exclude Mass Mail if disabled in global configuration
 131              if ($item->scope === 'massmail' && ($app->get('massmailoff', 0) == 1)) {
 132                  $parent->removeChild($item);
 133                  continue;
 134              }
 135  
 136              if ($item->element === 'com_fields') {
 137                  parse_str($item->link, $query);
 138  
 139                  // Only display Fields menus when enabled in the component
 140                  $createFields = null;
 141  
 142                  if (isset($query['context'])) {
 143                      $createFields = ComponentHelper::getParams(strstr($query['context'], '.', true))->get('custom_fields_enable', 1);
 144                  }
 145  
 146                  if (!$createFields || !$user->authorise('core.manage', 'com_users')) {
 147                      $parent->removeChild($item);
 148                      continue;
 149                  }
 150              } elseif ($item->element === 'com_workflow') {
 151                  parse_str($item->link, $query);
 152  
 153                  // Only display Workflow menus when enabled in the component
 154                  $workflow = null;
 155  
 156                  if (isset($query['extension'])) {
 157                      $parts = explode('.', $query['extension']);
 158  
 159                      $workflow = ComponentHelper::getParams($parts[0])->get('workflow_enabled');
 160                  }
 161  
 162                  if (!$workflow) {
 163                      $parent->removeChild($item);
 164                      continue;
 165                  }
 166  
 167                  [$assetName] = isset($query['extension']) ? explode('.', $query['extension'], 2) : array('com_workflow');
 168              } elseif (\in_array($item->element, array('com_config', 'com_privacy', 'com_actionlogs'), true) && !$user->authorise('core.admin')) {
 169                  // Special case for components which only allow super user access
 170                  $parent->removeChild($item);
 171                  continue;
 172              } elseif ($item->element === 'com_joomlaupdate' && !$user->authorise('core.admin')) {
 173                  $parent->removeChild($item);
 174                  continue;
 175              } elseif (
 176                  ($item->link === 'index.php?option=com_installer&view=install' || $item->link === 'index.php?option=com_installer&view=languages')
 177                  && !$user->authorise('core.admin')
 178              ) {
 179                  continue;
 180              } elseif ($item->element === 'com_admin') {
 181                  parse_str($item->link, $query);
 182  
 183                  if (isset($query['view']) && $query['view'] === 'sysinfo' && !$user->authorise('core.admin')) {
 184                      $parent->removeChild($item);
 185                      continue;
 186                  }
 187              } elseif ($item->element && !$user->authorise(($item->scope === 'edit') ? 'core.create' : 'core.manage', $item->element)) {
 188                  $parent->removeChild($item);
 189                  continue;
 190              } elseif ($item->element === 'com_menus') {
 191                  // Get badges for Menus containing a Home page.
 192                  $iconImage = $item->icon;
 193  
 194                  if ($iconImage) {
 195                      if (substr($iconImage, 0, 6) === 'class:' && substr($iconImage, 6) === 'icon-home') {
 196                          $iconImage = '<span class="home-image icon-home" aria-hidden="true"></span>';
 197                          $iconImage .= '<span class="visually-hidden">' . Text::_('JDEFAULT') . '</span>';
 198                      } elseif (substr($iconImage, 0, 6) === 'image:') {
 199                          $iconImage = '&nbsp;<span class="badge bg-secondary">' . substr($iconImage, 6) . '</span>';
 200                      }
 201  
 202                      $item->iconImage = $iconImage;
 203                  }
 204              }
 205  
 206              if ($item->hasChildren()) {
 207                  self::preprocess($item);
 208              }
 209  
 210              // Ok we passed everything, load language at last only
 211              if ($item->element) {
 212                  $language->load($item->element . '.sys', JPATH_ADMINISTRATOR) ||
 213                  $language->load($item->element . '.sys', JPATH_ADMINISTRATOR . '/components/' . $item->element);
 214              }
 215  
 216              if ($item->type === 'separator' && $item->getParams()->get('text_separator') == 0) {
 217                  $item->title = '';
 218              }
 219  
 220              $item->text = Text::_($item->title);
 221          }
 222      }
 223  }


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