[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_associations/src/View/Association/ -> HtmlView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_associations
   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\Associations\Administrator\View\Association;
  12  
  13  use Joomla\CMS\Application\AdministratorApplication;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Form\Form;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\View\GenericDataException;
  18  use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
  19  use Joomla\CMS\Object\CMSObject;
  20  use Joomla\CMS\Pagination\Pagination;
  21  use Joomla\CMS\Router\Route;
  22  use Joomla\CMS\Toolbar\Toolbar;
  23  use Joomla\CMS\Toolbar\ToolbarHelper;
  24  use Joomla\Component\Associations\Administrator\Helper\AssociationsHelper;
  25  use Joomla\Component\Associations\Administrator\Model\AssociationModel;
  26  use Joomla\Input\Input;
  27  use Joomla\Registry\Registry;
  28  use Joomla\Utilities\ArrayHelper;
  29  
  30  // phpcs:disable PSR1.Files.SideEffects
  31  \defined('_JEXEC') or die;
  32  // phpcs:enable PSR1.Files.SideEffects
  33  
  34  /**
  35   * View class for a list of articles.
  36   *
  37   * @since  3.7.0
  38   */
  39  class HtmlView extends BaseHtmlView
  40  {
  41      /**
  42       * An array of items
  43       *
  44       * @var    array
  45       *
  46       * @since  3.7.0
  47       */
  48      protected $items = [];
  49  
  50      /**
  51       * The pagination object
  52       *
  53       * @var    Pagination
  54       *
  55       * @since  3.7.0
  56       */
  57      protected $pagination;
  58  
  59      /**
  60       * The model state
  61       *
  62       * @var    CMSObject
  63       *
  64       * @since  3.7.0
  65       */
  66      protected $state;
  67  
  68      /**
  69       * Selected item type properties.
  70       *
  71       * @var    Registry
  72       *
  73       * @since  3.7.0
  74       */
  75      protected $itemType;
  76  
  77      /**
  78       * The application
  79       *
  80       * @var    AdministratorApplication
  81       * @since  3.7.0
  82       */
  83      protected $app;
  84  
  85      /**
  86       * The ID of the reference language
  87       *
  88       * @var    integer
  89       * @since  3.7.0
  90       */
  91      protected $referenceId = 0;
  92  
  93      /**
  94       * The type name
  95       *
  96       * @var    string
  97       * @since  3.7.0
  98       */
  99      protected $typeName = '';
 100  
 101      /**
 102       * The reference language
 103       *
 104       * @var    string
 105       * @since  3.7.0
 106       */
 107      protected $referenceLanguage = '';
 108  
 109      /**
 110       * The title of the reference language
 111       *
 112       * @var    string
 113       * @since  3.7.0
 114       */
 115      protected $referenceTitle = '';
 116  
 117      /**
 118       * The value of the reference title
 119       *
 120       * @var    string
 121       * @since  3.7.0
 122       */
 123      protected $referenceTitleValue = '';
 124  
 125      /**
 126       * The URL to the edit screen
 127       *
 128       * @var    string
 129       * @since  3.7.0
 130       */
 131      protected $editUri = '';
 132  
 133      /**
 134       * The ID of the target field
 135       *
 136       * @var    string
 137       * @since  3.7.0
 138       */
 139      protected $targetId = '';
 140  
 141      /**
 142       * The target language
 143       *
 144       * @var    string
 145       * @since  3.7.0
 146       */
 147      protected $targetLanguage = '';
 148  
 149      /**
 150       * The source of the target field
 151       *
 152       * @var    string
 153       * @since  3.7.0
 154       */
 155      protected $defaultTargetSrc = '';
 156  
 157      /**
 158       * The action to perform for the target field
 159       *
 160       * @var    string
 161       * @since  3.7.0
 162       */
 163      protected $targetAction = '';
 164  
 165      /**
 166       * The title of the target field
 167       *
 168       * @var    string
 169       * @since  3.7.0
 170       */
 171      protected $targetTitle = '';
 172  
 173      /**
 174       * The edit form
 175       *
 176       * @var    Form
 177       * @since  3.7.0
 178       */
 179      protected $form;
 180  
 181      /**
 182       * Set if the option is set to save as copy
 183       *
 184       * @var    boolean
 185       * @since  3.7.0
 186       */
 187      private $save2copy = false;
 188  
 189      /**
 190       * The type of language
 191       *
 192       * @var    Registry
 193       * @since  3.7.0
 194       */
 195      private $type;
 196  
 197      /**
 198       * The supported types
 199       *
 200       * @var    array
 201       * @since  3.7.0
 202       */
 203      private $typeSupports = [];
 204  
 205      /**
 206       * The extension name
 207       *
 208       * @var    string
 209       * @since  3.7.0
 210       */
 211      private $extensionName = '';
 212  
 213      /**
 214       * Display the view
 215       *
 216       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 217       *
 218       * @return  void
 219       *
 220       * @since   3.7.0
 221       *
 222       * @throws  \Exception
 223       */
 224      public function display($tpl = null): void
 225      {
 226          /** @var AssociationModel $model */
 227          $model = $this->getModel();
 228  
 229          // Check for errors.
 230          if (\count($errors = $model->getErrors())) {
 231              throw new GenericDataException(implode("\n", $errors), 500);
 232          }
 233  
 234          $this->app  = Factory::getApplication();
 235          $this->form = $model->getForm();
 236          /** @var Input $input */
 237          $input             = $this->app->input;
 238          $this->referenceId = $input->get('id', 0, 'int');
 239  
 240          [$extensionName, $typeName] = explode('.', $input->get('itemtype', '', 'string'), 2);
 241  
 242          /** @var Registry $extension */
 243          $extension = AssociationsHelper::getSupportedExtension($extensionName);
 244          $types     = $extension->get('types');
 245  
 246          if (\array_key_exists($typeName, $types)) {
 247              $this->type         = $types[$typeName];
 248              $this->typeSupports = [];
 249              $details            = $this->type->get('details');
 250              $this->save2copy    = false;
 251  
 252              if (\array_key_exists('support', $details)) {
 253                  $support            = $details['support'];
 254                  $this->typeSupports = $support;
 255              }
 256  
 257              if (!empty($this->typeSupports['save2copy'])) {
 258                  $this->save2copy = true;
 259              }
 260          }
 261  
 262          $this->extensionName = $extensionName;
 263          $this->typeName      = $typeName;
 264          $this->itemType      = $extensionName . '.' . $typeName;
 265  
 266          $languageField = AssociationsHelper::getTypeFieldName($extensionName, $typeName, 'language');
 267          $referenceId   = $input->get('id', 0, 'int');
 268          $reference     = ArrayHelper::fromObject(AssociationsHelper::getItem($extensionName, $typeName, $referenceId));
 269  
 270          $this->referenceLanguage   = $reference[$languageField];
 271          $this->referenceTitle      = AssociationsHelper::getTypeFieldName($extensionName, $typeName, 'title');
 272          $this->referenceTitleValue = $reference[$this->referenceTitle];
 273  
 274          // Check for special case category
 275          $typeNameExploded = explode('.', $typeName);
 276  
 277          if (array_pop($typeNameExploded) === 'category') {
 278              $this->typeName = 'category';
 279  
 280              if ($typeNameExploded) {
 281                  $extensionName .= '.' . implode('.', $typeNameExploded);
 282              }
 283  
 284              $options = [
 285                  'option'    => 'com_categories',
 286                  'view'      => 'category',
 287                  'extension' => $extensionName,
 288                  'tmpl'      => 'component',
 289              ];
 290          } else {
 291              $options = [
 292                  'option'    => $extensionName,
 293                  'view'      => $typeName,
 294                  'extension' => $extensionName,
 295                  'tmpl'      => 'component',
 296              ];
 297          }
 298  
 299          // Reference and target edit links.
 300          $this->editUri = 'index.php?' . http_build_query($options);
 301  
 302          // Get target language.
 303          $this->targetId         = '0';
 304          $this->targetLanguage   = '';
 305          $this->defaultTargetSrc = '';
 306          $this->targetAction     = '';
 307          $this->targetTitle      = '';
 308  
 309          if ($target = $input->get('target', '', 'string')) {
 310              $matches              = preg_split("#[\:]+#", $target);
 311              $this->targetAction   = $matches[2];
 312              $this->targetId       = $matches[1];
 313              $this->targetLanguage = $matches[0];
 314              $this->targetTitle    = AssociationsHelper::getTypeFieldName($extensionName, $typeName, 'title');
 315              $task                 = $typeName . '.' . $this->targetAction;
 316  
 317              /**
 318               * Let's put the target src into a variable to use in the javascript code
 319               * to avoid race conditions when the reference iframe loads.
 320               */
 321              $this->document->addScriptOptions('targetSrc', Route::_($this->editUri . '&task=' . $task . '&id=' . (int) $this->targetId));
 322              $this->form->setValue('itemlanguage', '', $this->targetLanguage . ':' . $this->targetId . ':' . $this->targetAction);
 323          }
 324  
 325          $this->addToolbar();
 326  
 327          parent::display($tpl);
 328      }
 329  
 330      /**
 331       * Add the page title and toolbar.
 332       *
 333       * @return  void
 334       *
 335       * @since  3.7.0
 336       *
 337       * @throws \Exception
 338       */
 339      protected function addToolbar(): void
 340      {
 341          // Hide main menu.
 342          $this->app->input->set('hidemainmenu', 1);
 343  
 344          $helper = AssociationsHelper::getExtensionHelper($this->extensionName);
 345          $title  = $helper->getTypeTitle($this->typeName);
 346  
 347          $languageKey = strtoupper($this->extensionName . '_' . $title . 'S');
 348  
 349          if ($this->typeName === 'category') {
 350              $languageKey = strtoupper($this->extensionName) . '_CATEGORIES';
 351          }
 352  
 353          ToolbarHelper::title(
 354              Text::sprintf(
 355                  'COM_ASSOCIATIONS_TITLE_EDIT',
 356                  Text::_($this->extensionName),
 357                  Text::_($languageKey)
 358              ),
 359              'language assoc'
 360          );
 361  
 362          $bar = Toolbar::getInstance();
 363  
 364          $bar->appendButton(
 365              'Custom',
 366              '<joomla-toolbar-button><button onclick="Joomla.submitbutton(\'reference\')" '
 367              . 'class="btn btn-success"><span class="icon-save" aria-hidden="true"></span>'
 368              . Text::_('COM_ASSOCIATIONS_SAVE_REFERENCE') . '</button></joomla-toolbar-button>',
 369              'reference'
 370          );
 371  
 372          $bar->appendButton(
 373              'Custom',
 374              '<joomla-toolbar-button><button onclick="Joomla.submitbutton(\'target\')" '
 375              . 'class="btn btn-success"><span class="icon-save" aria-hidden="true"></span>'
 376              . Text::_('COM_ASSOCIATIONS_SAVE_TARGET') . '</button></joomla-toolbar-button>',
 377              'target'
 378          );
 379  
 380          if ($this->typeName === 'category' || $this->extensionName === 'com_menus' || $this->save2copy === true) {
 381              ToolbarHelper::custom('copy', 'copy.png', '', 'COM_ASSOCIATIONS_COPY_REFERENCE', false);
 382          }
 383  
 384          ToolbarHelper::cancel('association.cancel', 'JTOOLBAR_CLOSE');
 385          ToolbarHelper::help('Multilingual_Associations:_Edit');
 386      }
 387  }


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