PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_menus/helpers/menus.php

https://github.com/joebushi/joomla
PHP | 205 lines | 123 code | 27 blank | 55 comment | 17 complexity | 9dc89b749d14692298e5907f5359100d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access
  8. defined('_JEXEC') or die;
  9. /**
  10. * Menus component helper.
  11. *
  12. * @package Joomla.Administrator
  13. * @subpackage com_menus
  14. * @since 1.6
  15. */
  16. class MenusHelper
  17. {
  18. /**
  19. * Defines the valid request variables for the reverse lookup.
  20. */
  21. protected static $_filter = array('option', 'view', 'layout');
  22. /**
  23. * Configure the Linkbar.
  24. *
  25. * @param string The name of the active view.
  26. */
  27. public static function addSubmenu($vName)
  28. {
  29. JSubMenuHelper::addEntry(
  30. JText::_('Menus_Submenu_Menus'),
  31. 'index.php?option=com_menus&view=menus',
  32. $vName == 'menus'
  33. );
  34. JSubMenuHelper::addEntry(
  35. JText::_('Menus_Submenu_Items'),
  36. 'index.php?option=com_menus&view=items',
  37. $vName == 'items'
  38. );
  39. }
  40. /**
  41. * Gets a standard form of a link for lookups.
  42. *
  43. * @param mixed A link string or array of request variables.
  44. *
  45. * @return mixed A link in standard option-view-layout form, or false if the supplied response is invalid.
  46. */
  47. public static function getLinkKey($request)
  48. {
  49. if (empty($request)) {
  50. return false;
  51. }
  52. // Check if the link is in the form of index.php?...
  53. if (is_string($request))
  54. {
  55. $args = array();
  56. if (strpos($request, 'index.php') === 0) {
  57. parse_str(parse_url(htmlspecialchars_decode($request), PHP_URL_QUERY), $args);
  58. }
  59. else {
  60. parse_str($request, $args);
  61. }
  62. $request = $args;
  63. }
  64. // Only take the option, view and layout parts.
  65. foreach ($request as $name => $value)
  66. {
  67. if (!in_array($name, self::$_filter))
  68. {
  69. // Remove the variables we want to ignore.
  70. unset($request[$name]);
  71. }
  72. }
  73. ksort($request);
  74. return 'index.php?'.http_build_query($request,'','&');
  75. }
  76. /**
  77. * Get the menu list for create a menu module
  78. *
  79. * @return array The menu array list
  80. * @since 1.6
  81. */
  82. public static function getMenuTypes()
  83. {
  84. $db = JFactory::getDbo();
  85. $db->setQuery('SELECT a.menutype FROM #__menu_types AS a');
  86. return $db->loadResultArray();
  87. }
  88. /**
  89. * Get a list of menu links for one or all menus.
  90. *
  91. * @param string An option menu to filter the list on, otherwise all menu links are returned as a grouped array.
  92. * @param int An optional parent ID to pivot results around.
  93. * @param int An optional mode. If parent ID is set and mode=2, the parent and children are excluded from the list.
  94. * @param array An optional array of states
  95. */
  96. public static function getMenuLinks($menuType = null, $parentId = 0, $mode = 0, $published=array())
  97. {
  98. $db = JFactory::getDbo();
  99. $query = new JQuery;
  100. $query->select('a.id AS value, a.title AS text, a.level, a.menutype, a.type');
  101. $query->from('#__menu AS a');
  102. $query->join('LEFT', '`#__menu` AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  103. // Filter by the type
  104. if ($menuType)
  105. {
  106. $query->where('(a.menutype = '.$db->quote($menuType).' OR a.parent_id = 0)');
  107. }
  108. if ($parentId)
  109. {
  110. if ($mode == 2)
  111. {
  112. // Prevent the parent and children from showing.
  113. $query->join('LEFT', '`#__menu` AS p ON p.id = '.(int) $parentId);
  114. $query->where('(a.lft <= p.lft OR a.rgt >= p.rgt)');
  115. }
  116. }
  117. if(!empty($published))
  118. {
  119. if (is_array($published)) $published = '(' . implode(',',$published) .')';
  120. $query->where('a.published IN ' . $published);
  121. }
  122. $query->group('a.id');
  123. $query->order('a.lft ASC');
  124. // Get the options.
  125. $db->setQuery($query);
  126. $links = $db->loadObjectList();
  127. // Check for a database error.
  128. if ($error = $db->getErrorMsg())
  129. {
  130. JError::raiseWarning(500, $error);
  131. return false;
  132. }
  133. // Pad the option text with spaces using depth level as a multiplier.
  134. foreach ($links as &$link)
  135. {
  136. $link->text = str_repeat('- ',$link->level).$link->text;
  137. }
  138. if (empty($menuType))
  139. {
  140. // If the menutype is empty, group the items by menutype.
  141. $query = new JQuery;
  142. $query->select('*');
  143. $query->from('#__menu_types');
  144. $query->where('menutype <> '.$db->quote(''));
  145. $query->order('title, menutype');
  146. $db->setQuery($query);
  147. $menuTypes = $db->loadObjectList();
  148. // Check for a database error.
  149. if ($error = $db->getErrorMsg())
  150. {
  151. JError::raiseWarning(500, $error);
  152. return false;
  153. }
  154. // Create a reverse lookup and aggregate the links.
  155. $rlu = array();
  156. foreach ($menuTypes as &$type)
  157. {
  158. $rlu[$type->menutype] = &$type;
  159. $type->links = array();
  160. }
  161. // Loop through the list of menu links.
  162. foreach ($links as &$link)
  163. {
  164. if (isset($rlu[$link->menutype]))
  165. {
  166. $rlu[$link->menutype]->links[] = &$link;
  167. // Cleanup garbage.
  168. unset($link->menutype);
  169. }
  170. }
  171. return $menuTypes;
  172. }
  173. else
  174. {
  175. return $links;
  176. }
  177. }
  178. }