[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_categories/src/View/Category/ -> HtmlView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_categories
   6   *
   7   * @copyright   (C) 2008 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\Categories\Administrator\View\Category;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Helper\ContentHelper;
  16  use Joomla\CMS\Helper\TagsHelper;
  17  use Joomla\CMS\Language\Associations;
  18  use Joomla\CMS\Language\Text;
  19  use Joomla\CMS\MVC\View\GenericDataException;
  20  use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
  21  use Joomla\CMS\Object\CMSObject;
  22  use Joomla\CMS\Toolbar\ToolbarHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * HTML View class for the Categories component
  30   *
  31   * @since  1.6
  32   */
  33  class HtmlView extends BaseHtmlView
  34  {
  35      /**
  36       * The Form object
  37       *
  38       * @var  \Joomla\CMS\Form\Form
  39       */
  40      protected $form;
  41  
  42      /**
  43       * The active item
  44       *
  45       * @var  object
  46       */
  47      protected $item;
  48  
  49      /**
  50       * The model state
  51       *
  52       * @var  CMSObject
  53       */
  54      protected $state;
  55  
  56      /**
  57       * Flag if an association exists
  58       *
  59       * @var  boolean
  60       */
  61      protected $assoc;
  62  
  63      /**
  64       * The actions the user is authorised to perform
  65       *
  66       * @var  CMSObject
  67       */
  68      protected $canDo;
  69  
  70      /**
  71       * Is there a content type associated with this category alias
  72       *
  73       * @var    boolean
  74       * @since  4.0.0
  75       */
  76      protected $checkTags = false;
  77  
  78      /**
  79       * Display the view.
  80       *
  81       * @param   string|null  $tpl  The name of the template file to parse; automatically searches through the template paths.
  82       *
  83       * @return  void
  84       */
  85      public function display($tpl = null)
  86      {
  87          $this->form = $this->get('Form');
  88          $this->item = $this->get('Item');
  89          $this->state = $this->get('State');
  90          $section = $this->state->get('category.section') ? $this->state->get('category.section') . '.' : '';
  91          $this->canDo = ContentHelper::getActions($this->state->get('category.component'), $section . 'category', $this->item->id);
  92          $this->assoc = $this->get('Assoc');
  93  
  94          // Check for errors.
  95          if (count($errors = $this->get('Errors'))) {
  96              throw new GenericDataException(implode("\n", $errors), 500);
  97          }
  98  
  99          // Check if we have a content type for this alias
 100          if (!empty(TagsHelper::getTypes('objectList', array($this->state->get('category.extension') . '.category'), true))) {
 101              $this->checkTags = true;
 102          }
 103  
 104          Factory::getApplication()->input->set('hidemainmenu', true);
 105  
 106          // If we are forcing a language in modal (used for associations).
 107          if ($this->getLayout() === 'modal' && $forcedLanguage = Factory::getApplication()->input->get('forcedLanguage', '', 'cmd')) {
 108              // Set the language field to the forcedLanguage and disable changing it.
 109              $this->form->setValue('language', null, $forcedLanguage);
 110              $this->form->setFieldAttribute('language', 'readonly', 'true');
 111  
 112              // Only allow to select categories with All language or with the forced language.
 113              $this->form->setFieldAttribute('parent_id', 'language', '*,' . $forcedLanguage);
 114  
 115              // Only allow to select tags with All language or with the forced language.
 116              $this->form->setFieldAttribute('tags', 'language', '*,' . $forcedLanguage);
 117          }
 118  
 119          $this->addToolbar();
 120  
 121          parent::display($tpl);
 122      }
 123  
 124      /**
 125       * Add the page title and toolbar.
 126       *
 127       * @return  void
 128       *
 129       * @since   1.6
 130       */
 131      protected function addToolbar()
 132      {
 133          $extension = Factory::getApplication()->input->get('extension');
 134          $user = $this->getCurrentUser();
 135          $userId = $user->id;
 136  
 137          $isNew = ($this->item->id == 0);
 138          $checkedOut = !(is_null($this->item->checked_out) || $this->item->checked_out == $userId);
 139  
 140          // Avoid nonsense situation.
 141          if ($extension == 'com_categories') {
 142              return;
 143          }
 144  
 145          // The extension can be in the form com_foo.section
 146          $parts = explode('.', $extension);
 147          $component = $parts[0];
 148          $section = (count($parts) > 1) ? $parts[1] : null;
 149          $componentParams = ComponentHelper::getParams($component);
 150  
 151          // Need to load the menu language file as mod_menu hasn't been loaded yet.
 152          $lang = Factory::getLanguage();
 153          $lang->load($component, JPATH_BASE)
 154          || $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component);
 155  
 156          // Get the results for each action.
 157          $canDo = $this->canDo;
 158  
 159          // If a component categories title string is present, let's use it.
 160          if ($lang->hasKey($component_title_key = $component . ($section ? "_$section" : '') . '_CATEGORY_' . ($isNew ? 'ADD' : 'EDIT') . '_TITLE')) {
 161              $title = Text::_($component_title_key);
 162          } elseif ($lang->hasKey($component_section_key = $component . ($section ? "_$section" : ''))) {
 163              // Else if the component section string exists, let's use it.
 164              $title = Text::sprintf('COM_CATEGORIES_CATEGORY_' . ($isNew ? 'ADD' : 'EDIT')
 165                  . '_TITLE', $this->escape(Text::_($component_section_key)));
 166          } else {
 167              // Else use the base title
 168              $title = Text::_('COM_CATEGORIES_CATEGORY_BASE_' . ($isNew ? 'ADD' : 'EDIT') . '_TITLE');
 169          }
 170  
 171          // Load specific css component
 172          /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
 173          $wa = $this->document->getWebAssetManager();
 174          $wa->getRegistry()->addExtensionRegistryFile($component);
 175  
 176          if ($wa->assetExists('style', $component . '.admin-categories')) {
 177              $wa->useStyle($component . '.admin-categories');
 178          } else {
 179              $wa->registerAndUseStyle($component . '.admin-categories', $component . '/administrator/categories.css');
 180          }
 181  
 182          // Prepare the toolbar.
 183          ToolbarHelper::title(
 184              $title,
 185              'folder category-' . ($isNew ? 'add' : 'edit')
 186                  . ' ' . substr($component, 4) . ($section ? "-$section" : '') . '-category-' . ($isNew ? 'add' : 'edit')
 187          );
 188  
 189          // For new records, check the create permission.
 190          if ($isNew && (count($user->getAuthorisedCategories($component, 'core.create')) > 0)) {
 191              ToolbarHelper::apply('category.apply');
 192              ToolbarHelper::saveGroup(
 193                  [
 194                      ['save', 'category.save'],
 195                      ['save2new', 'category.save2new']
 196                  ],
 197                  'btn-success'
 198              );
 199  
 200              ToolbarHelper::cancel('category.cancel');
 201          } else {
 202              // If not checked out, can save the item.
 203              // Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
 204              $itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_user_id == $userId);
 205  
 206              $toolbarButtons = [];
 207  
 208              // Can't save the record if it's checked out and editable
 209              if (!$checkedOut && $itemEditable) {
 210                  ToolbarHelper::apply('category.apply');
 211  
 212                  $toolbarButtons[] = ['save', 'category.save'];
 213  
 214                  if ($canDo->get('core.create')) {
 215                      $toolbarButtons[] = ['save2new', 'category.save2new'];
 216                  }
 217              }
 218  
 219              // If an existing item, can save to a copy.
 220              if ($canDo->get('core.create')) {
 221                  $toolbarButtons[] = ['save2copy', 'category.save2copy'];
 222              }
 223  
 224              ToolbarHelper::saveGroup(
 225                  $toolbarButtons,
 226                  'btn-success'
 227              );
 228  
 229              ToolbarHelper::cancel('category.cancel', 'JTOOLBAR_CLOSE');
 230  
 231              if (ComponentHelper::isEnabled('com_contenthistory') && $componentParams->get('save_history', 0) && $itemEditable) {
 232                  $typeAlias = $extension . '.category';
 233                  ToolbarHelper::versions($typeAlias, $this->item->id);
 234              }
 235  
 236              if (Associations::isEnabled() && ComponentHelper::isEnabled('com_associations')) {
 237                  ToolbarHelper::custom('category.editAssociations', 'contract', '', 'JTOOLBAR_ASSOCIATIONS', false, false);
 238              }
 239          }
 240  
 241          ToolbarHelper::divider();
 242  
 243          // Look first in form for help key
 244          $ref_key = (string) $this->form->getXml()->help['key'];
 245  
 246          // Try with a language string
 247          if (!$ref_key) {
 248              // Compute the ref_key if it does exist in the component
 249              $languageKey = strtoupper($component . ($section ? "_$section" : '')) . '_CATEGORY_' . ($isNew ? 'ADD' : 'EDIT') . '_HELP_KEY';
 250  
 251              if ($lang->hasKey($languageKey)) {
 252                  $ref_key = $languageKey;
 253              } else {
 254                  $languageKey = 'JHELP_COMPONENTS_'
 255                      . strtoupper(substr($component, 4) . ($section ? "_$section" : ''))
 256                      . '_CATEGORY_' . ($isNew ? 'ADD' : 'EDIT');
 257  
 258                  if ($lang->hasKey($languageKey)) {
 259                      $ref_key = $languageKey;
 260                  }
 261              }
 262          }
 263  
 264          /*
 265           * Get help for the category/section view for the component by
 266           * -remotely searching in a URL defined in the category form
 267           * -remotely searching in a language defined dedicated URL: *component*_HELP_URL
 268           * -locally  searching in a component help file if helpURL param exists in the component and is set to ''
 269           * -remotely searching in a component URL if helpURL param exists in the component and is NOT set to ''
 270           */
 271          $url = (string) $this->form->getXml()->help['url'];
 272  
 273          if (!$url) {
 274              if ($lang->hasKey($lang_help_url = strtoupper($component) . '_HELP_URL')) {
 275                  $debug = $lang->setDebug(false);
 276                  $url = Text::_($lang_help_url);
 277                  $lang->setDebug($debug);
 278              }
 279          }
 280  
 281          ToolbarHelper::help($ref_key, $componentParams->exists('helpURL'), $url, $component);
 282      }
 283  }


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