[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_categories/src/Field/Modal/ -> CategoryField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_categories
   6   *
   7   * @copyright   (C) 2013 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\Field\Modal;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\LanguageHelper;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\Session\Session;
  19  use Joomla\Database\ParameterType;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Supports a modal category picker.
  27   *
  28   * @since  3.1
  29   */
  30  class CategoryField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var     string
  36       * @since   1.6
  37       */
  38      protected $type = 'Modal_Category';
  39  
  40      /**
  41       * Method to get the field input markup.
  42       *
  43       * @return  string  The field input markup.
  44       *
  45       * @since   1.6
  46       */
  47      protected function getInput()
  48      {
  49          if ($this->element['extension']) {
  50              $extension = (string) $this->element['extension'];
  51          } else {
  52              $extension = (string) Factory::getApplication()->input->get('extension', 'com_content');
  53          }
  54  
  55          $allowNew       = ((string) $this->element['new'] == 'true');
  56          $allowEdit      = ((string) $this->element['edit'] == 'true');
  57          $allowClear     = ((string) $this->element['clear'] != 'false');
  58          $allowSelect    = ((string) $this->element['select'] != 'false');
  59          $allowPropagate = ((string) $this->element['propagate'] == 'true');
  60  
  61          $languages = LanguageHelper::getContentLanguages(array(0, 1), false);
  62  
  63          // Load language.
  64          Factory::getLanguage()->load('com_categories', JPATH_ADMINISTRATOR);
  65  
  66          // The active category id field.
  67          $value = (int) $this->value ?: '';
  68  
  69          // Create the modal id.
  70          $modalId = 'Category_' . $this->id;
  71  
  72          /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
  73          $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
  74  
  75          // Add the modal field script to the document head.
  76          $wa->useScript('field.modal-fields');
  77  
  78          // Script to proxy the select modal function to the modal-fields.js file.
  79          if ($allowSelect) {
  80              static $scriptSelect = null;
  81  
  82              if (is_null($scriptSelect)) {
  83                  $scriptSelect = array();
  84              }
  85  
  86              if (!isset($scriptSelect[$this->id])) {
  87                  $wa->addInlineScript(
  88                      "
  89                  window.jSelectCategory_" . $this->id . " = function (id, title, object) {
  90                      window.processModalSelect('Category', '" . $this->id . "', id, title, '', object);
  91                  }",
  92                      [],
  93                      ['type' => 'module']
  94                  );
  95  
  96                  Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');
  97  
  98                  $scriptSelect[$this->id] = true;
  99              }
 100          }
 101  
 102          // Setup variables for display.
 103          $linkCategories = 'index.php?option=com_categories&amp;view=categories&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1'
 104              . '&amp;extension=' . $extension;
 105          $linkCategory  = 'index.php?option=com_categories&amp;view=category&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1'
 106              . '&amp;extension=' . $extension;
 107          $modalTitle    = Text::_('COM_CATEGORIES_SELECT_A_CATEGORY');
 108  
 109          if (isset($this->element['language'])) {
 110              $linkCategories .= '&amp;forcedLanguage=' . $this->element['language'];
 111              $linkCategory   .= '&amp;forcedLanguage=' . $this->element['language'];
 112              $modalTitle     .= ' &#8212; ' . $this->element['label'];
 113          }
 114  
 115          $urlSelect = $linkCategories . '&amp;function=jSelectCategory_' . $this->id;
 116          $urlEdit   = $linkCategory . '&amp;task=category.edit&amp;id=\' + document.getElementById("' . $this->id . '_id").value + \'';
 117          $urlNew    = $linkCategory . '&amp;task=category.add';
 118  
 119          if ($value) {
 120              $db    = $this->getDatabase();
 121              $query = $db->getQuery(true)
 122                  ->select($db->quoteName('title'))
 123                  ->from($db->quoteName('#__categories'))
 124                  ->where($db->quoteName('id') . ' = :value')
 125                  ->bind(':value', $value, ParameterType::INTEGER);
 126              $db->setQuery($query);
 127  
 128              try {
 129                  $title = $db->loadResult();
 130              } catch (\RuntimeException $e) {
 131                  Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 132              }
 133          }
 134  
 135          $title = empty($title) ? Text::_('COM_CATEGORIES_SELECT_A_CATEGORY') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
 136  
 137          // The current category display field.
 138          $html  = '';
 139  
 140          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 141              $html .= '<span class="input-group">';
 142          }
 143  
 144          $html .= '<input class="form-control" id="' . $this->id . '_name" type="text" value="' . $title . '" readonly size="35">';
 145  
 146          // Select category button.
 147          if ($allowSelect) {
 148              $html .= '<button'
 149                  . ' class="btn btn-primary' . ($value ? ' hidden' : '') . '"'
 150                  . ' id="' . $this->id . '_select"'
 151                  . ' data-bs-toggle="modal"'
 152                  . ' type="button"'
 153                  . ' data-bs-target="#ModalSelect' . $modalId . '">'
 154                  . '<span class="icon-file" aria-hidden="true"></span> ' . Text::_('JSELECT')
 155                  . '</button>';
 156          }
 157  
 158          // New category button.
 159          if ($allowNew) {
 160              $html .= '<button'
 161                  . ' class="btn btn-secondary' . ($value ? ' hidden' : '') . '"'
 162                  . ' id="' . $this->id . '_new"'
 163                  . ' data-bs-toggle="modal"'
 164                  . ' type="button"'
 165                  . ' data-bs-target="#ModalNew' . $modalId . '">'
 166                  . '<span class="icon-plus" aria-hidden="true"></span> ' . Text::_('JACTION_CREATE')
 167                  . '</button>';
 168          }
 169  
 170          // Edit category button.
 171          if ($allowEdit) {
 172              $html .= '<button'
 173                  . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 174                  . ' id="' . $this->id . '_edit"'
 175                  . ' data-bs-toggle="modal"'
 176                  . ' type="button"'
 177                  . ' data-bs-target="#ModalEdit' . $modalId . '">'
 178                  . '<span class="icon-pen-square" aria-hidden="true"></span> ' . Text::_('JACTION_EDIT')
 179                  . '</button>';
 180          }
 181  
 182          // Clear category button.
 183          if ($allowClear) {
 184              $html .= '<button'
 185                  . ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
 186                  . ' id="' . $this->id . '_clear"'
 187                  . ' type="button"'
 188                  . ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">'
 189                  . '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
 190                  . '</button>';
 191          }
 192  
 193          // Propagate category button
 194          if ($allowPropagate && \count($languages) > 2) {
 195              // Strip off language tag at the end
 196              $tagLength = (int) \strlen($this->element['language']);
 197              $callbackFunctionStem = substr("jSelectCategory_" . $this->id, 0, -$tagLength);
 198  
 199              $html .= '<button'
 200              . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 201              . ' type="button"'
 202              . ' id="' . $this->id . '_propagate"'
 203              . ' title="' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
 204              . ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
 205              . '<span class="icon-sync" aria-hidden="true"></span> ' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
 206              . '</button>';
 207          }
 208  
 209          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 210              $html .= '</span>';
 211          }
 212  
 213          // Select category modal.
 214          if ($allowSelect) {
 215              $html .= HTMLHelper::_(
 216                  'bootstrap.renderModal',
 217                  'ModalSelect' . $modalId,
 218                  array(
 219                      'title'       => $modalTitle,
 220                      'url'         => $urlSelect,
 221                      'height'      => '400px',
 222                      'width'       => '800px',
 223                      'bodyHeight'  => 70,
 224                      'modalWidth'  => 80,
 225                      'footer'      => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
 226                                          . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
 227                  )
 228              );
 229          }
 230  
 231          // New category modal.
 232          if ($allowNew) {
 233              $html .= HTMLHelper::_(
 234                  'bootstrap.renderModal',
 235                  'ModalNew' . $modalId,
 236                  array(
 237                      'title'       => Text::_('COM_CATEGORIES_NEW_CATEGORY'),
 238                      'backdrop'    => 'static',
 239                      'keyboard'    => false,
 240                      'closeButton' => false,
 241                      'url'         => $urlNew,
 242                      'height'      => '400px',
 243                      'width'       => '800px',
 244                      'bodyHeight'  => 70,
 245                      'modalWidth'  => 80,
 246                      'footer'      => '<button type="button" class="btn btn-secondary"'
 247                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'category\', \'cancel\', \'item-form\'); return false;">'
 248                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 249                              . '<button type="button" class="btn btn-primary"'
 250                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'category\', \'save\', \'item-form\'); return false;">'
 251                              . Text::_('JSAVE') . '</button>'
 252                              . '<button type="button" class="btn btn-success"'
 253                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'category\', \'apply\', \'item-form\'); return false;">'
 254                              . Text::_('JAPPLY') . '</button>',
 255                  )
 256              );
 257          }
 258  
 259          // Edit category modal.
 260          if ($allowEdit) {
 261              $html .= HTMLHelper::_(
 262                  'bootstrap.renderModal',
 263                  'ModalEdit' . $modalId,
 264                  array(
 265                      'title'       => Text::_('COM_CATEGORIES_EDIT_CATEGORY'),
 266                      'backdrop'    => 'static',
 267                      'keyboard'    => false,
 268                      'closeButton' => false,
 269                      'url'         => $urlEdit,
 270                      'height'      => '400px',
 271                      'width'       => '800px',
 272                      'bodyHeight'  => 70,
 273                      'modalWidth'  => 80,
 274                      'footer'      => '<button type="button" class="btn btn-secondary"'
 275                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'category\', \'cancel\', \'item-form\'); return false;">'
 276                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 277                              . '<button type="button" class="btn btn-primary"'
 278                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'category\', \'save\', \'item-form\'); return false;">'
 279                              . Text::_('JSAVE') . '</button>'
 280                              . '<button type="button" class="btn btn-success"'
 281                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'category\', \'apply\', \'item-form\'); return false;">'
 282                              . Text::_('JAPPLY') . '</button>',
 283                  )
 284              );
 285          }
 286  
 287          // Note: class='required' for client side validation
 288          $class = $this->required ? ' class="required modal-value"' : '';
 289  
 290          $html .= '<input type="hidden" id="' . $this->id . '_id"' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
 291              . '" data-text="' . htmlspecialchars(Text::_('COM_CATEGORIES_SELECT_A_CATEGORY', true), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '">';
 292  
 293          return $html;
 294      }
 295  
 296      /**
 297       * Method to get the field label markup.
 298       *
 299       * @return  string  The field label markup.
 300       *
 301       * @since   3.7.0
 302       */
 303      protected function getLabel()
 304      {
 305          return str_replace($this->id, $this->id . '_name', parent::getLabel());
 306      }
 307  }


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