[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/Field/ -> MenuItemByTypeField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_menus
   6   *
   7   * @copyright   (C) 2017 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\Menus\Administrator\Field;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Field\GroupedlistField;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Supports an HTML grouped select list of menu item grouped by menu
  24   *
  25   * @since  3.8.0
  26   */
  27  class MenuItemByTypeField extends GroupedlistField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var    string
  33       * @since  3.8.0
  34       */
  35      public $type = 'MenuItemByType';
  36  
  37      /**
  38       * The menu type.
  39       *
  40       * @var    string
  41       * @since  3.8.0
  42       */
  43      protected $menuType;
  44  
  45      /**
  46       * The client id.
  47       *
  48       * @var    string
  49       * @since  3.8.0
  50       */
  51      protected $clientId;
  52  
  53      /**
  54       * The language.
  55       *
  56       * @var    array
  57       * @since  3.8.0
  58       */
  59      protected $language;
  60  
  61      /**
  62       * The published status.
  63       *
  64       * @var    array
  65       * @since  3.8.0
  66       */
  67      protected $published;
  68  
  69      /**
  70       * The disabled status.
  71       *
  72       * @var    array
  73       * @since  3.8.0
  74       */
  75      protected $disable;
  76  
  77      /**
  78       * Method to get certain otherwise inaccessible properties from the form field object.
  79       *
  80       * @param   string  $name  The property name for which to get the value.
  81       *
  82       * @return  mixed  The property value or null.
  83       *
  84       * @since   3.8.0
  85       */
  86      public function __get($name)
  87      {
  88          switch ($name) {
  89              case 'menuType':
  90              case 'clientId':
  91              case 'language':
  92              case 'published':
  93              case 'disable':
  94                  return $this->$name;
  95          }
  96  
  97          return parent::__get($name);
  98      }
  99  
 100      /**
 101       * Method to set certain otherwise inaccessible properties of the form field object.
 102       *
 103       * @param   string  $name   The property name for which to set the value.
 104       * @param   mixed   $value  The value of the property.
 105       *
 106       * @return  void
 107       *
 108       * @since   3.8.0
 109       */
 110      public function __set($name, $value)
 111      {
 112          switch ($name) {
 113              case 'menuType':
 114                  $this->menuType = (string) $value;
 115                  break;
 116  
 117              case 'clientId':
 118                  $this->clientId = (int) $value;
 119                  break;
 120  
 121              case 'language':
 122              case 'published':
 123              case 'disable':
 124                  $value = (string) $value;
 125                  $this->$name = $value ? explode(',', $value) : array();
 126                  break;
 127  
 128              default:
 129                  parent::__set($name, $value);
 130          }
 131      }
 132  
 133      /**
 134       * Method to attach a JForm object to the field.
 135       *
 136       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 137       * @param   mixed              $value    The form field value to validate.
 138       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 139       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 140       *                                       full field name would end up being "bar[foo]".
 141       *
 142       * @return  boolean  True on success.
 143       *
 144       * @see     \Joomla\CMS\Form\FormField::setup()
 145       * @since   3.8.0
 146       */
 147      public function setup(\SimpleXMLElement $element, $value, $group = null)
 148      {
 149          $result = parent::setup($element, $value, $group);
 150  
 151          if ($result == true) {
 152              $menuType = (string) $this->element['menu_type'];
 153  
 154              if (!$menuType) {
 155                  $app = Factory::getApplication();
 156                  $currentMenuType = $app->getUserState('com_menus.items.menutype', '');
 157                  $menuType        = $app->input->getString('menutype', $currentMenuType);
 158              }
 159  
 160              $this->menuType  = $menuType;
 161              $this->clientId  = (int) $this->element['client_id'];
 162              $this->published = $this->element['published'] ? explode(',', (string) $this->element['published']) : array();
 163              $this->disable   = $this->element['disable'] ? explode(',', (string) $this->element['disable']) : array();
 164              $this->language  = $this->element['language'] ? explode(',', (string) $this->element['language']) : array();
 165          }
 166  
 167          return $result;
 168      }
 169  
 170      /**
 171       * Method to get the field option groups.
 172       *
 173       * @return  array  The field option objects as a nested array in groups.
 174       *
 175       * @since   3.8.0
 176       */
 177      protected function getGroups()
 178      {
 179          $groups = array();
 180  
 181          $menuType = $this->menuType;
 182  
 183          // Get the menu items.
 184          $items = MenusHelper::getMenuLinks($menuType, 0, 0, $this->published, $this->language, $this->clientId);
 185  
 186          // Build group for a specific menu type.
 187          if ($menuType) {
 188              // If the menutype is empty, group the items by menutype.
 189              $db    = $this->getDatabase();
 190              $query = $db->getQuery(true)
 191                  ->select($db->quoteName('title'))
 192                  ->from($db->quoteName('#__menu_types'))
 193                  ->where($db->quoteName('menutype') . ' = :menuType')
 194                  ->bind(':menuType', $menuType);
 195              $db->setQuery($query);
 196  
 197              try {
 198                  $menuTitle = $db->loadResult();
 199              } catch (\RuntimeException $e) {
 200                  $menuTitle = $menuType;
 201              }
 202  
 203              // Initialize the group.
 204              $groups[$menuTitle] = array();
 205  
 206              // Build the options array.
 207              foreach ($items as $key => $link) {
 208                  // Unset if item is menu_item_root
 209                  if ($link->text === 'Menu_Item_Root') {
 210                      unset($items[$key]);
 211                      continue;
 212                  }
 213  
 214                  $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
 215  
 216                  // Displays language code if not set to All
 217                  if ($link->language !== '*') {
 218                      $lang = ' (' . $link->language . ')';
 219                  } else {
 220                      $lang = '';
 221                  }
 222  
 223                  $groups[$menuTitle][] = HTMLHelper::_(
 224                      'select.option',
 225                      $link->value,
 226                      $levelPrefix . $link->text . $lang,
 227                      'value',
 228                      'text',
 229                      in_array($link->type, $this->disable)
 230                  );
 231              }
 232          } else {
 233              // Build groups for all menu types.
 234              // Build the groups arrays.
 235              foreach ($items as $menu) {
 236                  // Initialize the group.
 237                  $groups[$menu->title] = array();
 238  
 239                  // Build the options array.
 240                  foreach ($menu->links as $link) {
 241                      $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
 242  
 243                      // Displays language code if not set to All
 244                      if ($link->language !== '*') {
 245                          $lang = ' (' . $link->language . ')';
 246                      } else {
 247                          $lang = '';
 248                      }
 249  
 250                      $groups[$menu->title][] = HTMLHelper::_(
 251                          'select.option',
 252                          $link->value,
 253                          $levelPrefix . $link->text . $lang,
 254                          'value',
 255                          'text',
 256                          in_array($link->type, $this->disable)
 257                      );
 258                  }
 259              }
 260          }
 261  
 262          // Merge any additional groups in the XML definition.
 263          $groups = array_merge(parent::getGroups(), $groups);
 264  
 265          return $groups;
 266      }
 267  }


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