| 1 |
<?php |
| 2 |
// |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | PHP Version 4 | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Copyright (c) 1997-2003 The PHP Group | |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | This source file is subject to version 2.02 of the PHP license, | |
| 9 |
// | that is bundled with this package in the file LICENSE, and is | |
| 10 |
// | available at through the world-wide-web at | |
| 11 |
// | http://www.php.net/license/2_02.txt. | |
| 12 |
// | If you did not receive a copy of the PHP license and are unable to | |
| 13 |
// | obtain it through the world-wide-web, please send a note to | |
| 14 |
// | license@php.net so we can mail you a copy immediately. | |
| 15 |
// +----------------------------------------------------------------------+ |
| 16 |
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> | |
| 17 |
// +----------------------------------------------------------------------+ |
| 18 |
// |
| 19 |
// $Id: Array.php,v 1.9 2003/03/17 16:02:54 cain Exp $ |
| 20 |
|
| 21 |
require_once('Tree/Error.php'); |
| 22 |
|
| 23 |
/** |
| 24 |
* EXPERIMENTAL |
| 25 |
* |
| 26 |
* @access public |
| 27 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 28 |
* @version 2002/08/30 |
| 29 |
* @package Tree |
| 30 |
*/ |
| 31 |
class Tree_Memory_Array |
| 32 |
{ |
| 33 |
|
| 34 |
var $data = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* this is the internal id that will be assigned if no id is given |
| 38 |
* it simply counts from 1, so we can check if( $id ) i am lazy :-) |
| 39 |
*/ |
| 40 |
var $_id = 1; |
| 41 |
|
| 42 |
/** |
| 43 |
* set up this object |
| 44 |
* |
| 45 |
* @version 2002/08/30 |
| 46 |
* @access public |
| 47 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 48 |
* @param string $dsn the path on the filesystem |
| 49 |
* @param array $options additional options you can set |
| 50 |
*/ |
| 51 |
function Tree_Memory_Array( &$array , $options=array() ) |
| 52 |
{ |
| 53 |
$this->_array = &$array; |
| 54 |
$this->_options = $options; // not in use currently |
| 55 |
} // end of function |
| 56 |
|
| 57 |
/** |
| 58 |
* |
| 59 |
* |
| 60 |
* @version 2002/08/30 |
| 61 |
* @access public |
| 62 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 63 |
* @return boolean true on success |
| 64 |
*/ |
| 65 |
function setup() |
| 66 |
{ |
| 67 |
unset($this->data); // unset the data to be sure to get the real data again, no old data |
| 68 |
if (is_array($this->_array)) { |
| 69 |
$this->data[0] = null; |
| 70 |
$theData = array(&$this->_array); |
| 71 |
$this->_setup($theData); |
| 72 |
} |
| 73 |
|
| 74 |
/*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 |
| 83 |
* so methods like 'add' etc can find the elements they are searching for, |
| 84 |
* if you dont like your data to be modified dont pass them as reference! |
| 85 |
*/ |
| 86 |
function _setup( &$array , $parentId=0 ) |
| 87 |
{ |
| 88 |
foreach ($array as $nodeKey=>$aNode) { |
| 89 |
$newData = $aNode; |
| 90 |
if (!isset($newData['id']) || !$newData['id']) { // if the current element has no id, we generate one |
| 91 |
$newData['id'] = $this->_id++; // build a unique numeric id |
| 92 |
$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; // set the parent-id, since we only have a 'children' array |
| 101 |
$children = null; |
| 102 |
foreach ( $newData as $key=>$val ) { // remove the 'children' array, since this is only info for this class |
| 103 |
if ($key=='children') { |
| 104 |
unset($newData[$key]); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$this->data[$newData['id']] = $newData; |
| 109 |
if (isset($aNode['children']) && $aNode['children']) { |
| 110 |
if (!isset($array[$nodeKey]['children'])) { |
| 111 |
$array[$nodeKey]['children'] = array(); |
| 112 |
} |
| 113 |
$this->_setup( $array[$nodeKey]['children'] , $newData['id'] ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 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 |
| 155 |
* @version 2002/03/02 |
| 156 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 157 |
* @param |
| 158 |
* @return |
| 159 |
*/ |
| 160 |
function _throwError( $msg , $line , $mode=null ) |
| 161 |
{ |
| 162 |
return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->db->last_query ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* prepare multiple results |
| 167 |
* |
| 168 |
* @see _prepareResult() |
| 169 |
* @access private |
| 170 |
* @version 2002/03/03 |
| 171 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 172 |
* @param |
| 173 |
* @return |
| 174 |
*/ |
| 175 |
function _prepareResults( $results ) |
| 176 |
{ |
| 177 |
$newResults = array(); |
| 178 |
foreach( $results as $aResult ) |
| 179 |
$newResults[] = $this->_prepareResult($aResult); |
| 180 |
return $newResults; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* map back the index names to get what is expected |
| 185 |
* |
| 186 |
* @access private |
| 187 |
* @version 2002/03/03 |
| 188 |
* @author Wolfram Kriesing <wolfram@kriesing.de> |
| 189 |
* @param |
| 190 |
* @return |
| 191 |
*/ |
| 192 |
function _prepareResult( $result ) |
| 193 |
{ |
| 194 |
$map = $this->getOption('columnNameMaps'); |
| 195 |
|
| 196 |
if( $map ) |
| 197 |
foreach( $map as $key=>$columnName ) |
| 198 |
{ |
| 199 |
$result[$key] = $result[$columnName]; |
| 200 |
unset($result[$columnName]); |
| 201 |
} |
| 202 |
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=null ) |
| 214 |
{ |
| 215 |
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; |
| 223 |
$this->data[$data['id']] = $data; |
| 224 |
|
| 225 |
//$path = $this->getPathById($parentId); |
| 226 |
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 |
| 234 |
return $data['id']; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 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 ) |
| 250 |
{ |
| 251 |
if ($val['id']==$data[1]) { // is the id of the current elment ($val) == to the parentId ($data[1]) |
| 252 |
if (isset($data[2]) && $data[2]===0 ) { |
| 253 |
// if the previousId is 0 means, add it as the first member |
| 254 |
$val['children'] = array_merge(array($data[0]),$val['children']); |
| 255 |
} 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); |
| 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 |
|
| 349 |
} // end of class |
| 350 |
?> |