commit | author | age
|
b5a2f8
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
436ed8
|
4 |
Copyright (c) 2007, Till Brehm, projektfarm Gmbh |
b5a2f8
|
5 |
All rights reserved. |
T |
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
class node { |
|
32 |
var $childs; |
|
33 |
var $data; |
|
34 |
var $id; |
|
35 |
var $parent; |
|
36 |
} |
|
37 |
|
|
38 |
class tree |
|
39 |
{ |
|
40 |
|
b1a6a5
|
41 |
var $obj; |
MC |
42 |
var $events; |
|
43 |
|
|
44 |
// Feld Definitionen |
|
45 |
var $data_field = 'name'; |
|
46 |
var $primary_field = 'media_cat_id'; |
|
47 |
var $parent_field = 'parent'; |
|
48 |
var $root_id = '0'; |
|
49 |
var $opt_spacer = ' '; |
|
50 |
|
b5a2f8
|
51 |
// interne Vars |
T |
52 |
var $_last_id; |
b1a6a5
|
53 |
|
b5a2f8
|
54 |
/* |
T |
55 |
Funktion zum laden des Baumes aus Array |
|
56 |
*/ |
b1a6a5
|
57 |
|
MC |
58 |
function loadFromArray ($nodes) { |
|
59 |
|
|
60 |
$this->obj[$this->root_id] = new node(); |
b5a2f8
|
61 |
if(is_array($nodes)) { |
b1a6a5
|
62 |
foreach($nodes as $row) { |
b5a2f8
|
63 |
|
b1a6a5
|
64 |
$id = $row[$this->primary_field]; |
MC |
65 |
$data = $row[$this->data_field]; |
|
66 |
$ordner = $row[$this->parent_field]; |
b5a2f8
|
67 |
|
b1a6a5
|
68 |
//$this->raw_data[$id] = $row; |
MC |
69 |
|
|
70 |
if($id > $this->_last_id) $this->_last_id = $id; |
|
71 |
|
|
72 |
if(!is_object($this->obj[$id])) $this->obj[$id] = new node(); |
|
73 |
|
|
74 |
$this->obj[$id]->data = $data; |
|
75 |
$this->obj[$id]->id = $row[$this->primary_field]; |
|
76 |
$this->obj[$id]->parent = $row[$this->parent_field]; |
|
77 |
|
|
78 |
if(is_object($this->obj[$ordner])) { |
|
79 |
$this->obj[$ordner]->childs[$id] = &$this->obj[$id]; |
|
80 |
} else { |
|
81 |
$this->obj[$ordner] = new node(); |
|
82 |
$this->obj[$ordner]->childs[$id] = &$this->obj[$id]; |
|
83 |
} |
|
84 |
} |
b5a2f8
|
85 |
} |
b1a6a5
|
86 |
} |
MC |
87 |
|
|
88 |
function optionlist($nroot = '') |
|
89 |
{ |
|
90 |
|
|
91 |
if($nroot == '') $nroot = $this->obj[$this->root_id]; |
b5a2f8
|
92 |
$opt_spacer = $this->opt_spacer; |
b1a6a5
|
93 |
|
MC |
94 |
$this->ptree($nroot, '', $optionlist, $opt_spacer); |
|
95 |
|
|
96 |
if(is_array($optionlist)){ |
b5a2f8
|
97 |
return $optionlist; |
T |
98 |
} else { |
|
99 |
return false; |
|
100 |
} |
b1a6a5
|
101 |
} |
b5a2f8
|
102 |
|
b1a6a5
|
103 |
function ptree($myobj, $ebene, &$optionlist, $opt_spacer){ |
MC |
104 |
$ebene .= $opt_spacer; |
|
105 |
|
|
106 |
if(is_array($myobj->childs)) { |
|
107 |
foreach($myobj->childs as $val) { |
|
108 |
$id = $val->id; |
|
109 |
if(!empty($id)) $optionlist[$id] = array( data => $ebene . $val->data, |
|
110 |
id => $id); |
|
111 |
$this->ptree($val, $ebene, $optionlist, $opt_spacer); |
|
112 |
} |
b5a2f8
|
113 |
} |
b1a6a5
|
114 |
} |
MC |
115 |
|
|
116 |
function add($parent, $data) { |
|
117 |
|
|
118 |
$id = $this->_last_id + 1; |
|
119 |
$this->obj[$id] = new node; |
|
120 |
$this->obj[$id]->data = $data; |
|
121 |
$this->obj[$id]->id = $id; |
|
122 |
$this->obj[$id]->parent = $parent; |
|
123 |
$this->obj[$parent]->childs[$id] = &$this->obj[$id]; |
|
124 |
|
|
125 |
// Event Aufrufen |
|
126 |
$this->_callEvent('insert', $this->obj[$id]); |
|
127 |
|
|
128 |
} |
|
129 |
|
|
130 |
/* |
a9d0b2
|
131 |
Löschen von Einträgen ohne Child's |
b5a2f8
|
132 |
*/ |
b1a6a5
|
133 |
|
MC |
134 |
function del($id) { |
|
135 |
if(count($this->obj[$id]->childs) == 0) { |
|
136 |
$this->obj[$id] = NULL; |
|
137 |
unset($this->obj[$id]); |
|
138 |
return true; |
|
139 |
} else { |
|
140 |
return false; |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
/* |
a9d0b2
|
145 |
Rekursives löschen von Einträgen |
b5a2f8
|
146 |
*/ |
b1a6a5
|
147 |
|
MC |
148 |
function deltree($tree_id) { |
|
149 |
// lösche Einträge recursiv |
|
150 |
$this->_deltree_recurse($this->obj[$this->root_id], $tree_id, 0); |
|
151 |
} |
|
152 |
|
|
153 |
/* |
a9d0b2
|
154 |
Hilfsfunktion für deltree |
b5a2f8
|
155 |
*/ |
b1a6a5
|
156 |
|
MC |
157 |
function _deltree_recurse(&$myobj, $tree_id, $delete) { |
b5a2f8
|
158 |
if(is_array($myobj->childs)) { |
b1a6a5
|
159 |
foreach($myobj->childs as $val) { |
MC |
160 |
|
|
161 |
// Setze Delete Flag |
|
162 |
if($val->id == $tree_id) { |
|
163 |
$delete = 1; |
|
164 |
} |
|
165 |
|
|
166 |
// recurse durch Objekte |
|
167 |
$this->_deltree_recurse($val, $tree_id, $delete); |
|
168 |
|
|
169 |
// lösche Eintrag |
|
170 |
if($delete == 1) { |
|
171 |
$tmp_id = $val->id; |
|
172 |
$this->obj[$tmp_id] = NULL; |
|
173 |
unset($this->obj[$tmp_id]); |
|
174 |
$this->_callEvent('delete', $val); |
|
175 |
//echo "Deleting ID: $tmp_id \r\n"; |
|
176 |
} |
|
177 |
|
|
178 |
// entferne Delete Flag |
|
179 |
if($val->id == $tree_id) { |
|
180 |
$delete = 0; |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
|
|
187 |
/* |
b5a2f8
|
188 |
private Funktion zum aufrufen der eventHandler |
T |
189 |
*/ |
b1a6a5
|
190 |
|
MC |
191 |
function _callEvent($event, $myobj, $myobj_old = '') { |
|
192 |
global $app; |
b5a2f8
|
193 |
if(is_array($this->events)) { |
b1a6a5
|
194 |
foreach($this->events as $val) { |
MC |
195 |
if($val["event"] == $event) { |
|
196 |
$class_name = $val["class_name"]; |
|
197 |
$function_name = $val["function_name"]; |
|
198 |
if($val["class_name"] != '') { |
|
199 |
$app->uses($class_name); |
|
200 |
$app->$class_name->$function_name($myobj, $myobj_old); |
|
201 |
} else { |
|
202 |
call_user_func($function_name, $myobj, $myobj_old); |
|
203 |
} |
|
204 |
} |
b5a2f8
|
205 |
} |
b1a6a5
|
206 |
} |
MC |
207 |
} |
|
208 |
|
|
209 |
/* |
a9d0b2
|
210 |
Funktion zum Verschieben von Einträgen |
b5a2f8
|
211 |
*/ |
b1a6a5
|
212 |
|
MC |
213 |
function move($id, $new_parent) { |
|
214 |
|
|
215 |
$obj_old = $this->obj[$id]; |
|
216 |
$parent = $this->obj[$id]->parent; |
|
217 |
$this->obj[$new_parent]->childs[$id] = &$this->obj[$id]; |
|
218 |
$this->obj[$id]->parent = $new_parent; |
|
219 |
unset($this->obj[$parent]->childs[$id]); |
|
220 |
|
|
221 |
// event aufrufen |
|
222 |
$this->_callEvent('update', $this->obj[$id], $obj_old); |
|
223 |
|
|
224 |
} |
|
225 |
|
|
226 |
/* |
b5a2f8
|
227 |
Funktion zum updaten der Daten eines Nodes |
T |
228 |
*/ |
b1a6a5
|
229 |
|
MC |
230 |
function update($id, $data) { |
|
231 |
|
|
232 |
$obj_old = $this->obj[$id]; |
|
233 |
$this->obj[$id]->data = $data; |
|
234 |
$this->_callEvent('update', $this->obj[$id], $obj_old); |
|
235 |
|
|
236 |
} |
|
237 |
|
|
238 |
/* |
b5a2f8
|
239 |
Funktion zum registrieren von Events |
a9d0b2
|
240 |
mögliche events: insert, update, delete |
b1a6a5
|
241 |
|
b5a2f8
|
242 |
*/ |
b1a6a5
|
243 |
|
MC |
244 |
function regEvent($event, $class_name, $function_name) { |
|
245 |
|
|
246 |
$this->events[] = array( event => $event, |
|
247 |
class_name => $class_name, |
|
248 |
function_name => $function_name); |
|
249 |
|
|
250 |
} |
b5a2f8
|
251 |
|
T |
252 |
} |
|
253 |
|
|
254 |
|
b1a6a5
|
255 |
?> |