primo commit
This commit is contained in:
1763
administrator/components/com_menus/src/Model/ItemModel.php
Normal file
1763
administrator/components/com_menus/src/Model/ItemModel.php
Normal file
File diff suppressed because it is too large
Load Diff
584
administrator/components/com_menus/src/Model/ItemsModel.php
Normal file
584
administrator/components/com_menus/src/Model/ItemsModel.php
Normal file
@ -0,0 +1,584 @@
|
||||
<?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\Model;
|
||||
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\Language\Associations;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Log\Log;
|
||||
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\Database\QueryInterface;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Item List Model for Menus.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class ItemsModel extends ListModel
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $config An optional associative array of configuration settings.
|
||||
* @param ?MVCFactoryInterface $factory The factory.
|
||||
*
|
||||
* @see \Joomla\CMS\MVC\Model\BaseDatabaseModel
|
||||
* @since 3.2
|
||||
*/
|
||||
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
|
||||
{
|
||||
if (empty($config['filter_fields'])) {
|
||||
$config['filter_fields'] = [
|
||||
'id', 'a.id',
|
||||
'menutype', 'a.menutype', 'menutype_title',
|
||||
'title', 'a.title',
|
||||
'alias', 'a.alias',
|
||||
'published', 'a.published',
|
||||
'access', 'a.access', 'access_level',
|
||||
'language', 'a.language',
|
||||
'checked_out', 'a.checked_out',
|
||||
'checked_out_time', 'a.checked_out_time',
|
||||
'lft', 'a.lft',
|
||||
'rgt', 'a.rgt',
|
||||
'level', 'a.level',
|
||||
'path', 'a.path',
|
||||
'client_id', 'a.client_id',
|
||||
'home', 'a.home',
|
||||
'parent_id', 'a.parent_id',
|
||||
'publish_up', 'a.publish_up',
|
||||
'publish_down', 'a.publish_down',
|
||||
'e.element', 'componentName',
|
||||
'a.ordering',
|
||||
];
|
||||
|
||||
if (Associations::isEnabled()) {
|
||||
$config['filter_fields'][] = 'association';
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct($config, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @param string $ordering An optional ordering field.
|
||||
* @param string $direction An optional direction (asc|desc).
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function populateState($ordering = 'a.lft', $direction = 'asc')
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
|
||||
$forcedLanguage = $app->getInput()->get('forcedLanguage', '', 'cmd');
|
||||
|
||||
// Adjust the context to support modal layouts.
|
||||
if ($layout = $app->getInput()->get('layout')) {
|
||||
$this->context .= '.' . $layout;
|
||||
}
|
||||
|
||||
// Adjust the context to support forced languages.
|
||||
if ($forcedLanguage) {
|
||||
$this->context .= '.' . $forcedLanguage;
|
||||
}
|
||||
|
||||
// Watch changes in client_id and menutype and keep sync whenever needed.
|
||||
$currentClientId = $app->getUserState($this->context . '.client_id', 0);
|
||||
$clientId = $app->getInput()->getInt('client_id', $currentClientId);
|
||||
|
||||
// Load mod_menu.ini file when client is administrator
|
||||
if ($clientId == 1) {
|
||||
Factory::getLanguage()->load('mod_menu', JPATH_ADMINISTRATOR);
|
||||
}
|
||||
|
||||
$currentMenuType = $app->getUserState($this->context . '.menutype', '');
|
||||
$menuType = $app->getInput()->getString('menutype', $currentMenuType);
|
||||
|
||||
// If client_id changed clear menutype and reset pagination
|
||||
if ($clientId != $currentClientId) {
|
||||
$menuType = '';
|
||||
|
||||
$app->getInput()->set('limitstart', 0);
|
||||
$app->getInput()->set('menutype', '');
|
||||
}
|
||||
|
||||
// If menutype changed reset pagination.
|
||||
if ($menuType != $currentMenuType) {
|
||||
$app->getInput()->set('limitstart', 0);
|
||||
}
|
||||
|
||||
if (!$menuType) {
|
||||
$app->setUserState($this->context . '.menutype', '');
|
||||
$this->setState('menutypetitle', '');
|
||||
$this->setState('menutypeid', '');
|
||||
} elseif ($menuType == 'main') {
|
||||
// Special menu types, if selected explicitly, will be allowed as a filter
|
||||
// Adjust client_id to match the menutype. This is safe as client_id was not changed in this request.
|
||||
$app->getInput()->set('client_id', 1);
|
||||
|
||||
$app->setUserState($this->context . '.menutype', $menuType);
|
||||
$this->setState('menutypetitle', ucfirst($menuType));
|
||||
$this->setState('menutypeid', -1);
|
||||
} elseif ($cMenu = $this->getMenu($menuType, true)) {
|
||||
// Get the menutype object with appropriate checks.
|
||||
// Adjust client_id to match the menutype. This is safe as client_id was not changed in this request.
|
||||
$app->getInput()->set('client_id', $cMenu->client_id);
|
||||
|
||||
$app->setUserState($this->context . '.menutype', $menuType);
|
||||
$this->setState('menutypetitle', $cMenu->title);
|
||||
$this->setState('menutypeid', $cMenu->id);
|
||||
} else {
|
||||
// This menutype does not exist, leave client id unchanged but reset menutype and pagination
|
||||
$menuType = '';
|
||||
|
||||
$app->getInput()->set('limitstart', 0);
|
||||
$app->getInput()->set('menutype', $menuType);
|
||||
|
||||
$app->setUserState($this->context . '.menutype', $menuType);
|
||||
$this->setState('menutypetitle', '');
|
||||
$this->setState('menutypeid', '');
|
||||
}
|
||||
|
||||
// Client id filter
|
||||
$clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
|
||||
$this->setState('filter.client_id', $clientId);
|
||||
|
||||
// Use a different filter file when client is administrator
|
||||
if ($clientId == 1) {
|
||||
$this->filterFormName = 'filter_itemsadmin';
|
||||
}
|
||||
|
||||
$this->setState('filter.menutype', $menuType);
|
||||
|
||||
$language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
|
||||
$this->setState('filter.language', $language);
|
||||
|
||||
// Component parameters.
|
||||
$params = ComponentHelper::getParams('com_menus');
|
||||
$this->setState('params', $params);
|
||||
|
||||
// List state information.
|
||||
parent::populateState($ordering, $direction);
|
||||
|
||||
// Force a language.
|
||||
if (!empty($forcedLanguage)) {
|
||||
$this->setState('filter.language', $forcedLanguage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a store id based on model configuration state.
|
||||
*
|
||||
* This is necessary because the model is used by the component and
|
||||
* different modules that might need different sets of data or different
|
||||
* ordering requirements.
|
||||
*
|
||||
* @param string $id A prefix for the store id.
|
||||
*
|
||||
* @return string A store id.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getStoreId($id = '')
|
||||
{
|
||||
// Compile the store id.
|
||||
$id .= ':' . $this->getState('filter.access');
|
||||
$id .= ':' . $this->getState('filter.published');
|
||||
$id .= ':' . $this->getState('filter.language');
|
||||
$id .= ':' . $this->getState('filter.search');
|
||||
$id .= ':' . $this->getState('filter.parent_id');
|
||||
$id .= ':' . $this->getState('filter.menutype');
|
||||
$id .= ':' . $this->getState('filter.client_id');
|
||||
|
||||
return parent::getStoreId($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an SQL query to load the list data.
|
||||
*
|
||||
* @return QueryInterface A query object.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getListQuery()
|
||||
{
|
||||
// Create a new query object.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
$user = $this->getCurrentUser();
|
||||
$clientId = (int) $this->getState('filter.client_id');
|
||||
|
||||
// Select all fields from the table.
|
||||
$query->select(
|
||||
// We can't quote state values because they could contain expressions.
|
||||
$this->getState(
|
||||
'list.select',
|
||||
[
|
||||
$db->quoteName('a.id'),
|
||||
$db->quoteName('a.menutype'),
|
||||
$db->quoteName('a.title'),
|
||||
$db->quoteName('a.alias'),
|
||||
$db->quoteName('a.note'),
|
||||
$db->quoteName('a.path'),
|
||||
$db->quoteName('a.link'),
|
||||
$db->quoteName('a.type'),
|
||||
$db->quoteName('a.parent_id'),
|
||||
$db->quoteName('a.level'),
|
||||
$db->quoteName('a.component_id'),
|
||||
$db->quoteName('a.checked_out'),
|
||||
$db->quoteName('a.checked_out_time'),
|
||||
$db->quoteName('a.browserNav'),
|
||||
$db->quoteName('a.access'),
|
||||
$db->quoteName('a.img'),
|
||||
$db->quoteName('a.template_style_id'),
|
||||
$db->quoteName('a.params'),
|
||||
$db->quoteName('a.lft'),
|
||||
$db->quoteName('a.rgt'),
|
||||
$db->quoteName('a.home'),
|
||||
$db->quoteName('a.language'),
|
||||
$db->quoteName('a.client_id'),
|
||||
$db->quoteName('a.publish_up'),
|
||||
$db->quoteName('a.publish_down'),
|
||||
]
|
||||
)
|
||||
)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('l.title', 'language_title'),
|
||||
$db->quoteName('l.image', 'language_image'),
|
||||
$db->quoteName('l.sef', 'language_sef'),
|
||||
$db->quoteName('u.name', 'editor'),
|
||||
$db->quoteName('c.element', 'componentname'),
|
||||
$db->quoteName('ag.title', 'access_level'),
|
||||
$db->quoteName('mt.id', 'menutype_id'),
|
||||
$db->quoteName('mt.title', 'menutype_title'),
|
||||
$db->quoteName('e.enabled'),
|
||||
$db->quoteName('e.name'),
|
||||
'CASE WHEN ' . $db->quoteName('a.type') . ' = ' . $db->quote('component')
|
||||
. ' THEN ' . $db->quoteName('a.published') . ' +2 * (' . $db->quoteName('e.enabled') . ' -1)'
|
||||
. ' ELSE ' . $db->quoteName('a.published') . ' END AS ' . $db->quoteName('published'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__menu', 'a'));
|
||||
|
||||
// Join over the language
|
||||
$query->join('LEFT', $db->quoteName('#__languages', 'l'), $db->quoteName('l.lang_code') . ' = ' . $db->quoteName('a.language'));
|
||||
|
||||
// Join over the users.
|
||||
$query->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('a.checked_out'));
|
||||
|
||||
// Join over components
|
||||
$query->join('LEFT', $db->quoteName('#__extensions', 'c'), $db->quoteName('c.extension_id') . ' = ' . $db->quoteName('a.component_id'));
|
||||
|
||||
// Join over the asset groups.
|
||||
$query->join('LEFT', $db->quoteName('#__viewlevels', 'ag'), $db->quoteName('ag.id') . ' = ' . $db->quoteName('a.access'));
|
||||
|
||||
// Join over the menu types.
|
||||
$query->join('LEFT', $db->quoteName('#__menu_types', 'mt'), $db->quoteName('mt.menutype') . ' = ' . $db->quoteName('a.menutype'));
|
||||
|
||||
// Join over the extensions
|
||||
$query->join('LEFT', $db->quoteName('#__extensions', 'e'), $db->quoteName('e.extension_id') . ' = ' . $db->quoteName('a.component_id'));
|
||||
|
||||
// Join over the associations.
|
||||
if (Associations::isEnabled()) {
|
||||
$subQuery = $db->getQuery(true)
|
||||
->select('COUNT(' . $db->quoteName('asso1.id') . ') > 1')
|
||||
->from($db->quoteName('#__associations', 'asso1'))
|
||||
->join('INNER', $db->quoteName('#__associations', 'asso2'), $db->quoteName('asso1.key') . ' = ' . $db->quoteName('asso2.key'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('asso1.id') . ' = ' . $db->quoteName('a.id'),
|
||||
$db->quoteName('asso1.context') . ' = ' . $db->quote('com_menus.item'),
|
||||
]
|
||||
);
|
||||
|
||||
$query->select('(' . $subQuery . ') AS ' . $db->quoteName('association'));
|
||||
}
|
||||
|
||||
// Exclude the root category.
|
||||
$query->where(
|
||||
[
|
||||
$db->quoteName('a.id') . ' > 1',
|
||||
$db->quoteName('a.client_id') . ' = :clientId',
|
||||
]
|
||||
)
|
||||
->bind(':clientId', $clientId, ParameterType::INTEGER);
|
||||
|
||||
// Filter on the published state.
|
||||
$published = $this->getState('filter.published', '');
|
||||
|
||||
if (is_numeric($published)) {
|
||||
$published = (int) $published;
|
||||
$query->where($db->quoteName('a.published') . ' = :published')
|
||||
->bind(':published', $published, ParameterType::INTEGER);
|
||||
} elseif ($published === '') {
|
||||
$query->where($db->quoteName('a.published') . ' IN (0, 1)');
|
||||
}
|
||||
|
||||
// Filter by search in title, alias or id
|
||||
if ($search = trim($this->getState('filter.search', ''))) {
|
||||
if (stripos($search, 'id:') === 0) {
|
||||
$search = (int) substr($search, 3);
|
||||
$query->where($db->quoteName('a.id') . ' = :search')
|
||||
->bind(':search', $search, ParameterType::INTEGER);
|
||||
} elseif (stripos($search, 'link:') === 0) {
|
||||
if ($search = str_replace(' ', '%', trim(substr($search, 5)))) {
|
||||
$query->where($db->quoteName('a.link') . ' LIKE :search')
|
||||
->bind(':search', $search);
|
||||
}
|
||||
} else {
|
||||
$search = '%' . str_replace(' ', '%', trim($search)) . '%';
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.title') . ' LIKE :search1',
|
||||
$db->quoteName('a.alias') . ' LIKE :search2',
|
||||
$db->quoteName('a.note') . ' LIKE :search3',
|
||||
],
|
||||
'OR'
|
||||
)
|
||||
->bind([':search1', ':search2', ':search3'], $search);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter the items over the parent id if set.
|
||||
$parentId = (int) $this->getState('filter.parent_id');
|
||||
$level = (int) $this->getState('filter.level');
|
||||
|
||||
if ($parentId) {
|
||||
// Create a subquery for the sub-items list
|
||||
$subQuery = $db->getQuery(true)
|
||||
->select($db->quoteName('sub.id'))
|
||||
->from($db->quoteName('#__menu', 'sub'))
|
||||
->join(
|
||||
'INNER',
|
||||
$db->quoteName('#__menu', 'this'),
|
||||
$db->quoteName('sub.lft') . ' > ' . $db->quoteName('this.lft')
|
||||
. ' AND ' . $db->quoteName('sub.rgt') . ' < ' . $db->quoteName('this.rgt')
|
||||
)
|
||||
->where($db->quoteName('this.id') . ' = :parentId1');
|
||||
|
||||
if ($level) {
|
||||
$subQuery->where($db->quoteName('sub.level') . ' <= ' . $db->quoteName('this.level') . ' + :level - 1');
|
||||
$query->bind(':level', $level, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
// Add the subquery to the main query
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.parent_id') . ' = :parentId2',
|
||||
$db->quoteName('a.parent_id') . ' IN (' . (string) $subQuery . ')',
|
||||
],
|
||||
'OR'
|
||||
)
|
||||
->bind([':parentId1', ':parentId2'], $parentId, ParameterType::INTEGER);
|
||||
} elseif ($level) {
|
||||
// Filter on the level.
|
||||
$query->where($db->quoteName('a.level') . ' <= :level')
|
||||
->bind(':level', $level, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
// Filter the items over the menu id if set.
|
||||
$menuType = $this->getState('filter.menutype');
|
||||
|
||||
// A value "" means all
|
||||
if ($menuType == '') {
|
||||
// Load all menu types we have manage access
|
||||
$query2 = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('id'),
|
||||
$db->quoteName('menutype'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__menu_types'))
|
||||
->where($db->quoteName('client_id') . ' = :clientId')
|
||||
->bind(':clientId', $clientId, ParameterType::INTEGER)
|
||||
->order($db->quoteName('title'));
|
||||
|
||||
// Show protected items on explicit filter only
|
||||
$query->where($db->quoteName('a.menutype') . ' != ' . $db->quote('main'));
|
||||
|
||||
$menuTypes = $db->setQuery($query2)->loadObjectList();
|
||||
|
||||
if ($menuTypes) {
|
||||
$types = [];
|
||||
|
||||
foreach ($menuTypes as $type) {
|
||||
if ($user->authorise('core.manage', 'com_menus.menu.' . (int) $type->id)) {
|
||||
$types[] = $type->menutype;
|
||||
}
|
||||
}
|
||||
|
||||
if ($types) {
|
||||
$query->whereIn($db->quoteName('a.menutype'), $types);
|
||||
} else {
|
||||
$query->where(0);
|
||||
}
|
||||
}
|
||||
} elseif (\strlen($menuType)) {
|
||||
// Default behavior => load all items from a specific menu
|
||||
$query->where($db->quoteName('a.menutype') . ' = :menuType')
|
||||
->bind(':menuType', $menuType);
|
||||
} else {
|
||||
// Empty menu type => error
|
||||
$query->where('1 != 1');
|
||||
}
|
||||
|
||||
// Filter on the access level.
|
||||
if ($access = (int) $this->getState('filter.access')) {
|
||||
$query->where($db->quoteName('a.access') . ' = :access')
|
||||
->bind(':access', $access, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
// Implement View Level Access
|
||||
if (!$user->authorise('core.admin')) {
|
||||
if ($groups = $user->getAuthorisedViewLevels()) {
|
||||
$query->whereIn($db->quoteName('a.access'), $groups);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on the language.
|
||||
if ($language = $this->getState('filter.language')) {
|
||||
$query->where($db->quoteName('a.language') . ' = :language')
|
||||
->bind(':language', $language);
|
||||
}
|
||||
|
||||
// Filter on componentName
|
||||
if ($componentName = $this->getState('filter.componentName')) {
|
||||
$query->where($db->quoteName('e.element') . ' = :component')
|
||||
->bind(':component', $componentName);
|
||||
}
|
||||
|
||||
// Add the list ordering clause.
|
||||
$query->order($db->escape($this->getState('list.ordering', 'a.lft')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to allow derived classes to preprocess the form.
|
||||
*
|
||||
* @param Form $form A Form object.
|
||||
* @param mixed $data The data expected for the form.
|
||||
* @param string $group The name of the plugin group to import (defaults to "content").
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.2
|
||||
* @throws \Exception if there is an error in the form event.
|
||||
*/
|
||||
protected function preprocessForm(Form $form, $data, $group = 'content')
|
||||
{
|
||||
$name = $form->getName();
|
||||
|
||||
if ($name == 'com_menus.items.filter') {
|
||||
$clientId = $this->getState('filter.client_id');
|
||||
$form->setFieldAttribute('menutype', 'clientid', $clientId);
|
||||
} elseif (false !== strpos($name, 'com_menus.items.modal.')) {
|
||||
$form->removeField('client_id');
|
||||
|
||||
$clientId = $this->getState('filter.client_id');
|
||||
$form->setFieldAttribute('menutype', 'clientid', $clientId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the client id for a menu
|
||||
*
|
||||
* @param string $menuType The menutype identifier for the menu
|
||||
* @param boolean $check Flag whether to perform check against ACL as well as existence
|
||||
*
|
||||
* @return integer
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getMenu($menuType, $check = false)
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
$query->select($db->quoteName('a') . '.*')
|
||||
->from($db->quoteName('#__menu_types', 'a'))
|
||||
->where($db->quoteName('menutype') . ' = :menuType')
|
||||
->bind(':menuType', $menuType);
|
||||
|
||||
$cMenu = $db->setQuery($query)->loadObject();
|
||||
|
||||
if ($check) {
|
||||
// Check if menu type exists.
|
||||
if (!$cMenu) {
|
||||
Log::add(Text::_('COM_MENUS_ERROR_MENUTYPE_NOT_FOUND'), Log::ERROR, 'jerror');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->getCurrentUser()->authorise('core.manage', 'com_menus.menu.' . $cMenu->id)) {
|
||||
// Check if menu type is valid against ACL.
|
||||
Log::add(Text::_('JERROR_ALERTNOAUTHOR'), Log::ERROR, 'jerror');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $cMenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of data items.
|
||||
*
|
||||
* @return mixed An array of data items on success, false on failure.
|
||||
*
|
||||
* @since 3.0.1
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
$store = $this->getStoreId();
|
||||
|
||||
if (!isset($this->cache[$store])) {
|
||||
$items = parent::getItems();
|
||||
$lang = Factory::getLanguage();
|
||||
$client = $this->state->get('filter.client_id');
|
||||
|
||||
if ($items) {
|
||||
foreach ($items as $item) {
|
||||
if ($extension = $item->componentname) {
|
||||
$lang->load("$extension.sys", JPATH_ADMINISTRATOR)
|
||||
|| $lang->load("$extension.sys", JPATH_ADMINISTRATOR . '/components/' . $extension);
|
||||
}
|
||||
|
||||
// Translate component name
|
||||
if ($client === 1) {
|
||||
$item->title = Text::_($item->title);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->cache[$store] = $items;
|
||||
}
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
}
|
||||
419
administrator/components/com_menus/src/Model/MenuModel.php
Normal file
419
administrator/components/com_menus/src/Model/MenuModel.php
Normal file
@ -0,0 +1,419 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_menus
|
||||
*
|
||||
* @copyright (C) 2006 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\Model;
|
||||
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\MVC\Model\AdminModel;
|
||||
use Joomla\CMS\Object\CMSObject;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\CMS\Table\Table;
|
||||
use Joomla\Registry\Registry;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Item Model for Menus.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenuModel extends AdminModel
|
||||
{
|
||||
/**
|
||||
* The prefix to use with controller messages.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.6
|
||||
*/
|
||||
protected $text_prefix = 'COM_MENUS_MENU';
|
||||
|
||||
/**
|
||||
* Model context string.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_context = 'com_menus.menu';
|
||||
|
||||
/**
|
||||
* Method to test whether a record can be deleted.
|
||||
*
|
||||
* @param object $record A record object.
|
||||
*
|
||||
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function canDelete($record)
|
||||
{
|
||||
return $this->getCurrentUser()->authorise('core.delete', 'com_menus.menu.' . (int) $record->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to test whether the state of a record can be edited.
|
||||
*
|
||||
* @param object $record A record object.
|
||||
*
|
||||
* @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function canEditState($record)
|
||||
{
|
||||
return $this->getCurrentUser()->authorise('core.edit.state', 'com_menus.menu.' . (int) $record->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Table object, always creating it
|
||||
*
|
||||
* @param string $type The table type to instantiate
|
||||
* @param string $prefix A prefix for the table class name. Optional.
|
||||
* @param array $config Configuration array for model. Optional.
|
||||
*
|
||||
* @return Table A database object
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function getTable($type = 'MenuType', $prefix = '\\Joomla\\CMS\\Table\\', $config = [])
|
||||
{
|
||||
return Table::getInstance($type, $prefix, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-populate the model state.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function populateState()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
|
||||
// Load the User state.
|
||||
$id = $app->getInput()->getInt('id');
|
||||
$this->setState('menu.id', $id);
|
||||
|
||||
// Load the parameters.
|
||||
$params = ComponentHelper::getParams('com_menus');
|
||||
$this->setState('params', $params);
|
||||
|
||||
// Load the clientId.
|
||||
$clientId = $app->getUserStateFromRequest('com_menus.menus.client_id', 'client_id', 0, 'int');
|
||||
$this->setState('client_id', $clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a menu item.
|
||||
*
|
||||
* @param integer $itemId The id of the menu item to get.
|
||||
*
|
||||
* @return mixed Menu item data object on success, false on failure.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function getItem($itemId = null)
|
||||
{
|
||||
$itemId = (!empty($itemId)) ? $itemId : (int) $this->getState('menu.id');
|
||||
|
||||
// Get a menu item row instance.
|
||||
$table = $this->getTable();
|
||||
|
||||
// Attempt to load the row.
|
||||
$return = $table->load($itemId);
|
||||
|
||||
// Check for a table object error.
|
||||
if ($return === false && $table->getError()) {
|
||||
$this->setError($table->getError());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$properties = $table->getProperties(1);
|
||||
$value = ArrayHelper::toObject($properties, CMSObject::class);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the menu item form.
|
||||
*
|
||||
* @param array $data Data for the form.
|
||||
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
|
||||
*
|
||||
* @return Form|boolean A Form object on success, false on failure
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function getForm($data = [], $loadData = true)
|
||||
{
|
||||
// Get the form.
|
||||
$form = $this->loadForm('com_menus.menu', 'menu', ['control' => 'jform', 'load_data' => $loadData]);
|
||||
|
||||
if (empty($form)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->getState('client_id', 0)) {
|
||||
$form->removeField('preset');
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the data that should be injected in the form.
|
||||
*
|
||||
* @return mixed The data for the form.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function loadFormData()
|
||||
{
|
||||
// Check the session for previously entered form data.
|
||||
$data = Factory::getApplication()->getUserState('com_menus.edit.menu.data', []);
|
||||
|
||||
if (empty($data)) {
|
||||
$data = $this->getItem();
|
||||
|
||||
if (empty($data->id)) {
|
||||
$data->client_id = $this->state->get('client_id', 0);
|
||||
}
|
||||
} else {
|
||||
unset($data['preset']);
|
||||
}
|
||||
|
||||
$this->preprocessData('com_menus.menu', $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to validate the form data.
|
||||
*
|
||||
* @param Form $form The form to validate against.
|
||||
* @param array $data The data to validate.
|
||||
* @param string $group The name of the field group to validate.
|
||||
*
|
||||
* @return array|boolean Array of filtered data if valid, false otherwise.
|
||||
*
|
||||
* @see \Joomla\CMS\Form\FormRule
|
||||
* @see \Joomla\CMS\Filter\InputFilter
|
||||
* @since 3.9.23
|
||||
*/
|
||||
public function validate($form, $data, $group = null)
|
||||
{
|
||||
if (!$this->getCurrentUser()->authorise('core.admin', 'com_menus')) {
|
||||
if (isset($data['rules'])) {
|
||||
unset($data['rules']);
|
||||
}
|
||||
}
|
||||
|
||||
return parent::validate($form, $data, $group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to save the form data.
|
||||
*
|
||||
* @param array $data The form data.
|
||||
*
|
||||
* @return boolean True on success.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function save($data)
|
||||
{
|
||||
$id = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('menu.id');
|
||||
$isNew = true;
|
||||
|
||||
// Get a row instance.
|
||||
$table = $this->getTable();
|
||||
|
||||
// Include the plugins for the save events.
|
||||
PluginHelper::importPlugin('content');
|
||||
|
||||
// Load the row if saving an existing item.
|
||||
if ($id > 0) {
|
||||
$isNew = false;
|
||||
$table->load($id);
|
||||
}
|
||||
|
||||
// Bind the data.
|
||||
if (!$table->bind($data)) {
|
||||
$this->setError($table->getError());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check the data.
|
||||
if (!$table->check()) {
|
||||
$this->setError($table->getError());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Trigger the before event.
|
||||
$result = Factory::getApplication()->triggerEvent('onContentBeforeSave', [$this->_context, &$table, $isNew, $data]);
|
||||
|
||||
// Store the data.
|
||||
if (\in_array(false, $result, true) || !$table->store()) {
|
||||
$this->setError($table->getError());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Trigger the after save event.
|
||||
Factory::getApplication()->triggerEvent('onContentAfterSave', [$this->_context, &$table, $isNew]);
|
||||
|
||||
$this->setState('menu.id', $table->id);
|
||||
|
||||
// Clean the cache
|
||||
$this->cleanCache();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to delete groups.
|
||||
*
|
||||
* @param array $itemIds An array of item ids.
|
||||
*
|
||||
* @return boolean Returns true on success, false on failure.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function delete(&$pks)
|
||||
{
|
||||
// Sanitize the ids.
|
||||
$itemIds = ArrayHelper::toInteger((array) $pks);
|
||||
|
||||
// Get a group row instance.
|
||||
$table = $this->getTable();
|
||||
|
||||
// Include the plugins for the delete events.
|
||||
PluginHelper::importPlugin('content');
|
||||
|
||||
// Iterate the items to delete each one.
|
||||
foreach ($itemIds as $itemId) {
|
||||
if ($table->load($itemId)) {
|
||||
// Trigger the before delete event.
|
||||
$result = Factory::getApplication()->triggerEvent('onContentBeforeDelete', [$this->_context, $table]);
|
||||
|
||||
if (\in_array(false, $result, true) || !$table->delete($itemId)) {
|
||||
$this->setError($table->getError());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Trigger the after delete event.
|
||||
Factory::getApplication()->triggerEvent('onContentAfterDelete', [$this->_context, $table]);
|
||||
|
||||
// @todo: Delete the menu associations - Menu items and Modules
|
||||
}
|
||||
}
|
||||
|
||||
// Clean the cache
|
||||
$this->cleanCache();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all mod_mainmenu modules and collates them by menutype
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function &getModules()
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
|
||||
$query = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('a.id'),
|
||||
$db->quoteName('a.title'),
|
||||
$db->quoteName('a.params'),
|
||||
$db->quoteName('a.position'),
|
||||
$db->quoteName('ag.title', 'access_title'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__modules', 'a'))
|
||||
->join('LEFT', $db->quoteName('#__viewlevels', 'ag'), $db->quoteName('ag.id') . ' = ' . $db->quoteName('a.access'))
|
||||
->where($db->quoteName('a.module') . ' = ' . $db->quote('mod_menu'));
|
||||
$db->setQuery($query);
|
||||
|
||||
$modules = $db->loadObjectList();
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($modules as &$module) {
|
||||
$params = new Registry($module->params);
|
||||
|
||||
$menuType = $params->get('menutype');
|
||||
|
||||
if (!isset($result[$menuType])) {
|
||||
$result[$menuType] = [];
|
||||
}
|
||||
|
||||
$result[$menuType][] = &$module;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the extension elements for the given items
|
||||
*
|
||||
* @param array $itemIds The item ids
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 4.2.0
|
||||
*/
|
||||
public function getExtensionElementsForMenuItems(array $itemIds): array
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
$query
|
||||
->select($db->quoteName('e.element'))
|
||||
->from($db->quoteName('#__extensions', 'e'))
|
||||
->join('INNER', $db->quoteName('#__menu', 'm'), $db->quoteName('m.component_id') . ' = ' . $db->quoteName('e.extension_id'))
|
||||
->whereIn($db->quoteName('m.id'), ArrayHelper::toInteger($itemIds));
|
||||
|
||||
return $db->setQuery($query)->loadColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom clean the cache
|
||||
*
|
||||
* @param string $group Cache group name.
|
||||
* @param integer $clientId No longer used, will be removed without replacement
|
||||
* @deprecated 4.3 will be removed in 6.0
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function cleanCache($group = null, $clientId = 0)
|
||||
{
|
||||
parent::cleanCache('com_menus');
|
||||
parent::cleanCache('com_modules');
|
||||
parent::cleanCache('mod_menu');
|
||||
}
|
||||
}
|
||||
312
administrator/components/com_menus/src/Model/MenusModel.php
Normal file
312
administrator/components/com_menus/src/Model/MenusModel.php
Normal file
@ -0,0 +1,312 @@
|
||||
<?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\Model;
|
||||
|
||||
use Joomla\CMS\Helper\ModuleHelper;
|
||||
use Joomla\CMS\Language\LanguageHelper;
|
||||
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\Database\QueryInterface;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu List Model for Menus.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenusModel extends ListModel
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $config An optional associative array of configuration settings.
|
||||
* @param ?MVCFactoryInterface $factory The factory.
|
||||
*
|
||||
* @see \Joomla\CMS\MVC\Model\BaseDatabaseModel
|
||||
* @since 3.2
|
||||
*/
|
||||
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
|
||||
{
|
||||
if (empty($config['filter_fields'])) {
|
||||
$config['filter_fields'] = [
|
||||
'id', 'a.id',
|
||||
'title', 'a.title',
|
||||
'menutype', 'a.menutype',
|
||||
'client_id', 'a.client_id',
|
||||
'ordering', 'a.ordering',
|
||||
];
|
||||
}
|
||||
|
||||
parent::__construct($config, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the getItems method to attach additional metrics to the list.
|
||||
*
|
||||
* @return mixed An array of data items on success, false on failure.
|
||||
*
|
||||
* @since 1.6.1
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
// Get a storage key.
|
||||
$store = $this->getStoreId('getItems');
|
||||
|
||||
// Try to load the data from internal storage.
|
||||
if (!empty($this->cache[$store])) {
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
// Load the list items.
|
||||
$items = parent::getItems();
|
||||
|
||||
// If empty or an error, just return.
|
||||
if (empty($items)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Getting the following metric by joins is WAY TOO SLOW.
|
||||
// Faster to do three queries for very large menu trees.
|
||||
|
||||
// Get the menu types of menus in the list.
|
||||
$db = $this->getDatabase();
|
||||
$menuTypes = array_column((array) $items, 'menutype');
|
||||
|
||||
$query = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('m.menutype'),
|
||||
'COUNT(DISTINCT ' . $db->quoteName('m.id') . ') AS ' . $db->quoteName('count_published'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__menu', 'm'))
|
||||
->where($db->quoteName('m.published') . ' = :published')
|
||||
->whereIn($db->quoteName('m.menutype'), $menuTypes, ParameterType::STRING)
|
||||
->group($db->quoteName('m.menutype'))
|
||||
->bind(':published', $published, ParameterType::INTEGER);
|
||||
|
||||
$db->setQuery($query);
|
||||
|
||||
// Get the published menu counts.
|
||||
try {
|
||||
$published = 1;
|
||||
$countPublished = $db->loadAssocList('menutype', 'count_published');
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->setError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the unpublished menu counts.
|
||||
try {
|
||||
$published = 0;
|
||||
$countUnpublished = $db->loadAssocList('menutype', 'count_published');
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->setError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the trashed menu counts.
|
||||
try {
|
||||
$published = -2;
|
||||
$countTrashed = $db->loadAssocList('menutype', 'count_published');
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->setError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Inject the values back into the array.
|
||||
foreach ($items as $item) {
|
||||
$item->count_published = $countPublished[$item->menutype] ?? 0;
|
||||
$item->count_unpublished = $countUnpublished[$item->menutype] ?? 0;
|
||||
$item->count_trashed = $countTrashed[$item->menutype] ?? 0;
|
||||
}
|
||||
|
||||
// Add the items to the internal cache.
|
||||
$this->cache[$store] = $items;
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to build an SQL query to load the list data.
|
||||
*
|
||||
* @return QueryInterface An SQL query
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getListQuery()
|
||||
{
|
||||
// Create a new query object.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
$clientId = (int) $this->getState('client_id');
|
||||
|
||||
// Select all fields from the table.
|
||||
$query->select(
|
||||
$this->getState(
|
||||
'list.select',
|
||||
[
|
||||
$db->quoteName('a.id'),
|
||||
$db->quoteName('a.menutype'),
|
||||
$db->quoteName('a.title'),
|
||||
$db->quoteName('a.description'),
|
||||
$db->quoteName('a.client_id'),
|
||||
$db->quoteName('a.ordering'),
|
||||
]
|
||||
)
|
||||
)
|
||||
->from($db->quoteName('#__menu_types', 'a'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('a.id') . ' > 0',
|
||||
$db->quoteName('a.client_id') . ' = :clientId',
|
||||
]
|
||||
)
|
||||
->bind(':clientId', $clientId, ParameterType::INTEGER);
|
||||
|
||||
// Filter by search in title or menutype
|
||||
if ($search = trim($this->getState('filter.search', ''))) {
|
||||
$search = '%' . str_replace(' ', '%', $search) . '%';
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.title') . ' LIKE :search1' ,
|
||||
$db->quoteName('a.menutype') . ' LIKE :search2',
|
||||
],
|
||||
'OR'
|
||||
)
|
||||
->bind([':search1', ':search2'], $search);
|
||||
}
|
||||
|
||||
// Add the list ordering clause.
|
||||
$query->order($db->escape($this->getState('list.ordering', 'a.id')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @param string $ordering An optional ordering field.
|
||||
* @param string $direction An optional direction (asc|desc).
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function populateState($ordering = 'a.ordering', $direction = 'asc')
|
||||
{
|
||||
$clientId = (int) $this->getUserStateFromRequest($this->context . '.client_id', 'client_id', 0, 'int');
|
||||
$this->setState('client_id', $clientId);
|
||||
|
||||
// List state information.
|
||||
parent::populateState($ordering, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the extension id of the core mod_menu module.
|
||||
*
|
||||
* @return integer
|
||||
*
|
||||
* @since 2.5
|
||||
*/
|
||||
public function getModMenuId()
|
||||
{
|
||||
$clientId = (int) $this->getState('client_id');
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select($db->quoteName('e.extension_id'))
|
||||
->from($db->quoteName('#__extensions', 'e'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('e.type') . ' = ' . $db->quote('module'),
|
||||
$db->quoteName('e.element') . ' = ' . $db->quote('mod_menu'),
|
||||
$db->quoteName('e.client_id') . ' = :clientId',
|
||||
]
|
||||
)
|
||||
->bind(':clientId', $clientId, ParameterType::INTEGER);
|
||||
$db->setQuery($query);
|
||||
|
||||
return $db->loadResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all mod_mainmenu modules and collates them by menutype
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function &getModules()
|
||||
{
|
||||
$model = $this->bootComponent('com_menus')
|
||||
->getMVCFactory()->createModel('Menu', 'Administrator', ['ignore_request' => true]);
|
||||
$result = $model->getModules();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the missing module languages.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 4.2.0
|
||||
*/
|
||||
public function getMissingModuleLanguages(): array
|
||||
{
|
||||
// Check custom administrator menu modules
|
||||
if (!ModuleHelper::isAdminMultilang()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$languages = LanguageHelper::getInstalledLanguages(1, true);
|
||||
$langCodes = [];
|
||||
|
||||
foreach ($languages as $language) {
|
||||
$languageName = $language->metadata['nativeName'] ?? $language->metadata['name'];
|
||||
|
||||
$langCodes[$language->metadata['tag']] = $languageName;
|
||||
}
|
||||
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
$query->select($db->quoteName('m.language'))
|
||||
->from($db->quoteName('#__modules', 'm'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('m.module') . ' = ' . $db->quote('mod_menu'),
|
||||
$db->quoteName('m.published') . ' = 1',
|
||||
$db->quoteName('m.client_id') . ' = 1',
|
||||
]
|
||||
)
|
||||
->group($db->quoteName('m.language'));
|
||||
|
||||
$mLanguages = $db->setQuery($query)->loadColumn();
|
||||
|
||||
// Check if we have a mod_menu module set to All languages or a mod_menu module for each admin language.
|
||||
if (!\in_array('*', $mLanguages) && \count($langMissing = array_diff(array_keys($langCodes), $mLanguages))) {
|
||||
return array_intersect_key($langCodes, array_flip($langMissing));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
591
administrator/components/com_menus/src/Model/MenutypesModel.php
Normal file
591
administrator/components/com_menus/src/Model/MenutypesModel.php
Normal file
@ -0,0 +1,591 @@
|
||||
<?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\Model;
|
||||
|
||||
use Joomla\CMS\Application\ApplicationHelper;
|
||||
use Joomla\CMS\Event\Menu\AfterGetMenuTypeOptionsEvent;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Filesystem\Folder;
|
||||
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
|
||||
use Joomla\CMS\Object\CMSObject;
|
||||
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Menu Item Types Model for Menus.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
class MenutypesModel extends BaseDatabaseModel
|
||||
{
|
||||
/**
|
||||
* A reverse lookup of the base link URL to Title
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rlu = [];
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* This method should only be called once per instantiation and is designed
|
||||
* to be called on the first call to the getState() method unless the model
|
||||
* configuration flag to ignore the request is set.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @note Calling getState in this method will result in recursion.
|
||||
* @since 3.0.1
|
||||
*/
|
||||
protected function populateState()
|
||||
{
|
||||
parent::populateState();
|
||||
|
||||
$clientId = Factory::getApplication()->getInput()->get('client_id', 0);
|
||||
|
||||
$this->state->set('client_id', $clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the reverse lookup of the base link URL to Title
|
||||
*
|
||||
* @return array Array of reverse lookup of the base link URL to Title
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function getReverseLookup()
|
||||
{
|
||||
if (empty($this->rlu)) {
|
||||
$this->getTypeOptions();
|
||||
}
|
||||
|
||||
return $this->rlu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the available menu item type options.
|
||||
*
|
||||
* @return array Array of groups with menu item types.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public function getTypeOptions()
|
||||
{
|
||||
$lang = Factory::getLanguage();
|
||||
$list = [];
|
||||
|
||||
// Get the list of components.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select(
|
||||
[
|
||||
$db->quoteName('name'),
|
||||
$db->quoteName('element', 'option'),
|
||||
]
|
||||
)
|
||||
->from($db->quoteName('#__extensions'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('type') . ' = ' . $db->quote('component'),
|
||||
$db->quoteName('enabled') . ' = 1',
|
||||
]
|
||||
)
|
||||
->order($db->quoteName('name') . ' ASC');
|
||||
$db->setQuery($query);
|
||||
$components = $db->loadObjectList();
|
||||
|
||||
foreach ($components as $component) {
|
||||
$options = $this->getTypeOptionsByComponent($component->option);
|
||||
|
||||
if ($options) {
|
||||
$list[$component->name] = $options;
|
||||
|
||||
// Create the reverse lookup for link-to-name.
|
||||
foreach ($options as $option) {
|
||||
if (isset($option->request)) {
|
||||
$this->addReverseLookupUrl($option);
|
||||
|
||||
if (isset($option->request['option'])) {
|
||||
$componentLanguageFolder = JPATH_ADMINISTRATOR . '/components/' . $option->request['option'];
|
||||
$lang->load($option->request['option'] . '.sys', JPATH_ADMINISTRATOR)
|
||||
|| $lang->load($option->request['option'] . '.sys', $componentLanguageFolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Allow a system plugin to insert dynamic menu types to the list shown in menus:
|
||||
return $this->getDispatcher()->dispatch('onAfterGetMenuTypeOptions', new AfterGetMenuTypeOptionsEvent('onAfterGetMenuTypeOptions', [
|
||||
'items' => &$list, // @todo: Remove reference in Joomla 6, see AfterGetMenuTypeOptionsEvent::__constructor()
|
||||
'subject' => $this,
|
||||
]))->getArgument('items', $list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to create the reverse lookup for link-to-name.
|
||||
* (can be used from onAfterGetMenuTypeOptions handlers)
|
||||
*
|
||||
* @param CMSObject $option Object with request array or string and title public variables
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
public function addReverseLookupUrl($option)
|
||||
{
|
||||
$this->rlu[MenusHelper::getLinkKey($option->request)] = $option->get('title');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get menu types by component.
|
||||
*
|
||||
* @param string $component Component URL option.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getTypeOptionsByComponent($component)
|
||||
{
|
||||
$options = [];
|
||||
$client = ApplicationHelper::getClientInfo($this->getState('client_id'));
|
||||
$mainXML = $client->path . '/components/' . $component . '/metadata.xml';
|
||||
|
||||
if (is_file($mainXML)) {
|
||||
$options = $this->getTypeOptionsFromXml($mainXML, $component);
|
||||
}
|
||||
|
||||
if (empty($options)) {
|
||||
$options = $this->getTypeOptionsFromMvc($component);
|
||||
}
|
||||
|
||||
if ($client->id == 1 && empty($options)) {
|
||||
$options = $this->getTypeOptionsFromManifest($component);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu types from an XML file
|
||||
*
|
||||
* @param string $file File path
|
||||
* @param string $component Component option as in URL
|
||||
*
|
||||
* @return array|boolean
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getTypeOptionsFromXml($file, $component)
|
||||
{
|
||||
$options = [];
|
||||
|
||||
// Attempt to load the xml file.
|
||||
if (!$xml = simplexml_load_file($file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Look for the first menu node off of the root node.
|
||||
if (!$menu = $xml->xpath('menu[1]')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$menu = $menu[0];
|
||||
|
||||
// If we have no options to parse, just add the base component to the list of options.
|
||||
if (!empty($menu['options']) && $menu['options'] == 'none') {
|
||||
// Create the menu option for the component.
|
||||
$o = new CMSObject();
|
||||
$o->title = (string) $menu['name'];
|
||||
$o->description = (string) $menu['msg'];
|
||||
$o->request = ['option' => $component];
|
||||
|
||||
$options[] = $o;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
// Look for the first options node off of the menu node.
|
||||
if (!$optionsNode = $menu->xpath('options[1]')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$optionsNode = $optionsNode[0];
|
||||
|
||||
// Make sure the options node has children.
|
||||
if (!$children = $optionsNode->children()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Process each child as an option.
|
||||
foreach ($children as $child) {
|
||||
if ($child->getName() == 'option') {
|
||||
// Create the menu option for the component.
|
||||
$o = new CMSObject();
|
||||
$o->title = (string) $child['name'];
|
||||
$o->description = (string) $child['msg'];
|
||||
$o->request = ['option' => $component, (string) $optionsNode['var'] => (string) $child['value']];
|
||||
|
||||
$options[] = $o;
|
||||
} elseif ($child->getName() == 'default') {
|
||||
// Create the menu option for the component.
|
||||
$o = new CMSObject();
|
||||
$o->title = (string) $child['name'];
|
||||
$o->description = (string) $child['msg'];
|
||||
$o->request = ['option' => $component];
|
||||
|
||||
$options[] = $o;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get menu types from MVC
|
||||
*
|
||||
* @param string $component Component option like in URLs
|
||||
*
|
||||
* @return array|boolean
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getTypeOptionsFromMvc($component)
|
||||
{
|
||||
$options = [];
|
||||
$views = [];
|
||||
|
||||
foreach ($this->getFolders($component) as $path) {
|
||||
if (!is_dir($path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$views = array_merge($views, Folder::folders($path, '.', false, true));
|
||||
}
|
||||
|
||||
foreach ($views as $viewPath) {
|
||||
$view = basename($viewPath);
|
||||
|
||||
// Ignore private views.
|
||||
if (strpos($view, '_') !== 0) {
|
||||
// Determine if a metadata file exists for the view.
|
||||
$file = $viewPath . '/metadata.xml';
|
||||
|
||||
if (is_file($file)) {
|
||||
// Attempt to load the xml file.
|
||||
if ($xml = simplexml_load_file($file)) {
|
||||
// Look for the first view node off of the root node.
|
||||
if ($menu = $xml->xpath('view[1]')) {
|
||||
$menu = $menu[0];
|
||||
|
||||
// If the view is hidden from the menu, discard it and move on to the next view.
|
||||
if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
|
||||
unset($xml);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Do we have an options node or should we process layouts?
|
||||
// Look for the first options node off of the menu node.
|
||||
if ($optionsNode = $menu->xpath('options[1]')) {
|
||||
$optionsNode = $optionsNode[0];
|
||||
|
||||
// Make sure the options node has children.
|
||||
if ($children = $optionsNode->children()) {
|
||||
// Process each child as an option.
|
||||
foreach ($children as $child) {
|
||||
if ($child->getName() == 'option') {
|
||||
// Create the menu option for the component.
|
||||
$o = new CMSObject();
|
||||
$o->title = (string) $child['name'];
|
||||
$o->description = (string) $child['msg'];
|
||||
$o->request = ['option' => $component, 'view' => $view, (string) $optionsNode['var'] => (string) $child['value']];
|
||||
|
||||
$options[] = $o;
|
||||
} elseif ($child->getName() == 'default') {
|
||||
// Create the menu option for the component.
|
||||
$o = new CMSObject();
|
||||
$o->title = (string) $child['name'];
|
||||
$o->description = (string) $child['msg'];
|
||||
$o->request = ['option' => $component, 'view' => $view];
|
||||
|
||||
$options[] = $o;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
|
||||
}
|
||||
}
|
||||
|
||||
unset($xml);
|
||||
}
|
||||
} else {
|
||||
$options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get menu types from Component manifest
|
||||
*
|
||||
* @param string $component Component option like in URLs
|
||||
*
|
||||
* @return array|boolean
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getTypeOptionsFromManifest($component)
|
||||
{
|
||||
// Load the component manifest
|
||||
$fileName = JPATH_ADMINISTRATOR . '/components/' . $component . '/' . str_replace('com_', '', $component) . '.xml';
|
||||
|
||||
if (!is_file($fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!($manifest = simplexml_load_file($fileName))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for a valid XML root tag.
|
||||
if ($manifest->getName() != 'extension') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$options = [];
|
||||
|
||||
// Start with the component root menu.
|
||||
$rootMenu = $manifest->administration->menu;
|
||||
|
||||
// If the menu item doesn't exist or is hidden do nothing.
|
||||
if (!$rootMenu || \in_array((string) $rootMenu['hidden'], ['true', 'hidden'])) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
// Create the root menu option.
|
||||
$ro = new \stdClass();
|
||||
$ro->title = (string) trim($rootMenu);
|
||||
$ro->description = '';
|
||||
$ro->request = ['option' => $component];
|
||||
|
||||
// Process submenu options.
|
||||
$submenu = $manifest->administration->submenu;
|
||||
|
||||
if (!$submenu) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
foreach ($submenu->menu as $child) {
|
||||
$attributes = $child->attributes();
|
||||
|
||||
$o = new \stdClass();
|
||||
$o->title = (string) trim($child);
|
||||
$o->description = '';
|
||||
|
||||
if ((string) $attributes->link) {
|
||||
parse_str((string) $attributes->link, $request);
|
||||
} else {
|
||||
$request = [];
|
||||
|
||||
$request['option'] = $component;
|
||||
$request['act'] = (string) $attributes->act;
|
||||
$request['task'] = (string) $attributes->task;
|
||||
$request['controller'] = (string) $attributes->controller;
|
||||
$request['view'] = (string) $attributes->view;
|
||||
$request['layout'] = (string) $attributes->layout;
|
||||
$request['sub'] = (string) $attributes->sub;
|
||||
}
|
||||
|
||||
$o->request = array_filter($request, function ($value) {
|
||||
if (\is_array($value)) {
|
||||
return !empty($value);
|
||||
}
|
||||
|
||||
return \strlen($value);
|
||||
});
|
||||
|
||||
$options[] = new CMSObject($o);
|
||||
|
||||
// Do not repeat the default view link (index.php?option=com_abc).
|
||||
if (\count($o->request) == 1) {
|
||||
$ro = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($ro) {
|
||||
$options[] = new CMSObject($ro);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu types from component layouts
|
||||
*
|
||||
* @param string $component Component option as in URLs
|
||||
* @param string $view Name of the view
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
protected function getTypeOptionsFromLayouts($component, $view)
|
||||
{
|
||||
$options = [];
|
||||
$layouts = [];
|
||||
$layoutNames = [];
|
||||
$lang = Factory::getLanguage();
|
||||
$client = ApplicationHelper::getClientInfo($this->getState('client_id'));
|
||||
|
||||
// Get the views for this component.
|
||||
foreach ($this->getFolders($component) as $folder) {
|
||||
$path = $folder . '/' . $view . '/tmpl';
|
||||
|
||||
if (!is_dir($path)) {
|
||||
$path = $folder . '/' . $view;
|
||||
}
|
||||
|
||||
if (!is_dir($path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$layouts = array_merge($layouts, Folder::files($path, '.xml$', false, true));
|
||||
}
|
||||
|
||||
// Build list of standard layout names
|
||||
foreach ($layouts as $layout) {
|
||||
// Ignore private layouts.
|
||||
if (strpos(basename($layout), '_') === false) {
|
||||
// Get the layout name.
|
||||
$layoutNames[] = basename($layout, '.xml');
|
||||
}
|
||||
}
|
||||
|
||||
// Get the template layouts
|
||||
// @todo: This should only search one template -- the current template for this item (default of specified)
|
||||
$folders = Folder::folders($client->path . '/templates', '', false, true);
|
||||
|
||||
// Array to hold association between template file names and templates
|
||||
$templateName = [];
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
if (is_dir($folder . '/html/' . $component . '/' . $view)) {
|
||||
$template = basename($folder);
|
||||
$lang->load('tpl_' . $template . '.sys', $client->path)
|
||||
|| $lang->load('tpl_' . $template . '.sys', $client->path . '/templates/' . $template);
|
||||
|
||||
$templateLayouts = Folder::files($folder . '/html/' . $component . '/' . $view, '.xml$', false, true);
|
||||
|
||||
foreach ($templateLayouts as $layout) {
|
||||
// Get the layout name.
|
||||
$templateLayoutName = basename($layout, '.xml');
|
||||
|
||||
// Add to the list only if it is not a standard layout
|
||||
if (array_search($templateLayoutName, $layoutNames) === false) {
|
||||
$layouts[] = $layout;
|
||||
|
||||
// Set template name array so we can get the right template for the layout
|
||||
$templateName[$layout] = basename($folder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the found layouts.
|
||||
foreach ($layouts as $layout) {
|
||||
// Ignore private layouts.
|
||||
if (strpos(basename($layout), '_') === false) {
|
||||
$file = $layout;
|
||||
|
||||
// Get the layout name.
|
||||
$layout = basename($layout, '.xml');
|
||||
|
||||
// Create the menu option for the layout.
|
||||
$o = new CMSObject();
|
||||
$o->title = ucfirst($layout);
|
||||
$o->description = '';
|
||||
$o->request = ['option' => $component, 'view' => $view];
|
||||
|
||||
// Only add the layout request argument if not the default layout.
|
||||
if ($layout != 'default') {
|
||||
// If the template is set, add in format template:layout so we save the template name
|
||||
$o->request['layout'] = isset($templateName[$file]) ? $templateName[$file] . ':' . $layout : $layout;
|
||||
}
|
||||
|
||||
// Load layout metadata if it exists.
|
||||
if (is_file($file)) {
|
||||
// Attempt to load the xml file.
|
||||
if ($xml = simplexml_load_file($file)) {
|
||||
// Look for the first view node off of the root node.
|
||||
if ($menu = $xml->xpath('layout[1]')) {
|
||||
$menu = $menu[0];
|
||||
|
||||
// If the view is hidden from the menu, discard it and move on to the next view.
|
||||
if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
|
||||
unset($xml);
|
||||
unset($o);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Populate the title and description if they exist.
|
||||
if (!empty($menu['title'])) {
|
||||
$o->title = trim((string) $menu['title']);
|
||||
}
|
||||
|
||||
if (!empty($menu->message[0])) {
|
||||
$o->description = trim((string) $menu->message[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the layout to the options array.
|
||||
$options[] = $o;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the folders with template files for the given component.
|
||||
*
|
||||
* @param string $component Component option as in URLs
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
private function getFolders($component)
|
||||
{
|
||||
$client = ApplicationHelper::getClientInfo($this->getState('client_id'));
|
||||
|
||||
if (!is_dir($client->path . '/components/' . $component)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$folders = Folder::folders($client->path . '/components/' . $component, '^view[s]?$', false, true);
|
||||
$folders = array_merge($folders, Folder::folders($client->path . '/components/' . $component, '^tmpl?$', false, true));
|
||||
|
||||
if (!$folders) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $folders;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user