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

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