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