[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/modules/mod_mainmenu/ -> helper.php (source)

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


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1