home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Menu / DirectTreeRenderer.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.9 KB  |  194 lines

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