[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_menus/src/View/Items/ -> HtmlView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_menus
   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\Component\Menus\Administrator\View\Items;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Helper\ContentHelper;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Multilanguage;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\MVC\View\GenericDataException;
  19  use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
  20  use Joomla\CMS\Toolbar\Toolbar;
  21  use Joomla\CMS\Toolbar\ToolbarHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * The HTML Menus Menu Items View.
  29   *
  30   * @since  1.6
  31   */
  32  class HtmlView extends BaseHtmlView
  33  {
  34      /**
  35       * Array used for displaying the levels filter
  36       *
  37       * @var    \stdClass[]
  38       * @since  4.0.0
  39       */
  40      protected $f_levels;
  41  
  42      /**
  43       * An array of items
  44       *
  45       * @var  array
  46       */
  47      protected $items;
  48  
  49      /**
  50       * The pagination object
  51       *
  52       * @var  \Joomla\CMS\Pagination\Pagination
  53       */
  54      protected $pagination;
  55  
  56      /**
  57       * The model state
  58       *
  59       * @var  \Joomla\CMS\Object\CMSObject
  60       */
  61      protected $state;
  62  
  63      /**
  64       * Form object for search filters
  65       *
  66       * @var    \Joomla\CMS\Form\Form
  67       *
  68       * @since  4.0.0
  69       */
  70      public $filterForm;
  71  
  72      /**
  73       * The active search filters
  74       *
  75       * @var    array
  76       * @since  4.0.0
  77       */
  78      public $activeFilters;
  79  
  80      /**
  81       * Ordering of the items
  82       *
  83       * @var    array
  84       * @since  4.0.0
  85       */
  86      protected $ordering;
  87  
  88      /**
  89       * Display the view
  90       *
  91       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  92       *
  93       * @return  void
  94       *
  95       * @since   1.6
  96       */
  97      public function display($tpl = null)
  98      {
  99          $lang = Factory::getLanguage();
 100          $this->items         = $this->get('Items');
 101          $this->pagination    = $this->get('Pagination');
 102          $this->total         = $this->get('Total');
 103          $this->state         = $this->get('State');
 104          $this->filterForm    = $this->get('FilterForm');
 105          $this->activeFilters = $this->get('ActiveFilters');
 106  
 107          // Check for errors.
 108          if (count($errors = $this->get('Errors'))) {
 109              throw new GenericDataException(implode("\n", $errors), 500);
 110          }
 111  
 112          $this->ordering = array();
 113  
 114          // Preprocess the list of items to find ordering divisions.
 115          foreach ($this->items as $item) {
 116              $this->ordering[$item->parent_id][] = $item->id;
 117  
 118              // Item type text
 119              switch ($item->type) {
 120                  case 'url':
 121                      $value = Text::_('COM_MENUS_TYPE_EXTERNAL_URL');
 122                      break;
 123  
 124                  case 'alias':
 125                      $value = Text::_('COM_MENUS_TYPE_ALIAS');
 126                      break;
 127  
 128                  case 'separator':
 129                      $value = Text::_('COM_MENUS_TYPE_SEPARATOR');
 130                      break;
 131  
 132                  case 'heading':
 133                      $value = Text::_('COM_MENUS_TYPE_HEADING');
 134                      break;
 135  
 136                  case 'container':
 137                      $value = Text::_('COM_MENUS_TYPE_CONTAINER');
 138                      break;
 139  
 140                  case 'component':
 141                  default:
 142                      // Load language
 143                          $lang->load($item->componentname . '.sys', JPATH_ADMINISTRATOR)
 144                      || $lang->load($item->componentname . '.sys', JPATH_ADMINISTRATOR . '/components/' . $item->componentname);
 145  
 146                      if (!empty($item->componentname)) {
 147                          $titleParts   = array();
 148                          $titleParts[] = Text::_($item->componentname);
 149                          $vars         = null;
 150  
 151                          parse_str($item->link, $vars);
 152  
 153                          if (isset($vars['view'])) {
 154                              // Attempt to load the view xml file.
 155                              $file = JPATH_SITE . '/components/' . $item->componentname . '/views/' . $vars['view'] . '/metadata.xml';
 156  
 157                              if (!is_file($file)) {
 158                                  $file = JPATH_SITE . '/components/' . $item->componentname . '/view/' . $vars['view'] . '/metadata.xml';
 159                              }
 160  
 161                              if (is_file($file) && $xml = simplexml_load_file($file)) {
 162                                  // Look for the first view node off of the root node.
 163                                  if ($view = $xml->xpath('view[1]')) {
 164                                      // Add view title if present.
 165                                      if (!empty($view[0]['title'])) {
 166                                          $viewTitle = trim((string) $view[0]['title']);
 167  
 168                                          // Check if the key is valid. Needed due to B/C so we don't show untranslated keys. This check should be removed with Joomla 4.
 169                                          if ($lang->hasKey($viewTitle)) {
 170                                              $titleParts[] = Text::_($viewTitle);
 171                                          }
 172                                      }
 173                                  }
 174                              }
 175  
 176                              $vars['layout'] = $vars['layout'] ?? 'default';
 177  
 178                              // Attempt to load the layout xml file.
 179                              // If Alternative Menu Item, get template folder for layout file
 180                              if (strpos($vars['layout'], ':') > 0) {
 181                                  // Use template folder for layout file
 182                                  $temp = explode(':', $vars['layout']);
 183                                  $file = JPATH_SITE . '/templates/' . $temp[0] . '/html/' . $item->componentname . '/' . $vars['view'] . '/' . $temp[1] . '.xml';
 184  
 185                                  // Load template language file
 186                                  $lang->load('tpl_' . $temp[0] . '.sys', JPATH_SITE)
 187                                  ||  $lang->load('tpl_' . $temp[0] . '.sys', JPATH_SITE . '/templates/' . $temp[0]);
 188                              } else {
 189                                  $base = $this->state->get('filter.client_id') == 0 ? JPATH_SITE : JPATH_ADMINISTRATOR;
 190  
 191                                  // Get XML file from component folder for standard layouts
 192                                  $file = $base . '/components/' . $item->componentname . '/tmpl/' . $vars['view']
 193                                      . '/' . $vars['layout'] . '.xml';
 194  
 195                                  if (!file_exists($file)) {
 196                                      $file = $base . '/components/' . $item->componentname . '/views/'
 197                                          . $vars['view'] . '/tmpl/' . $vars['layout'] . '.xml';
 198  
 199                                      if (!file_exists($file)) {
 200                                          $file = $base . '/components/' . $item->componentname . '/view/'
 201                                              . $vars['view'] . '/tmpl/' . $vars['layout'] . '.xml';
 202                                      }
 203                                  }
 204                              }
 205  
 206                              if (is_file($file) && $xml = simplexml_load_file($file)) {
 207                                  // Look for the first view node off of the root node.
 208                                  if ($layout = $xml->xpath('layout[1]')) {
 209                                      if (!empty($layout[0]['title'])) {
 210                                          $titleParts[] = Text::_(trim((string) $layout[0]['title']));
 211                                      }
 212                                  }
 213  
 214                                  if (!empty($layout[0]->message[0])) {
 215                                      $item->item_type_desc = Text::_(trim((string) $layout[0]->message[0]));
 216                                  }
 217                              }
 218  
 219                              unset($xml);
 220  
 221                              // Special case if neither a view nor layout title is found
 222                              if (count($titleParts) == 1) {
 223                                  $titleParts[] = $vars['view'];
 224                              }
 225                          }
 226  
 227                          $value = implode(' ยป ', $titleParts);
 228                      } else {
 229                          if (preg_match("/^index.php\?option=([a-zA-Z\-0-9_]*)/", $item->link, $result)) {
 230                              $value = Text::sprintf('COM_MENUS_TYPE_UNEXISTING', $result[1]);
 231                          } else {
 232                              $value = Text::_('COM_MENUS_TYPE_UNKNOWN');
 233                          }
 234                      }
 235                      break;
 236              }
 237  
 238              $item->item_type = $value;
 239              $item->protected = $item->menutype == 'main';
 240          }
 241  
 242          // Levels filter.
 243          $options   = array();
 244          $options[] = HTMLHelper::_('select.option', '1', Text::_('J1'));
 245          $options[] = HTMLHelper::_('select.option', '2', Text::_('J2'));
 246          $options[] = HTMLHelper::_('select.option', '3', Text::_('J3'));
 247          $options[] = HTMLHelper::_('select.option', '4', Text::_('J4'));
 248          $options[] = HTMLHelper::_('select.option', '5', Text::_('J5'));
 249          $options[] = HTMLHelper::_('select.option', '6', Text::_('J6'));
 250          $options[] = HTMLHelper::_('select.option', '7', Text::_('J7'));
 251          $options[] = HTMLHelper::_('select.option', '8', Text::_('J8'));
 252          $options[] = HTMLHelper::_('select.option', '9', Text::_('J9'));
 253          $options[] = HTMLHelper::_('select.option', '10', Text::_('J10'));
 254  
 255          $this->f_levels = $options;
 256  
 257          // We don't need toolbar in the modal window.
 258          if ($this->getLayout() !== 'modal') {
 259              $this->addToolbar();
 260  
 261              // We do not need to filter by language when multilingual is disabled
 262              if (!Multilanguage::isEnabled()) {
 263                  unset($this->activeFilters['language']);
 264                  $this->filterForm->removeField('language', 'filter');
 265              }
 266          } else {
 267              // In menu associations modal we need to remove language filter if forcing a language.
 268              if ($forcedLanguage = Factory::getApplication()->input->get('forcedLanguage', '', 'CMD')) {
 269                  // If the language is forced we can't allow to select the language, so transform the language selector filter into a hidden field.
 270                  $languageXml = new \SimpleXMLElement('<field name="language" type="hidden" default="' . $forcedLanguage . '" />');
 271                  $this->filterForm->setField($languageXml, 'filter', true);
 272  
 273                  // Also, unset the active language filter so the search tools is not open by default with this filter.
 274                  unset($this->activeFilters['language']);
 275              }
 276          }
 277  
 278          // Allow a system plugin to insert dynamic menu types to the list shown in menus:
 279          Factory::getApplication()->triggerEvent('onBeforeRenderMenuItems', array($this));
 280  
 281          parent::display($tpl);
 282      }
 283  
 284      /**
 285       * Add the page title and toolbar.
 286       *
 287       * @return  void
 288       *
 289       * @since   1.6
 290       */
 291      protected function addToolbar()
 292      {
 293          $menutypeId = (int) $this->state->get('menutypeid');
 294  
 295          $canDo = ContentHelper::getActions('com_menus', 'menu', (int) $menutypeId);
 296          $user  = $this->getCurrentUser();
 297  
 298          // Get the menu title
 299          $menuTypeTitle = $this->get('State')->get('menutypetitle');
 300  
 301          // Get the toolbar object instance
 302          $toolbar = Toolbar::getInstance('toolbar');
 303  
 304          if ($menuTypeTitle) {
 305              ToolbarHelper::title(Text::sprintf('COM_MENUS_VIEW_ITEMS_MENU_TITLE', $menuTypeTitle), 'list menumgr');
 306          } else {
 307              ToolbarHelper::title(Text::_('COM_MENUS_VIEW_ITEMS_ALL_TITLE'), 'list menumgr');
 308          }
 309  
 310          if ($canDo->get('core.create')) {
 311              $toolbar->addNew('item.add');
 312          }
 313  
 314          $protected = $this->state->get('filter.menutype') == 'main';
 315  
 316          if (
 317              ($canDo->get('core.edit.state') || $this->getCurrentUser()->authorise('core.admin')) && !$protected
 318              || $canDo->get('core.edit.state') && $this->state->get('filter.client_id') == 0
 319          ) {
 320              $dropdown = $toolbar->dropdownButton('status-group')
 321                  ->text('JTOOLBAR_CHANGE_STATUS')
 322                  ->toggleSplit(false)
 323                  ->icon('icon-ellipsis-h')
 324                  ->buttonClass('btn btn-action')
 325                  ->listCheck(true);
 326  
 327              $childBar = $dropdown->getChildToolbar();
 328  
 329              if ($canDo->get('core.edit.state') && !$protected) {
 330                  $childBar->publish('items.publish')->listCheck(true);
 331  
 332                  $childBar->unpublish('items.unpublish')->listCheck(true);
 333              }
 334  
 335              if ($this->getCurrentUser()->authorise('core.admin') && !$protected) {
 336                  $childBar->checkin('items.checkin')->listCheck(true);
 337              }
 338  
 339              if ($canDo->get('core.edit.state') && $this->state->get('filter.published') != -2) {
 340                  if ($this->state->get('filter.client_id') == 0) {
 341                      $childBar->makeDefault('items.setDefault')->listCheck(true);
 342                  }
 343  
 344                  if (!$protected) {
 345                      $childBar->trash('items.trash')->listCheck(true);
 346                  }
 347              }
 348  
 349              // Add a batch button
 350              if (
 351                  !$protected && $user->authorise('core.create', 'com_menus')
 352                  && $user->authorise('core.edit', 'com_menus')
 353                  && $user->authorise('core.edit.state', 'com_menus')
 354              ) {
 355                  $childBar->popupButton('batch')
 356                      ->text('JTOOLBAR_BATCH')
 357                      ->selector('collapseModal')
 358                      ->listCheck(true);
 359              }
 360          }
 361  
 362          if ($this->getCurrentUser()->authorise('core.admin')) {
 363              $toolbar->standardButton('refresh')
 364                  ->text('JTOOLBAR_REBUILD')
 365                  ->task('items.rebuild');
 366          }
 367  
 368          if (!$protected && $this->state->get('filter.published') == -2 && $canDo->get('core.delete')) {
 369              $toolbar->delete('items.delete')
 370                  ->text('JTOOLBAR_EMPTY_TRASH')
 371                  ->message('JGLOBAL_CONFIRM_DELETE')
 372                  ->listCheck(true);
 373          }
 374  
 375          if ($canDo->get('core.admin') || $canDo->get('core.options')) {
 376              $toolbar->preferences('com_menus');
 377          }
 378  
 379          $toolbar->help('Menus:_Items');
 380      }
 381  }


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