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

Source for file TreeMenu.php

Documentation is available at TreeMenu.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: DB_NestedSet_TreeMenu                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Jason Rust <jrust@rustyparts.com>                           |
  16. // +----------------------------------------------------------------------+
  17. // $Id: TreeMenu.php 204899 2006-01-10 08:41:13Z bate $
  18. require_once 'HTML/TreeMenu.php';
  19. // {{{ DB_NestedSet_TreeMenu:: class
  20. /**
  21.  * A helper class to translate the data from a nested set table into a HTML_TreeMenu object
  22.  * so that it can be used to create a dynamic tree menu using the PEAR HTML_TreeMenu class.
  23.  *
  24.  * @see docs/TreeMenu_example.php
  25.  * @author Jason Rust <jrust@rustyparts.com>
  26.  * @package DB_NestedSet
  27.  * @version $Revision: 204899 $
  28.  * @access public
  29.  */
  30. // }}}
  31. /**
  32.  * DB_NestedSet_TreeMenu
  33.  *
  34.  * @package
  35.  * @author daniel
  36.  * @copyright Copyright (c) 2004
  37.  * @version $Id: TreeMenu.php 204899 2006-01-10 08:41:13Z bate $
  38.  * @access public
  39.  ***/
  40.     // {{{ properties
  41.     /**
  42.      *
  43.      * @var array The current menu structure
  44.      * @access private
  45.      */
  46.     var $_structTreeMenu = false;
  47.  
  48.     /**
  49.      *
  50.      * @var array Default field mappings
  51.      * @access private
  52.      */
  53.     var $_paramDefaults = array('textField' => 'text',
  54.         'linkField' => 'link',
  55.         'iconField' => 'icon',
  56.         'expandedIconField' => 'expandedIcon',
  57.         'classField' => 'cssClass',
  58.         'expandedField' => 'expanded',
  59.         'linkTargetField' => 'linkTarget',
  60.         'isDynamicField' => 'isDynamic',
  61.         'ensureVisibleField' => 'ensureVisible'
  62.         );
  63.     // }}}
  64.     // {{{ DB_NestedSet_TreeMenu
  65.     /**
  66.      * The constructor
  67.      *
  68.      * @param array $params The config parameters used for building the
  69.      *                           tree.
  70.      * @see _createFromStructure
  71.      * @access public
  72.      * @return void 
  73.      */
  74.     function DB_NestedSet_TreeMenu($params{
  75.         $this->_structTreeMenu $this->_createFromStructure($params);
  76.     }
  77.     // }}}
  78.     // {{{ _createFromStructure()
  79.     /**
  80.      * <pre>Creates a HTML_TreeMenu structure based off of the results
  81.      * from getAllNodes() method of the DB_NestedSet class.
  82.      * Note that these parameters may be added to the individual nodes
  83.      * to control their behavior:
  84.      * o 'ensureVisible' => (optional) Whether or not the field should be
  85.      *                          forced as visible creating it such as 'icon'
  86.      *                          or 'expandedIcon'
  87.      * o 'events' => (optional) An array of any events to pass to the
  88.      *                   node when creating it such as 'onclick' or
  89.      *                   'onexpand'
  90.      *
  91.      * @param array $params The configuration parameters.  Available
  92.      *                           params are:
  93.      *  o 'structure'            => [REQU] The result from $nestedSet->getAllNodes(true)
  94.      *  o 'textField'            => [REQU] The field in the table that has the text for node
  95.      *  o 'linkField'            => [REQU] The field in the table that has the link for the node
  96.      *  The following params are optional. Please refer to HTML_TreeMenu's manual.
  97.      *  The params are equal to the HTML_TreeMenu::Node properties without the 'Field' appended
  98.      *  o 'iconField'            => [OPT]
  99.      *  o 'expandedIconField'    => [OPT]
  100.      *  o 'classField'           => [OPT]
  101.      *  o 'expandedField'        => [OPT]
  102.      *  o 'linkTargetField'      => [OPT]
  103.      *  o 'isDynamicField'       => [OPT]
  104.      *  o 'ensureVisibleField'   => [OPT]
  105.      *  o 'options' => (optional) An array of any additional options to
  106.      *                     pass to the node when it is created (i.e. icon,
  107.      *                     class).  See HTML_TreeNode for the options)
  108.      *  </pre>
  109.      * @access public
  110.      * @return object HTML_TreeMenu object
  111.      */
  112.     function _createFromStructure($params{
  113.         // Basically we go through the array of nodes checking to see
  114.         // if each node has children and if so recursing.  The reason this
  115.         // works is because the data from getAllNodes() is ordered by level
  116.         // so a root node will always be first, and sub children will always
  117.         // be after them.
  118.         if (!isset($params['treeMenu'])) {
  119.             $treeMenu new HTML_TreeMenu();
  120.         else {
  121.             $treeMenu $params['treeMenu'];
  122.         }
  123.         // always start at level 1
  124.         if (!isset($params['currentLevel'])) {
  125.             $params['currentLevel'= 1;
  126.         }
  127.         // Set the default field mappings if not set in userland
  128.         if (!isset($params['defaultsSet'])) {
  129.             $this->_setParamDefaults($params);
  130.         }
  131.         // have to use a while loop here because foreach works on a copy of the array and
  132.         // the child nodes are passed by reference during the recursion so that the parent
  133.         // will know when they have been hit.
  134.         reset($params['structure']);
  135.         while (list($key$nodeeach($params['structure'])) {
  136.             // see if we've already been here before
  137.             if (isset($node['hit'])) {
  138.                 continue;
  139.             }
  140.             // mark that we've hit this node
  141.             $params['structure'][$key]['hit'$node['hit'= true;
  142.  
  143.             $tag = array('text' => $node[$params['textField']],
  144.                 'link' => $node[$params['linkField']],
  145.                 'icon' => isset($node[$params['iconField']]$node[$params['iconField']] : false,
  146.                 'expandedIcon' => isset($node[$params['expandedIconField']]$node[$params['expandedIconField']] : false,
  147.                 'cssClass' => isset($node[$params['classField']]$node[$params['classField']] : false,
  148.                 'expanded' => isset($node[$params['expandedField']]$node[$params['expandedField']] : false,
  149.                 'linkTarget' => isset($node[$params['linkTargetField']]$node[$params['linkTargetField']] : false,
  150.                 'isDynamic' => isset($node[$params['isDynamicField']]$node[$params['isDynamicField']] : true,
  151.                 'ensureVisible' => isset($node[$params['ensureVisibleField']]$node[$params['ensureVisibleField']] : false);
  152.  
  153.             $options = isset($params['options']array_merge($params['options']$tag$tag;
  154.             
  155.             foreach ($params['options'as $key => $value{
  156.                 if ($options[$key=== false{
  157.                     $options[$key$value;
  158.                 }
  159.             }
  160.  
  161.             $events = isset($node['events']$node['events': array();
  162.             $parentNode $treeMenu->addItem(new HTML_TreeNode($options$events));
  163.             // see if it has children
  164.             if (($node['r'- 1!= $node['l']{
  165.                 $children = array();
  166.                 // harvest all the children
  167.                 $tempStructure $params['structure'];
  168.                 foreach ($tempStructure as $childKey => $childNode{
  169.                     if (!isset($childNode['hit']&& $childNode['l'$node['l'&& $childNode['r'$node['r'&& $childNode['rootid'== $node['rootid']{
  170.                         // important that we assign it by reference here, so that when the child
  171.                         // marks itself 'hit' the parent loops will know
  172.                         $children[$params['structure'][$childKey];
  173.                     }
  174.                 }
  175.  
  176.                 $recurseParams $params;
  177.                 $recurseParams['structure'$children;
  178.                 $recurseParams['treeMenu'$parentNode;
  179.                 $recurseParams['currentLevel']++;
  180.                 $this->_createFromStructure($recurseParams);
  181.             }
  182.         }
  183.  
  184.         return $treeMenu;
  185.     }
  186.     // }}}
  187.     // {{{ printTree()
  188.     /**
  189.      * Print's the current tree using the output driver
  190.      *
  191.      * @access public
  192.      */
  193.     function printTree({
  194.         $options $this->_getOptions('printTree');
  195.         $tree new HTML_TreeMenu_DHTML($this->_structTreeMenu$options);
  196.         $tree->printMenu();
  197.     }
  198.     // }}}
  199.     // {{{ printListbox()
  200.     /**
  201.      * Print's a listbox representing the current tree
  202.      *
  203.      * @access public
  204.      */
  205.     function printListbox({
  206.         $options $this->_getOptions('printListbox');
  207.         $listBox new HTML_TreeMenu_Listbox($this->_structTreeMenu$options);
  208.         $listBox->printMenu();
  209.     }
  210.     // }}}
  211.     // {{{ tree_toHTML()
  212.     /**
  213.      * Returns the HTML for the DHTML-menu. This method can be
  214.      * used instead of printMenu() to use the menu system
  215.      * with a template system.
  216.      *
  217.      * @access public
  218.      * @return string The HTML for the menu
  219.      * @Author Emanuel Zueger
  220.      */
  221.     function tree_toHTML({
  222.         $options $this->_getOptions('toHTML');
  223.         $tree new HTML_TreeMenu_DHTML($this->_structTreeMenu$options);
  224.         return $tree->toHTML();
  225.     }
  226.     // }}}
  227.     // {{{ listbox_toHTML()
  228.     /**
  229.      * Returns the HTML for the listbox. This method can be
  230.      * used instead of printListbox() to use the menu system
  231.      * with a template system.
  232.      *
  233.      * @access public
  234.      * @return string The HTML for the listbox
  235.      * @author Emanuel Zueger
  236.      */
  237.     function listbox_toHTML({
  238.         $options $this->_getOptions('toHTML');
  239.         $listBox new HTML_TreeMenu_Listbox($this->_structTreeMenu$options);
  240.         return $listBox->toHTML();
  241.     }
  242.     // }}}
  243.     // {{{ _setParamDefaults()
  244.     /**
  245.      * DB_NestedSet_TreeMenu::_setParamDefaults()
  246.      *
  247.      * @param  $params Param array passed from userland
  248.      * @return bool True on completion
  249.      * @access private
  250.      */
  251.     function _setParamDefaults($params{
  252.         $defaults $this->_paramDefaults;
  253.         foreach($defaults AS $fieldName => $fieldAlias{
  254.             if (!isset($params[$fieldName])) {
  255.                 $params[$fieldName$fieldAlias;
  256.             }
  257.         }
  258.         $params['defaultsSet'= true;
  259.         return true;
  260.     }
  261.     // }}}
  262. }
  263.  
  264. ?>

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