Source for file DBMS_TREE.phpclass
Documentation is available at DBMS_TREE.phpclass
* Class file dbms_treenode.phpclass
* @project Open CSP-Management
* @author Peter Krebs (pk) <pitlinz@users.sourceforge.net >
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: DBMS_TREE.phpclass,v 1.9 2008/06/17 07:45:34 pitlinz Exp $
define('OCSP_TREETYPE_TABLE',0);
define('OCSP_TREETYPE_LIST',1);
* ABSTRACT class DBMS_TREE to handel table tree data in an object
* @project Open CSP-Management
* @author Peter Krebs (pk) <pitlinz@users.sourceforge.net>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version pk-07-06-15 added parent class DBMS_TABLEOBJ to have some common db methods
* @version $Id: DBMS_TREE.phpclass,v 1.9 2008/06/17 07:45:34 pitlinz Exp $
* @staticvar string $myIdFld NOT FINAL name column which is the parent column
* @staticvar string $myParentFld NOT FINAL name of the parent column
* @staticvar string $mySortFld NOT FINAL name of the sort field
* @var string $myGlobalTreeKey index in $OCSP_VAL['DBMS_TREE'][]
* @var int $myRootId index of the root element (is not loaded)
* @var string $myChildClass NOT FINAL name of the class children should have
* it is used in eval("new ".$myChildClass."()")
* @var string $childClassColumn name of the column holding the class name of children
* @var string $myChildKeys NOT FINAL comma sperated list of keys the children
* @var array $myChildren array starts at 1 !! key=sortorder
* @var doubl $myChildrenPopulateTS time() when childrens have been populated
* @var string $myLink link the entry
* @var array $myFilterArr
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* @param string $aGlobalTreeKey
function DBMS_TREE($aGlobalTreeKey=
"",$debug=
FALSE)
if (!empty($aGlobalTreeKey))
} else if (!empty($this->myTable)) {
// #############################################################
* returns the GlobalTreeKey
* @version pk-06-07-26 generate a tree key if none exists
while(isset
($OCSP_VAL['DBMS_TREE'][$s_testKey])) {
$s_testKey=
$s_treeKey.
"_".
($i_count++
);
if ($debug) echoDebug(__FILE__
,"<blockquote style=\"font-size:80%\">NEW TreeKey: ".
$s_testKey.
"</blockquote>");
$OCSP_OBJ['DBMS_TREE'][$aKey]=
&$this;
* returns the tree root id
function setKey($kName,$kValue) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* returns the key tree [PARENT][ID]=>array()
* returns a node in the key tree
* to store more information to the keytree overwrite this in
return array('ID' =>
$arr_row[$this->myIdFld]);
* returns a pointers to the global tree array
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* adds a key value pair to $myFilterArr
if ($debug) echo
"<p><b>DBMS_TREE_NODE::filterAdd($key,$val,...)</b> (".
get_class($this).
")</p>";
if ($debug) echo
"<blockquote><pre>".
print_r($this->myFilterArr,TRUE).
"</pre></blockquote>";
* removes a key form the filter
if ($debug) echo
"<p><b>DBMS_TREE_NODE::filterRemoveKey($key,...)</b> (".
get_class($this).
")</p>";
if ($debug) echo
"<blockquote><pre>".
print_r($this->myFilterArr,TRUE).
"</pre></blockquote>";
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* @param &DBMS_TREE_NODE $child
$child->setTreeObj($this);
$child->setConfigurationArr($confArr,$debug);
* opens a course to fetch node objects under the root (1st level)
if ($debug) echoDebugMethod(__FILE__
,get_class($this),"DBMS_TREE::getChildCursor()",($filtered ?
"<b>FILTERED</b>" :
""));
$s_query =
"SELECT * FROM ".
$this->myTable;
$s_query.=
" WHERE ".
$this->myParentFld.
"=".
$this->getRootId(); // IDs are integer COLUMNS and VALUES
foreach($keyCols as $col) {
$s_query.=
" AND ".
$col.
" = ".
$this->myDBObj->qs_getSlashedValue($this->getKey($col));
$s_query.=
" AND ".
$s_Col.
"=".
$this->myDBObj->qs_getSlashedValue($s_Val);
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"child query: $s_query");
$obj_ret =
$this->myDBObj->query($s_query);
* populates the children of the tree (1st level nodes)
function populate($useCache=
TRUE,$debug=
FALSE,$filtered=
FALSE) {
if ($debug) echoDebugMethod(__FILE__
,get_class($this),"DBMS_TREE::populate()","\$useCache=".
($useCache ?
"TRUE" :
"FALSE"));
while($a_row=
$o_cursor->fetchArrayFld()) {
$ptr_tmpObj->setOrderNr(++
$i_noChildren);
} else if ($ptr_tmpObj=
$this->newNodeObject($aId,$a_row,$debug)) {
$ptr_tmpObj->setOrderNr(++
$i_noChildren);
* populates the hole tree structure
* @param string $where (a full SQL where condition exampl: "WHERE PROJ_ID=1 OR PROJ_ID=0")
* @global array $OCSP_VAL
function populateTree($useCache=
TRUE,$keysOnly=
TRUE,$where=
"",$debug=
FALSE)
$str_query =
"SELECT * FROM ".
$this->myTable;
$arr_cache['KEYS']=
array();
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"Loading tree from database with: <br />".
$str_query);
if ($o_cursor=
$this->myDBObj->query($str_query))
while($arr_row=
$o_cursor->fetchArrayFld())
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"KeyTree: <pre>".
print_r($arr_cache['KEYS'],TRUE).
"</pre>");
$arr_toPopulate=
array(); // array of nodes which are to populate
foreach($arr_cache['KEYS'] as $idVal =>
$arr_vals)
$arr_toPopulate[]=
$idVal;
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"All nodes are already populated");
$str_query =
$str_query =
"SELECT * FROM ".
$this->myTable;
$str_query.=
"WHERE ".
$this->myDBObj->qs_getWhereIn($this->myIdFld,$arr_toPopulate,FALSE);
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"Loading missing tree nodes from database with: <br />".
$str_query);
if ($o_cursor=
$this->myDBObj->query($str_query))
while($arr_row=
$o_cursor->fetchArrayFld())
* selects a node row from the database and returns the
* DB Values as array with quickQuery to the database object
if ($debug) echo
"<blockquote style=\"font-size: 80%;backgroud-color: #A0A0A0\">";
if ($debug) echo
"</blockquote>";
$s_query =
"SELECT * FROM ".
$this->myTable;
$s_query.=
" WHERE ".
$this->myIdFld.
"=".
intval($aId); // IDs are integer COLUMNS and VALUES
foreach($keyCols as $col) {
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"adding childkey $col: ".
$this->{$col});
$s_query.=
" AND ".
$col.
" = ".
$this->myDBObj->qs_getSlashedValue($this->{$col});
if ($debug) {echoDebugLine(__FILE__
,__LINE__
,"$s_query");echo
"</blockquote>";}
return $this->myDBObj->quickQuery($s_query,-
1,$debug,TRUE);
* if $useCache the cache is checked if the node already exists
* if !$useCache the node is laoded from the db and the cache object is replaced
* @version pk-06-08-08 bugfix
if ($debug) echoDebugMethod(__FILE__
,get_class($this),"DBMS_TREE::populateNode()","\$aId=$aId,\$useCache=".
($useCache ?
"TRUE" :
"FALSE"));
if ($debug) echoDebug(__FILE__
,"<p>Root ID => NULL returned</p>");
$this->populate($useCache,$debug); // <pk-06-06-20>
if ($debug) echoDebug(__FILE__
,"<p>returning cached object</p>");
// overwrite existing object
$o_tmpObj->setDBRow($a_row,TRUE,$debug);
//$o_tmpObj->setOrderNr($noc);
if ($debug) echo
"<p>NO NODE FOUND </p></blockquote>";
* generates a new child object and add it to the cache
* @param int $aId the (cache) id of the object
* @param string $aClassName if !empty $this-myChildClass is overwriten if class exists
$s_className=
'DBMS_TREE_NODE';
$s_cmd=
"\$o_nodeObj=new ".
$s_className.
"();";
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"Object cmd: ".
$s_cmd);
* @version pk-07-06-16 use global array
function &getNode($aId,$debug=
FALSE) {
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"DBMS_TREE::getNode -> returning NULL");
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
* adds a node to the cache
* @param DBMS_TREE_NODE:: &$aNode
* @global array $OCSP_VAL
$arr_tree['NODES'][$aId]=
$aNode;
$i_parentId=
intval($aNode->getParentId());
if (!isset
($arr_tree['KEYS'][$i_parentId][$aNode->getId()]))
* returns a pointer to a cached node object
* or NULL if the object is not in the cache
* @global array $OCSP_VAL
* @returns DBMS_TREE_NODE
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"<b>WARNING:</b> returns NULL");
// #######################################################
* returns an array of pointers to the elements till root with all
* in case of an error false is return (use '===' type equal )
* @param mixed $aNode (DMBS_TREE_NODE) a node object or (int) a node id
* @returns array array of pointers to nodes key=DBMS_TREE_NODE::getId()
* @version pk-06-07-26 call the node method getRootPath()
* @version pk-07-08-15 changed type of $aNode
function getRootPath($aNode,$useCache=
TRUE,$debug=
FALSE) {
if ($debug) echo
"<blockquote style=\"backgroud-color: #febf00;font-size:80%\">";
if ($debug) echo
"</blockquote>";
if (empty($aNode)) return array();
if ($o_node=
$this->getNode($aNode,$debug))
if ($debug) echo
"</blockquote>";
ocsp_logError(__FILE__
,__LINE__
,"$aNode is not in the tree",E_WARNING);
if ($debug) echo
"</blockquote>";
* returns a array of pointers to the child objects
if ($debug) echoDebugMethod(__FILE__
,get_class($this),"DBMS_TREE::getChildrenList()","\$filtered=".
($filtered ?
"TRUE" :
"FALSE"));
$ret[$ndx]=
$this->getNode($id,$debug);
// if ($debug) echo "<blockquote><pre>".pcf_object_info($ret[$ndx])."</pre></blockquote>";
* returns a array of pointers to childs matching $myFilterArr
* @version pk-06-11-13 set $filtered to TRUE when calling populate
$this->populate(TRUE,$debug,TRUE); // <pk-06-11-13 /> set $filtered to TRUE
if ($debug) echo
"<p>$treeKey => $treeIdx</p>";
if (is_object($GLOBALS['DBMS_TREE'][$treeKey][$treeIdx])) {
if ($GLOBALS['DBMS_TREE'][$treeKey][$treeIdx]->matchFilter($debug)) {
$ret[$ndx++
]=
&$GLOBALS['DBMS_TREE'][$treeKey][$treeIdx];
* returns an array sorted as tree
* the index is like [1] [1.1] [1.2] [2] [2.1] ....
* the value is an array with following fields:
* NODE pointer to the node
* [NODENAME] if $nameField is set
* @param DBMS_TREE_NODE $startNode
* @param string $startIdx
* @param string $startIntend
* @param string $intendStr
* @param string $nameField
* @version pk-06-08-09 return array() instead of FALSE
function getIndexedTreeArr($startNode=
NULL,$startIdx=
"",$startIntend=
"",$intendStr=
" ",$nameField=
"",$debug=
FALSE) {
echo
"<blockquote>\$childArr:<pre>".
pcf_print_r($childArr,TRUE,2).
"</pre></blockquote>";
foreach($childArr as $node) {
"INTEND" =>
$startIntend,
"NODEID" =>
$node->{$this->myIdFld}
if (!empty($nameField)) {
$ret[$key]['NODENAME']=
$node->{$nameField};
if ($cArr=
$this->getIndexedTreeArr($node,$key.
".",$startIntend.
$intendStr,$intendStr,$nameField,$debug)) {
foreach($cArr as $cKey =>
$cVal) {
return array(); // <pk-06-08-09 />
return array(); // <pk-06-08-09 />
Documentation generated on Thu, 08 Jan 2009 17:43:57 +0100 by phpDocumentor 1.4.0a2