PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/mod_menu/helper.php

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