1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 abstract class
18 {
19 20 21 22 23 24
25 protected static = array();
26
27 28 29 30 31 32
33 protected static $items = array();
34
35 36 37 38 39 40 41 42 43
44 public static function ($clientId = 0)
45 {
46 $key = serialize($clientId);
47
48 if (!isset(static::$menus[$key]))
49 {
50 $db = JFactory::getDbo();
51
52 $query = $db->getQuery(true)
53 ->select($db->qn(array('id', 'menutype', 'title', 'client_id'), array('id', 'value', 'text', 'client_id')))
54 ->from($db->quoteName('#__menu_types'))
55 ->order('client_id, title');
56
57 if (isset($clientId))
58 {
59 $query->where('client_id = ' . (int) $clientId);
60 }
61
62 static::$menus[$key] = $db->setQuery($query)->loadObjectList();
63 }
64
65 return static::$menus[$key];
66 }
67
68 69 70 71 72 73 74 75 76
77 public static function ($config = array())
78 {
79 $key = serialize($config);
80
81 if (empty(static::$items[$key]))
82 {
83
84 $clientId = array_key_exists('clientid', $config) ? $config['clientid'] : 0;
85 $menus = static::menus($clientId);
86
87 $db = JFactory::getDbo();
88 $query = $db->getQuery(true)
89 ->select('a.id AS value, a.title AS text, a.level, a.menutype, a.client_id')
90 ->from('#__menu AS a')
91 ->where('a.parent_id > 0');
92
93
94 if (isset($clientId))
95 {
96 $query->where('a.client_id = ' . (int) $clientId);
97 }
98
99
100 if (isset($config['published']))
101 {
102 if (is_numeric($config['published']))
103 {
104 $query->where('a.published = ' . (int) $config['published']);
105 }
106 elseif ($config['published'] === '')
107 {
108 $query->where('a.published IN (0,1)');
109 }
110 }
111
112 $query->order('a.lft');
113
114 $db->setQuery($query);
115 $items = $db->loadObjectList();
116
117
118 $lookup = array();
119
120 foreach ($items as &$item)
121 {
122 if (!isset($lookup[$item->menutype]))
123 {
124 $lookup[$item->menutype] = array();
125 }
126
127 $lookup[$item->menutype][] = &$item;
128
129 $item->text = str_repeat('- ', $item->level) . $item->text;
130 }
131
132 static::$items[$key] = array();
133
134 $user = JFactory::getUser();
135
136 $aclcheck = !empty($config['checkacl']) ? (int) $config['checkacl'] : 0;
137
138 foreach ($menus as &$menu)
139 {
140 if ($aclcheck)
141 {
142 $action = $aclcheck == $menu->id ? 'edit' : 'create';
143
144 if (!$user->authorise('core.' . $action, 'com_menus.menu.' . $menu->id))
145 {
146 continue;
147 }
148 }
149
150
151 static::$items[$key][] = JHtml::_('select.optgroup', $menu->text);
152
153
154 static::$items[$key][] = JHtml::_('select.option', $menu->value . '.1', JText::_('JLIB_HTML_ADD_TO_THIS_MENU'));
155
156
157 if (isset($lookup[$menu->value]))
158 {
159 foreach ($lookup[$menu->value] as &$item)
160 {
161 static::$items[$key][] = JHtml::_('select.option', $menu->value . '.' . $item->value, $item->text);
162 }
163 }
164
165
166 static::$items[$key][] = JHtml::_('select.optgroup', $menu->text);
167 }
168 }
169
170 return static::$items[$key];
171 }
172
173 174 175 176 177 178 179 180 181 182 183 184
185 public static function ($name, $selected = null, $attribs = null, $config = array())
186 {
187 static $count;
188
189 $options = static::menuItems($config);
190
191 return JHtml::_(
192 'select.genericlist', $options, $name,
193 array(
194 'id' => isset($config['id']) ? $config['id'] : 'assetgroups_' . (++$count),
195 'list.attr' => $attribs === null ? 'class="inputbox" size="1"' : $attribs,
196 'list.select' => (int) $selected,
197 'list.translate' => false,
198 )
199 );
200 }
201
202 203 204 205 206 207 208 209 210 211
212 public static function ordering(&$row, $id)
213 {
214 if ($id)
215 {
216 $db = JFactory::getDbo();
217 $query = $db->getQuery(true)
218 ->select('ordering AS value, title AS text')
219 ->from($db->quoteName('#__menu'))
220 ->where($db->quoteName('menutype') . ' = ' . $db->quote($row->menutype))
221 ->where($db->quoteName('parent_id') . ' = ' . (int) $row->parent_id)
222 ->where($db->quoteName('published') . ' != -2')
223 ->order('ordering');
224 $order = JHtml::_('list.genericordering', $query);
225 $ordering = JHtml::_(
226 'select.genericlist', $order, 'ordering',
227 array('list.attr' => 'class="inputbox" size="1"', 'list.select' => (int) $row->ordering)
228 );
229 }
230 else
231 {
232 $ordering = '<input type="hidden" name="ordering" value="' . $row->ordering . '" />' . JText::_('JGLOBAL_NEWITEMSLAST_DESC');
233 }
234
235 return $ordering;
236 }
237
238 239 240 241 242 243 244 245 246 247 248
249 public static function linkOptions($all = false, $unassigned = false, $clientId = 0)
250 {
251 $db = JFactory::getDbo();
252
253
254 $query = $db->getQuery(true)
255 ->select('m.id, m.parent_id, m.title, m.menutype, m.client_id')
256 ->from($db->quoteName('#__menu') . ' AS m')
257 ->where($db->quoteName('m.published') . ' = 1')
258 ->order('m.client_id, m.menutype, m.parent_id');
259
260 if (isset($clientId))
261 {
262 $query->where('m.client_id = ' . (int) $clientId);
263 }
264
265 $db->setQuery($query);
266
267 $mitems = $db->loadObjectList();
268
269 if (!$mitems)
270 {
271 $mitems = array();
272 }
273
274
275 $children = array();
276
277
278 foreach ($mitems as $v)
279 {
280 $pt = $v->parent_id;
281 $list = @$children[$pt] ? $children[$pt] : array();
282 $list[] = $v;
283 $children[$pt] = $list;
284 }
285
286
287 $list = static::treerecurse((int) $mitems[0]->parent_id, '', array(), $children, 9999, 0, 0);
288
289
290 $mitems = array();
291
292 if ($all | $unassigned)
293 {
294 $mitems[] = JHtml::_('select.option', '<OPTGROUP>', JText::_('JOPTION_MENUS'));
295
296 if ($all)
297 {
298 $mitems[] = JHtml::_('select.option', 0, JText::_('JALL'));
299 }
300
301 if ($unassigned)
302 {
303 $mitems[] = JHtml::_('select.option', -1, JText::_('JOPTION_UNASSIGNED'));
304 }
305
306 $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
307 }
308
309 $lastMenuType = null;
310 $tmpMenuType = null;
311
312 foreach ($list as $list_a)
313 {
314 if ($list_a->menutype != $lastMenuType)
315 {
316 if ($tmpMenuType)
317 {
318 $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
319 }
320
321 $mitems[] = JHtml::_('select.option', '<OPTGROUP>', $list_a->menutype);
322 $lastMenuType = $list_a->menutype;
323 $tmpMenuType = $list_a->menutype;
324 }
325
326 $mitems[] = JHtml::_('select.option', $list_a->id, $list_a->title);
327 }
328
329 if ($lastMenuType !== null)
330 {
331 $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
332 }
333
334 return $mitems;
335 }
336
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
352 public static function treerecurse($id, $indent, $list, &$children, $maxlevel = 9999, $level = 0, $type = 1)
353 {
354 if ($level <= $maxlevel && @$children[$id])
355 {
356 if ($type)
357 {
358 $pre = '<sup>|_</sup> ';
359 $spacer = '.      ';
360 }
361 else
362 {
363 $pre = '- ';
364 $spacer = '  ';
365 }
366
367 foreach ($children[$id] as $v)
368 {
369 $id = $v->id;
370
371 if ($v->parent_id == 0)
372 {
373 $txt = $v->title;
374 }
375 else
376 {
377 $txt = $pre . $v->title;
378 }
379
380 $list[$id] = $v;
381 $list[$id]->treename = $indent . $txt;
382 $list[$id]->children = count(@$children[$id]);
383 $list = static::treerecurse($id, $indent . $spacer, $list, $children, $maxlevel, $level + 1, $type);
384 }
385 }
386
387 return $list;
388 }
389 }
390