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

/libraries/gantry/facets/menu/gantrymenu.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 251 lines | 187 code | 33 blank | 31 comment | 59 complexity | d4b3339e7277fe4f75eb108cfd0cefe7 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @version 3.2.22 August 3, 2012
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  9. *
  10. */
  11. // no direct access
  12. defined('GANTRY_VERSION') or die('Restricted access');
  13. // Include the syndicate functions only once
  14. gantry_import('facets.menu.gantrymenutree');
  15. class GantryMenu {
  16. var $_menudata = null;
  17. var $_formatter = null;
  18. var $_layout_path = null;
  19. var $__cacheables = array(
  20. '_menudata'
  21. );
  22. function __sleep() {
  23. return $this->__cacheables;
  24. }
  25. // static function to get an instance of the GantryMenu
  26. function &getInstance(GantryRegistry $params) {
  27. global $gantry;
  28. $conf = JFactory::getConfig();
  29. if ($conf->getValue('config.caching') && $params->get("module_cache", 0)) {
  30. $user =& JFactory::getUser();
  31. $cache =& JFactory::getCache('Gantry');
  32. $cache->setCaching(true);
  33. $cache->setLifeTime($gantry->get("cache-time", $conf->getValue('config.cachetime') * 60));
  34. $args = array(&$params);
  35. $checksum = md5($args[0]->_raw);
  36. $gantrymenu = $cache->get(array('GantryMenu', '_getInstance'), $args, 'GantryMenu-' . $user->get('aid', 0) . '-' . $checksum);
  37. }
  38. else {
  39. $gantrymenu = GantryMenu::_getInstance($params);
  40. }
  41. return $gantrymenu;
  42. }
  43. function &_getInstance($params) {
  44. $gantrymenu = new GantryMenu($params);
  45. return $gantrymenu;
  46. }
  47. function GantryMenu($params) {
  48. if (empty($params)) {
  49. $params = new GantryRegistry();
  50. }
  51. $params->def('menutype', 'mainmenu');
  52. $params->def('class_sfx', '');
  53. $params->def('menu_images', 0);
  54. // Added in 1.5
  55. $params->def('startLevel', 0);
  56. $params->def('endLevel', 0);
  57. $params->def('showAllChildren', 0);
  58. $this->_menudata = GantryMenu::_getMenuData($params);
  59. }
  60. function render($params) {
  61. $theme_name = $params->get('theme', 'basic');
  62. $this->_loadTheme($theme_name);
  63. // Run the basic formatter
  64. GantryMenu::_applyBasicFormatting($this->_menudata);
  65. if (!empty($this->_formatter)){
  66. $this->_formatter->format_tree($this->_menudata);
  67. }
  68. // format the menu data $menu is passed to the layout
  69. $menu = &$this->_menudata;
  70. $menurender = "Unable to render menu missing layout.php for theme " . $theme_name;
  71. if (!empty($this->_layout_path) && file_exists($this->_layout_path) && is_readable($this->_layout_path)){
  72. ob_start();
  73. require($this->_layout_path);
  74. $menurender = ob_get_contents();
  75. ob_end_clean();
  76. }
  77. return $menurender;
  78. }
  79. function _getMenuData(&$params) {
  80. $menu = new GantryMenuTree();
  81. $menu->_params = &$params;
  82. $items = &JSite::getMenu();
  83. // Get Menu Items
  84. $rows = $items->getItems('menutype', $params->get('menutype'));
  85. $maxdepth = $menu->getParameter('endLevel', 10);
  86. // Build Menu Tree root down (orphan proof - child might have lower id than parent)
  87. $user =& JFactory::getUser();
  88. $ids = array();
  89. $ids[0] = true;
  90. $last = null;
  91. $unresolved = array();
  92. // pop the first item until the array is empty if there is any item
  93. if (is_array($rows)) {
  94. while (count($rows) && !is_null($row = array_shift($rows)))
  95. {
  96. $row->ionly = $params->get('menu_images_link');
  97. if (!$menu->addNode($params, $row)) {
  98. if (!array_key_exists($row->id, $unresolved) || $unresolved[$row->id] < $maxdepth) {
  99. array_push($rows, $row);
  100. if (!isset($unresolved[$row->id])) $unresolved[$row->id] = 1;
  101. else $unresolved[$row->id]++;
  102. }
  103. }
  104. }
  105. }
  106. return $menu;
  107. }
  108. function _loadTheme($theme_name) {
  109. global $gantry;
  110. // Load up the theme info if there is not one already
  111. if (empty($this->_formatter)) {
  112. $theme_parent_paths = array(
  113. $gantry->templatePath . '/facets/menu/themes',
  114. $gantry->gantryPath . '/facets/menu/themes'
  115. );
  116. foreach ($theme_parent_paths as $theme_parent_path) {
  117. if (file_exists($theme_parent_path) && is_dir($theme_parent_path)) {
  118. $d = dir($theme_parent_path);
  119. while (false !== ($entry = $d->read())) {
  120. if ($entry != '.' && $entry != '..') {
  121. if ($entry == $theme_name && is_dir($theme_parent_path . DS . $entry)) {
  122. $formatter_file = $theme_parent_path . DS . $entry . DS . 'formatter.php';
  123. $layout_file = $theme_parent_path . DS . $entry . DS . 'layout.php';
  124. $formatter_class = 'GantryMenuFormatter' . ucfirst($entry);
  125. if (!file_exists($formatter_file)) {
  126. return false;
  127. }
  128. // Load the Formatter File
  129. require_once($formatter_file);
  130. if (!class_exists($formatter_class)) {
  131. return false;
  132. }
  133. $this->_formatter = new $formatter_class();
  134. if (file_exists($layout_file)) {
  135. $this->_layout_path = $layout_file;
  136. }
  137. break(2); // exit top level foreach
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Perform the basic common formatting to all menu nodes
  147. */
  148. function _applyBasicFormatting(&$menu) {
  149. //set the active tree branch
  150. $joomlamenu = &JSite::getMenu();
  151. $active = $joomlamenu->getActive();
  152. if (isset($active) && isset($active->tree) && count($active->tree)) {
  153. reset($active->tree);
  154. while (list($key, $value) = each($active->tree)) {
  155. $active_node =& $active->tree[$key];
  156. $active_child =& $menu->findChild($active_node);
  157. if ($active_child !== false) {
  158. $active_child->addListItemClass('active');
  159. }
  160. }
  161. }
  162. // set the current node
  163. if (isset($active)) {
  164. $current_child =& $menu->findChild($active->id);
  165. if ($current_child !== false && !$current_child->menualias) {
  166. $current_child->css_id = 'current';
  167. }
  168. }
  169. // Limit the levels of the tree is called for By limitLevels
  170. if ($menu->getParameter('limit_levels')) {
  171. $start = $menu->getParameter('startLevel');
  172. $end = $menu->getParameter('endLevel');
  173. //Limit to the active path if the start is more the level 0
  174. if ($start > 0) {
  175. $found = false;
  176. // get active path and find the start level that matches
  177. if (isset($active) && isset($active->tree) && count($active->tree)) {
  178. reset($active->tree);
  179. while (list($key, $value) = each($active->tree)) {
  180. $active_child = $menu->findChild($active->tree[$key]);
  181. if ($active_child != null && $active_child !== false) {
  182. if ($active_child->level == $start - 1) {
  183. $menu->resetTop($active_child->id);
  184. $found = true;
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. if (!$found) {
  191. $menu->_children = array();
  192. }
  193. }
  194. //remove lower then the defined end level
  195. $menu->removeLevel($end);
  196. }
  197. // Remove the child nodes that were not needed to display unless showAllChildren is set
  198. $showAllChildren = $menu->getParameter('showAllChildren');
  199. if (!$showAllChildren) {
  200. if ($menu->hasChildren()) {
  201. reset($menu->_children);
  202. while (list($key, $value) = each($menu->_children)) {
  203. $toplevel =& $menu->_children[$key];
  204. if (isset($active) && isset($active->tree) && in_array($toplevel->id, $active->tree) !== false) {
  205. $last_active =& $menu->findChild($active->tree[count($active->tree) - 1]);
  206. if ($last_active !== false) {
  207. $toplevel->removeIfNotInTree($active->tree, $last_active->id);
  208. //$toplevel->removeLevel($last_active->level+1);
  209. }
  210. }
  211. else {
  212. $toplevel->removeLevel($toplevel->level);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }