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

/modules/mod_menu/helper.php

https://github.com/cosmocommerce/joomla
PHP | 166 lines | 107 code | 25 blank | 34 comment | 23 complexity | 8a0a3763b29ceecbcc314986e9091cdb 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. * @package Joomla.Site
  11. * @subpackage mod_menu
  12. */
  13. class modMenuHelper
  14. {
  15. /**
  16. * Get a list of the menu items.
  17. */
  18. static function getList(&$params)
  19. {
  20. // Initialise variables.
  21. $list = array();
  22. $db = JFactory::getDbo();
  23. $user = JFactory::getUser();
  24. $menu = JSite::getMenu();
  25. // If no active menu, use default
  26. $active = ($menu->getActive()) ? $menu->getActive() : $menu->getDefault();
  27. $rlu = array();
  28. $start = (int) $params->get('startLevel');
  29. $end = (int) $params->get('endLevel');
  30. $showAll = $params->get('showAllChildren');
  31. $query = $db->getQuery(true);
  32. // Deal with start level.
  33. if ($start) {
  34. // Need a second query to backtrace.
  35. $query->select('m.id');
  36. $query->from('#__menu AS m');
  37. $query->join('LEFT', '#__menu AS p ON p.id = '.(int) $active->id);
  38. $query->where('m.level = '.$start);
  39. $query->where('m.lft <= p.lft');
  40. $query->where('m.rgt >= p.rgt');
  41. $db->setQuery($query);
  42. $startId = $db->loadResult();
  43. // Check for a database error.
  44. if ($db->getErrorNum()) {
  45. JError::raiseWarning($db->getErrorMsg());
  46. return $list;
  47. }
  48. if (empty($startId)) {
  49. return $list;
  50. }
  51. }
  52. // Get the menu items as a tree.
  53. $query->clear();
  54. $query->select('n.id, n.parent_id, n.title, n.alias, n.path, n.level, n.link, n.type, n.browserNav, n.params, n.home');
  55. $query->from('#__menu AS n');
  56. // Deal with start level.
  57. if ($start) {
  58. $query->join('INNER', '#__menu AS p ON p.id = '.(int) $startId);
  59. } else {
  60. $query->join('INNER', '#__menu AS p ON p.lft = 0');
  61. }
  62. // Deal with end level.
  63. if ($end) {
  64. $query->where('n.level <= '.$end);
  65. }
  66. $query->where('n.lft > p.lft');
  67. $query->where('n.lft < p.rgt');
  68. $query->order('n.lft');
  69. // Filter over the appropriate menu.
  70. $query->where('n.menutype = '.$db->quote($params->get('menutype', 'mainmenu')));
  71. // Filter over authorized access levels and publishing state.
  72. $query->where('n.published = 1');
  73. $query->where('n.access IN ('.implode(',', (array) $user->authorisedLevels()).')');
  74. // Get the list of menu items.
  75. $db->setQuery($query);
  76. $list = $db->loadObjectList();
  77. // Check for a database error.
  78. if ($db->getErrorNum()) {
  79. JError::raiseWarning($db->getErrorMsg());
  80. return array();
  81. }
  82. // Set some values to make nested HTML rendering easier.
  83. foreach ($list as $i => &$item) {
  84. $rlu[$item->id] = $i;
  85. // Compute tree step information.
  86. $item->deeper = (isset($list[$i+1]) && ($item->level < $list[$i+1]->level));
  87. $item->shallower = (isset($list[$i+1]) && ($item->level > $list[$i+1]->level));
  88. $item->level_diff = (isset($list[$i+1])) ? ($item->level - $list[$i+1]->level) : 0;
  89. $item->active = false;
  90. $item->params = new JObject(json_decode($item->params));
  91. switch ($item->type)
  92. {
  93. case 'separator':
  94. // No further action needed.
  95. continue;
  96. case 'url':
  97. if ((strpos($item->link, 'index.php?') === 0) && (strpos($item->link, 'Itemid=') === false)) {
  98. // If this is an internal Joomla link, ensure the Itemid is set.
  99. $item->link = $tmp->link.'&amp;Itemid='.$item->id;
  100. }
  101. break;
  102. case 'alias':
  103. // If this is an alias use the item id stored in the parameters to make the link.
  104. $item->link = 'index.php?Itemid='.$item->params->aliasoptions;
  105. break;
  106. default:
  107. $router = JSite::getRouter();
  108. if ($router->getMode() == JROUTER_MODE_SEF) {
  109. $item->link = 'index.php?Itemid='.$item->id;
  110. } else {
  111. $item->link .= '&Itemid='.$item->id;
  112. }
  113. break;
  114. }
  115. if ($item->home == 1)
  116. {
  117. // Correct the URL for the home page.
  118. $item->link = JURI::base();
  119. } elseif (strcasecmp(substr($item->link, 0, 4), 'http') && (strpos($item->link, 'index.php?') !== false)) {
  120. // This is an internal Joomla web site link.
  121. $item->link = JRoute::_($item->link, true, $item->params->get('secure'));
  122. } else {
  123. // Correct the & in the link.
  124. $item->link = str_replace('&', '&amp;', $item->link);
  125. }
  126. }
  127. // Set the active state of items from active to the tree root.
  128. $itemId = $active->id;
  129. $runaway = count($list);
  130. while ($itemId && $runaway--)
  131. {
  132. if (@$list[$rlu[$itemId]]) {
  133. $list[$rlu[$itemId]]->active = true;
  134. $itemId = $list[$rlu[$itemId]]->parent_id;
  135. } else {
  136. $itemId = 0;
  137. }
  138. }
  139. return $list;
  140. }
  141. }