HTML_Menu
[ class tree: HTML_Menu ] [ index: HTML_Menu ] [ all elements ]

Source for file DirectTreeRenderer.php

Documentation is available at DirectTreeRenderer.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexey Borzov <avb@php.net>                                 |
  17. // |          Uwe Mindrup <uwe@mindrup.de>                                |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: DirectTreeRenderer.php,v 1.3 2006/06/01 18:17:28 avb Exp $
  21. //
  22.  
  23. require_once 'HTML/Menu/Renderer.php';
  24.  
  25. /**
  26.  * The 'direct' renderer for 'tree' and 'sitemap' menu types where level is
  27.  * represented by tags nesting.
  28.  * 
  29.  * Thanks to Uwe Mindrup for the idea and initial implementation.
  30.  * 
  31.  * @version $Revision: 1.3 $
  32.  * @author  Alexey Borzov <avb@php.net>
  33.  * @author  Uwe Mindrup <uwe@mindrup.de>
  34.  * @access  public
  35.  * @package HTML_Menu
  36.  */
  37. {
  38.    /**
  39.     * Generated HTML for the menu
  40.     * @var string 
  41.     */
  42.     var $_html '';
  43.  
  44.    /**
  45.     * Generated HTML for the current branches
  46.     * @var string 
  47.     */
  48.     var $_levelHtml = array();
  49.  
  50.    /**
  51.     * Generated HTML for the current menu items
  52.     * @var string 
  53.     */
  54.     var $_itemHtml = array();
  55.  
  56.    /**
  57.     * The HTML that will wrap around a complete (sub)menu
  58.     * @see setLevelTemplate()
  59.     * @var array 
  60.     */
  61.     var $_levelTemplate = array('<ul>''</ul>');
  62.  
  63.    /**
  64.     * The HTML that will wrap around menu item
  65.     * @see setItemTemplate()
  66.     * @var array 
  67.     */
  68.     var $_itemTemplate = array('<li>''</li>');
  69.  
  70.    /**
  71.     * Templates for menu entries
  72.     * @see setEntryTemplate()
  73.     * @var array 
  74.     */
  75.     var $_entryTemplates = array(
  76.         HTML_MENU_ENTRY_INACTIVE    => '<a href="{url}">{title}</a>',
  77.         HTML_MENU_ENTRY_ACTIVE      => '<strong>{title}</strong>',
  78.         HTML_MENU_ENTRY_ACTIVEPATH  => '<a href="{url}"><em>{title}</em></a>'
  79.     );
  80.  
  81.  
  82.     function setMenuType($menuType)
  83.     {
  84.         if ('tree' == $menuType || 'sitemap' == $menuType{
  85.             $this->_menuType $menuType;
  86.         else {
  87.             require_once 'PEAR.php';
  88.             return PEAR::raiseError("HTML_Menu_DirectTreeRenderer: unable to render '$menuType' type menu");
  89.         }
  90.     }
  91.  
  92.  
  93.     function finishLevel($level)
  94.     {
  95.         isset($this->_levelHtml[$level]or $this->_levelHtml[$level'';
  96.         $this->_levelHtml[$level.= $this->_itemTemplate[0$this->_itemHtml[$level$this->_itemTemplate[1];
  97.         if (0 < $level{
  98.             $this->_itemHtml[$level - 1.= $this->_levelTemplate[0$this->_levelHtml[$level$this->_levelTemplate[1];
  99.         else {
  100.             $this->_html $this->_levelTemplate[0$this->_levelHtml[$level$this->_levelTemplate[1];
  101.         }
  102.         unset($this->_itemHtml[$level]$this->_levelHtml[$level]);
  103.     }
  104.  
  105.  
  106.     function renderEntry($node$level$type)
  107.     {
  108.         if (!empty($this->_itemHtml[$level])) {
  109.             isset($this->_levelHtml[$level]or $this->_levelHtml[$level'';
  110.             $this->_levelHtml[$level.= $this->_itemTemplate[0$this->_itemHtml[$level$this->_itemTemplate[1];
  111.         }
  112.         $keys $values = array();
  113.         foreach ($node as $k => $v{
  114.             if ('sub' != $k && is_scalar($v)) {
  115.                 $keys[]   '{' $k '}';
  116.                 $values[$v;
  117.             }
  118.         }
  119.         $this->_itemHtml[$levelstr_replace($keys$values$this->_entryTemplates[$type]);
  120.     }
  121.  
  122.  
  123.    /**
  124.     * returns the HTML generated for the menu
  125.     *
  126.     * @access public
  127.     * @return string 
  128.     */
  129.     function toHtml()
  130.     {
  131.         return $this->_html;
  132.     // end func toHtml
  133.  
  134.  
  135.    /**
  136.     * Sets the item template (HTML that wraps around entries)
  137.     * 
  138.     * @access public
  139.     * @param  string    this will be prepended to the entry HTML
  140.     * @param  string    this will be appended to the entry HTML
  141.     */
  142.     function setItemTemplate($prepend$append)
  143.     {
  144.         $this->_itemTemplate = array($prepend$append);
  145.     }
  146.  
  147.  
  148.    /**
  149.     * Sets the level template (HTML that wraps around the submenu)
  150.     * 
  151.     * @access public
  152.     * @param  string    this will be prepended to the submenu HTML
  153.     * @param  string    this will be appended to the submenu HTML
  154.     */
  155.     function setLevelTemplate($prepend$append)
  156.     {
  157.         $this->_levelTemplate = array($prepend$append);
  158.     }
  159.  
  160.  
  161.    /**
  162.     * Sets the template for menu entry.
  163.     * 
  164.     * The template should contain at least the {title} placeholder, can also contain
  165.     * {url} and {indent} placeholders, depending on entry type.
  166.     * 
  167.     * @access public
  168.     * @param  mixed     either type (one of HTML_MENU_ENTRY_* constants) or an array 'type' => 'template'
  169.     * @param  string    template for this entry type if $type is not an array
  170.     */
  171.     function setEntryTemplate($type$template = null)
  172.     {
  173.         if (is_array($type)) {
  174.             // array_merge() will not work here: the keys are numeric
  175.             foreach ($type as $typeId => $typeTemplate{
  176.                 if (isset($this->_entryTemplates[$typeId])) {
  177.                     $this->_entryTemplates[$typeId$typeTemplate;
  178.                 }
  179.             }
  180.         else {
  181.             $this->_entryTemplates[$type$template;
  182.         }
  183.     }
  184. }
  185. ?>

Documentation generated on Mon, 11 Mar 2019 14:46:20 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.