redray
2008-12-01 436ed820192d05299a639b45ade96f3916bb51f2
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
41     var $obj;
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     = '&nbsp;';
50     
51     // interne Vars
52     var $_last_id;
53     
54     /*
55         Funktion zum laden des Baumes aus Array
56     */
57     
58     function loadFromArray ($nodes) {
59         
60         $this->obj[$this->root_id] = new node();
61         if(is_array($nodes)) {
62             foreach($nodes as $row) {
63
64                 $id = $row[$this->primary_field];
65                 $data = $row[$this->data_field];
66                 $ordner = $row[$this->parent_field];
67             
68                 //$this->raw_data[$id] = $row;
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             }
85         }
86     }
87     
88     function optionlist($nroot = '')
89     {
90         
91         if($nroot == '') $nroot = $this->obj[$this->root_id];
92         $opt_spacer = $this->opt_spacer;
93         
94         $this->ptree($nroot,'',$optionlist, $opt_spacer); 
95         
96         if(is_array($optionlist)){
97             return $optionlist;
98         } else {
99             return false;
100         }
101     }
102     
103     function ptree($myobj, $ebene, &$optionlist, $opt_spacer){
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                 }
113             }
114         }
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     /*
131         Löschen von Einträgen ohne Child's
132     */
133     
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     /*
145         Rekursives löschen von Einträgen
146     */
147     
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     /*
154         Hilfsfunktion für deltree
155     */
156     
157     function _deltree_recurse($myobj,$tree_id,$delete) {
158         if(is_array($myobj->childs)) {
159             foreach($myobj->childs as $val) {
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     /*
188         private Funktion zum aufrufen der eventHandler
189     */
190     
191     function _callEvent($event, $myobj, $myobj_old = '') {
192         global $app;
193         if(is_array($this->events)) {
194             foreach($this->events as $val) {
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                 }
205             }
206         }
207     }
208     
209     /*
210         Funktion zum Verschieben von Einträgen
211     */
212     
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     /*
227         Funktion zum updaten der Daten eines Nodes
228     */
229     
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     /*
239         Funktion zum registrieren von Events
240         mögliche events: insert, update, delete
241         
242     */
243     
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     }
251
252 }
253
254
255 ?>