[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> MenuField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2010 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\Form\Field;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\Database\ParameterType;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Supports an HTML select list of menus
  22   *
  23   * @since  1.6
  24   */
  25  class MenuField extends GroupedlistField
  26  {
  27      /**
  28       * The form field type.
  29       *
  30       * @var    string
  31       * @since  1.6
  32       */
  33      public $type = 'Menu';
  34  
  35      /**
  36       * Method to get the field option groups.
  37       *
  38       * @return  array  The field option objects as a nested array in groups.
  39       *
  40       * @since   1.7.0
  41       * @throws  \UnexpectedValueException
  42       */
  43      protected function getGroups()
  44      {
  45          $clientId   = (string) $this->element['clientid'];
  46          $accessType = (string) $this->element['accesstype'];
  47          $showAll    = (string) $this->element['showAll'] === 'true';
  48  
  49          $db    = $this->getDatabase();
  50          $query = $db->getQuery(true)
  51              ->select(
  52                  [
  53                      $db->quoteName('id'),
  54                      $db->quoteName('menutype', 'value'),
  55                      $db->quoteName('title', 'text'),
  56                      $db->quoteName('client_id'),
  57                  ]
  58              )
  59              ->from($db->quoteName('#__menu_types'))
  60              ->order(
  61                  [
  62                      $db->quoteName('client_id'),
  63                      $db->quoteName('title'),
  64                  ]
  65              );
  66  
  67          if (\strlen($clientId)) {
  68              $client = (int) $clientId;
  69              $query->where($db->quoteName('client_id') . ' = :client')
  70                  ->bind(':client', $client, ParameterType::INTEGER);
  71          }
  72  
  73          $menus = $db->setQuery($query)->loadObjectList();
  74  
  75          if ($accessType) {
  76              $user = Factory::getUser();
  77  
  78              foreach ($menus as $key => $menu) {
  79                  switch ($accessType) {
  80                      case 'create':
  81                      case 'manage':
  82                          if (!$user->authorise('core.' . $accessType, 'com_menus.menu.' . (int) $menu->id)) {
  83                              unset($menus[$key]);
  84                          }
  85                          break;
  86  
  87                      // Editing a menu item is a bit tricky, we have to check the current menutype for core.edit and all others for core.create
  88                      case 'edit':
  89                          $check = $this->value == $menu->value ? 'edit' : 'create';
  90  
  91                          if (!$user->authorise('core.' . $check, 'com_menus.menu.' . (int) $menu->id)) {
  92                              unset($menus[$key]);
  93                          }
  94                          break;
  95                  }
  96              }
  97          }
  98  
  99          $opts = array();
 100  
 101          // Protected menutypes can be shown if requested
 102          if ($clientId == 1 && $showAll) {
 103              $opts[] = (object) array(
 104                  'value'     => 'main',
 105                  'text'      => Text::_('COM_MENUS_MENU_TYPE_PROTECTED_MAIN_LABEL'),
 106                  'client_id' => 1,
 107              );
 108          }
 109  
 110          $options = array_merge($opts, $menus);
 111          $groups  = array();
 112  
 113          if (\strlen($clientId)) {
 114              $groups[0] = $options;
 115          } else {
 116              foreach ($options as $option) {
 117                  // If client id is not specified we group the items.
 118                  $label = ($option->client_id == 1 ? Text::_('JADMINISTRATOR') : Text::_('JSITE'));
 119  
 120                  $groups[$label][] = $option;
 121              }
 122          }
 123  
 124          // Merge any additional options in the XML definition.
 125          return array_merge(parent::getGroups(), $groups);
 126      }
 127  }


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