Source for file DBMS_SIMPLETREE.phpclass

Documentation is available at DBMS_SIMPLETREE.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file DBMS_SIMPLETREE.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package dbms
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-22.11.2008
  12.   * @version $Id: DBMS_SIMPLETREE.phpclass,v 1.1 2008/12/04 16:45:40 peterkrebs Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  20.  
  21. /**
  22.   * openCSP class DBMS_SIMPLETREE
  23.   *
  24.   * handles a simple tree
  25.   * 
  26.   * a single table with an integer parent field
  27.   * the root index is 0
  28.   * 
  29.   * @project Open CSP-Management
  30.   * @package dbms
  31.   *
  32.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  33.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  34.   *
  35.   * @since pk-22.11.2008
  36.   * @version $Id: DBMS_SIMPLETREE.phpclass,v 1.1 2008/12/04 16:45:40 peterkrebs Exp $
  37.   */
  38. abstract class DBMS_SIMPLETREE extends DBMS_TABLEOBJ
  39. {
  40.     // ---------------------------------------------------------------------------
  41.     // constants
  42.     // ---------------------------------------------------------------------------
  43.     
  44.     /**
  45.      * @constant string CLASS_SRC_FILE
  46.      */
  47.     const CLASS_SRC_FILE = __FILE__;
  48.  
  49.     // ---------------------------------------------------------------------------
  50.     // class (static)
  51.     // ---------------------------------------------------------------------------
  52.     
  53.     /*** class vars ------------------------------------------------------ */
  54.  
  55.     /*abstract*/ protected static $myTreeClass "";
  56.     /*abstract*/ protected static $myTreeTable "";
  57.     /*abstract*/ protected static $myTreeIndexCol "";
  58.     /*abstract*/ protected static $myTreeParentCol "";    
  59.     /*abstract*/ protected static $myTreeSort "";
  60.     
  61.     protected static $myTreeRootId 0;
  62.     
  63.     protected static $myTreeCache array();
  64.     protected static $myTreePopulateTS array();
  65.  
  66.     /*** class methods ------------------------------------------------------ */
  67.     
  68.     /**
  69.      * returns if the tree is already populated
  70.      *
  71.      * @return boolean 
  72.      */
  73.     
  74.     public static function treeIsPopulated()
  75.     {
  76.         return (isset($myTreePopulateTS[self::$myTreeTable]));
  77.     }
  78.     
  79.     
  80.     /**
  81.      * popluates the tree
  82.      *
  83.      * @param array $arr_filter ('KEY' => value pairs)
  84.      * @param boolean $debug 
  85.      * 
  86.      * @return int (number of loaded nodes)
  87.      */
  88.     public static function populateTree($filter=Null,$debug=False)
  89.     {
  90.         if ($debugechoDebugMethod(__FILE__,"static","DBMS_SIMPLETREE::populateTree()",self::$myTreeTable);
  91.         
  92.         $str_sort self::$myTreeParentCol (!empty(self::$myTreeSort"," self::$myTreeSort "");
  93.         
  94.         self::$myTreeCache[self::$myTreeTablearray();
  95.         self::getInstance(self::$myTreeRootId);
  96.         
  97.         if ($arr_children OCSP_OBJ::defaultReadDBObj()->getArray(self::$myTreeTable,$filter,0,0,$str_sort))
  98.         {
  99.              foreach($arr_children as $arr_row)
  100.              {
  101.                  $obj_node self::factoryFromDBRow($arr_row);
  102.                  self::$myTreeCache[self::$myTreeTable][$obj_node->getId()]['NODE'$obj_node;
  103.                  self::$myTreeCache[self::$myTreeTable][$obj_node->getParentId()]['CHILDREN'][$obj_node->getId();
  104.              }
  105.         }    
  106.         
  107.         $myTreePopulateTS[self::$myTreeTable$filter;
  108.         return self::getRootNode();
  109.     }
  110.     
  111.     /**
  112.      * returns the instance with id $aId
  113.      * 
  114.      * if the instance has not been loaded factoryFromId is called
  115.      *
  116.      * @param int $aId 
  117.      * 
  118.      * @return DBMS_SIMPLETREE 
  119.      */
  120.     public static function getInstance($aId)
  121.     {
  122.         if (!isset(self::$myTreeCache[self::$myTreeTable][intval($aId)]))
  123.         {
  124.             if ($obj_node self::factoryFromId(intval($aId)))
  125.             {
  126.                 self::$myTreeCache[self::$myTreeTable][intval($aId)]['NODE'$obj_node;
  127.                 if (!$obj_node->isRoot())
  128.                 {
  129.                     self::$myTreeCache[self::$myTreeTable][$obj_node->getParentId()]['CHILDREN'][$obj_node;    
  130.                 }
  131.             }
  132.         }
  133.         return self::$myTreeCache[self::$myTreeTable][intval($aId)];
  134.     }
  135.     
  136.     /**
  137.      * returns the root node
  138.      *
  139.      * @return DMBS_SIMPLETREE 
  140.      */
  141.     public static function getRootNode()
  142.     {
  143.         return self::getInstance(self::$myTreeRootId);
  144.     }
  145.     
  146.     /**
  147.      * returns an array with all populated instances
  148.      *
  149.      * @return array; 
  150.      */
  151.     public static function getAllInstances()
  152.     {
  153.         $arr_ret array();
  154.         if (is_array(self::$myTreeCache[self::$myTreeTable]))
  155.         {
  156.             foreach(self::$myTreeCache[self::$myTreeTableas $int_id => $arr_nodes)
  157.             {
  158.                 $arr_ret[$int_id&$arr_nodes['NODE'];                
  159.             }
  160.         }
  161.         return $arr_ret;
  162.     }
  163.     
  164.     // ---------------------------------------------------------------------------
  165.     // object vars
  166.     // ---------------------------------------------------------------------------
  167.     
  168.     /*** compostion --------------------------------------------------- */
  169.     
  170.     /*** attributes  -------------------------------------------------- */
  171.     
  172.     
  173.     // ---------------------------------------------------------------------------
  174.     // factory / construct
  175.     // ---------------------------------------------------------------------------
  176.     
  177.     /**
  178.      * factories a node
  179.      *
  180.      * @param int $aId 
  181.      * 
  182.      * @return DBMS_SIMPLETREE 
  183.      */
  184.     abstract public static function &factoryFromId($aId);
  185.     
  186.     /**
  187.      * factories a node
  188.      *
  189.      * @param int $aId 
  190.      * 
  191.      * @return DBMS_SIMPLETREE 
  192.      */
  193.     abstract public static function &factoryFromDBRow($aRow);
  194.     
  195.     /**
  196.      * sets the table name for DBMS_TABLEOBJ
  197.      *
  198.      * @param boolean $debug 
  199.      * @param boolean $noCache 
  200.      * 
  201.      */
  202.     function init($debug=False,$noCache=False)
  203.     {
  204.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_SIMPLETREE::init()",self::$myTreeTable);
  205.         $this->myTable = self::$myTreeTable;
  206.         parent::init($debug,$noCache);
  207.     }
  208.     
  209.     // ---------------------------------------------------------------------------
  210.     // getter / setter
  211.     // ---------------------------------------------------------------------------    
  212.  
  213.     /**
  214.      * returns the id
  215.      *
  216.      * @return int 
  217.      */
  218.     public function getId()
  219.     {
  220.         return intval($this->getDBField(self::$myTreeIndexCol));
  221.     }
  222.     
  223.     /**
  224.      * returns the parent id
  225.      *
  226.      * @return int 
  227.      */
  228.     public function getParentId()
  229.     {
  230.         return intval($this->getDBField(self::$myTreeParentCol));
  231.     }
  232.     
  233.     /**
  234.      * returns if the node is the root node
  235.      *
  236.      * @return boolean 
  237.      */
  238.     public function isRoot()
  239.     {
  240.         return ($this->getId(== self::$myTreeRootId);
  241.     }
  242.     
  243. }
  244.  
  245. ?>

Documentation generated on Thu, 08 Jan 2009 17:43:42 +0100 by phpDocumentor 1.4.0a2