[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/DB/NestedSet/ -> TreeMenu.php (source)

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


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7