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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486 |
<?php
/*
* Created on 2008 Apr 12
* by Martin Wernstahl <m4rw3r@gmail.com>
*/
/*
* Copyright (c) 2008, Martin Wernstahl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of Martin Wernstahl may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Martin Wernstahl ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Martin Wernstahl BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @addtogroup IgnitedRecord
* @{
*/
/**
* The tree behaviour for the IgnitedRecord class.
*
* Provides the IgnitedRecord class with methods for handling trees, and adds a child helper.
*
* @author Martin Wernstahl <m4rw3r@gmail.com>
* @par Copyright
* Copyright (c) 2008, Martin Wernstahl <m4rw3r@gmail.com>
*/
class IgnitedRecord_tree{
function IgnitedRecord_tree(&$ORM, $opts){
// set opts
$opts['table'] = $ORM->__table;
$opts['left'] = isset($opts['left']) ? $opts['left'] : 'lft';
$opts['right'] = isset($opts['right']) ? $opts['right'] : 'rgt';
$opts['id'] = isset($opts['id']) ? $opts['id'] : 'id';
$opts['title'] = isset($opts['title']) ? $opts['title'] : 'title';
// save reference to IgnitedRecord class
$this->ORM =& $ORM;
$this->opts = $opts;
// load mpttree
require_once APPPATH.'/models/mpttree.php';
$this->mpttree = new MPTtree();
$this->mpttree->set_opts($opts);
// add child helper
$ORM->__child_class_helpers['tree'] = 'IgnitedRecord_tree_helper';
// hooks
$ORM->add_hook('post_delete',array(&$this,'_post_delete'));
}
/**
* Returns the node at the end of the path $path.
*
* @param $path The path to the requested node, this/root node not included
* @param $separator The separator in the path, not needed if input is array, default: '/'
*
* @return An IgnitedRecord_record if node was found, false otherwise
*/
function &xpath($path,$separator = '/'){
$data = $this->mpttree->xpath($path,$separator);
if($data == false)
return $data;
$obj =& $this->ORM->_dbobj2ORM($data);
return $obj;
}
/**
* Runs remove gaps on the tree after delete, promotes children.
*/
function _post_delete(){
// remove_gaps() doesn't autoaquire a lock, so acquire a lock
$this->mpttree->lock_tree_table();
$this->mpttree->remove_gaps();
$this->mpttree->unlock_tree_table();
}
}
/**
* Child class helper for the IgnitedRecord_tree behaviour.
*/
class IgnitedRecord_tree_helper{
/**
* The IgnitedRecord instance.
*/
var $orm;
/**
* The record associated with this helper.
*/
var $record;
/**
* The options supplied to IgnitedRecord_tree (and MPTtree).
*/
var $opts;
/**
* The MPTtree instance.
*/
var $mpttree;
/**
* Cache for children for this node.
*/
var $children;
/**
* Cache for parent of this node.
*/
var $parent;
/**
* Constructor
*/
function IgnitedRecord_tree_helper(&$record){
$this->record =& $record;
$this->orm =& $this->record->__instance;
$this->opts =& $record->__instance->tree->opts;
$this->mpttree =& $this->orm->tree->mpttree;
}
/**
* Returns true if this node is the root node of a tree.
*
* @return bool
*/
function is_root(){
return ($this->record->{$this->opts['left']} == 1);
}
/**
* Checks if this node is part of a tree.
*
* The criteria for a node which is part of a tree is:
* The node must be saved in the tree (the record's __id property is set)
* The left and right column values must be set
*/
function is_orphan(){
return ($this->record->in_db() || !(isset($this->record->{$this->opts['left']}) && $this->record->{$this->opts['left']} != null && isset($this->record->{$this->opts['right']}) && $this->record->{$this->opts['right']} != null));
}
/**
* Returns the root node.
*
* @return an IgnitedRecord_object, or false if no root exists
*/
function &root(){
static $root;
if(isset($root) && $root != false)
return $root;
$root = $this->orm->find_by($this->opts['left'],1);
return $root;
}
/**
* Returns the parent of the current node.
*
* @return An IgnitedRecord_record, or false if no parent is found
*/
function &parent(){
if(isset($this->parent))
return $this->parent;
if($this->is_orphan()){
$false = false;
return $false;
}
$this->orm->db->order_by($this->lft_col, 'desc');
$this->parent =& $this->orm->find_by(array($this->opts['left'].' <',$this->opts['right'].' >'),array($this->record->{$this->opts['left']},$this->record->{$this->opts['right']}));
return $this->parent;
}
/**
* Returns all children of this node.
*
* @return An array containing IgnitedRecord_records
*/
function &children(){
if(isset($this->children))
return $this->children;
$ret = array();
if($this->is_orphan())
return array();
foreach($this->mpttree->get_children($this->record->{$this->opts['left']},$this->record->{$this->opts['right']}) as $child){
$ref =& $this->orm->_dbobj2ORM($child);
$ref->tree->parent =& $this;
$ret[] =& $ref;
}
$this->children =& $ret;
return $this->children;
}
/**
* Returns true if this node has children.
*
* @return bool
*/
function has_children(){
return ($this->is_orphan() && ($this->record->{$this->opts['right']} - $this->record->{$this->opts['left']} > 1));
}
/**
* Returns the number of childrens that this node has.
*
* @return int
*/
function count_children(){
// If this is an orphan, return 0
if($this->is_orphan())
return 0;
// if we already have the children loaded, count that array
if(isset($this->children))
return count($this->children);
// call MPTtree
return $this->mpttree->count_children($this->record->{$this->opts['left']},$this->record->{$this->opts['right']});
}
/**
* Returns the number of descendants this node has.
*
* If this node is an orphan, 0 is returned
*/
function count_descendants(){
// If this is an orphan, return 0
if($this->is_orphan())
return 0;
return $this->mpttree->count_descendants($this->record->{$this->opts['left']},$this->record->{$this->opts['right']});
}
/**
* Returns the path to the current node.
*
* The segments consists of the title column specified in the MPTtree instance used by this IgnitedRecord_record node.
* If $true_path is set to false, the path can be used by xpath() directly.
*
* @param $true_path Whether to include the root node in the path, default: false
*
* @return An array with the path to the current node (including or excluding root node, depending on $true_path), false if node is an orphan
*/
function path($true_path = false){
// if this is an orphan, return false
if($this->is_orphan())
return false;
$path = array($this->record->{$this->opts['title']});
$parent =& $this->parent();
while($parent != false){
$path[] = $parent->record->{$this->opts['title']};
$parent =& $parent->tree->parent();
}
if(!$true_path){
array_pop($path);
}
return array_reverse($path);
}
/**
* Returns the node at the end of the path $path.
*
* If this node is an orphan, relative paths will not work (defaults to full paths instead)
*
* @param $path The path to the requested node, this/root node not included
* @param $separator The separator in the path, not needed if input is array, default: '/'
* @param $relative If the path should be relative, default: true
*
* @return An IgnitedRecord_record if node was found, false otherwise
*/
function &xpath($path, $separator = '/', $relative = true){
if($relative && !$this->is_orphan())
$data = $this->mpttree->xpath($path,$separator,$this->record->{$this->opts['left']});
else
$data = $this->mpttree->xpath($path,$separator);
if($data == false)
return $data;
$obj =& $this->orm->_dbobj2ORM($data);
return $obj;
}
/////////////////////////////////////
// Insert methods
/////////////////////////////////////
/**
* Inserts this node as a root node, if the tree has none and this node is not part of any tree.
*
* @return True if inserted, false if fail (if root exists or if this node is already part of a tree)
*/
function insert_as_root(){
if(!$this->is_orphan() || $this->root())
return false;
$this->mpttree->insert_root($this->orm->_strip_data($this->record));
// grab id
$Ci =& get_instance();
$this->record->__id = $Ci->db->insert_id();
// set defaults
$this->record->{$this->opts['left']} = 1;
$this->record->{$this->opts['right']} = 2;
// save in cache
$this->root[$this->mpttree->tree_table] =& $this;
return true;
}
/**
* Inserts this node as a sibling above the node $node.
*
* Will not insert node if the node is already part of a tree.
*
* @param $node The sibling
* @return True if success, false otherwise
* @todo Update the parent node, so it's children gets updated
*/
function insert_above(&$node){
if($node->tree->is_root() || !$this->is_orphan() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->instance->insert_node_before($node->{$this->opts['left']},$this->orm->_strip_data($this->record))){
list($this->record->{$this->opts['left']},$this->record->{$this->opts['right']},$this->record->__id) = $ret;
$this->parent =& $node->tree->parent();
return true;
}
return false;
}
/**
* Inserts this node as a sibling below the node $node.
*
* Will not insert node if the node is already part of a tree.
*
* @param $node The sibling
* @return True if successful, false otherwise
*
* @todo Update the parent node, so it's children gets updated
*/
function insert_below(&$node){
if($node->tree->is_root() || !$this->is_orphan() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->instance->insert_node_after($node->{$this->opts['left']},$this->orm->_strip_data($this->record))){
list($this->record->{$this->opts['left']},$this->record->{$this->opts['right']},$this->record->__id) = $ret;
$this->parent =& $node->tree->parent();
return true;
}
return false;
}
/**
* Inserts this node as the first child of the node $node.
*
* Will not insert node if the node is already part of a tree.
*
* @param $node The node to be parent
*
* @return True if success, false otherwise
*/
function insert_as_first_child_of(&$node){
if(!$this->is_orphan() ||
$node->tree->is_orphan() ||
$node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->instance->append_node($node->{$this->opts['left']},$this->orm->_strip_data($this->record))){
list($this->record->{$this->opts['left']},$this->record->{$this->opts['right']},$this->record->__id) = $ret;
$this->parent =& $node;
if($this->parent->tree->children != null) // check if parent already has data
$this->array_unshift_ref($this->parent->tree->children,$this);
return true;
}
return false;
}
/**
* Inserts this node as the last child of the node $node.
*
* Will not insert node if the node is already part of a tree.
*
* @param $node The node to be parent
*
* @return True if success, false otherwise
*/
function insert_as_last_child_of(&$node){
if(!$this->is_orphan() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->instance->append_node_last($node->{$this->opts['left']},$this->orm->_strip_data($this->record))){
list($this->record->{$this->opts['left']},$this->record->{$this->opts['right']},$this->id) = $ret;
$this->parent =& $node;
if($this->parent->tree->children != null) // check if parent already has data
$this->parent->tree->children[] =& $this;
return true;
}
return false;
}
/////////////////////////////////////
// Move methods
/////////////////////////////////////
/**
* Moves this node to the position of sibling above the node $node.
*
* @param $node The sibling node
*
* @return false if insert failed, true if success
*
* @todo Update the parent node, so it's children gets updated
*/
function move_above(&$node){
if($this->is_orphan())
return $this->insert_above($node);
if($node->tree->is_orphan() || $node->tree->is_root() || $this->is_root() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->mpttree->move_node_before($this->record->{$this->opts['left']},$node->{$this->opts['left']})){
list($this->record->{$this->opts['left']} , $this->record->{$this->opts['right']}) = $ret;
$this->parent =& $node->tree->parent();
return true;
}
return false;
}
/**
* Moves this node to the position of sibling below the node $node.
*
* @param $node The sibling node
*
* @return false if insert failed, true if success
*
* @todo Update the parent node, so it's children gets updated
*/
function move_below(&$node){
if($this->is_orphan())
return $this->insert_below($node);
if($node->tree->is_root() || $this->is_root() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->mpttree->move_node_after($this->record->{$this->opts['left']},$node->{$this->opts['left']})){
list($this->record->{$this->opts['left']} , $this->record->{$this->opts['right']}) = $ret;
$this->parent =& $node->tree->parent();
return true;
}
return false;
}
/**
* Moves this node to the position of first child of the node $node.
*
* @param $node The node to be parent
*
* @return false if insert failed, true if success
*/
function move_to_first_child_of(&$node){
if($this->is_orphan())
return $this->insert_as_first_child_of($node);
if($this->is_root() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->mpttree->move_node_append($this->record->{$this->opts['left']},$node->{$this->opts['left']})){
list($this->record->{$this->opts['left']} , $this->record->{$this->opts['right']}) = $ret;
$this->parent =& $node;
if($this->parent->tree->children != null) // check if parent already has data
$this->array_unshift_ref($this->parent->tree->children,$this);
return true;
}
return false;
}
/**
* Moves this node to the position of last child of the node $node.
*
* @param $node The node to be parent
*
* @return false if insert failed, true if success
*/
function move_to_last_child_of(&$node){
if($this->record->__id == null)
return $this->insert_as_last_child_of($node);
if($this->is_root() || $node->tree->is_orphan() || $node->tree->mpttree->tree_table != $this->mpttree->tree_table)
return false;
if($ret = $this->mpttree->move_node_append_last($this->record->{$this->opts['left']},$node->{$this->opts['left']})){
list($this->record->{$this->opts['left']} , $this->record->{$this->opts['right']}) = $ret;
$this->parent =& $node;
if($this->parent->tree->children != null) // check if parent already has data
$this->parent->tree->children[] =& $this;
return true;
}
return false;
}
/**
* Unshifts an array with a reference
*
* Prepend a reference to an element to the beginning of an array. Renumbers numeric keys, so $value is always inserted to $array[0]
*
* @param $array array
* @param $value mixed
*
* @return an int from array_unshift()
*/
function array_unshift_ref(&$array, &$value){
$return = array_unshift($array,'');
$array[0] =& $value;
return $return;
}
}
/**
* @}
*/
?> |