[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tree/src/ -> lmbTreeNestedCollection.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/lmbCollectionDecorator.class.php');
  10  
  11  /**

  12   * class lmbTreeNestedCollection.

  13   *

  14   * @package tree

  15   * @version $Id: lmbTreeNestedCollection.class.php 5945 2007-06-06 08:31:43Z pachanga $

  16   */
  17  class lmbTreeNestedCollection extends lmbCollectionDecorator
  18  {
  19    protected $node_field = 'id';
  20    protected $parent_field = 'parent_id';
  21  
  22    function setNodeField($name)
  23    {
  24      $this->node_field = $name;
  25    }
  26  
  27    function setParentField($name)
  28    {
  29      $this->parent_field = $name;
  30    }
  31  
  32    function rewind()
  33    {
  34      parent :: rewind();
  35  
  36      if($this->iterator->valid())
  37      {
  38        $nested_array = array();
  39        self :: _doMakeNested($this->iterator, $nested_array);
  40        $iterator = new lmbCollection($nested_array);
  41      }
  42      else
  43        $iterator = new lmbCollection();
  44  
  45      $this->iterator = $iterator;
  46  
  47      return $this->iterator->rewind();
  48    }
  49  
  50    function _doMakeNested($rs, &$nested_array, $parent_id=null, $level=0)
  51    {
  52      $prev_item_id = null;
  53  
  54      while($rs->valid())
  55      {
  56        $item = $rs->current();
  57  
  58        if($level == 0 && $item->get($this->parent_field) !== $prev_item_id)
  59          $parent_id = $item->get($this->parent_field);
  60  
  61        if($item->get($this->parent_field) == $parent_id)
  62        {
  63          $nested_array[] = $item->export();
  64          $rs->next();
  65        }
  66        elseif($item->get($this->parent_field) === $prev_item_id)
  67        {
  68          $nested_array[sizeof($nested_array) - 1]['children'] = array();
  69          $new_nested =& $nested_array[sizeof($nested_array) - 1]['children'];
  70          self :: _doMakeNested($rs, $new_nested, $prev_item_id, $level + 1);
  71        }
  72        else
  73          return;
  74  
  75        $prev_item_id = $item->get($this->node_field);
  76      }
  77    }
  78  }
  79  
  80  ?>


Generated: Tue Dec 2 03:54:09 2008 Cross-referenced by PHPXref 0.7