primo commit
This commit is contained in:
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\ListField;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
/**
|
||||
* MenuItem by Component field.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
class MenuItemByComponentField extends ListField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.3.0
|
||||
*/
|
||||
protected $type = 'MenuItemByComponent';
|
||||
|
||||
/**
|
||||
* Method to get a list of options for a list input.
|
||||
*
|
||||
* @return array An array of JHtml options.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
// Initialise variable.
|
||||
$db = $this->getDatabase();
|
||||
$options = [];
|
||||
|
||||
$query = $db->getQuery(true);
|
||||
$query->select('DISTINCT ' . $db->quoteName('extensions.element'))
|
||||
->from($db->quoteName('#__menu', 'menu'))
|
||||
->join(
|
||||
'INNER',
|
||||
$db->quoteName('#__extensions', 'extensions'),
|
||||
$db->quoteName('extensions.extension_id') . ' = ' . $db->quoteName('menu.component_id')
|
||||
)
|
||||
->where($db->quoteName('menu.client_id') . ' = 0')
|
||||
->where($db->quoteName('menu.type') . ' = ' . $db->quote('component'))
|
||||
->where($db->quoteName('extensions.extension_id') . ' IS NOT NULL');
|
||||
|
||||
$app = Factory::getApplication();
|
||||
$currentMenuType = $app->getInput()->getString('menutype', $app->getUserState($this->context . '.menutype', ''));
|
||||
|
||||
if ($currentMenuType) {
|
||||
$query->where($db->quoteName('menu.menutype') . ' = :currentMenuType')
|
||||
->bind(':currentMenuType', $currentMenuType);
|
||||
}
|
||||
|
||||
$db->setQuery($query);
|
||||
$components = $db->loadColumn();
|
||||
|
||||
foreach ($components as $component) {
|
||||
// Load component language files
|
||||
$lang = $app->getLanguage();
|
||||
$lang->load($component, JPATH_BASE)
|
||||
|| $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component);
|
||||
|
||||
$option = new \stdClass();
|
||||
$option->value = $component;
|
||||
$option->text = Text::_(strtoupper($component));
|
||||
$options[] = $option;
|
||||
}
|
||||
|
||||
// Sort by name
|
||||
$options = ArrayHelper::sortObjects($options, 'text', 1, true, true);
|
||||
|
||||
// Merge any additional options in the XML definition.
|
||||
$options = array_merge(parent::getOptions(), $options);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\GroupedlistField;
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Supports an HTML grouped select list of menu item grouped by menu
|
||||
*
|
||||
* @since 3.8.0
|
||||
*/
|
||||
class MenuItemByTypeField extends GroupedlistField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.8.0
|
||||
*/
|
||||
public $type = 'MenuItemByType';
|
||||
|
||||
/**
|
||||
* The menu type.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $menuType;
|
||||
|
||||
/**
|
||||
* The client id.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $clientId;
|
||||
|
||||
/**
|
||||
* The language.
|
||||
*
|
||||
* @var array
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $language;
|
||||
|
||||
/**
|
||||
* The published status.
|
||||
*
|
||||
* @var array
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $published;
|
||||
|
||||
/**
|
||||
* The disabled status.
|
||||
*
|
||||
* @var array
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $disable;
|
||||
|
||||
/**
|
||||
* 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.8.0
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'menuType':
|
||||
case 'clientId':
|
||||
case 'language':
|
||||
case 'published':
|
||||
case 'disable':
|
||||
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.8.0
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'menuType':
|
||||
$this->menuType = (string) $value;
|
||||
break;
|
||||
|
||||
case 'clientId':
|
||||
$this->clientId = (int) $value;
|
||||
break;
|
||||
|
||||
case 'language':
|
||||
case 'published':
|
||||
case 'disable':
|
||||
$value = (string) $value;
|
||||
$this->$name = $value ? explode(',', $value) : [];
|
||||
break;
|
||||
|
||||
default:
|
||||
parent::__set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to attach a Form object to the field.
|
||||
*
|
||||
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` 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 \Joomla\CMS\Form\FormField::setup()
|
||||
* @since 3.8.0
|
||||
*/
|
||||
public function setup(\SimpleXMLElement $element, $value, $group = null)
|
||||
{
|
||||
$result = parent::setup($element, $value, $group);
|
||||
|
||||
if ($result == true) {
|
||||
$menuType = (string) $this->element['menu_type'];
|
||||
|
||||
if (!$menuType) {
|
||||
$app = Factory::getApplication();
|
||||
$currentMenuType = $app->getUserState('com_menus.items.menutype', '');
|
||||
$menuType = $app->getInput()->getString('menutype', $currentMenuType);
|
||||
}
|
||||
|
||||
$this->menuType = $menuType;
|
||||
$this->clientId = (int) $this->element['client_id'];
|
||||
$this->published = $this->element['published'] ? explode(',', (string) $this->element['published']) : [];
|
||||
$this->disable = $this->element['disable'] ? explode(',', (string) $this->element['disable']) : [];
|
||||
$this->language = $this->element['language'] ? explode(',', (string) $this->element['language']) : [];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the field option groups.
|
||||
*
|
||||
* @return array The field option objects as a nested array in groups.
|
||||
*
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected function getGroups()
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
$menuType = $this->menuType;
|
||||
|
||||
// Get the menu items.
|
||||
$items = MenusHelper::getMenuLinks($menuType, 0, 0, $this->published, $this->language, $this->clientId);
|
||||
|
||||
// Build group for a specific menu type.
|
||||
if ($menuType) {
|
||||
// If the menutype is empty, group the items by menutype.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select($db->quoteName('title'))
|
||||
->from($db->quoteName('#__menu_types'))
|
||||
->where($db->quoteName('menutype') . ' = :menuType')
|
||||
->bind(':menuType', $menuType);
|
||||
$db->setQuery($query);
|
||||
|
||||
try {
|
||||
$menuTitle = $db->loadResult();
|
||||
} catch (\RuntimeException $e) {
|
||||
$menuTitle = $menuType;
|
||||
}
|
||||
|
||||
// Initialize the group.
|
||||
$groups[$menuTitle] = [];
|
||||
|
||||
// Build the options array.
|
||||
foreach ($items as $key => $link) {
|
||||
// Unset if item is menu_item_root
|
||||
if ($link->text === 'Menu_Item_Root') {
|
||||
unset($items[$key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$levelPrefix = str_repeat('- ', max(0, $link->level - 1));
|
||||
|
||||
// Displays language code if not set to All
|
||||
if ($link->language !== '*') {
|
||||
$lang = ' (' . $link->language . ')';
|
||||
} else {
|
||||
$lang = '';
|
||||
}
|
||||
|
||||
$text = Text::_($link->text);
|
||||
|
||||
$groups[$menuTitle][] = HTMLHelper::_(
|
||||
'select.option',
|
||||
$link->value,
|
||||
$levelPrefix . $text . $lang,
|
||||
'value',
|
||||
'text',
|
||||
\in_array($link->type, $this->disable)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Build groups for all menu types.
|
||||
// Build the groups arrays.
|
||||
foreach ($items as $menu) {
|
||||
// Initialize the group.
|
||||
$groups[$menu->title] = [];
|
||||
|
||||
// Build the options array.
|
||||
foreach ($menu->links as $link) {
|
||||
$levelPrefix = str_repeat('- ', max(0, $link->level - 1));
|
||||
|
||||
// Displays language code if not set to All
|
||||
if ($link->language !== '*') {
|
||||
$lang = ' (' . $link->language . ')';
|
||||
} else {
|
||||
$lang = '';
|
||||
}
|
||||
|
||||
$text = Text::_($link->text);
|
||||
|
||||
$groups[$menu->title][] = HTMLHelper::_(
|
||||
'select.option',
|
||||
$link->value,
|
||||
$levelPrefix . $text . $lang,
|
||||
'value',
|
||||
'text',
|
||||
\in_array($link->type, $this->disable)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merge any additional groups in the XML definition.
|
||||
$groups = array_merge(parent::getGroups(), $groups);
|
||||
|
||||
return $groups;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\ListField;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Database\ParameterType;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Ordering field.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenuOrderingField extends ListField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.7
|
||||
*/
|
||||
protected $type = 'MenuOrdering';
|
||||
|
||||
/**
|
||||
* Method to get the list of siblings in a menu.
|
||||
* The method requires that parent be set.
|
||||
*
|
||||
* @return array|boolean The field option objects or false if the parent field has not been set
|
||||
*
|
||||
* @since 1.7
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = [];
|
||||
|
||||
// Get the parent
|
||||
$parent_id = (int) $this->form->getValue('parent_id', 0);
|
||||
|
||||
if (!$parent_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('a.id', 'value'),
|
||||
$db->quoteName('a.title', 'text'),
|
||||
$db->quoteName('a.client_id', 'clientId'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__menu', 'a'))
|
||||
|
||||
->where($db->quoteName('a.published') . ' >= 0')
|
||||
->where($db->quoteName('a.parent_id') . ' = :parentId')
|
||||
->bind(':parentId', $parent_id, ParameterType::INTEGER);
|
||||
|
||||
if ($menuType = $this->form->getValue('menutype')) {
|
||||
$query->where($db->quoteName('a.menutype') . ' = :menuType')
|
||||
->bind(':menuType', $menuType);
|
||||
} else {
|
||||
$query->where($db->quoteName('a.menutype') . ' != ' . $db->quote(''));
|
||||
}
|
||||
|
||||
$query->order($db->quoteName('a.lft') . ' ASC');
|
||||
|
||||
// Get the options.
|
||||
$db->setQuery($query);
|
||||
|
||||
try {
|
||||
$options = $db->loadObjectList();
|
||||
} catch (\RuntimeException $e) {
|
||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
||||
}
|
||||
|
||||
// Allow translation of custom admin menus
|
||||
foreach ($options as &$option) {
|
||||
if ($option->clientId != 0) {
|
||||
$option->text = Text::_($option->text);
|
||||
}
|
||||
}
|
||||
|
||||
$options = array_merge(
|
||||
[['value' => '-1', 'text' => Text::_('COM_MENUS_ITEM_FIELD_ORDERING_VALUE_FIRST')]],
|
||||
$options,
|
||||
[['value' => '-2', 'text' => Text::_('COM_MENUS_ITEM_FIELD_ORDERING_VALUE_LAST')]]
|
||||
);
|
||||
|
||||
// Merge any additional options in the XML definition.
|
||||
$options = array_merge(parent::getOptions(), $options);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the field input markup.
|
||||
*
|
||||
* @return string The field input markup.
|
||||
*
|
||||
* @since 1.7
|
||||
*/
|
||||
protected function getInput()
|
||||
{
|
||||
if ($this->form->getValue('id', 0) == 0) {
|
||||
return '<span class="readonly">' . Text::_('COM_MENUS_ITEM_FIELD_ORDERING_TEXT') . '</span>';
|
||||
}
|
||||
|
||||
return parent::getInput();
|
||||
}
|
||||
}
|
||||
116
administrator/components/com_menus/src/Field/MenuParentField.php
Normal file
116
administrator/components/com_menus/src/Field/MenuParentField.php
Normal file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\ListField;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Database\ParameterType;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Parent field.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenuParentField extends ListField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.6
|
||||
*/
|
||||
protected $type = 'MenuParent';
|
||||
|
||||
/**
|
||||
* Method to get the field options.
|
||||
*
|
||||
* @return array The field option objects.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = [];
|
||||
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
'DISTINCT ' . $db->quoteName('a.id', 'value'),
|
||||
$db->quoteName('a.title', 'text'),
|
||||
$db->quoteName('a.level'),
|
||||
$db->quoteName('a.lft'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__menu', 'a'));
|
||||
|
||||
// Filter by menu type.
|
||||
if ($menuType = $this->form->getValue('menutype')) {
|
||||
$query->where($db->quoteName('a.menutype') . ' = :menuType')
|
||||
->bind(':menuType', $menuType);
|
||||
} else {
|
||||
// Skip special menu types
|
||||
$query->where($db->quoteName('a.menutype') . ' != ' . $db->quote(''));
|
||||
$query->where($db->quoteName('a.menutype') . ' != ' . $db->quote('main'));
|
||||
}
|
||||
|
||||
// Filter by client id.
|
||||
$clientId = $this->getAttribute('clientid');
|
||||
|
||||
if (!\is_null($clientId)) {
|
||||
$clientId = (int) $clientId;
|
||||
$query->where($db->quoteName('a.client_id') . ' = :clientId')
|
||||
->bind(':clientId', $clientId, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
// Prevent parenting to children of this item.
|
||||
if ($id = (int) $this->form->getValue('id')) {
|
||||
$query->join('LEFT', $db->quoteName('#__menu', 'p'), $db->quoteName('p.id') . ' = :id')
|
||||
->bind(':id', $id, ParameterType::INTEGER)
|
||||
->where(
|
||||
'NOT(' . $db->quoteName('a.lft') . ' >= ' . $db->quoteName('p.lft')
|
||||
. ' AND ' . $db->quoteName('a.rgt') . ' <= ' . $db->quoteName('p.rgt') . ')'
|
||||
);
|
||||
}
|
||||
|
||||
$query->where($db->quoteName('a.published') . ' != -2')
|
||||
->order($db->quoteName('a.lft') . ' ASC');
|
||||
|
||||
// Get the options.
|
||||
$db->setQuery($query);
|
||||
|
||||
try {
|
||||
$options = $db->loadObjectList();
|
||||
} catch (\RuntimeException $e) {
|
||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
||||
}
|
||||
|
||||
// Pad the option text with spaces using depth level as a multiplier.
|
||||
foreach ($options as $option) {
|
||||
if ($clientId != 0) {
|
||||
// Allow translation of custom admin menus
|
||||
$option->text = str_repeat('- ', $option->level) . Text::_($option->text);
|
||||
} else {
|
||||
$option->text = str_repeat('- ', $option->level) . $option->text;
|
||||
}
|
||||
}
|
||||
|
||||
// Merge any additional options in the XML definition.
|
||||
$options = array_merge(parent::getOptions(), $options);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Form\Field\ListField;
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Administrator Menu Presets list field.
|
||||
*
|
||||
* @since 3.8.0
|
||||
*/
|
||||
class MenuPresetField extends ListField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected $type = 'MenuPreset';
|
||||
|
||||
/**
|
||||
* Method to get the field options.
|
||||
*
|
||||
* @return array The field option objects.
|
||||
*
|
||||
* @since 3.8.0
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$options = [];
|
||||
$presets = MenusHelper::getPresets();
|
||||
|
||||
foreach ($presets as $preset) {
|
||||
$options[] = HTMLHelper::_('select.option', $preset->name, Text::_($preset->title));
|
||||
}
|
||||
|
||||
return array_merge(parent::getOptions(), $options);
|
||||
}
|
||||
}
|
||||
148
administrator/components/com_menus/src/Field/MenutypeField.php
Normal file
148
administrator/components/com_menus/src/Field/MenutypeField.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Menus\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\ModalSelectField;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Type field.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenutypeField extends ModalSelectField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.6
|
||||
*/
|
||||
protected $type = 'menutype';
|
||||
|
||||
/**
|
||||
* Method to attach a Form object to the field.
|
||||
*
|
||||
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
|
||||
* @param mixed $value The form field value to validate.
|
||||
* @param string $group The field name group control value.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*
|
||||
* @see FormField::setup()
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public function setup(\SimpleXMLElement $element, $value, $group = null)
|
||||
{
|
||||
$result = parent::setup($element, $value, $group);
|
||||
|
||||
if (!$result) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$recordId = (int) $this->form->getValue('id');
|
||||
$clientId = (int) $this->element['clientid'] ?: 0;
|
||||
|
||||
$url = 'index.php?option=com_menus&view=menutypes&tmpl=component&client_id=' . $clientId . '&recordId=' . $recordId;
|
||||
|
||||
$this->urls['select'] = $url;
|
||||
$this->canDo['clear'] = false;
|
||||
$this->modalTitles['select'] = Text::_('COM_MENUS_ITEM_FIELD_TYPE_LABEL');
|
||||
$this->buttonIcons['select'] = 'icon-list';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to retrieve the title of selected item.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
protected function getValueTitle()
|
||||
{
|
||||
$title = '';
|
||||
$clientId = (int) $this->element['clientid'] ?: 0;
|
||||
|
||||
// Get a reverse lookup of the base link URL to Title
|
||||
switch ($this->value) {
|
||||
case 'url':
|
||||
$title = Text::_('COM_MENUS_TYPE_EXTERNAL_URL');
|
||||
break;
|
||||
|
||||
case 'alias':
|
||||
$title = Text::_('COM_MENUS_TYPE_ALIAS');
|
||||
break;
|
||||
|
||||
case 'separator':
|
||||
$title = Text::_('COM_MENUS_TYPE_SEPARATOR');
|
||||
break;
|
||||
|
||||
case 'heading':
|
||||
$title = Text::_('COM_MENUS_TYPE_HEADING');
|
||||
break;
|
||||
|
||||
case 'container':
|
||||
$title = Text::_('COM_MENUS_TYPE_CONTAINER');
|
||||
break;
|
||||
|
||||
default:
|
||||
$link = $this->form->getValue('link');
|
||||
|
||||
if ($link !== null) {
|
||||
/** @var \Joomla\Component\Menus\Administrator\Model\MenutypesModel $model */
|
||||
$model = Factory::getApplication()->bootComponent('com_menus')
|
||||
->getMVCFactory()->createModel('Menutypes', 'Administrator', ['ignore_request' => true]);
|
||||
$model->setState('client_id', $clientId);
|
||||
|
||||
$rlu = $model->getReverseLookup();
|
||||
|
||||
// Clean the link back to the option, view and layout
|
||||
$title = Text::_(ArrayHelper::getValue($rlu, MenusHelper::getLinkKey($link)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the field input markup.
|
||||
*
|
||||
* @return string The field input markup.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
protected function getInput()
|
||||
{
|
||||
// Get the layout data
|
||||
$data = $this->getLayoutData();
|
||||
|
||||
// Load the content title here to avoid a double DB Query
|
||||
$data['valueTitle'] = $this->getValueTitle();
|
||||
|
||||
// On new item creation the model forces the value to be 'component',
|
||||
// However this is need to be empty in the input for correct validation and rendering.
|
||||
if ($data['value'] === 'component' && !$data['valueTitle'] && !$this->form->getValue('link')) {
|
||||
$data['value'] = '';
|
||||
}
|
||||
|
||||
return $this->getRenderer($this->layout)->render($data);
|
||||
}
|
||||
}
|
||||
292
administrator/components/com_menus/src/Field/Modal/MenuField.php
Normal file
292
administrator/components/com_menus/src/Field/Modal/MenuField.php
Normal file
@ -0,0 +1,292 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @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\Field\ModalSelectField;
|
||||
use Joomla\CMS\Form\FormField;
|
||||
use Joomla\CMS\Language\LanguageHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Layout\FileLayout;
|
||||
use Joomla\CMS\Session\Session;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
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 ModalSelectField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected $type = 'Modal_Menu';
|
||||
|
||||
/**
|
||||
* 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':
|
||||
// @TODO: The override only for backward compatibility. Remove in Joomla 6.
|
||||
$map = [
|
||||
'allowSelect' => 'select',
|
||||
'allowClear' => 'clear',
|
||||
'allowNew' => 'new',
|
||||
'allowEdit' => 'edit',
|
||||
'allowPropagate' => 'propagate',
|
||||
];
|
||||
$newName = $map[$name];
|
||||
|
||||
@trigger_error(
|
||||
\sprintf(
|
||||
'MenuField::__get property "%s" is deprecated, and will not work in Joomla 6. Use "%s" property instead.',
|
||||
$name,
|
||||
$newName
|
||||
),
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
return parent::__get($newName);
|
||||
}
|
||||
|
||||
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':
|
||||
// @TODO: The override only for backward compatibility. Remove in Joomla 6.
|
||||
$map = [
|
||||
'allowSelect' => 'select',
|
||||
'allowClear' => 'clear',
|
||||
'allowNew' => 'new',
|
||||
'allowEdit' => 'edit',
|
||||
'allowPropagate' => 'propagate',
|
||||
];
|
||||
$newName = $map[$name];
|
||||
|
||||
@trigger_error(
|
||||
\sprintf(
|
||||
'MenuField::__set property "%s" is deprecated, and will not work in Joomla 6. Use "%s" property instead.',
|
||||
$name,
|
||||
$newName
|
||||
),
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
$value = (string) $value;
|
||||
$value = !($value === 'false' || $value === 'off' || $value === '0');
|
||||
|
||||
parent::__set($newName, $value);
|
||||
break;
|
||||
|
||||
default:
|
||||
parent::__set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to attach a Form object to the field.
|
||||
*
|
||||
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` 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)
|
||||
{
|
||||
// Check if the value consist with id:alias, extract the id only
|
||||
if ($value && str_contains($value, ':')) {
|
||||
[$id] = explode(':', $value, 2);
|
||||
$value = (int) $id;
|
||||
}
|
||||
|
||||
$return = parent::setup($element, $value, $group);
|
||||
|
||||
if (!$return) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
$app = Factory::getApplication();
|
||||
|
||||
$app->getLanguage()->load('com_menus', JPATH_ADMINISTRATOR);
|
||||
|
||||
$languages = LanguageHelper::getContentLanguages([0, 1], false);
|
||||
$language = (string) $this->element['language'];
|
||||
$clientId = (int) $this->element['clientid'];
|
||||
|
||||
// Prepare enabled actions
|
||||
$this->canDo['propagate'] = ((string) $this->element['propagate'] === 'true') && \count($languages) > 2;
|
||||
|
||||
// Creating/editing menu items is not supported in frontend.
|
||||
if (!$app->isClient('administrator')) {
|
||||
$this->canDo['new'] = false;
|
||||
$this->canDo['edit'] = false;
|
||||
}
|
||||
|
||||
// Prepare Urls
|
||||
$linkItems = (new Uri())->setPath(Uri::base(true) . '/index.php');
|
||||
$linkItems->setQuery([
|
||||
'option' => 'com_menus',
|
||||
'view' => 'items',
|
||||
'layout' => 'modal',
|
||||
'tmpl' => 'component',
|
||||
'client_id' => $clientId,
|
||||
Session::getFormToken() => 1,
|
||||
]);
|
||||
$linkItem = clone $linkItems;
|
||||
$linkItem->setVar('view', 'item');
|
||||
$linkCheckin = (new Uri())->setPath(Uri::base(true) . '/index.php');
|
||||
$linkCheckin->setQuery([
|
||||
'option' => 'com_menus',
|
||||
'task' => 'items.checkin',
|
||||
'format' => 'json',
|
||||
Session::getFormToken() => 1,
|
||||
]);
|
||||
|
||||
if ($language) {
|
||||
$linkItems->setVar('forcedLanguage', $language);
|
||||
$linkItem->setVar('forcedLanguage', $language);
|
||||
|
||||
$modalTitle = Text::_('COM_MENUS_SELECT_A_MENUITEM') . ' — ' . $this->getTitle();
|
||||
|
||||
$this->dataAttributes['data-language'] = $language;
|
||||
} else {
|
||||
$modalTitle = Text::_('COM_MENUS_SELECT_A_MENUITEM');
|
||||
}
|
||||
|
||||
$urlSelect = $linkItems;
|
||||
$urlEdit = clone $linkItem;
|
||||
$urlEdit->setVar('task', 'item.edit');
|
||||
$urlNew = clone $linkItem;
|
||||
$urlNew->setVar('task', 'item.add');
|
||||
|
||||
$this->urls['select'] = (string) $urlSelect;
|
||||
$this->urls['new'] = (string) $urlNew;
|
||||
$this->urls['edit'] = (string) $urlEdit;
|
||||
$this->urls['checkin'] = (string) $linkCheckin;
|
||||
|
||||
// Prepare titles
|
||||
$this->modalTitles['select'] = $modalTitle;
|
||||
$this->modalTitles['new'] = Text::_('COM_MENUS_NEW_MENUITEM');
|
||||
$this->modalTitles['edit'] = Text::_('COM_MENUS_EDIT_MENUITEM');
|
||||
|
||||
$this->hint = $this->hint ?: Text::_('COM_MENUS_SELECT_A_MENUITEM');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to retrieve the title of selected item.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
protected function getValueTitle()
|
||||
{
|
||||
$value = (int) $this->value ?: '';
|
||||
$title = '';
|
||||
|
||||
if ($value) {
|
||||
try {
|
||||
$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);
|
||||
|
||||
$title = $db->loadResult();
|
||||
} catch (\Throwable $e) {
|
||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
return $title ?: $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the data to be passed to the layout for rendering.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
protected function getLayoutData()
|
||||
{
|
||||
$data = parent::getLayoutData();
|
||||
$data['language'] = (string) $this->element['language'];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the renderer
|
||||
*
|
||||
* @param string $layoutId Id to load
|
||||
*
|
||||
* @return FileLayout
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
protected function getRenderer($layoutId = 'default')
|
||||
{
|
||||
$layout = parent::getRenderer($layoutId);
|
||||
$layout->setComponent('com_menus');
|
||||
$layout->setClient(1);
|
||||
|
||||
return $layout;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user