PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mod_mainmenu/helper.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 392 lines | 264 code | 46 blank | 82 comment | 55 complexity | 20118062793e12dce26ef1c56e033760 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: helper.php 10112 2008-03-07 12:12:30Z eddieajau $
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // no direct access
  14. defined('_JEXEC') or die('Restricted access');
  15. jimport('joomla.base.tree');
  16. jimport('joomla.utilities.simplexml');
  17. /**
  18. * mod_mainmenu Helper class
  19. *
  20. * @static
  21. * @package Joomla
  22. * @subpackage Menus
  23. * @since 1.5
  24. */
  25. class modMainMenuHelper
  26. {
  27. function buildXML(&$params)
  28. {
  29. $menu = new JMenuTree($params);
  30. $items = &JSite::getMenu();
  31. // Get Menu Items
  32. $rows = $items->getItems('menutype', $params->get('menutype'));
  33. $maxdepth = $params->get('maxdepth',10);
  34. // Build Menu Tree root down (orphan proof - child might have lower id than parent)
  35. $user =& JFactory::getUser();
  36. $ids = array();
  37. $ids[0] = true;
  38. $last = null;
  39. $unresolved = array();
  40. // pop the first item until the array is empty if there is any item
  41. if ( is_array($rows)) {
  42. while (count($rows) && !is_null($row = array_shift($rows)))
  43. {
  44. if (array_key_exists($row->parent, $ids)) {
  45. $menu->addNode($row);
  46. // record loaded parents
  47. $ids[$row->id] = true;
  48. } else {
  49. // no parent yet so push item to back of list
  50. // SAM: But if the key isn't in the list and we dont _add_ this is infinite, so check the unresolved queue
  51. if(!array_key_exists($row->id, $unresolved) || $unresolved[$row->id] < $maxdepth) {
  52. array_push($rows, $row);
  53. // so let us do max $maxdepth passes
  54. // TODO: Put a time check in this loop in case we get too close to the PHP timeout
  55. if(!isset($unresolved[$row->id])) $unresolved[$row->id] = 1;
  56. else $unresolved[$row->id]++;
  57. }
  58. }
  59. }
  60. }
  61. return $menu->toXML();
  62. }
  63. function &getXML($type, &$params, $decorator)
  64. {
  65. static $xmls;
  66. if (!isset($xmls[$type])) {
  67. $cache =& JFactory::getCache('mod_mainmenu');
  68. $string = $cache->call(array('modMainMenuHelper', 'buildXML'), $params);
  69. $xmls[$type] = $string;
  70. }
  71. // Get document
  72. $xml = JFactory::getXMLParser('Simple');
  73. $xml->loadString($xmls[$type]);
  74. $doc = &$xml->document;
  75. $menu = &JSite::getMenu();
  76. $active = $menu->getActive();
  77. $start = $params->get('startLevel');
  78. $end = $params->get('endLevel');
  79. $sChild = $params->get('showAllChildren');
  80. $path = array();
  81. // Get subtree
  82. if ($start)
  83. {
  84. $found = false;
  85. $root = true;
  86. if(!isset($active)){
  87. $doc = false;
  88. }
  89. else{
  90. $path = $active->tree;
  91. for ($i=0,$n=count($path);$i<$n;$i++)
  92. {
  93. foreach ($doc->children() as $child)
  94. {
  95. if ($child->attributes('id') == $path[$i]) {
  96. $doc = &$child->ul[0];
  97. $root = false;
  98. break;
  99. }
  100. }
  101. if ($i == $start-1) {
  102. $found = true;
  103. break;
  104. }
  105. }
  106. if ((!is_a($doc, 'JSimpleXMLElement')) || (!$found) || ($root)) {
  107. $doc = false;
  108. }
  109. }
  110. }
  111. if ($doc && is_callable($decorator)) {
  112. $doc->map($decorator, array('end'=>$end, 'children'=>$sChild));
  113. }
  114. return $doc;
  115. }
  116. function render(&$params, $callback)
  117. {
  118. switch ( $params->get( 'menu_style', 'list' ) )
  119. {
  120. case 'list_flat' :
  121. // Include the legacy library file
  122. require_once(dirname(__FILE__).DS.'legacy.php');
  123. mosShowHFMenu($params, 1);
  124. break;
  125. case 'horiz_flat' :
  126. // Include the legacy library file
  127. require_once(dirname(__FILE__).DS.'legacy.php');
  128. mosShowHFMenu($params, 0);
  129. break;
  130. case 'vert_indent' :
  131. // Include the legacy library file
  132. require_once(dirname(__FILE__).DS.'legacy.php');
  133. mosShowVIMenu($params);
  134. break;
  135. default :
  136. // Include the new menu class
  137. $xml = modMainMenuHelper::getXML($params->get('menutype'), $params, $callback);
  138. if ($xml) {
  139. $class = $params->get('class_sfx');
  140. $xml->addAttribute('class', 'menu'.$class);
  141. if ($tagId = $params->get('tag_id')) {
  142. $xml->addAttribute('id', $tagId);
  143. }
  144. $result = JFilterOutput::ampReplace($xml->toString((bool)$params->get('show_whitespace')));
  145. $result = str_replace(array('<ul/>', '<ul />'), '', $result);
  146. echo $result;
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. /**
  153. * Main Menu Tree Class.
  154. *
  155. * @package Joomla
  156. * @subpackage Menus
  157. * @since 1.5
  158. */
  159. class JMenuTree extends JTree
  160. {
  161. /**
  162. * Node/Id Hash for quickly handling node additions to the tree.
  163. */
  164. var $_nodeHash = array();
  165. /**
  166. * Menu parameters
  167. */
  168. var $_params = null;
  169. /**
  170. * Menu parameters
  171. */
  172. var $_buffer = null;
  173. function __construct(&$params)
  174. {
  175. $this->_params =& $params;
  176. $this->_root = new JMenuNode(0, 'ROOT');
  177. $this->_nodeHash[0] =& $this->_root;
  178. $this->_current =& $this->_root;
  179. }
  180. function addNode($item)
  181. {
  182. // Get menu item data
  183. $data = $this->_getItemData($item);
  184. // Create the node and add it
  185. $node = new JMenuNode($item->id, $item->name, $item->access, $data);
  186. if (isset($item->mid)) {
  187. $nid = $item->mid;
  188. } else {
  189. $nid = $item->id;
  190. }
  191. $this->_nodeHash[$nid] =& $node;
  192. $this->_current =& $this->_nodeHash[$item->parent];
  193. if ($this->_current) {
  194. $this->addChild($node, true);
  195. } else {
  196. // sanity check
  197. JError::raiseError( 500, 'Orphan Error. Could not find parent for Item '.$item->id );
  198. }
  199. }
  200. function toXML()
  201. {
  202. // Initialize variables
  203. $this->_current =& $this->_root;
  204. // Recurse through children if they exist
  205. while ($this->_current->hasChildren())
  206. {
  207. $this->_buffer .= '<ul>';
  208. foreach ($this->_current->getChildren() as $child)
  209. {
  210. $this->_current = & $child;
  211. $this->_getLevelXML(0);
  212. }
  213. $this->_buffer .= '</ul>';
  214. }
  215. if($this->_buffer == '') { $this->_buffer = '<ul />'; }
  216. return $this->_buffer;
  217. }
  218. function _getLevelXML($depth)
  219. {
  220. $depth++;
  221. // Start the item
  222. $this->_buffer .= '<li access="'.$this->_current->access.'" level="'.$depth.'" id="'.$this->_current->id.'">';
  223. // Append item data
  224. $this->_buffer .= $this->_current->link;
  225. // Recurse through item's children if they exist
  226. while ($this->_current->hasChildren())
  227. {
  228. $this->_buffer .= '<ul>';
  229. foreach ($this->_current->getChildren() as $child)
  230. {
  231. $this->_current = & $child;
  232. $this->_getLevelXML($depth);
  233. }
  234. $this->_buffer .= '</ul>';
  235. }
  236. // Finish the item
  237. $this->_buffer .= '</li>';
  238. }
  239. function _getItemData($item)
  240. {
  241. $data = null;
  242. // Menu Link is a special type that is a link to another item
  243. if ($item->type == 'menulink')
  244. {
  245. $menu = &JSite::getMenu();
  246. if ($tmp = clone($menu->getItem($item->query['Itemid']))) {
  247. $tmp->name = '<span><![CDATA['.$item->name.']]></span>';
  248. $tmp->mid = $item->id;
  249. $tmp->parent = $item->parent;
  250. } else {
  251. return false;
  252. }
  253. } else {
  254. $tmp = clone($item);
  255. $tmp->name = '<span><![CDATA['.$item->name.']]></span>';
  256. }
  257. $iParams = new JParameter($tmp->params);
  258. if ($iParams->get('menu_image') && $iParams->get('menu_image') != -1) {
  259. $image = '<img src="'.JURI::base(true).'/images/stories/'.$iParams->get('menu_image').'" alt="" />';
  260. } else {
  261. $image = null;
  262. }
  263. switch ($tmp->type)
  264. {
  265. case 'separator' :
  266. return '<span class="separator">'.$image.$tmp->name.'</span>';
  267. break;
  268. case 'url' :
  269. if ((strpos($tmp->link, 'index.php?') !== false) && (strpos($tmp->link, 'Itemid=') === false)) {
  270. $tmp->url = $tmp->link.'&amp;Itemid='.$tmp->id;
  271. } else {
  272. $tmp->url = $tmp->link;
  273. }
  274. break;
  275. default :
  276. $router = JSite::getRouter();
  277. $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
  278. break;
  279. }
  280. // Print a link if it exists
  281. if ($tmp->url != null)
  282. {
  283. // Handle SSL links
  284. $iSecure = $iParams->def('secure', 0);
  285. if ($tmp->home == 1) {
  286. $tmp->url = JURI::base();
  287. } elseif (strcasecmp(substr($tmp->url, 0, 4), 'http') && (strpos($tmp->link, 'index.php?') !== false)) {
  288. $tmp->url = JRoute::_($tmp->url, true, $iSecure);
  289. } else {
  290. $tmp->url = str_replace('&', '&amp;', $tmp->url);
  291. }
  292. switch ($tmp->browserNav)
  293. {
  294. default:
  295. case 0:
  296. // _top
  297. $data = '<a href="'.$tmp->url.'">'.$image.$tmp->name.'</a>';
  298. break;
  299. case 1:
  300. // _blank
  301. $data = '<a href="'.$tmp->url.'" target="_blank">'.$image.$tmp->name.'</a>';
  302. break;
  303. case 2:
  304. // window.open
  305. $attribs = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'.$this->_params->get('window_open');
  306. // hrm...this is a bit dickey
  307. $link = str_replace('index.php', 'index2.php', $tmp->url);
  308. $data = '<a href="'.$link.'" onclick="window.open(this.href,\'targetWindow\',\''.$attribs.'\');return false;">'.$image.$tmp->name.'</a>';
  309. break;
  310. }
  311. } else {
  312. $data = '<a>'.$image.$tmp->name.'</a>';
  313. }
  314. return $data;
  315. }
  316. }
  317. /**
  318. * Main Menu Tree Node Class.
  319. *
  320. * @package Joomla
  321. * @subpackage Menus
  322. * @since 1.5
  323. */
  324. class JMenuNode extends JNode
  325. {
  326. /**
  327. * Node Title
  328. */
  329. var $title = null;
  330. /**
  331. * Node Link
  332. */
  333. var $link = null;
  334. /**
  335. * CSS Class for node
  336. */
  337. var $class = null;
  338. function __construct($id, $title, $access = null, $link = null, $class = null)
  339. {
  340. $this->id = $id;
  341. $this->title = $title;
  342. $this->access = $access;
  343. $this->link = $link;
  344. $this->class = $class;
  345. }
  346. }