PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/jplatform/joomla/html/html/menu.php

https://github.com/ot2sen/Molajo
PHP | 299 lines | 198 code | 40 blank | 61 comment | 25 complexity | f0271ad0bc89b9e98e22ab9259da42d2 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Utility class working with menu select lists
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class JHtmlMenu
  18. {
  19. /**
  20. * @var array Cached array of the menus.
  21. */
  22. protected static $menus = null;
  23. /**
  24. * @var array Cached array of the menus items.
  25. */
  26. protected static $items = null;
  27. /**
  28. * Get a list of the available menus.
  29. *
  30. * @return string
  31. * @since 11.1
  32. */
  33. public static function menus()
  34. {
  35. if (empty(self::$menus))
  36. {
  37. $db = JFactory::getDbo();
  38. $db->setQuery(
  39. 'SELECT menu_id As value, title As text' .
  40. ' FROM #__menus' .
  41. ' ORDER BY title'
  42. );
  43. self::$menus = $db->loadObjectList();
  44. }
  45. return self::$menus;
  46. }
  47. /**
  48. * Returns an array of menu items groups by menu.
  49. *
  50. * @param array An array of configuration options.
  51. *
  52. * @return array
  53. */
  54. public static function menuitems($config = array())
  55. {
  56. if (empty(self::$items))
  57. {
  58. $db = JFactory::getDbo();
  59. $db->setQuery(
  60. 'SELECT menu_id AS value, title AS text' .
  61. ' FROM #__menus' .
  62. ' ORDER BY title'
  63. );
  64. $menus = $db->loadObjectList();
  65. $query = $db->getQuery(true);
  66. $query->select('a.id AS value, a.title AS text, a.level, a.menu_id');
  67. $query->from('#__menu_items AS a');
  68. $query->where('a.parent_id > 0');
  69. $query->where('a.type <> '.$db->quote('url'));
  70. $query->where('a.client_id = 0');
  71. // Filter on the published state
  72. if (isset($config['published'])) {
  73. if (is_numeric($config['published'])) {
  74. $query->where('a.published = '.(int) $config['published']);
  75. } else if ($config['published'] === '') {
  76. $query->where('a.published IN (0,1)');
  77. }
  78. }
  79. $query->order('a.lft');
  80. $db->setQuery($query);
  81. $items = $db->loadObjectList();
  82. // Collate menu items based on menu_id
  83. $lookup = array();
  84. foreach ($items as &$item) {
  85. if (!isset($lookup[$item->menu_id])) {
  86. $lookup[$item->menu_id] = array();
  87. }
  88. $lookup[$item->menu_id][] = &$item;
  89. $item->text = str_repeat('- ',$item->level).$item->text;
  90. }
  91. self::$items = array();
  92. foreach ($menus as &$menu) {
  93. // Start group:
  94. self::$items[] = JHtml::_('select.optgroup', $menu->text);
  95. // Special "Add to this Menu" option:
  96. self::$items[] = JHtml::_('select.option', $menu->value.'.1', JText::_('MOLAJO_HTML_ADD_TO_THIS_MENU'));
  97. // Menu items:
  98. if (isset($lookup[$menu->value])) {
  99. foreach ($lookup[$menu->value] as &$item) {
  100. self::$items[] = JHtml::_('select.option', $menu->value.'.'.$item->value, $item->text);
  101. }
  102. }
  103. // Finish group:
  104. self::$items[] = JHtml::_('select.optgroup', $menu->text);
  105. }
  106. }
  107. return self::$items;
  108. }
  109. /**
  110. * Displays an HTML select list of menu items.
  111. *
  112. * @param string The name of the control.
  113. * @param string The value of the selected option.
  114. * @param string Attributes for the control.
  115. * @param array An array of options for the control.
  116. *
  117. * @return string
  118. */
  119. public static function menuitemlist($name, $selected = null, $attribs = null, $config = array())
  120. {
  121. static $count;
  122. $options = self::menuitems($config);
  123. return JHtml::_(
  124. 'select.genericlist',
  125. $options,
  126. $name,
  127. array(
  128. 'id' => isset($config['id']) ? $config['id'] : 'assetgroups_'.++$count,
  129. 'list.attr' => (is_null($attribs) ? 'class="inputbox" size="1"' : $attribs),
  130. 'list.select' => (int) $selected,
  131. 'list.translate' => false
  132. )
  133. );
  134. }
  135. /**
  136. * Build the select list for Menu Ordering
  137. */
  138. public static function ordering(&$row, $id)
  139. {
  140. $db = JFactory::getDbo();
  141. if ($id)
  142. {
  143. $query = 'SELECT ordering AS value, title AS text'
  144. . ' FROM #__menu_items'
  145. . ' WHERE menu_id = '.$db->Quote($row->menu_id)
  146. . ' AND parent_id = '.(int) $row->parent_id
  147. . ' AND published != -2'
  148. . ' ORDER BY ordering';
  149. $order = JHtml::_('list.genericordering', $query);
  150. $ordering = JHtml::_(
  151. 'select.genericlist',
  152. $order,
  153. 'ordering',
  154. array('list.attr' => 'class="inputbox" size="1"', 'list.select' => intval($row->ordering))
  155. );
  156. }
  157. else
  158. {
  159. $ordering = '<input type="hidden" name="ordering" value="'. $row->ordering .'" />'. JText::_('JGLOBAL_NEWITEMSLAST_DESC');
  160. }
  161. return $ordering;
  162. }
  163. /**
  164. * Build the multiple select list for Menu Links/Pages
  165. */
  166. public static function linkoptions($all=false, $unassigned=false)
  167. {
  168. $db = JFactory::getDbo();
  169. // get a list of the menu items
  170. $query = 'SELECT m.id, m.parent_id, m.title, m.menu_id'
  171. . ' FROM #__menu_items AS m'
  172. . ' WHERE m.published = 1'
  173. . ' ORDER BY m.menu_id, m.parent_id, m.ordering'
  174. ;
  175. $db->setQuery($query);
  176. $mitems = $db->loadObjectList();
  177. // Check for a database error.
  178. if ($db->getErrorNum()) {
  179. JError::raiseNotice(500, $db->getErrorMsg());
  180. }
  181. if (!$mitems) {
  182. $mitems = array();
  183. }
  184. $mitems_temp = $mitems;
  185. // Establish the hierarchy of the menu
  186. $children = array();
  187. // First pass - collect children
  188. foreach ($mitems as $v)
  189. {
  190. $id = $v->id;
  191. $pt = $v->parent_id;
  192. $list = @$children[$pt] ? $children[$pt] : array();
  193. array_push($list, $v);
  194. $children[$pt] = $list;
  195. }
  196. // Second pass - get an indent list of the items
  197. $list = JHtmlMenu::TreeRecurse(intval($mitems[0]->parent_id), '', array(), $children, 9999, 0, 0);
  198. // Code that adds menu name to Display of Page(s)
  199. $mitems_spacer = $mitems_temp[0]->menu_id;
  200. $mitems = array();
  201. if ($all | $unassigned) {
  202. $mitems[] = JHtml::_('select.option', '<OPTGROUP>', JText::_('JOPTION_MENUS'));
  203. if ($all) {
  204. $mitems[] = JHtml::_('select.option', 0, JText::_('JALL'));
  205. }
  206. if ($unassigned) {
  207. $mitems[] = JHtml::_('select.option', -1, JText::_('JOPTION_UNASSIGNED'));
  208. }
  209. $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
  210. }
  211. $lastMenuType = null;
  212. $tmpMenuType = null;
  213. foreach ($list as $list_a)
  214. {
  215. if ($list_a->menu_id != $lastMenuType)
  216. {
  217. if ($tmpMenuType) {
  218. $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
  219. }
  220. $mitems[] = JHtml::_('select.option', '<OPTGROUP>', $list_a->menu_id);
  221. $lastMenuType = $list_a->menu_id;
  222. $tmpMenuType = $list_a->menu_id;
  223. }
  224. $mitems[] = JHtml::_('select.option', $list_a->id, $list_a->title);
  225. }
  226. if ($lastMenuType !== null) {
  227. $mitems[] = JHtml::_('select.option', '</OPTGROUP>');
  228. }
  229. return $mitems;
  230. }
  231. public static function treerecurse($id, $indent, $list, &$children, $maxlevel=9999, $level=0, $type=1)
  232. {
  233. if (@$children[$id] && $level <= $maxlevel)
  234. {
  235. foreach ($children[$id] as $v)
  236. {
  237. $id = $v->id;
  238. if ($type) {
  239. $pre = '<sup>|_</sup>&#160;';
  240. $spacer = '.&#160;&#160;&#160;&#160;&#160;&#160;';
  241. } else {
  242. $pre = '- ';
  243. $spacer = '&#160;&#160;';
  244. }
  245. if ($v->parent_id == 0) {
  246. $txt = $v->title;
  247. } else {
  248. $txt = $pre . $v->title;
  249. }
  250. $pt = $v->parent_id;
  251. $list[$id] = $v;
  252. $list[$id]->treename = "$indent$txt";
  253. $list[$id]->children = count(@$children[$id]);
  254. $list = JHtmlMenu::TreeRecurse($id, $indent . $spacer, $list, $children, $maxlevel, $level+1, $type);
  255. }
  256. }
  257. return $list;
  258. }
  259. }