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

Source for file TreeRoot.class.php

Documentation is available at TreeRoot.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package data_structures
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. require_onceCLASSPATH."data_structures/Vector.class.php" );
  21.  
  22. /**
  23.  *
  24.  */
  25. require_onceCLASSPATH."data_structures/TreeNode.class.php" );
  26.  
  27. /**
  28.  *
  29.  */
  30. require_onceCLASSPATH."core/Arrays.inc.php" );
  31.  
  32. /**
  33.  * @version   0.1.11
  34.  * @package   data_structures
  35.  * @author    Daniel Plücken <daniel@debakel.net>
  36.  * @license   http://www.gnu.org/copyleft/lesser.html
  37.  *             GNU Lesser General Public License
  38.  * @copyright Copyright (C) 2005 Daniel Plücken <daniel@debakel.net>
  39.  *
  40.  *  This library is free software; you can redistribute it and/or
  41.  *  modify it under the terms of the GNU Lesser General Public
  42.  *  License as published by the Free Software Foundation; either
  43.  *  version 2.1 of the License.
  44.  *
  45.  *  This library is distributed in the hope that it will be useful,
  46.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  47.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  48.  *  GNU Lesser General Public License for more details.
  49.  *
  50.  *  You should have received a copy of the GNU Lesser General
  51.  *  Public License along with this library; if not, write to the
  52.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  53.  *  Boston, MA 02111-1307 USA
  54.  */
  55. class TreeRoot
  56. {
  57.   /**
  58.    * @var    Vector $child 
  59.    * @access public
  60.    */
  61.   var $children = null;
  62.   
  63.   
  64.   /**
  65.    * Constructor
  66.    */  
  67.   function TreeRoot()
  68.   {}
  69.  
  70.  
  71.  
  72.   /**
  73.    * Builds a tree from a given two dimensional array.
  74.    *
  75.    * @version 1.1
  76.    * @since   0.1.1
  77.    * @author  Daniel Plücken <daniel@debakel.net>
  78.    * @access  public
  79.    * @param   array  &$array        A two dimensional array.
  80.    * @param   string $primary_key   The keyname whose value represents an id.
  81.    * @param   string $recursive_key The keyname whose value refers to the value
  82.    *                                 of $primary_key
  83.    * @return  void 
  84.    */
  85.   function buildTree&$array$primary_key$recursive_key )
  86.   {
  87.     ifpreg_match"!^\d+$!"$array[0][$primary_key) )
  88.       Arrays::sortSecondDimension$array$recursive_key"intcmp" );
  89.     else
  90.       Arrays::sortSecondDimension$array$recursive_key );
  91.     
  92.     $tmp_arr array();
  93.     for$i 0$i count$array )$i++ )
  94.       if!empty$array[$i][$recursive_key) )
  95.       {
  96.         $tmp_arr[=new TreeNode();
  97.         
  98.         foreach$array[$ias $key => $value )
  99.           if!preg_match"!^\d!"$key ) )
  100.             $tmp_arr[count($tmp_arr)-1]->$key $value;
  101.       }
  102.       else
  103.       {
  104.         $tmp_arr[=new TreeNode();
  105.         $last_key count$tmp_arr 1;
  106.         $this->children[=$tmp_arr[$last_key];
  107.         
  108.         foreach$array[$ias $key => $value )
  109.           if!preg_match"!^\d!"$key ) )
  110.             $this->children[$last_key]->$key $value;
  111.         
  112.         $this->children[$last_key]->parent =$this;
  113.       }
  114.     
  115.     
  116.     for$i 0$i count$tmp_arr )$i++ )
  117.       for$k 0$k count$tmp_arr )$k++ )
  118.          if$tmp_arr[$i]->$primary_key == $tmp_arr[$k]->$recursive_key )
  119.          {
  120.             $tmp_arr[$i]->children[=$tmp_arr[$k];
  121.             $tmp_arr[$k]->parent     =$tmp_arr[$i];
  122.          }
  123.   }
  124.   
  125.   
  126.   /**
  127.    * Adds a childnode of type TreeNode to this object.
  128.    *
  129.    * @version 1.0
  130.    * @since   0.1.0
  131.    * @author  Daniel Plücken <daniel@debakel.net>
  132.    * @access  public
  133.    * @param   TreeNode $obj_ref 
  134.    * @param   integer  $pos 
  135.    * @return  void 
  136.    */
  137.   function addChild&$obj_ref$pos "" )
  138.   {
  139.     if(
  140.         !function_exists"is_a" )
  141.      && (
  142.           get_class$obj_ref == "treenode"
  143.        || get_parent_class$obj_ref == "treenode"
  144.         )
  145.       )
  146.       $this->parent =$obj_ref;
  147.     else
  148.     if(
  149.         function_exists"is_a" )
  150.      && (
  151.           is_a$obj_ref"TreeNode" )
  152.        || is_subclass_of$obj_ref"TreeNode" )
  153.         )
  154.       )
  155.       $this->child->add$obj_ref$pos );
  156.     else
  157.       die(
  158.            "<h3>The given parameter is not a reference "
  159.               ."of an object of a treenode!</h3>"
  160.          );
  161.   }
  162. // END of class TreeRoot
  163. ?>

Documentation generated on Thu, 05 Jun 2008 19:15:32 +0200 by phpDocumentor 1.4.1