blob: 48153d0cc35aab8cee70a910a021049fe8a5e544 (
plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
|
<?php
/**
* @version $Header$
*
* ase tree maker
*
* @author zaufi@sendmail.ru
* @package TreeMaker
*
*/
/**
* required setup
*/
if ( defined( 'DEBUG_PKG_PATH' ) ) {
require_once( DEBUG_PKG_PATH.'debugger.php' );
}
/**
* Base class for all tree makers
*
* Define base interface and provide common algotithm for tree generation
*
* Format of element in array for make_tree() call:
* id => number of ID of current node
* parent => number of ID of parant node
* data => user provided data to be placed as node text
*
* @package TreeMaker
*/
class TreeMaker {
/// Unique prefix for cookies generated for this tree
public $prefix;
/// Constructor
function TreeMaker($prefix) {
$this->prefix = $prefix;
}
/// Generate HTML code for tree
function make_tree($rootid, $ar) {
return $this->make_tree_r($rootid, $ar);
}
/// Recursive make (do not call directly)
function make_tree_r($rootid, &$ar) {
//global $debugger;
//$debugger->msg("TreeMaker::make_tree_r: Root ID=" . $rootid);
$result = '';
if (count($ar) > 0) {
$cli = array();
$tmp = array();
foreach ($ar as $i)
if ($rootid == $i["parent"])
$cli[] = $i;
else
$tmp[] = $i;
//
foreach ($cli as $i) {
$child_result = $this->make_tree_r($i["id"], $tmp);
$have_childs = (strlen($child_result) > 0);
//
// NOTE: The main rule is to call all methods in
// stricty defined order!!
//
$nsc = $this->node_start_code($i);
$flipper = '';
if ($have_childs)
$flipper = $this->node_flipper_code($i);
$ndsc = $this->node_data_start_code($i);
$ndec = $this->node_data_end_code($i);
$ncsc = '';
$ncec = '';
if ($have_childs) {
$ncsc = $this->node_child_start_code($i);
$ncec = $this->node_child_end_code($i);
}
$nec = $this->node_end_code($i);
// Form result
$result .= $nsc . $flipper . $ndsc . $i["data"] . $ndec . $ncsc . $child_result . $ncec . $nec;
}
}
return $result;
}
/**
* To change behavior (look and feel :) of generated tree
* it is enough to redefine follwing methods..
* (thanx that PHP have implicit vurtual functions :)
*
* General layout of generated tree code looks like this:
*
* [node start code]
* [node flipper code] (1)
* [node data start code]
* [node data end code]
* [node childs start code] (1)
* [node childs end code] (1)
* [node end code]
*
* (1) -- this code will be generated if node have childs
*
* NOTE: Methods called exactly in that order. This fact can be
* (and actualy do) used by child classes to define
* and use some variables depends on pervious call...
*
* NOTE: This is abstract base class... it doing nothig
* except defining algirithm...
* So to make smth other use inheritance and redifine
* corresponding function :)
*/
function node_start_code($nodeinfo) {
return '';
}
//
function node_flipper_code($nodeinfo) {
return '';
}
//
function node_data_start_code($nodeinfo) {
return '';
}
//
function node_data_end_code($nodeinfo) {
return '';
}
//
function node_child_start_code($nodeinfo) {
return '';
}
//
function node_child_end_code($nodeinfo) {
return '';
}
//
function node_end_code($nodeinfo) {
return '';
}
}
?>
|