menus.php
5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @package Joomla.Administrator
* @subpackage com_menus
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\Utilities\ArrayHelper;
/**
* Menu List Model for Menus.
*
* @since 1.6
*/
class MenusModelMenus extends JModelList
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id', 'a.id',
'title', 'a.title',
'menutype', 'a.menutype',
'client_id', 'a.client_id',
);
}
parent::__construct($config);
}
/**
* 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 emtpy or an error, just return.
if (empty($items))
{
return array();
}
// 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->getDbo();
$menuTypes = ArrayHelper::getColumn((array) $items, 'menutype');
// Quote the strings.
$menuTypes = implode(
',',
array_map(array($db, 'quote'), $menuTypes)
);
// Get the published menu counts.
$query = $db->getQuery(true)
->select('m.menutype, COUNT(DISTINCT m.id) AS count_published')
->from('#__menu AS m')
->where('m.published = 1')
->where('m.menutype IN (' . $menuTypes . ')')
->group('m.menutype');
$db->setQuery($query);
try
{
$countPublished = $db->loadAssocList('menutype', 'count_published');
}
catch (RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
// Get the unpublished menu counts.
$query->clear('where')
->where('m.published = 0')
->where('m.menutype IN (' . $menuTypes . ')');
$db->setQuery($query);
try
{
$countUnpublished = $db->loadAssocList('menutype', 'count_published');
}
catch (RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
// Get the trashed menu counts.
$query->clear('where')
->where('m.published = -2')
->where('m.menutype IN (' . $menuTypes . ')');
$db->setQuery($query);
try
{
$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 = isset($countPublished[$item->menutype]) ? $countPublished[$item->menutype] : 0;
$item->count_unpublished = isset($countUnpublished[$item->menutype]) ? $countUnpublished[$item->menutype] : 0;
$item->count_trashed = isset($countTrashed[$item->menutype]) ? $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 string An SQL query
*
* @since 1.6
*/
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select all fields from the table.
$query->select($this->getState('list.select', 'a.id, a.menutype, a.title, a.description, a.client_id'))
->from($db->quoteName('#__menu_types') . ' AS a')
->where('a.id > 0');
$query->where('a.client_id = ' . (int) $this->getState('client_id'));
// Filter by search in title or menutype
if ($search = trim($this->getState('filter.search')))
{
$search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
$query->where('(' . 'a.title LIKE ' . $search . ' OR a.menutype LIKE ' . $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.title', $direction = 'asc')
{
$search = $this->getUserStateFromRequest($this->context . '.search', 'filter_search');
$this->setState('filter.search', $search);
$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()
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('e.extension_id')
->from('#__extensions AS e')
->where('e.type = ' . $db->quote('module'))
->where('e.element = ' . $db->quote('mod_menu'))
->where('e.client_id = ' . (int) $this->getState('client_id'));
$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 = JModelLegacy::getInstance('Menu', 'MenusModel', array('ignore_request' => true));
$result = $model->getModules();
return $result;
}
}