[ Index ]
 

Code source de SPIP Agora 1.4

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

title

Body

[fermer]

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

   1  <?php
   2  // +----------------------------------------------------------------------+
   3  // | PEAR :: DB_NestedSet_Menu                                        |
   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: Daniel Khan <dk@webcluster.at>                              |
  16  // +----------------------------------------------------------------------+
  17  // $Id: Menu.php,v 1.5 2004/07/25 11:55:22 datenpunk Exp $
  18  // {{{ DB_NestedSet_Menu:: class
  19  /**
  20   * Simple helper class which takes a node array create by DB_NestedSet and transforms it
  21   * into an array useable by HTML_Menu
  22   *
  23   * @see docs/Menu_example.php
  24   * @author Daniel Khan <dk@webcluster.at>
  25   * @package DB_NestedSet
  26   * @version $Revision: 1.5 $
  27   * @access public
  28   */
  29  // }}}
  30  /**
  31   * DB_NestedSet_Menu
  32   *
  33   * @package
  34   * @author daniel
  35   * @copyright Copyright (c) 2004
  36   * @version $Id: Menu.php,v 1.5 2004/07/25 11:55:22 datenpunk Exp $
  37   * @access public
  38   */
  39  class DB_NestedSet_Menu extends DB_NestedSet_Output {
  40      // {{{ properties
  41      var $_structMenu = array();
  42  
  43      /**
  44       *
  45       * @var array Default field mappings
  46       * @access private
  47       */
  48      var $_paramDefaults = array('textField' => 'text',
  49          'titleField' => 'name',
  50          'urlField' => 'url'
  51          );
  52      // }}}
  53      // {{{ DB_NestedSet_Menu
  54      /**
  55       * The constructor
  56       *
  57       * @param array $params The config parameters used for building the array.
  58       * @see _createFromStructure
  59       * @access public
  60       * @return void
  61       */
  62      function & DB_NestedSet_Menu($params) {
  63          $this->_structMenu = & $this->_createFromStructure($params);
  64      }
  65      // }}}
  66      // {{{ _createFromStructure
  67      /**
  68       * DB_NestedSet_Menu::_createFromStructure()
  69           * Creates the HTML_Menu Structure
  70           *
  71       * <pre>
  72           * @param array $params The configuration parameters.  Available
  73       *                          params are:
  74       * o 'structure'            => [REQU] The result from $nestedSet->getAllNodes(true)
  75       * o 'titleField'           => [REQU] The field in the table that has the text for node
  76       * o 'urlField'             => [REQU] The field in the table that has the link for the node
  77           * </pre>
  78       * @param $params
  79       * @return array The menu array
  80       **/
  81      function _createFromStructure($params) {
  82          static $menuStructure, $menuParts;
  83          // always start at level 1
  84          if (!isset($params['currentLevel'])) {
  85              $params['currentLevel'] = 1;
  86          }
  87  
  88          if (!isset($params['currentParent'])) {
  89              $params['currentParent'] = false;
  90          }
  91  
  92          if (!isset($menuParts)) {
  93              $menuParts = array();
  94          }
  95  
  96          if (!isset($menuStructure)) {
  97              $menuStructure = array();
  98          }
  99  
 100          // Set the default field mappings if not set in userland
 101          if (!isset($params['defaultsSet'])) {
 102              $this->_setParamDefaults($params);
 103          }
 104          // have to use a while loop here because foreach works on a copy of the array and
 105          // the child nodes are passed by reference during the recursion so that the parent
 106          // will know when they have been hit.
 107  
 108          reset($params['structure']);
 109          while(list($nodeID,$node)=each($params['structure'])) {
 110              // see if we've already been here before
 111              if (isset($node['hit']) || $node['level'] < $params['currentLevel']) {
 112                  continue;
 113              }
 114              // mark that we've hit this node
 115              $params['structure'][$nodeID]['hit'] = $node['hit'] = true;
 116  
 117              // We are at a rootnode - let's add it to the structure
 118              if ($nodeID == $node['rootid']) {
 119  
 120                  $menuStructure[$node['id']] = array(
 121                                  'title' => isset($node[$params['titleField']]) ? $node[$params['titleField']] : false,
 122                      'url' => isset($node[$params['urlField']]) ? $node[$params['urlField']] : false
 123                      );
 124  
 125                  // Use a reference so we can happily modify $menuParts to change $menuStructure
 126                  $menuParts[$node['id']] = & $menuStructure[$node['id']];
 127              }
 128  
 129                          // Perform action for non-root nodes
 130              $currentParent = & $params['currentParent']['id'];
 131              if ($currentParent && isset($menuParts[$currentParent])) {
 132                  $currentPart = & $menuParts[$currentParent]['sub'];
 133                  $currentPart[$node['id']] = array(
 134                                          'title' => isset($node[$params['titleField']]) ? $node[$params['titleField']] : false,
 135                      'url' => isset($node[$params['urlField']]) ? $node[$params['urlField']] : false
 136                      );
 137                  $menuParts[$node['id']] = & $currentPart[$node['id']];
 138              }
 139              // see if it has children
 140              if (($node['r'] - 1) != $node['l']) {
 141                  $children = array();
 142                  // harvest all the children
 143                  $tempStructure = $params['structure'];
 144                  foreach ($tempStructure as $childKey => $childNode) {
 145                      if (!isset($childNode['hit']) && $node['rootid'] == $childNode['rootid'] && $node['l'] < $childNode['l'] && $node['r'] > $childNode['r'] && $childNode['level'] > $params['currentLevel']) {
 146                          // important that we assign it by reference here, so that when the child
 147                          // marks itself 'hit' the parent loops will know
 148                          $children[$childKey] = & $params['structure'][$childKey];
 149                      }
 150                  }
 151  
 152                  $recurseParams = $params;
 153                  $recurseParams['structure'] = $children;
 154                  $recurseParams['currentLevel']++;
 155                  $recurseParams['currentParent'] = & $node;
 156                  $this->_createFromStructure($recurseParams);
 157              }
 158          }
 159          return $menuStructure;
 160      }
 161      // }}}
 162      // {{{ returnStructure
 163      /**
 164       * DB_NestedSet_Menu::returnStructure()
 165       *
 166       * Returns an array suitable for HTML_Menu
 167       *
 168       * @return array An array useable for HTML_Menu
 169       */
 170      function returnStructure() {
 171          return $this->_structMenu;
 172      }
 173      // }}}
 174      // {{{ _setParamDefaults()
 175      /**
 176       * DB_NestedSet_Menu::_setParamDefaults()
 177       *
 178       * @param  $params Param array passed from userland
 179       * @return bool True on completion
 180       * @access private
 181       */
 182      function _setParamDefaults(& $params) {
 183          $defaults = $this->_paramDefaults;
 184          foreach($defaults AS $fieldName => $fieldAlias) {
 185              if (!isset($params[$fieldName])) {
 186                  $params[$fieldName] = $fieldAlias;
 187              }
 188          }
 189          $params['defaultsSet'] = true;
 190          return true;
 191      }
 192      // }}}
 193  }
 194  ?>


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