PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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