* @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\Component\Menus\Administrator\Field\Modal; use Joomla\CMS\Factory; use Joomla\CMS\Form\FormField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\LanguageHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Session\Session; use Joomla\Database\ParameterType; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Supports a modal menu item picker. * * @since 3.7.0 */ class MenuField extends FormField { /** * The form field type. * * @var string * @since 3.7.0 */ protected $type = 'Modal_Menu'; /** * Determinate, if the select button is shown * * @var boolean * @since 3.7.0 */ protected $allowSelect = true; /** * Determinate, if the clear button is shown * * @var boolean * @since 3.7.0 */ protected $allowClear = true; /** * Determinate, if the create button is shown * * @var boolean * @since 3.7.0 */ protected $allowNew = false; /** * Determinate, if the edit button is shown * * @var boolean * @since 3.7.0 */ protected $allowEdit = false; /** * Determinate, if the propagate button is shown * * @var boolean * @since 3.9.0 */ protected $allowPropagate = false; /** * Method to get certain otherwise inaccessible properties from the form field object. * * @param string $name The property name for which to get the value. * * @return mixed The property value or null. * * @since 3.7.0 */ public function __get($name) { switch ($name) { case 'allowSelect': case 'allowClear': case 'allowNew': case 'allowEdit': case 'allowPropagate': return $this->$name; } return parent::__get($name); } /** * Method to set certain otherwise inaccessible properties of the form field object. * * @param string $name The property name for which to set the value. * @param mixed $value The value of the property. * * @return void * * @since 3.7.0 */ public function __set($name, $value) { switch ($name) { case 'allowSelect': case 'allowClear': case 'allowNew': case 'allowEdit': case 'allowPropagate': $value = (string) $value; $this->$name = !($value === 'false' || $value === 'off' || $value === '0'); break; default: parent::__set($name, $value); } } /** * Method to attach a JForm object to the field. * * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `` tag for the form field object. * @param mixed $value The form field value to validate. * @param string $group The field name group control value. This acts as an array container for the field. * For example if the field has name="foo" and the group value is set to "bar" then the * full field name would end up being "bar[foo]". * * @return boolean True on success. * * @see FormField::setup() * @since 3.7.0 */ public function setup(\SimpleXMLElement $element, $value, $group = null) { $return = parent::setup($element, $value, $group); if ($return) { $this->allowSelect = ((string) $this->element['select']) !== 'false'; $this->allowClear = ((string) $this->element['clear']) !== 'false'; $this->allowPropagate = ((string) $this->element['propagate']) === 'true'; // Creating/editing menu items is not supported in frontend. $isAdministrator = Factory::getApplication()->isClient('administrator'); $this->allowNew = $isAdministrator ? ((string) $this->element['new']) === 'true' : false; $this->allowEdit = $isAdministrator ? ((string) $this->element['edit']) === 'true' : false; } return $return; } /** * Method to get the field input markup. * * @return string The field input markup. * * @since 3.7.0 */ protected function getInput() { $clientId = (int) $this->element['clientid']; $languages = LanguageHelper::getContentLanguages(array(0, 1), false); // Load language Factory::getLanguage()->load('com_menus', JPATH_ADMINISTRATOR); // The active article id field. $value = (int) $this->value ?: ''; // Create the modal id. $modalId = 'Item_' . $this->id; /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */ $wa = Factory::getApplication()->getDocument()->getWebAssetManager(); // Add the modal field script to the document head. $wa->useScript('field.modal-fields'); // Script to proxy the select modal function to the modal-fields.js file. if ($this->allowSelect) { static $scriptSelect = null; if (is_null($scriptSelect)) { $scriptSelect = array(); } if (!isset($scriptSelect[$this->id])) { $wa->addInlineScript( " window.jSelectMenu_" . $this->id . " = function (id, title, object) { window.processModalSelect('Item', '" . $this->id . "', id, title, '', object); }", [], ['type' => 'module'] ); Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED'); $scriptSelect[$this->id] = true; } } // Setup variables for display. $linkSuffix = '&layout=modal&client_id=' . $clientId . '&tmpl=component&' . Session::getFormToken() . '=1'; $linkItems = 'index.php?option=com_menus&view=items' . $linkSuffix; $linkItem = 'index.php?option=com_menus&view=item' . $linkSuffix; $modalTitle = Text::_('COM_MENUS_SELECT_A_MENUITEM'); if (isset($this->element['language'])) { $linkItems .= '&forcedLanguage=' . $this->element['language']; $linkItem .= '&forcedLanguage=' . $this->element['language']; $modalTitle .= ' — ' . $this->element['label']; } $urlSelect = $linkItems . '&function=jSelectMenu_' . $this->id; $urlEdit = $linkItem . '&task=item.edit&id=\' + document.getElementById("' . $this->id . '_id").value + \''; $urlNew = $linkItem . '&task=item.add'; if ($value) { $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__menu')) ->where($db->quoteName('id') . ' = :id') ->bind(':id', $value, ParameterType::INTEGER); $db->setQuery($query); try { $title = $db->loadResult(); } catch (\RuntimeException $e) { Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); } } // Placeholder if option is present or not if (empty($title)) { if ($this->element->option && (string) $this->element->option['value'] == '') { $title_holder = Text::_($this->element->option); } else { $title_holder = Text::_('COM_MENUS_SELECT_A_MENUITEM'); } } $title = empty($title) ? $title_holder : htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); // The current menu item display field. $html = ''; if ($this->allowSelect || $this->allowNew || $this->allowEdit || $this->allowClear) { $html .= ''; } $html .= ''; // Select menu item button if ($this->allowSelect) { $html .= '' . ' ' . Text::_('JSELECT') . ''; } // New menu item button if ($this->allowNew) { $html .= '' . ' ' . Text::_('JACTION_CREATE') . ''; } // Edit menu item button if ($this->allowEdit) { $html .= '' . ' ' . Text::_('JACTION_EDIT') . ''; } // Clear menu item button if ($this->allowClear) { $html .= '' . ' ' . Text::_('JCLEAR') . ''; } // Propagate menu item button if ($this->allowPropagate && count($languages) > 2) { // Strip off language tag at the end $tagLength = (int) strlen($this->element['language']); $callbackFunctionStem = substr("jSelectMenu_" . $this->id, 0, -$tagLength); $html .= '' . ' ' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON') . ''; } if ($this->allowSelect || $this->allowNew || $this->allowEdit || $this->allowClear) { $html .= ''; } // Select menu item modal if ($this->allowSelect) { $html .= HTMLHelper::_( 'bootstrap.renderModal', 'ModalSelect' . $modalId, array( 'title' => $modalTitle, 'url' => $urlSelect, 'height' => '400px', 'width' => '800px', 'bodyHeight' => 70, 'modalWidth' => 80, 'footer' => '', ) ); } // New menu item modal if ($this->allowNew) { $html .= HTMLHelper::_( 'bootstrap.renderModal', 'ModalNew' . $modalId, array( 'title' => Text::_('COM_MENUS_NEW_MENUITEM'), 'backdrop' => 'static', 'keyboard' => false, 'closeButton' => false, 'url' => $urlNew, 'height' => '400px', 'width' => '800px', 'bodyHeight' => 70, 'modalWidth' => 80, 'footer' => '' . '' . '', ) ); } // Edit menu item modal if ($this->allowEdit) { $html .= HTMLHelper::_( 'bootstrap.renderModal', 'ModalEdit' . $modalId, array( 'title' => Text::_('COM_MENUS_EDIT_MENUITEM'), 'backdrop' => 'static', 'keyboard' => false, 'closeButton' => false, 'url' => $urlEdit, 'height' => '400px', 'width' => '800px', 'bodyHeight' => 70, 'modalWidth' => 80, 'footer' => '' . '' . '', ) ); } // Note: class='required' for client side validation. $class = $this->required ? ' class="required modal-value"' : ''; // Placeholder if option is present or not when clearing field if ($this->element->option && (string) $this->element->option['value'] == '') { $title_holder = Text::_($this->element->option); } else { $title_holder = Text::_('COM_MENUS_SELECT_A_MENUITEM'); } $html .= ''; return $html; } /** * Method to get the field label markup. * * @return string The field label markup. * * @since 3.7.0 */ protected function getLabel() { return str_replace($this->id, $this->id . '_name', parent::getLabel()); } }