phpDocumentor 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, 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 <[email protected]>                           |
  33. // |         Harald Radi <[email protected]>                              |
  34. // +-----------------------------------------------------------------------+
  35. //
  36. // $Id: TreeMenu.php,v 1.1 2005/10/17 18:36:53 jeichorn Exp $
  37.  
  38. /**
  39. @package HTML_TreeMenu
  40. */
  41. /**
  42. * HTML_TreeMenu Class
  43. *
  44. * A simple couple of PHP classes and some not so simple
  45. * Jabbascript which produces a tree menu. In IE this menu
  46. * is dynamic, with branches being collapsable. In IE5+ the
  47. * status of the collapsed/open branches persists across page
  48. * refreshes.In any other browser the tree is static. Code is
  49. * based on work of Harald Radi.
  50. *
  51. * Usage.
  52. *
  53. * After installing the package, copy the example php script to
  54. * your servers document root. Also place the TreeMenu.js and the
  55. * images folder in the same place. Running the script should
  56. * then produce the tree.
  57. *
  58. * Thanks go to Chip Chapin (http://www.chipchapin.com) for many
  59. * excellent ideas and improvements.
  60. *
  61. @author  Richard Heyes <[email protected]>
  62. @author  Harald Radi <[email protected]>
  63. @access  public
  64. @package HTML_TreeMenu
  65. */
  66.  
  67. {
  68.     /**
  69.     * Indexed array of subnodes
  70.     * @var array 
  71.     */
  72.     var $items;
  73.  
  74.     /**
  75.     * Constructor
  76.     *
  77.     * @access public
  78.     */
  79.     function HTML_TreeMenu()
  80.     {
  81.         // Not much to do here :(
  82.     }
  83.  
  84.     /**
  85.     * This function adds an item to the the tree.
  86.     *
  87.     * @access public
  88.     * @param  object $node The node to add. This object should be
  89.     *                       a HTML_TreeNode object.
  90.     * @return object       Returns a reference to the new node inside
  91.     *                       the tree.
  92.     */
  93.     function &addItem(&$node)
  94.     {
  95.         $this->items[&$node;
  96.         return $this->items[count($this->items1];
  97.     }
  98. // HTML_TreeMenu
  99.  
  100.  
  101. /**
  102. * HTML_TreeNode class
  103. * This class is supplementary to the above and provides a way to
  104. * add nodes to the tree. A node can have other nodes added to it.
  105. *
  106. @author  Richard Heyes <[email protected]>
  107. @author  Harald Radi <[email protected]>
  108. @access  public
  109. @package HTML_TreeMenu
  110. */
  111. {
  112.     /**
  113.     * The text for this node.
  114.     * @var string 
  115.     */
  116.     var $text;
  117.  
  118.     /**
  119.     * The link for this node.
  120.     * @var string 
  121.     */
  122.     var $link;
  123.  
  124.     /**
  125.     * The icon for this node.
  126.     * @var string 
  127.     */
  128.     var $icon;
  129.     
  130.     /**
  131.     * The css class for this node
  132.     * @var string 
  133.     */
  134.     var $cssClass;
  135.  
  136.     /**
  137.     * Indexed array of subnodes
  138.     * @var array 
  139.     */
  140.     var $items;
  141.  
  142.     /**
  143.     * Whether this node is expanded or not
  144.     * @var bool 
  145.     */
  146.     var $expanded;
  147.     
  148.     /**
  149.     * Whether this node is dynamic or not
  150.     * @var bool 
  151.     */
  152.     var $isDynamic;
  153.     
  154.     /**
  155.     * Should this node be made visible?
  156.     * @var bool 
  157.     */
  158.     var $ensureVisible;
  159.     
  160.     /**
  161.     * The parent node. Null if top level
  162.     * @var object 
  163.     */
  164.     var $parent;
  165.  
  166.     /**
  167.     * Javascript event handlers;
  168.     * @var array 
  169.     */
  170.     var $events;
  171.  
  172.     /**
  173.     * Constructor
  174.     *
  175.     * @access public
  176.     * @param  array $options An array of options which you can pass to change
  177.     *                         the way this node looks/acts. This can consist of:
  178.     *                          o text          The title of the node, defaults to blank
  179.     *                          o link          The link for the node, defaults to blank
  180.     *                          o icon          The icon for the node, defaults to blank
  181.     *                          o class         The CSS class for this node, defaults to blank
  182.     *                          o expanded      The default expanded status of this node, defaults to false
  183.     *                                          This doesn't affect non dynamic presentation types
  184.     *                          o isDynamic     If this node is dynamic or not. Only affects
  185.     *                                          certain presentation types.
  186.     *                          o ensureVisible If true this node will be made visible despite the expanded
  187.     *                                          settings, and client side persistence. Will not affect
  188.     *                                          some presentation styles, such as Listbox. Default is false
  189.     * @param  array $events An array of javascript events and the corresponding event handlers.
  190.     *                        Additionally to the standard javascript events you can specify handlers
  191.     *                        for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired
  192.     *                        whenever a node is collapsed and/or expanded.
  193.     */
  194.     function HTML_TreeNode($options array()$events array())
  195.     {
  196.         $this->text          = '';
  197.         $this->link          = '';
  198.         $this->icon          = '';
  199.         $this->cssClass      = '';
  200.         $this->expanded      = false;
  201.         $this->isDynamic     = true;
  202.         $this->ensureVisible = false;
  203.  
  204.         $this->parent        = null;
  205.         $this->events        = $events;
  206.         
  207.         foreach ($options as $option => $value{
  208.             $this->$option $value;
  209.         }
  210.     }
  211.  
  212.     /**
  213.     * Allows setting of various parameters after the initial
  214.     * constructor call. Possible options you can set are:
  215.     *  o text
  216.     *  o link
  217.     *  o icon
  218.     *  o cssClass
  219.     *  o expanded
  220.     *  o isDynamic
  221.     *  o ensureVisible
  222.     * ie The same options as in the constructor
  223.     *
  224.     * @access public
  225.     * @param  string $option Option to set
  226.     * @param  string $value  Value to set the option to
  227.     */
  228.     function setOption($option$value)
  229.     {
  230.         $this->$option $value;
  231.     }
  232.  
  233.     /**
  234.     * Adds a new subnode to this node.
  235.     *
  236.     * @access public
  237.     * @param  object $node The new node
  238.     */
  239.     function &addItem(&$node)
  240.     {
  241.         $node->parent  &$this;
  242.         $this->items[&$node;
  243.         
  244.         /**
  245.         * If the subnode has ensureVisible set it needs
  246.         * to be handled, and all parents set accordingly.
  247.         */
  248.         if ($node->ensureVisible{
  249.             $this->_ensureVisible();
  250.         }
  251.  
  252.         return $this->items[count($this->items1];
  253.     }
  254.     
  255.     /**
  256.     * Private function to handle ensureVisible stuff
  257.     *
  258.     * @access private
  259.     */
  260.     function _ensureVisible()
  261.     {
  262.         $this->ensureVisible = true;
  263.         $this->expanded      = true;
  264.  
  265.         if (!is_null($this->parent)) {
  266.             $this->parent->_ensureVisible();
  267.         }
  268.     }
  269. // HTML_TreeNode
  270.  
  271.  
  272. /**
  273. * HTML_TreeMenu_Presentation class
  274. * Base class for other presentation classes to
  275. * inherit from.
  276. @package HTML_TreeMenu
  277. */
  278. {
  279.     /**
  280.     * The TreeMenu structure
  281.     * @var object 
  282.     */
  283.     var $menu;
  284.  
  285.     /**
  286.     * Base constructor simply sets the menu object
  287.     *
  288.     * @param object $structure The menu structure
  289.     */
  290.     function HTML_TreeMenu_Presentation(&$structure)
  291.     {
  292.         $this->menu = &$structure;
  293.     }
  294.  
  295.     /**
  296.     * Prints the HTML generated by the toHTML() method.
  297.     * toHTML() must therefore be defined by the derived
  298.     * class.
  299.     *
  300.     * @access public
  301.     * @param  array  Options to set. Any options taken by
  302.     *                 the presentation class can be specified
  303.     *                 here.
  304.     */
  305.     function printMenu($options array())
  306.     {
  307.         foreach ($options as $option => $value{
  308.             $this->$option $value;
  309.         }
  310.  
  311.         echo $this->toHTML();
  312.     }
  313. }
  314.  
  315. /**
  316. * HTML_TreeMenu_DHTML class
  317. *
  318. * This class is a presentation class for the tree structure
  319. * created using the TreeMenu/TreeNode. It presents the
  320. * traditional tree, static for browsers that can't handle
  321. * the DHTML.
  322. @package HTML_TreeMenu
  323. */
  324. {
  325.     /**
  326.     * Dynamic status of the treemenu. If true (default) this has no effect. If
  327.     * false it will override all dynamic status vars and set the menu to be
  328.     * fully expanded an non-dynamic.
  329.     */
  330.     var $isDynamic;
  331.  
  332.     /**
  333.     * Path to the images
  334.     * @var string 
  335.     */
  336.     var $images;
  337.     
  338.     /**
  339.     * Target for the links generated
  340.     * @var string 
  341.     */
  342.     var $linkTarget;
  343.     
  344.     /**
  345.     * Whether to use clientside persistence or not
  346.     * @var bool 
  347.     */
  348.     var $userPersistence;
  349.     
  350.     /**
  351.     * The default CSS class for the nodes
  352.     */
  353.     var $defaultClass;
  354.     
  355.     /**
  356.     * Whether to skip first level branch images
  357.     * @var bool 
  358.     */
  359.     var $noTopLevelImages;
  360.  
  361.     /**
  362.     * Constructor, takes the tree structure as
  363.     * an argument and an array of options which
  364.     * can consist of:
  365.     *  o images            -  The path to the images folder. Defaults to "images"
  366.     *  o linkTarget        -  The target for the link. Defaults to "_self"
  367.     *  o defaultClass      -  The default CSS class to apply to a node. Default is none.
  368.     *  o usePersistence    -  Whether to use clientside persistence. This persistence
  369.     *                         is achieved using cookies. Default is true.
  370.     *  o noTopLevelImages  -  Whether to skip displaying the first level of images if
  371.     *                         there is multiple top level branches.
  372.     *
  373.     * And also a boolean for whether the entire tree is dynamic or not.
  374.     * This overrides any perNode dynamic settings.
  375.     *
  376.     * @param object $structure The menu structure
  377.     * @param array  $options   Array of options
  378.     * @param bool   $isDynamic Whether the tree is dynamic or not
  379.     */
  380.     function HTML_TreeMenu_DHTML(&$structure$options array()$isDynamic true)
  381.     {
  382.         $this->menu      = &$structure;
  383.         $this->isDynamic = $isDynamic;
  384.  
  385.         // Defaults
  386.         $this->images           = 'images';
  387.         $this->linkTarget       = '_self';
  388.         $this->defaultClass     = '';
  389.         $this->usePersistence   true;
  390.         $this->noTopLevelImages = false;
  391.  
  392.         foreach ($options as $option => $value{
  393.             $this->$option $value;
  394.         }
  395.     }
  396.  
  397.     /**
  398.     * Returns the HTML for the menu. This method can be
  399.     * used instead of printMenu() to use the menu system
  400.     * with a template system.
  401.     *
  402.     * @access public
  403.     * @return string The HTML for the menu
  404.     */    
  405.     function toHTML()
  406.     {
  407.         static $count 0;
  408.         $menuObj 'objTreeMenu_' . ++$count;
  409.  
  410.         $html  "\n";
  411.         $html .= '<script language="javascript" type="text/javascript">' "\n\t";
  412.         $html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);',
  413.                          $menuObj,
  414.                          $this->images,
  415.                          $menuObj,
  416.                          $this->linkTarget,
  417.                          $this->defaultClass,
  418.                          $this->usePersistence 'true' 'false',
  419.                          $this->noTopLevelImages ? 'true' 'false');
  420.  
  421.         $html .= "\n";
  422.  
  423.         /**
  424.         * Loop through subnodes
  425.         */
  426.         if (isset($this->menu->items)) {
  427.             for ($i=0$i<count($this->menu->items)$i++{
  428.                 $html .= $this->_nodeToHTML($this->menu->items[$i]$menuObj);
  429.             }
  430.         }
  431.  
  432.          $html .= sprintf("\n\t%s.drawMenu();"$menuObj);
  433.         if ($this->usePersistence && $this->isDynamic{
  434.             $html .= sprintf("\n\t%s.resetBranches();"$menuObj);
  435.         }
  436.         $html .= "\n</script>";
  437.  
  438.         return $html;
  439.     }
  440.     
  441.     /**
  442.     * Prints a node of the menu
  443.     *
  444.     * @access private
  445.     */
  446.     function _nodeToHTML($nodeObj$prefix$return 'newNode')
  447.     {
  448.         $expanded  $this->isDynamic ? ($nodeObj->expanded  'true' 'false''true';
  449.         $isDynamic $this->isDynamic ? ($nodeObj->isDynamic 'true' 'false''false';
  450.         $html sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s'));\n",
  451.                         $return,
  452.                         $prefix,
  453.                         $nodeObj->text,
  454.                         !empty($nodeObj->icon"'" $nodeObj->icon "'" 'null',
  455.                         !empty($nodeObj->link"'" addslashes($nodeObj->link"'" 'null',
  456.                         $expanded,
  457.                         $isDynamic,
  458.                         $nodeObj->cssClass);
  459.  
  460.         foreach ($nodeObj->events as $event => $handler{
  461.             $html .= sprintf("\t %s.setEvent('%s', '%s');\n",
  462.                              $return,
  463.                              $event,
  464.                              str_replace(array("\r""\n""'")array('\r''\n'"\'")$handler));
  465.         }
  466.  
  467.         /**
  468.         * Loop through subnodes
  469.         */
  470.         if (!empty($nodeObj->items)) {
  471.             for ($i=0$i<count($nodeObj->items)$i++{
  472.                 $html .= $this->_nodeToHTML($nodeObj->items[$i]$return$return '_' ($i 1));
  473.             }
  474.         }
  475.  
  476.         return $html;
  477.     }
  478. }
  479.  
  480.  
  481. /**
  482. * HTML_TreeMenu_Listbox class
  483. * This class presents the menu as a listbox
  484. @package HTML_TreeMenu
  485. */
  486. {
  487.     /**
  488.     * The text that is displayed in the first option
  489.     * @var string 
  490.     */
  491.     var $promoText;
  492.     
  493.     /**
  494.     * The character used for indentation
  495.     * @var string 
  496.     */
  497.     var $indentChar;
  498.     
  499.     /**
  500.     * How many of the indent chars to use
  501.     * per indentation level
  502.     * @var integer 
  503.     */
  504.     var $indentNum;
  505.     
  506.     /**
  507.     * Target for the links generated
  508.     * @var string 
  509.     */
  510.     var $linkTarget;
  511.  
  512.     /**
  513.     * Constructor
  514.     *
  515.     * @param object $structure The menu structure
  516.     * @param array  $options   Options whic affect the display of the listbox.
  517.     *                           These can consist of:
  518.     *                            o promoText  The text that appears at the the top of the listbox
  519.     *                                         Defaults to "Select..."
  520.     *                            o indentChar The character to use for indenting the nodes
  521.     *                                         Defaults to "&nbsp;"
  522.     *                            o indentNum  How many of the indentChars to use per indentation level
  523.     *                                         Defaults to 2
  524.     *                            o linkTarget Target for the links. Defaults to "_self"
  525.     *                            o submitText Text for the submit button. Defaults to "Go"
  526.     */
  527.     function HTML_TreeMenu_Listbox($structure$options array())
  528.     {
  529.         $this->menu       = $structure;
  530.         $this->promoText  = 'Select...';
  531.         $this->indentChar = '&nbsp;';
  532.         $this->indentNum  = 2;
  533.         $this->linkTarget = '_self';
  534.         $this->submitText 'Go';
  535.         
  536.         foreach ($options as $option => $value{
  537.             $this->$option $value;
  538.         }
  539.     }
  540.  
  541.     /**
  542.     * Returns the HTML generated
  543.     */
  544.     function toHTML()
  545.     {
  546.         static $count 0;
  547.         $nodeHTML '';
  548.  
  549.         /**
  550. /**
  551.         * Loop through subnodes
  552.         */
  553.         if (isset($this->menu->items)) {
  554.             for ($i=0$i<count($this->menu->items)$i++{
  555.                 $nodeHTML .= $this->_nodeToHTML($this->menu->items[$i]);
  556.             }
  557.         }
  558.  
  559.         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>',
  560.                        $this->linkTarget,
  561.                        'HTML_TreeMenu_Listbox_' . ++$count,
  562.                        'HTML_TreeMenu_Listbox_' $count,
  563.                        'HTML_TreeMenu_Listbox_' $count,
  564.                        $this->promoText,
  565.                        $nodeHTML,
  566.                        $this->submitText);
  567.     }
  568.     
  569.     /**
  570.     * Returns HTML for a single node
  571.     * 
  572.     * @access private
  573.     */
  574.     function _nodeToHTML($node$prefix '')
  575.     {
  576.         $html sprintf('<option value="%s">%s%s</option>'$node->link$prefix$node->text);
  577.         
  578.         /**
  579.         * Loop through subnodes
  580.         */
  581.         if (isset($node->items)) {
  582.             for ($i=0$i<count($node->items)$i++{
  583.                 $html .= $this->_nodeToHTML($node->items[$i]$prefix str_repeat($this->indentChar$this->indentNum));
  584.             }
  585.         }
  586.         
  587.         return $html;
  588.     }
  589. }
  590. ?>

Documentation generated on Tue, 24 Oct 2006 09:26:33 -0500 by phpDocumentor 1.3.1