16 |
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> | |
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> | |
17 |
// +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
18 |
// |
// |
|
// Id: Array.php,v 1.5 2003/01/18 15:36:22 cain Exp |
|
19 |
// $Id$ |
// $Id$ |
20 |
|
|
21 |
require_once('Tree/Error.php'); |
require_once('Tree/Error.php'); |
65 |
function setup() |
function setup() |
66 |
{ |
{ |
67 |
unset($this->data); // unset the data to be sure to get the real data again, no old data |
unset($this->data); // unset the data to be sure to get the real data again, no old data |
68 |
if( is_array($this->_array) ) |
if (is_array($this->_array)) { |
|
{ |
|
69 |
$this->data[0] = null; |
$this->data[0] = null; |
70 |
$theData = array(&$this->_array); |
$theData = array(&$this->_array); |
71 |
$this->_setup($theData); |
$this->_setup($theData); |
72 |
} |
} |
73 |
|
|
74 |
return $this->data; |
/*foreach($this->data as $val){print "\r\n"; |
75 |
|
foreach ($val as $k=>$v) |
76 |
|
print "$k=>$v\r\n"; |
77 |
|
}*/ |
78 |
|
return $this->data; |
79 |
} |
} |
80 |
|
|
81 |
/** |
/** |
82 |
* we modify the $this->_array in here, we also add the id |
* we modify the $this->_array in here, we also add the id |
83 |
* so methods like 'add' etc can find the elements they are searching for, |
* so methods like 'add' etc can find the elements they are searching for, |
85 |
*/ |
*/ |
86 |
function _setup( &$array , $parentId=0 ) |
function _setup( &$array , $parentId=0 ) |
87 |
{ |
{ |
88 |
foreach( $array as $nodeKey=>$aNode ) |
foreach ($array as $nodeKey=>$aNode) { |
|
{ |
|
89 |
$newData = $aNode; |
$newData = $aNode; |
90 |
if ( !isset($newData['id']) || !$newData['id'] ) { |
if (!isset($newData['id']) || !$newData['id']) { // if the current element has no id, we generate one |
91 |
$newData['id'] = $this->_id++; |
$newData['id'] = $this->_id++; // build a unique numeric id |
92 |
$array[$nodeKey]['id'] = $newData['id']; |
$array[$nodeKey]['id'] = $newData['id']; // set the id |
93 |
|
} else { |
94 |
|
$idAsInt = (int)$newData['id']; |
95 |
|
if ($idAsInt > $this->_id) { |
96 |
|
$this->_id = $idAsInt; |
97 |
|
} |
98 |
} |
} |
99 |
|
//print "a node name=".$aNode['name'].'<br>'; |
100 |
$newData['parentId'] = $parentId; |
$newData['parentId'] = $parentId; // set the parent-id, since we only have a 'children' array |
101 |
$children = null; |
$children = null; |
102 |
foreach ( $newData as $key=>$val ) // remove the 'children' array, since this is only info for this class |
foreach ( $newData as $key=>$val ) { // remove the 'children' array, since this is only info for this class |
103 |
{ |
if ($key=='children') { |
|
if( $key=='children' ) |
|
|
{ |
|
104 |
unset($newData[$key]); |
unset($newData[$key]); |
105 |
} |
} |
106 |
} |
} |
107 |
|
|
108 |
$this->data[] = $newData; |
$this->data[$newData['id']] = $newData; |
109 |
if ( isset($aNode['children']) && $aNode['children'] ) { |
if (isset($aNode['children']) && $aNode['children']) { |
110 |
if( !isset($array[$nodeKey]['children']) ) { |
if (!isset($array[$nodeKey]['children'])) { |
111 |
$array[$nodeKey]['children'] = array(); |
$array[$nodeKey]['children'] = array(); |
112 |
} |
} |
113 |
$this->_setup( $array[$nodeKey]['children'] , $newData['id'] ); |
$this->_setup( $array[$nodeKey]['children'] , $newData['id'] ); |
117 |
|
|
118 |
|
|
119 |
/** |
/** |
120 |
|
* this is mostly used by switchDataSource |
121 |
|
* this method put data gotten from getNode() in the $this->_array |
122 |
|
* |
123 |
|
*/ |
124 |
|
function setData($data) |
125 |
|
{ |
126 |
|
/* |
127 |
|
$root = array_shift($data); |
128 |
|
unset($root['children']); |
129 |
|
$this->_array = array('children'=> array($root)); |
130 |
|
foreach ($this->_array['children'][0] as $key=>$val) |
131 |
|
print "$key=>$val<br>"; |
132 |
|
print "<br>"; |
133 |
|
*/ |
134 |
|
$unsetKeys = array('childId','left','right'); |
135 |
|
|
136 |
|
foreach ( $data as $aNode ) { |
137 |
|
//print $aNode['id'].' : '.$aNode['name'].' parentId='.$aNode['parentId'].' size='.sizeof($this->_array['children'][0]['children']).'<br>'; |
138 |
|
foreach ($aNode as $key=>$val) { |
139 |
|
if (is_array($val) || in_array($key,$unsetKeys)) { |
140 |
|
unset($aNode[$key]); |
141 |
|
} |
142 |
|
} |
143 |
|
$this->add($aNode,$aNode['parentId']); |
144 |
|
} |
145 |
|
//foreach ($this->_array['children'][0]['children'] as $x){print "<br>"; |
146 |
|
//foreach ($x as $key=>$val) |
147 |
|
// print "$key=>$val<br>";} |
148 |
|
$this->_array = $this->_array['children'][0]; |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
* |
* |
153 |
* |
* |
154 |
* @access private |
* @access private |
201 |
} |
} |
202 |
return $result; |
return $result; |
203 |
} |
} |
204 |
|
|
205 |
/** |
/** |
206 |
* |
* add a new item to the tree |
207 |
|
* what is tricky here, we also need to add it to the source array |
208 |
|
* |
209 |
|
* @param array the data for the new node |
210 |
|
* @param int the ID of the parent node |
211 |
|
* @param int the ID of the previous node |
212 |
*/ |
*/ |
213 |
function add( $data , $parentId , $previousId=0 ) |
function add( $data , $parentId , $previousId=null ) |
214 |
{ |
{ |
215 |
$data['id'] = $this->_id++; |
if (!isset($data['id'])) { |
216 |
|
$data['id'] = ++$this->_id; |
217 |
|
} elseif((int)$data['id'] > $this->_id) { |
218 |
|
// update the $this->_id if the data['id'] has a higher number, since |
219 |
|
// we dont want to overwrite anything. just in case |
220 |
|
$this->_id = (int)$data['id']; |
221 |
|
} |
222 |
$data['parentId'] = $parentId; |
$data['parentId'] = $parentId; |
223 |
$this->data[] = $data; |
$this->data[$data['id']] = $data; |
224 |
|
|
|
// add the element itself also in the source array, where we actually read the structure from |
|
225 |
//$path = $this->getPathById($parentId); |
//$path = $this->getPathById($parentId); |
226 |
array_walk($this->_array['children'],array(&$this,'_add'),array($data,$parentId)); |
if (!isset($this->_array['children'])) { // there might not be a root element yet |
227 |
|
$data['parentId'] = 0; |
228 |
|
$this->_array['children'][] = $data; |
229 |
|
} else { |
230 |
|
array_walk($this->_array['children'],array(&$this,'_add'),array($data,$parentId,$previousId)); |
231 |
|
} |
232 |
|
|
233 |
//$this->_array |
//$this->_array |
234 |
return $data['id']; |
return $data['id']; |
235 |
} |
} |
236 |
|
|
237 |
// this one was a real quicky !!! |
/** |
238 |
|
* we need to add the node to the source array |
239 |
|
* for this we have this private method which loops through |
240 |
|
* the source array and adds it in the right place |
241 |
|
* |
242 |
|
* @param mixed the value of the array, as a reference, so we work right on the source |
243 |
|
* @param mixed the key of the node |
244 |
|
* @param array an array which contains the following |
245 |
|
* new data, |
246 |
|
* parent ID under which to add the node, |
247 |
|
* the prvious ID |
248 |
|
*/ |
249 |
function _add( &$val , $key , $data ) |
function _add( &$val , $key , $data ) |
250 |
{ |
{ |
251 |
if( $val['id']==$data[1] ) |
if ($val['id']==$data[1]) { // is the id of the current elment ($val) == to the parentId ($data[1]) |
252 |
$val['children'][] = $data[0]; |
if (isset($data[2]) && $data[2]===0 ) { |
253 |
else // if we havent found the new element go on searching |
// if the previousId is 0 means, add it as the first member |
254 |
{ |
$val['children'] = array_merge(array($data[0]),$val['children']); |
255 |
if( $val['children'] ) |
} else { |
256 |
|
$val['children'][] = $data[0]; |
257 |
|
} |
258 |
|
} else { // if we havent found the new element go on searching |
259 |
|
if (isset($val['children'])) { |
260 |
array_walk($val['children'],array(&$this,'_add'),$data); |
array_walk($val['children'],array(&$this,'_add'),$data); |
261 |
|
} |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
|
* update an entry with the given id and set the data as given in the array $data |
267 |
|
* |
268 |
|
* @param int the id of the element that shall be updated |
269 |
|
* @param array the data, [key]=>[value] |
270 |
|
* @return void |
271 |
|
*/ |
272 |
|
function update($id,$data) |
273 |
|
{ |
274 |
|
if ($this->_array['id']==$id) { |
275 |
|
foreach ($data as $key=>$newVal) { |
276 |
|
$this->_array[$key] = $newVal; |
277 |
|
} |
278 |
|
} else { |
279 |
|
array_walk($this->_array['children'],array(&$this,'_update'),array($id,$data)); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
|
* update the element with the given id |
285 |
|
* |
286 |
|
* @param array a reference to an element inside $this->_array |
287 |
|
* has to be a reference, so we can really modify the actual data |
288 |
|
* @param int not in use, but array_walk passes this param |
289 |
|
* @param array [0] is the id we are searching for |
290 |
|
* [1] are the new data we shall set |
291 |
|
* @return void |
292 |
|
*/ |
293 |
|
function _update( &$val , $key , $data ) |
294 |
|
{ |
295 |
|
//print $val['id'].'=='.$data[0].'<br>'; |
296 |
|
if ($val['id']==$data[0]) { // is the id of the current elment ($val) == to the parentId ($data[1]) |
297 |
|
foreach ($data[1] as $key=>$newVal) { |
298 |
|
//print "set ".$val['name']." $key = $newVal<br>"; |
299 |
|
$val[$key] = $newVal; |
300 |
|
} |
301 |
|
} else { // if we havent found the new element go on searching in the children |
302 |
|
if (isset($val['children'])) { |
303 |
|
array_walk($val['children'],array(&$this,'_update'),$data); |
304 |
|
} |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
|
* remove an element from the tree |
310 |
|
* this removes all the children too |
311 |
|
* |
312 |
|
* @param int the id of the element to be removed |
313 |
|
*/ |
314 |
|
function remove($id) |
315 |
|
{ |
316 |
|
if ($this->data[$id]) { // we only need to search for element that do exist :-) otherwise we save some processing time |
317 |
|
$this->_remove($this->_array,$id); |
318 |
|
} |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* remove the element with the given id |
323 |
|
* this will definitely remove all the children too |
324 |
|
* |
325 |
|
* @param array a reference to an element inside $this->_array |
326 |
|
* has to be a reference, so we can really modify the actual data |
327 |
|
* @param int the id of the element to be removed |
328 |
|
* @return void |
329 |
|
*/ |
330 |
|
function _remove( &$val , $id ) |
331 |
|
{ |
332 |
|
if (isset($val['children'])) { |
333 |
|
foreach ($val['children'] as $key=>$aVal) { |
334 |
|
//print $aVal['id'].'=='.$id."\r\n"; |
335 |
|
if ($aVal['id']==$id) { |
336 |
|
//print "remove ".$aVal['name']."\r\n"; |
337 |
|
if (sizeof($val['children'])<2) { |
338 |
|
unset($val['children']); |
339 |
|
} else { |
340 |
|
unset($val['children'][$key]); |
341 |
|
} |
342 |
|
} else { |
343 |
|
$this->_remove($val['children'][$key],$id); |
344 |
|
} |
345 |
|
} |
346 |
} |
} |
347 |
} |
} |
348 |
|
|