[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_menus
   6   *
   7   * @copyright   (C) 2016 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\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 menu item picker.
  27   *
  28   * @since  3.7.0
  29   */
  30  class MenuField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var     string
  36       * @since   3.7.0
  37       */
  38      protected $type = 'Modal_Menu';
  39  
  40      /**
  41       * Determinate, if the select button is shown
  42       *
  43       * @var     boolean
  44       * @since   3.7.0
  45       */
  46      protected $allowSelect = true;
  47  
  48      /**
  49       * Determinate, if the clear button is shown
  50       *
  51       * @var     boolean
  52       * @since   3.7.0
  53       */
  54      protected $allowClear = true;
  55  
  56      /**
  57       * Determinate, if the create button is shown
  58       *
  59       * @var     boolean
  60       * @since   3.7.0
  61       */
  62      protected $allowNew = false;
  63  
  64      /**
  65       * Determinate, if the edit button is shown
  66       *
  67       * @var     boolean
  68       * @since   3.7.0
  69       */
  70      protected $allowEdit = false;
  71  
  72      /**
  73       * Determinate, if the propagate button is shown
  74       *
  75       * @var     boolean
  76       * @since   3.9.0
  77       */
  78      protected $allowPropagate = false;
  79  
  80      /**
  81       * Method to get certain otherwise inaccessible properties from the form field object.
  82       *
  83       * @param   string  $name  The property name for which to get the value.
  84       *
  85       * @return  mixed  The property value or null.
  86       *
  87       * @since   3.7.0
  88       */
  89      public function __get($name)
  90      {
  91          switch ($name) {
  92              case 'allowSelect':
  93              case 'allowClear':
  94              case 'allowNew':
  95              case 'allowEdit':
  96              case 'allowPropagate':
  97                  return $this->$name;
  98          }
  99  
 100          return parent::__get($name);
 101      }
 102  
 103      /**
 104       * Method to set certain otherwise inaccessible properties of the form field object.
 105       *
 106       * @param   string  $name   The property name for which to set the value.
 107       * @param   mixed   $value  The value of the property.
 108       *
 109       * @return  void
 110       *
 111       * @since   3.7.0
 112       */
 113      public function __set($name, $value)
 114      {
 115          switch ($name) {
 116              case 'allowSelect':
 117              case 'allowClear':
 118              case 'allowNew':
 119              case 'allowEdit':
 120              case 'allowPropagate':
 121                  $value = (string) $value;
 122                  $this->$name = !($value === 'false' || $value === 'off' || $value === '0');
 123                  break;
 124  
 125              default:
 126                  parent::__set($name, $value);
 127          }
 128      }
 129  
 130      /**
 131       * Method to attach a JForm object to the field.
 132       *
 133       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 134       * @param   mixed              $value    The form field value to validate.
 135       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 136       *                                        For example if the field has name="foo" and the group value is set to "bar" then the
 137       *                                      full field name would end up being "bar[foo]".
 138       *
 139       * @return  boolean  True on success.
 140       *
 141       * @see     FormField::setup()
 142       * @since   3.7.0
 143       */
 144      public function setup(\SimpleXMLElement $element, $value, $group = null)
 145      {
 146          $return = parent::setup($element, $value, $group);
 147  
 148          if ($return) {
 149              $this->allowSelect = ((string) $this->element['select']) !== 'false';
 150              $this->allowClear = ((string) $this->element['clear']) !== 'false';
 151              $this->allowPropagate = ((string) $this->element['propagate']) === 'true';
 152  
 153              // Creating/editing menu items is not supported in frontend.
 154              $isAdministrator = Factory::getApplication()->isClient('administrator');
 155              $this->allowNew = $isAdministrator ? ((string) $this->element['new']) === 'true' : false;
 156              $this->allowEdit = $isAdministrator ? ((string) $this->element['edit']) === 'true' : false;
 157          }
 158  
 159          return $return;
 160      }
 161  
 162      /**
 163       * Method to get the field input markup.
 164       *
 165       * @return  string  The field input markup.
 166       *
 167       * @since   3.7.0
 168       */
 169      protected function getInput()
 170      {
 171          $clientId    = (int) $this->element['clientid'];
 172          $languages   = LanguageHelper::getContentLanguages(array(0, 1), false);
 173  
 174          // Load language
 175          Factory::getLanguage()->load('com_menus', JPATH_ADMINISTRATOR);
 176  
 177          // The active article id field.
 178          $value = (int) $this->value ?: '';
 179  
 180          // Create the modal id.
 181          $modalId = 'Item_' . $this->id;
 182  
 183          /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
 184          $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
 185  
 186          // Add the modal field script to the document head.
 187          $wa->useScript('field.modal-fields');
 188  
 189          // Script to proxy the select modal function to the modal-fields.js file.
 190          if ($this->allowSelect) {
 191              static $scriptSelect = null;
 192  
 193              if (is_null($scriptSelect)) {
 194                  $scriptSelect = array();
 195              }
 196  
 197              if (!isset($scriptSelect[$this->id])) {
 198                  $wa->addInlineScript(
 199                      "
 200                  window.jSelectMenu_" . $this->id . " = function (id, title, object) {
 201                      window.processModalSelect('Item', '" . $this->id . "', id, title, '', object);
 202                  }",
 203                      [],
 204                      ['type' => 'module']
 205                  );
 206  
 207                  Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');
 208  
 209                  $scriptSelect[$this->id] = true;
 210              }
 211          }
 212  
 213          // Setup variables for display.
 214          $linkSuffix = '&amp;layout=modal&amp;client_id=' . $clientId . '&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
 215          $linkItems  = 'index.php?option=com_menus&amp;view=items' . $linkSuffix;
 216          $linkItem   = 'index.php?option=com_menus&amp;view=item' . $linkSuffix;
 217          $modalTitle = Text::_('COM_MENUS_SELECT_A_MENUITEM');
 218  
 219          if (isset($this->element['language'])) {
 220              $linkItems  .= '&amp;forcedLanguage=' . $this->element['language'];
 221              $linkItem   .= '&amp;forcedLanguage=' . $this->element['language'];
 222              $modalTitle .= ' &#8212; ' . $this->element['label'];
 223          }
 224  
 225          $urlSelect = $linkItems . '&amp;function=jSelectMenu_' . $this->id;
 226          $urlEdit   = $linkItem . '&amp;task=item.edit&amp;id=\' + document.getElementById("' . $this->id . '_id").value + \'';
 227          $urlNew    = $linkItem . '&amp;task=item.add';
 228  
 229          if ($value) {
 230              $db    = $this->getDatabase();
 231              $query = $db->getQuery(true)
 232                  ->select($db->quoteName('title'))
 233                  ->from($db->quoteName('#__menu'))
 234                  ->where($db->quoteName('id') . ' = :id')
 235                  ->bind(':id', $value, ParameterType::INTEGER);
 236  
 237              $db->setQuery($query);
 238  
 239              try {
 240                  $title = $db->loadResult();
 241              } catch (\RuntimeException $e) {
 242                  Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 243              }
 244          }
 245  
 246          // Placeholder if option is present or not
 247          if (empty($title)) {
 248              if ($this->element->option && (string) $this->element->option['value'] == '') {
 249                  $title_holder = Text::_($this->element->option);
 250              } else {
 251                  $title_holder = Text::_('COM_MENUS_SELECT_A_MENUITEM');
 252              }
 253          }
 254  
 255          $title = empty($title) ? $title_holder : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
 256  
 257          // The current menu item display field.
 258          $html  = '';
 259  
 260          if ($this->allowSelect || $this->allowNew || $this->allowEdit || $this->allowClear) {
 261              $html .= '<span class="input-group">';
 262          }
 263  
 264          $html .= '<input class="form-control" id="' . $this->id . '_name" type="text" value="' . $title . '" disabled="disabled" size="35">';
 265  
 266          // Select menu item button
 267          if ($this->allowSelect) {
 268              $html .= '<button'
 269                  . ' class="btn btn-primary' . ($value ? ' hidden' : '') . '"'
 270                  . ' id="' . $this->id . '_select"'
 271                  . ' data-bs-toggle="modal"'
 272                  . ' type="button"'
 273                  . ' data-bs-target="#ModalSelect' . $modalId . '">'
 274                  . '<span class="icon-file" aria-hidden="true"></span> ' . Text::_('JSELECT')
 275                  . '</button>';
 276          }
 277  
 278          // New menu item button
 279          if ($this->allowNew) {
 280              $html .= '<button'
 281                  . ' class="btn btn-secondary' . ($value ? ' hidden' : '') . '"'
 282                  . ' id="' . $this->id . '_new"'
 283                  . ' data-bs-toggle="modal"'
 284                  . ' type="button"'
 285                  . ' data-bs-target="#ModalNew' . $modalId . '">'
 286                  . '<span class="icon-plus" aria-hidden="true"></span> ' . Text::_('JACTION_CREATE')
 287                  . '</button>';
 288          }
 289  
 290          // Edit menu item button
 291          if ($this->allowEdit) {
 292              $html .= '<button'
 293                  . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 294                  . ' id="' . $this->id . '_edit"'
 295                  . ' data-bs-toggle="modal"'
 296                  . ' type="button"'
 297                  . ' data-bs-target="#ModalEdit' . $modalId . '">'
 298                  . '<span class="icon-pen-square" aria-hidden="true"></span> ' . Text::_('JACTION_EDIT')
 299                  . '</button>';
 300          }
 301  
 302          // Clear menu item button
 303          if ($this->allowClear) {
 304              $html .= '<button'
 305                  . ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
 306                  . ' id="' . $this->id . '_clear"'
 307                  . ' type="button"'
 308                  . ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">'
 309                  . '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
 310                  . '</button>';
 311          }
 312  
 313          // Propagate menu item button
 314          if ($this->allowPropagate && count($languages) > 2) {
 315              // Strip off language tag at the end
 316              $tagLength = (int) strlen($this->element['language']);
 317              $callbackFunctionStem = substr("jSelectMenu_" . $this->id, 0, -$tagLength);
 318  
 319              $html .= '<button'
 320              . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 321              . ' type="button"'
 322              . ' id="' . $this->id . '_propagate"'
 323              . ' title="' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
 324              . ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
 325              . '<span class="icon-sync" aria-hidden="true"></span> ' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
 326              . '</button>';
 327          }
 328  
 329          if ($this->allowSelect || $this->allowNew || $this->allowEdit || $this->allowClear) {
 330              $html .= '</span>';
 331          }
 332  
 333          // Select menu item modal
 334          if ($this->allowSelect) {
 335              $html .= HTMLHelper::_(
 336                  'bootstrap.renderModal',
 337                  'ModalSelect' . $modalId,
 338                  array(
 339                      'title'       => $modalTitle,
 340                      'url'         => $urlSelect,
 341                      'height'      => '400px',
 342                      'width'       => '800px',
 343                      'bodyHeight'  => 70,
 344                      'modalWidth'  => 80,
 345                      'footer'      => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
 346                                          . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
 347                  )
 348              );
 349          }
 350  
 351          // New menu item modal
 352          if ($this->allowNew) {
 353              $html .= HTMLHelper::_(
 354                  'bootstrap.renderModal',
 355                  'ModalNew' . $modalId,
 356                  array(
 357                      'title'       => Text::_('COM_MENUS_NEW_MENUITEM'),
 358                      'backdrop'    => 'static',
 359                      'keyboard'    => false,
 360                      'closeButton' => false,
 361                      'url'         => $urlNew,
 362                      'height'      => '400px',
 363                      'width'       => '800px',
 364                      'bodyHeight'  => 70,
 365                      'modalWidth'  => 80,
 366                      'footer'      => '<button type="button" class="btn btn-secondary"'
 367                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'item\', \'cancel\', \'item-form\'); return false;">'
 368                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 369                              . '<button type="button" class="btn btn-primary"'
 370                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'item\', \'save\', \'item-form\'); return false;">'
 371                              . Text::_('JSAVE') . '</button>'
 372                              . '<button type="button" class="btn btn-success"'
 373                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'item\', \'apply\', \'item-form\'); return false;">'
 374                              . Text::_('JAPPLY') . '</button>',
 375                  )
 376              );
 377          }
 378  
 379          // Edit menu item modal
 380          if ($this->allowEdit) {
 381              $html .= HTMLHelper::_(
 382                  'bootstrap.renderModal',
 383                  'ModalEdit' . $modalId,
 384                  array(
 385                      'title'       => Text::_('COM_MENUS_EDIT_MENUITEM'),
 386                      'backdrop'    => 'static',
 387                      'keyboard'    => false,
 388                      'closeButton' => false,
 389                      'url'         => $urlEdit,
 390                      'height'      => '400px',
 391                      'width'       => '800px',
 392                      'bodyHeight'  => 70,
 393                      'modalWidth'  => 80,
 394                      'footer'      => '<button type="button" class="btn btn-secondary"'
 395                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'item\', \'cancel\', \'item-form\'); return false;">'
 396                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 397                              . '<button type="button" class="btn btn-primary"'
 398                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'item\', \'save\', \'item-form\'); return false;">'
 399                              . Text::_('JSAVE') . '</button>'
 400                              . '<button type="button" class="btn btn-success"'
 401                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'item\', \'apply\', \'item-form\'); return false;">'
 402                              . Text::_('JAPPLY') . '</button>',
 403                  )
 404              );
 405          }
 406  
 407          // Note: class='required' for client side validation.
 408          $class = $this->required ? ' class="required modal-value"' : '';
 409  
 410          // Placeholder if option is present or not when clearing field
 411          if ($this->element->option && (string) $this->element->option['value'] == '') {
 412              $title_holder = Text::_($this->element->option);
 413          } else {
 414              $title_holder = Text::_('COM_MENUS_SELECT_A_MENUITEM');
 415          }
 416  
 417          $html .= '<input type="hidden" id="' . $this->id . '_id" ' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
 418              . '" data-text="' . htmlspecialchars($title_holder, ENT_COMPAT, 'UTF-8') . '" value="' . $value . '">';
 419  
 420          return $html;
 421      }
 422  
 423      /**
 424       * Method to get the field label markup.
 425       *
 426       * @return  string  The field label markup.
 427       *
 428       * @since   3.7.0
 429       */
 430      protected function getLabel()
 431      {
 432          return str_replace($this->id, $this->id . '_name', parent::getLabel());
 433      }
 434  }


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