forked from ibuildingsnl/ATK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atktreetools.inc
123 lines (115 loc) · 3.19 KB
/
atktreetools.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* This file is part of the Achievo ATK distribution.
* Detailed copyright and licensing information can be found
* in the doc/COPYRIGHT and doc/LICENSE files which should be
* included in the distribution.
*
* @package atk
* @todo The atktreetools should be moved to the utils subpackage.
*
* @copyright (c)2000-2004 Ibuildings.nl BV
* @license http://www.achievo.org/atk/licensing ATK Open Source License
*
* @version $Revision$
* $Id$
*/
/**
* Node class, represents a node in a tree.
*
* @author Ivo Jansch <ivo@achievo.org>
* @package atk
*/
class node
{
var $m_id;
var $m_label; // DEPRECATED, use $m_object instead.
var $m_object;
var $m_img;
var $m_sub = array();
function node($id, $object, $img="")
{
$this->m_id = $id;
$this->m_object = $object;
$this->m_label = &$this->m_object; // DEPRECATED, but available for backwardcompatibility.
$this->m_img = $img;
}
}
/**
* Tree class, used to build trees of nodes.
*
* @author Ivo Jansch <ivo@achievo.org>
* @package atk
*/
class tree
{
var $m_tree = array();
var $m_allnodes = array();
var $m_parentless = array(); // Array to keep stuff that can not yet be inserted into the array.
function addNode($id, $naam, $parent=0, $img="")
{
$n = new node($id, $naam, $img);
$this->m_allnodes[$id] = &$n;
if (array_key_exists($id,$this->m_parentless) && is_array($this->m_parentless[$id]))
{
// In the parentless array, there are children that belong to this new record.
$n->m_sub = &$this->m_parentless[$id];
unset($this->m_parentless[$id]);
}
if (empty($parent))
{
$this->m_tree[] = &$n;
}
else
{
$tmp = &$this->m_allnodes[$parent];
if (is_object($tmp))
{
$tmp->m_sub[] = &$n;
}
else
{
// Dangling thingee.
$this->m_parentless[$parent][] = &$n;
}
}
}
/**
* Example render function. Implement your own.
*/
function render($tree="", $level=0)
{
// First time: root tree..
if ($tree == "") $tree = $this->m_tree;
$res="";
while (list($id,$objarr) = each($tree))
{
$res.= '<tr><td>'.str_repeat("-",(2*$level))." ".$objarr->m_label.'</td></tr>';
if (count($objarr->m_sub)>0)
{
$res.=$this->render($objarr->m_sub, $level+1);
}
}
return $res;
}
/**
* Pops tree's on the session
*/
function sessionTree()
{
global $ATK_VARS;
$postTree = $ATK_VARS["atktree"];
$sessionTree = sessionLoad("atktree");
if ($postTree !="" && $sessionTree != $postTree)
{
sessionStore("atktree",$postTree); // new in the session
$realTree = $postTree;
}
else
{
$realTree = $sessionTree; // use the last known tree
}
$ATK_VARS["atktree"] == $realTree; // postvars now should contain the last Knowtree
}
}
?>