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

Source for file TreeMenu.php

Documentation is available at TreeMenu.php

  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Copyright (c) 2002-2005, Richard Heyes, Harald Radi                   |
  4. // | All rights reserved.                                                  |
  5. // |                                                                       |
  6. // | Redistribution and use in source and binary forms, with or without    |
  7. // | modification, are permitted provided that the following conditions    |
  8. // | are met:                                                              |
  9. // |                                                                       |
  10. // | o Redistributions of source code must retain the above copyright      |
  11. // |   notice, this list of conditions and the following disclaimer.       |
  12. // | o Redistributions in binary form must reproduce the above copyright   |
  13. // |   notice, this list of conditions and the following disclaimer in the |
  14. // |   documentation and/or other materials provided with the distribution.|
  15. // | o The names of the authors may not be used to endorse or promote      |
  16. // |   products derived from this software without specific prior written  |
  17. // |   permission.                                                         |
  18. // |                                                                       |
  19. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  20. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  21. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  22. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  23. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  24. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  25. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  26. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  27. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  28. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  29. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  30. // |                                                                       |
  31. // +-----------------------------------------------------------------------+
  32. // | Author: Richard Heyes <http://www.phpguru.org/>                       |
  33. // |         Harald Radi <harald.radi@nme.at>                              |
  34. // +-----------------------------------------------------------------------+
  35. //
  36. // $Id: TreeMenu.php,v 1.21 2005/03/02 02:24:26 richard Exp $
  37.  
  38. /**
  39. * HTML_TreeMenu Class
  40. *
  41. * A simple couple of PHP classes and some not so simple
  42. * Jabbascript which produces a tree menu. In IE this menu
  43. * is dynamic, with branches being collapsable. In IE5+ the
  44. * status of the collapsed/open branches persists across page
  45. * refreshes.In any other browser the tree is static. Code is
  46. * based on work of Harald Radi.
  47. *
  48. * Usage.
  49. *
  50. * After installing the package, copy the example php script to
  51. * your servers document root. Also place the TreeMenu.js and the
  52. * images folder in the same place. Running the script should
  53. * then produce the tree.
  54. *
  55. * Thanks go to Chip Chapin (http://www.chipchapin.com) for many
  56. * excellent ideas and improvements.
  57. *
  58. @author  Richard Heyes <richard@php.net>
  59. @author  Harald Radi <harald.radi@nme.at>
  60. @access  public
  61. @package HTML_TreeMenu
  62. */
  63.  
  64. {
  65.     /**
  66.     * Indexed array of subnodes
  67.     * @var array 
  68.     */
  69.     var $items;
  70.  
  71.     /**
  72.     * Constructor
  73.     *
  74.     * @access public
  75.     */
  76.     function HTML_TreeMenu()
  77.     {
  78.         // Not much to do here :(
  79.     }
  80.  
  81.     /**
  82.     * This function adds an item to the the tree.
  83.     *
  84.     * @access public
  85.     * @param  object $node The node to add. This object should be
  86.     *                       a HTML_TreeNode object.
  87.     * @return object       Returns a reference to the new node inside
  88.     *                       the tree.
  89.     */
  90.     function &addItem(&$node)
  91.     {
  92.         $this->items[&$node;
  93.         return $this->items[count($this->items- 1];
  94.     }
  95.  
  96.     /**
  97.     * Import method for creating HTML_TreeMenu objects/structures
  98.     * out of existing tree objects/structures. Currently supported
  99.     * are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!)
  100.     * Tree class (available here: http://www.phpguru.org/). This
  101.     * method is intended to be used statically, eg:
  102.     * $treeMenu = &HTML_TreeMenu::createFromStructure($myTreeStructureObj);
  103.     *
  104.     * @param  array  $params   An array of parameters that determine
  105.     *                           how the import happens. This can consist of:
  106.     *                             structure   => The tree structure
  107.     *                             type        => The type of the structure, currently
  108.     *                                            can be either 'heyes' or 'kriesing'
  109.     *                             nodeOptions => Default options for each node
  110.     *                            
  111.     * @return object           The resulting HTML_TreeMenu object
  112.     */
  113.     function createFromStructure($params)
  114.     {
  115.         if (!isset($params['nodeOptions'])) {
  116.             $params['nodeOptions'= array();
  117.         }
  118.  
  119.         switch (@$params['type']{
  120.  
  121.             /**
  122.             * Wolfram Kriesings' PEAR Tree class
  123.             */
  124.             case 'kriesing':
  125.                 $className strtolower(get_class($params['structure']->dataSourceClass));
  126.                 $isXMLStruct strpos($className,'_xml'!== false ? true : false;
  127.  
  128.                 // Get the entire tree, the $nodes are sorted like in the tree view
  129.                 // from top to bottom, so we can easily put them in the nodes
  130.                 $nodes $params['structure']->getNode();
  131.  
  132.                 // Make a new menu and fill it with the values from the tree
  133.                 $treeMenu  = new HTML_TreeMenu();
  134.                 $curNode[0&$treeMenu;   // we need the current node as the reference to the
  135.  
  136.                 foreach $nodes as $aNode {
  137.                     $events = array();
  138.                     $data = array();
  139.  
  140.                     // In an XML, all the attributes are saved in an array, but since they might be
  141.                     // used as the parameters, we simply extract them here if we handle an XML-structure
  142.                     if $isXMLStruct && sizeof($aNode['attributes']) ){
  143.                         foreach $aNode['attributes'as $key=>$val {
  144.                             if !$aNode[$key// dont overwrite existing values
  145.                                 $aNode[$key$val;
  146.                             }
  147.                         }
  148.                     }
  149.  
  150.                     // Process all the data that are saved in $aNode and put them in the data and/or events array
  151.                     foreach $aNode as $key=>$val {
  152.                         if !is_array($val) ) {
  153.                             // Dont get the recursive data in here! they are always arrays
  154.                             if substr($key,0,2== 'on' ){  // get the events
  155.                                 $events[$key$val;
  156.                             }
  157.  
  158.                             // I put it in data too, so in case an options starts with 'on' its also passed to the node ... not too cool i know
  159.                             $data[$key$val;
  160.                         }
  161.                     }
  162.  
  163.                     // Normally the text is in 'name' in the Tree class, so we check both but 'text' is used if found
  164.                     $data['text'$aNode['text'$aNode['text'$aNode['name'];
  165.  
  166.                     // Add the item to the proper node
  167.                     $thisNode &$curNode[$aNode['level']]->addItemnew HTML_TreeNode$data $events ) );
  168.                     $curNode[$aNode['level']+1&$thisNode;
  169.                 }
  170.                 break;
  171.  
  172.             /**
  173.             * Richard Heyes' (me!) second (array based) Tree class
  174.             */
  175.             case 'heyes_array':
  176.                 // Need to create a HTML_TreeMenu object ?
  177.                 if (!isset($params['treeMenu'])) {
  178.                     $treeMenu &new HTML_TreeMenu();
  179.                     $parentID = 0;
  180.                 else {
  181.                     $treeMenu &$params['treeMenu'];
  182.                     $parentID $params['parentID'];
  183.                 }
  184.                 
  185.                 // Loop thru the trees nodes
  186.                 foreach ($params['structure']->getChildren($parentIDas $nodeID{
  187.                     $data $params['structure']->getData($nodeID);
  188.                     $parentNode &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions']$data)));
  189.  
  190.                     // Recurse ?
  191.                     if ($params['structure']->hasChildren($nodeID)) {
  192.                         $recurseParams['type']        'heyes_array';
  193.                         $recurseParams['parentID']    $nodeID;
  194.                         $recurseParams['nodeOptions'$params['nodeOptions'];
  195.                         $recurseParams['structure']   &$params['structure'];
  196.                         $recurseParams['treeMenu']    &$parentNode;
  197.                         HTML_TreeMenu::createFromStructure($recurseParams);
  198.                     }
  199.                 }
  200.                 
  201.                 break;
  202.  
  203.             /**
  204.             * Richard Heyes' (me!) original OO based Tree class
  205.             */
  206.             case 'heyes':
  207.             default:
  208.                 // Need to create a HTML_TreeMenu object ?
  209.                 if (!isset($params['treeMenu'])) {
  210.                     $treeMenu &new HTML_TreeMenu();
  211.                 else {
  212.                     $treeMenu &$params['treeMenu'];
  213.                 }
  214.                 
  215.                 // Loop thru the trees nodes
  216.                 foreach ($params['structure']->nodes->nodes as $node{
  217.                     $tag $node->getTag();
  218.                     $parentNode &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions']$tag)));
  219.  
  220.                     // Recurse ?
  221.                     if (!empty($node->nodes->nodes)) {
  222.                         $recurseParams['structure']   $node;
  223.                         $recurseParams['nodeOptions'$params['nodeOptions'];
  224.                         $recurseParams['treeMenu']    &$parentNode;
  225.                         HTML_TreeMenu::createFromStructure($recurseParams);
  226.                     }
  227.                 }
  228.                 break;
  229.  
  230.         }
  231.  
  232.         return $treeMenu;
  233.     }
  234.     
  235.     /**
  236.     * Creates a treeMenu from XML. The structure of your XML should be
  237.     * like so:
  238.     *
  239.     * <treemenu>
  240.     *     <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" />
  241.     *     <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif">
  242.     *         <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" />
  243.     *     </node>
  244.     *     <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif">
  245.     * </treemenu>
  246.     *
  247.     * Any of the options you can supply to the HTML_TreeNode constructor can be supplied as
  248.     * attributes to the <node> tag. If there are no subnodes for a particular node, you can
  249.     * use the XML shortcut <node ... /> instead of <node ... ></node>. The $xml argument can
  250.     * be either the XML as a string, or an pre-created XML_Tree object. Also, this method
  251.     * REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not
  252.     * been include()ed or require()ed this method will die().
  253.     *
  254.     * @param  mixed  $xml  This can be either a string containing the XML, or an XML_Tree object
  255.     *                       (the PEAR::XML_Tree package).
  256.     * @return object       The HTML_TreeMenu object
  257.     */
  258.     function createFromXML($xml)
  259.     {
  260.         if (!class_exists('Tree')) {
  261.             die('Could not find Tree class');
  262.         }
  263.  
  264.         // Supplied $xml is a string
  265.         if (is_string($xml)) {
  266.             require_once('XML/Tree.php');
  267.             $xmlTree &new XML_Tree();
  268.             $xmlTree->getTreeFromString($xml);
  269.  
  270.         // Supplied $xml is an XML_Tree object
  271.         else {
  272.             $xmlTree $xml;
  273.         }
  274.  
  275.         // Now process the XML_Tree object, setting the XML attributes
  276.         // to be the tag data (with out the XML tag name or contents).
  277.         $treeStructure = Tree::createFromXMLTree($xmlTreetrue);
  278.         $treeStructure->nodes->traverse(create_function('&$node''$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);'));
  279.  
  280.         
  281.         return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure));
  282.     }
  283. // HTML_TreeMenu
  284.  
  285.  
  286. /**
  287. * HTML_TreeNode class
  288. *
  289. * This class is supplementary to the above and provides a way to
  290. * add nodes to the tree. A node can have other nodes added to it.
  291. *
  292. @author  Richard Heyes <richard@php.net>
  293. @author  Harald Radi <harald.radi@nme.at>
  294. @access  public
  295. @package HTML_TreeMenu
  296. */
  297. {
  298.     /**
  299.     * The text for this node.
  300.     * @var string 
  301.     */
  302.     var $text;
  303.  
  304.     /**
  305.     * The link for this node.
  306.     * @var string 
  307.     */
  308.     var $link;
  309.  
  310.     /**
  311.     * The icon for this node.
  312.     * @var string 
  313.     */
  314.     var $icon;
  315.  
  316.     /**
  317.     * The icon to show when expanded for this node.
  318.     * @var string 
  319.     */
  320.     var $expandedIcon;
  321.  
  322.     /**
  323.     * The css class for this node
  324.     * @var string 
  325.     */
  326.     var $cssClass;
  327.  
  328.     /**
  329.     * The link target for this node
  330.     * @var string 
  331.     */
  332.     var $linkTarget;
  333.  
  334.     /**
  335.     * Indexed array of subnodes
  336.     * @var array 
  337.     */
  338.     var $items;
  339.  
  340.     /**
  341.     * Whether this node is expanded or not
  342.     * @var bool 
  343.     */
  344.     var $expanded;
  345.  
  346.     /**
  347.     * Whether this node is dynamic or not
  348.     * @var bool 
  349.     */
  350.     var $isDynamic;
  351.  
  352.     /**
  353.     * Should this node be made visible?
  354.     * @var bool 
  355.     */
  356.     var $ensureVisible;
  357.  
  358.     /**
  359.     * The parent node. Null if top level
  360.     * @var object 
  361.     */
  362.     var $parent;
  363.  
  364.     /**
  365.     * Javascript event handlers;
  366.     * @var array 
  367.     */
  368.     var $events;
  369.  
  370.     /**
  371.     * Constructor
  372.     *
  373.     * @access public
  374.     * @param  array $options An array of options which you can pass to change
  375.     *                         the way this node looks/acts. This can consist of:
  376.     *                          o text          The title of the node, defaults to blank
  377.     *                          o link          The link for the node, defaults to blank
  378.     *                          o icon          The icon for the node, defaults to blank
  379.     *                          o expandedIcon  The icon to show when the node is expanded
  380.     *                          o cssClass      The CSS class for this node, defaults to blank
  381.     *                          o expanded      The default expanded status of this node, defaults to false
  382.     *                                          This doesn't affect non dynamic presentation types
  383.     *                          o linkTarget    Target for the links. Defaults to linkTarget of the
  384.     *                                          HTML_TreeMenu_Presentation.
  385.     *                          o isDynamic     If this node is dynamic or not. Only affects
  386.     *                                          certain presentation types.
  387.     *                          o ensureVisible If true this node will be made visible despite the expanded
  388.     *                                          settings, and client side persistence. Will not affect
  389.     *                                          some presentation styles, such as Listbox. Default is false
  390.     * @param  array $events An array of javascript events and the corresponding event handlers.
  391.     *                        Additionally to the standard javascript events you can specify handlers
  392.     *                        for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired
  393.     *                        whenever a node is collapsed and/or expanded.
  394.     */
  395.     function HTML_TreeNode($options = array()$events = array())
  396.     {
  397.         $this->text          = '';
  398.         $this->link          = '';
  399.         $this->icon          = '';
  400.         $this->expandedIcon  = '';
  401.         $this->cssClass      = '';
  402.         $this->expanded      = false;
  403.         $this->isDynamic     = true;
  404.         $this->ensureVisible = false;
  405.         $this->linkTarget    = null;
  406.  
  407.         $this->parent        = null;
  408.         $this->events        = $events;
  409.  
  410.         foreach ($options as $option => $value{
  411.             $this->$option $value;
  412.         }
  413.     }
  414.  
  415.     /**
  416.     * Allows setting of various parameters after the initial
  417.     * constructor call. Possible options you can set are:
  418.     *  o text
  419.     *  o link
  420.     *  o icon
  421.     *  o cssClass
  422.     *  o expanded
  423.     *  o isDynamic
  424.     *  o ensureVisible
  425.     * ie The same options as in the constructor
  426.     *
  427.     * @access public
  428.     * @param  string $option Option to set
  429.     * @param  string $value  Value to set the option to
  430.     */
  431.     function setOption($option$value)
  432.     {
  433.         $this->$option $value;
  434.     }
  435.  
  436.     /**
  437.     * Adds a new subnode to this node.
  438.     *
  439.     * @access public
  440.     * @param  object $node The new node
  441.     */
  442.     function &addItem(&$node)
  443.     {
  444.         $node->parent  = &$this;
  445.         $this->items[&$node;
  446.  
  447.         /**
  448.         * If the subnode has ensureVisible set it needs
  449.         * to be handled, and all parents set accordingly.
  450.         */
  451.         if ($node->ensureVisible{
  452.             $this->_ensureVisible();
  453.         }
  454.  
  455.         return $this->items[count($this->items- 1];
  456.     }
  457.  
  458.     /**
  459.     * Private function to handle ensureVisible stuff
  460.     *
  461.     * @access private
  462.     */
  463.     function _ensureVisible()
  464.     {
  465.         $this->ensureVisible = true;
  466.         $this->expanded      = true;
  467.  
  468.         if (!is_null($this->parent)) {
  469.             $this->parent->_ensureVisible();
  470.         }
  471.     }
  472. // HTML_TreeNode
  473.  
  474.  
  475. /**
  476. * HTML_TreeMenu_Presentation class
  477. *
  478. * Base class for other presentation classes to
  479. * inherit from.
  480. */
  481. class HTML_TreeMenu_Presentation
  482. {
  483.     /**
  484.     * The TreeMenu structure
  485.     * @var object 
  486.     */
  487.     var $menu;
  488.  
  489.     /**
  490.     * Base constructor simply sets the menu object
  491.     *
  492.     * @param object $structure The menu structure
  493.     */
  494.     function HTML_TreeMenu_Presentation(&$structure)
  495.     {
  496.         $this->menu &$structure;
  497.     }
  498.  
  499.     /**
  500.     * Prints the HTML generated by the toHTML() method.
  501.     * toHTML() must therefore be defined by the derived
  502.     * class.
  503.     *
  504.     * @access public
  505.     * @param  array  Options to set. Any options taken by
  506.     *                 the presentation class can be specified
  507.     *                 here.
  508.     */
  509.     function printMenu($options = array())
  510.     {
  511.         foreach ($options as $option => $value{
  512.             $this->$option $value;
  513.         }
  514.  
  515.         echo $this->toHTML();
  516.     }
  517. }
  518.  
  519.  
  520. /**
  521. * HTML_TreeMenu_DHTML class
  522. *
  523. * This class is a presentation class for the tree structure
  524. * created using the TreeMenu/TreeNode. It presents the
  525. * traditional tree, static for browsers that can't handle
  526. * the DHTML.
  527. */
  528. class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation
  529. {
  530.     /**
  531.     * Dynamic status of the treemenu. If true (default) this has no effect. If
  532.     * false it will override all dynamic status vars and set the menu to be
  533.     * fully expanded an non-dynamic.
  534.     */
  535.     var $isDynamic;
  536.  
  537.     /**
  538.     * Path to the images
  539.     * @var string 
  540.     */
  541.     var $images;
  542.  
  543.     /**
  544.     * Target for the links generated
  545.     * @var string 
  546.     */
  547.     var $linkTarget;
  548.  
  549.     /**
  550.     * Whether to use clientside persistence or not
  551.     * @var bool 
  552.     */
  553.     var $usePersistence;
  554.  
  555.     /**
  556.     * The default CSS class for the nodes
  557.     */
  558.     var $defaultClass;
  559.  
  560.     /**
  561.     * Whether to skip first level branch images
  562.     * @var bool 
  563.     */
  564.     var $noTopLevelImages;
  565.     
  566.     /**
  567.     * Name of Jabbascript object to use
  568.     * @var string 
  569.     */
  570.     var $jsObjectName;
  571.  
  572.     /**
  573.     * Constructor, takes the tree structure as
  574.     * an argument and an array of options which
  575.     * can consist of:
  576.     *  o images            -  The path to the images folder. Defaults to "images"
  577.     *  o linkTarget        -  The target for the link. Defaults to "_self"
  578.     *  o defaultClass      -  The default CSS class to apply to a node. Default is none.
  579.     *  o usePersistence    -  Whether to use clientside persistence. This persistence
  580.     *                         is achieved using cookies. Default is true.
  581.     *  o noTopLevelImages  -  Whether to skip displaying the first level of images if
  582.     *                         there is multiple top level branches.
  583.     *  o maxDepth          -  The maximum depth of indentation. Useful for ensuring
  584.     *                         deeply nested trees don't go way off to the right of your
  585.     *                         page etc. Defaults to no limit.
  586.     *  o jsObjectName      -  Name to use for jabbascript object. Set this if you have
  587.     *                         different menus that should maintain their persistence
  588.     *                         information separately.
  589.     *
  590.     * And also a boolean for whether the entire tree is dynamic or not.
  591.     * This overrides any perNode dynamic settings.
  592.     *
  593.     * @param object $structure The menu structure
  594.     * @param array  $options   Array of options
  595.     * @param bool   $isDynamic Whether the tree is dynamic or not
  596.     */
  597.     function HTML_TreeMenu_DHTML(&$structure$options = array()$isDynamic = true)
  598.     {
  599.         $this->HTML_TreeMenu_Presentation($structure);
  600.         $this->isDynamic $isDynamic;
  601.  
  602.         // Defaults
  603.         $this->images           'images';
  604.         $this->maxDepth         = 0;        // No limit
  605.         $this->linkTarget       '_self';
  606.         $this->jsObjectName     'objTreeMenu';
  607.         $this->defaultClass     '';
  608.         $this->usePersistence   = true;
  609.         $this->noTopLevelImages = false;
  610.  
  611.         foreach ($options as $option => $value{
  612.             $this->$option $value;
  613.         }
  614.     }
  615.  
  616.     /**
  617.     * Returns the HTML for the menu. This method can be
  618.     * used instead of printMenu() to use the menu system
  619.     * with a template system.
  620.     *
  621.     * @access public
  622.     * @return string The HTML for the menu
  623.     */
  624.     function toHTML()
  625.     {
  626.         static $count = 0;
  627.         $menuObj     $this->jsObjectName . '_' . ++$count;
  628.  
  629.         $html  "\n";
  630.         $html .= '<script language="javascript" type="text/javascript">' "\n//<![CDATA[\n\t";
  631.         $html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);',
  632.                          $menuObj,
  633.                          $this->images,
  634.                          $menuObj,
  635.                          $this->linkTarget,
  636.                          $this->defaultClass,
  637.                          $this->usePersistence 'true' 'false',
  638.                          $this->noTopLevelImages 'true' 'false');
  639.  
  640.         $html .= "\n";
  641.  
  642.         /**
  643.         * Loop through subnodes
  644.         */
  645.         if (isset($this->menu->items)) {
  646.             for ($i=0; $i<count($this->menu->items)$i++{
  647.                 $html .= $this->_nodeToHTML($this->menu->items[$i]$menuObj);
  648.             }
  649.         }
  650.  
  651.         $html .= sprintf("\n\t%s.drawMenu();"$menuObj);
  652.         $html .= sprintf("\n\t%s.writeOutput();"$menuObj);
  653.  
  654.         if ($this->usePersistence && $this->isDynamic{
  655.             $html .= sprintf("\n\t%s.resetBranches();"$menuObj);
  656.         }
  657.         $html .= "\n// ]]>\n</script>";
  658.  
  659.         return $html;
  660.     }
  661.  
  662.     /**
  663.     * Prints a node of the menu
  664.     *
  665.     * @access private
  666.     */
  667.     function _nodeToHTML($nodeObj$prefix$return 'newNode'$currentDepth = 0$maxDepthPrefix = null)
  668.     {
  669.         $prefix = empty($maxDepthPrefix$prefix $maxDepthPrefix;
  670.         
  671.         $expanded  $this->isDynamic ($nodeObj->expanded  ? 'true' 'false''true';
  672.         $isDynamic $this->isDynamic ($nodeObj->isDynamic ? 'true' 'false''false';
  673.         $html sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n",
  674.                         $return,
  675.                         $prefix,
  676.                         str_replace("'""\\'"$nodeObj->text),
  677.                         !empty($nodeObj->icon"'" $nodeObj->icon . "'" 'null',
  678.                         !empty($nodeObj->link"'" $nodeObj->link . "'" 'null',
  679.                         $expanded,
  680.                         $isDynamic,
  681.                         $nodeObj->cssClass,
  682.                         $nodeObj->linkTarget,
  683.                         !empty($nodeObj->expandedIcon"'" $nodeObj->expandedIcon . "'" 'null');
  684.  
  685.         foreach ($nodeObj->events as $event => $handler{
  686.             $html .= sprintf("\t %s.setEvent('%s', '%s');\n",
  687.                              $return,
  688.                              $event,
  689.                              str_replace(array("\r""\n""'")array('\r''\n'"\'")$handler));
  690.         }
  691.  
  692.         if ($this->maxDepth > 0 AND $currentDepth == $this->maxDepth{
  693.             $maxDepthPrefix $prefix;
  694.         }
  695.  
  696.         /**
  697.         * Loop through subnodes
  698.         */
  699.         if (!empty($nodeObj->items)) {
  700.             for ($i=0; $i<count($nodeObj->items)$i++{
  701.                 $html .= $this->_nodeToHTML($nodeObj->items[$i]$return$return '_' ($i + 1)$currentDepth + 1$maxDepthPrefix);
  702.             }
  703.         }
  704.  
  705.         return $html;
  706.     }
  707. // End class HTML_TreeMenu_DHTML
  708.  
  709.  
  710. /**
  711. * HTML_TreeMenu_Listbox class
  712. *
  713. * This class presents the menu as a listbox
  714. */
  715. class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation
  716. {
  717.     /**
  718.     * The text that is displayed in the first option
  719.     * @var string 
  720.     */
  721.     var $promoText;
  722.  
  723.     /**
  724.     * The character used for indentation
  725.     * @var string 
  726.     */
  727.     var $indentChar;
  728.  
  729.     /**
  730.     * How many of the indent chars to use
  731.     * per indentation level
  732.     * @var integer 
  733.     */
  734.     var $indentNum;
  735.  
  736.     /**
  737.     * Target for the links generated
  738.     * @var string 
  739.     */
  740.     var $linkTarget;
  741.  
  742.     /**
  743.     * Constructor
  744.     *
  745.     * @param object $structure The menu structure
  746.     * @param array  $options   Options whic affect the display of the listbox.
  747.     *                           These can consist of:
  748.     *                            o promoText  The text that appears at the the top of the listbox
  749.     *                                         Defaults to "Select..."
  750.     *                            o indentChar The character to use for indenting the nodes
  751.     *                                         Defaults to "&nbsp;"
  752.     *                            o indentNum  How many of the indentChars to use per indentation level
  753.     *                                         Defaults to 2
  754.     *                            o linkTarget Target for the links. Defaults to "_self"
  755.     *                            o submitText Text for the submit button. Defaults to "Go"
  756.     */
  757.     function HTML_TreeMenu_Listbox($structure$options = array())
  758.     {
  759.         $this->HTML_TreeMenu_Presentation($structure);
  760.  
  761.         $this->promoText  'Select...';
  762.         $this->indentChar '&nbsp;';
  763.         $this->indentNum  = 2;
  764.         $this->linkTarget '_self';
  765.         $this->submitText 'Go';
  766.  
  767.         foreach ($options as $option => $value{
  768.             $this->$option $value;
  769.         }
  770.     }
  771.  
  772.     /**
  773.     * Returns the HTML generated
  774.     */
  775.     function toHTML()
  776.     {
  777.         static $count = 0;
  778.         $nodeHTML '';
  779.  
  780.         /**
  781. /**
  782.         * Loop through subnodes
  783.         */
  784.         if (isset($this->menu->items)) {
  785.             for ($i=0; $i<count($this->menu->items)$i++{
  786.                 $nodeHTML .= $this->_nodeToHTML($this->menu->items[$i]);
  787.             }
  788.         }
  789.  
  790.         return sprintf('<form target="%s" action="" onsubmit="var link = this.%s.options[this.%s.selectedIndex].value; if (link) {this.action = link; return true} else return false"><select name="%s"><option value="">%s</option>%s</select> <input type="submit" value="%s" /></form>',
  791.                        $this->linkTarget,
  792.                        'HTML_TreeMenu_Listbox_' . ++$count,
  793.                        'HTML_TreeMenu_Listbox_' $count,
  794.                        'HTML_TreeMenu_Listbox_' $count,
  795.                        $this->promoText,
  796.                        $nodeHTML,
  797.                        $this->submitText);
  798.     }
  799.  
  800.     /**
  801.     * Returns HTML for a single node
  802.     *
  803.     * @access private
  804.     */
  805.     function _nodeToHTML($node$prefix '')
  806.     {
  807.         $html sprintf('<option value="%s">%s%s</option>'$node->link$prefix$node->text);
  808.  
  809.         /**
  810.         * Loop through subnodes
  811.         */
  812.         if (isset($node->items)) {
  813.             for ($i=0; $i<count($node->items)$i++{
  814.                 $html .= $this->_nodeToHTML($node->items[$i]$prefix str_repeat($this->indentChar$this->indentNum));
  815.             }
  816.         }
  817.  
  818.         return $html;
  819.     }
  820. // End class HTML_TreeMenu_Listbox
  821. ?>

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