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

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

https://github.com/cosmocommerce/joomla
PHP | 191 lines | 110 code | 26 blank | 55 comment | 18 complexity | 9d6cd9d80db5601a04d1648e91386718 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  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 = $db->getQuery(true);
  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. $query->where('(a.menutype = '.$db->quote($menuType).' OR a.parent_id = 0)');
  106. }
  107. if ($parentId) {
  108. if ($mode == 2) {
  109. // Prevent the parent and children from showing.
  110. $query->join('LEFT', '`#__menu` AS p ON p.id = '.(int) $parentId);
  111. $query->where('(a.lft <= p.lft OR a.rgt >= p.rgt)');
  112. }
  113. }
  114. if (!empty($published)) {
  115. if (is_array($published)) $published = '(' . implode(',',$published) .')';
  116. $query->where('a.published IN ' . $published);
  117. }
  118. $query->group('a.id');
  119. $query->order('a.lft ASC');
  120. // Get the options.
  121. $db->setQuery($query);
  122. $links = $db->loadObjectList();
  123. // Check for a database error.
  124. if ($error = $db->getErrorMsg()) {
  125. JError::raiseWarning(500, $error);
  126. return false;
  127. }
  128. // Pad the option text with spaces using depth level as a multiplier.
  129. foreach ($links as &$link) {
  130. $link->text = str_repeat('- ',$link->level).$link->text;
  131. }
  132. if (empty($menuType)) {
  133. // If the menutype is empty, group the items by menutype.
  134. $query->clear();
  135. $query->select('*');
  136. $query->from('#__menu_types');
  137. $query->where('menutype <> '.$db->quote(''));
  138. $query->order('title, menutype');
  139. $db->setQuery($query);
  140. $menuTypes = $db->loadObjectList();
  141. // Check for a database error.
  142. if ($error = $db->getErrorMsg()) {
  143. JError::raiseWarning(500, $error);
  144. return false;
  145. }
  146. // Create a reverse lookup and aggregate the links.
  147. $rlu = array();
  148. foreach ($menuTypes as &$type) {
  149. $rlu[$type->menutype] = &$type;
  150. $type->links = array();
  151. }
  152. // Loop through the list of menu links.
  153. foreach ($links as &$link) {
  154. if (isset($rlu[$link->menutype])) {
  155. $rlu[$link->menutype]->links[] = &$link;
  156. // Cleanup garbage.
  157. unset($link->menutype);
  158. }
  159. }
  160. return $menuTypes;
  161. } else {
  162. return $links;
  163. }
  164. }
  165. }