summaryrefslogtreecommitdiff
path: root/modules/mptt/libraries/MPTT.php
blob: 84182a90f1e301a51981f1de25d83a15210f30a0 (plain)
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
<?php

/**
* File: libraries/MPTT.php
*
* An implementation of Joe Celko's Nested Sets as a Kohana model with ORM support.
*
*         
* Many thanks to 
*   * Thunder who supplied a similar class for Code Igniter and gave permission to me
*         to release it under whatever license I deem necessary:
*         http://www.codeigniter.com/wiki/Nested_Sets/
*   * mpatek, his class was the initial start
*         http://bakery.cakephp.org/articles/view/modified-preorder-tree-traversal-component
*   * Propel, for inspiring some methods and the parent_id and scope stuff
* 
* MPTT class
*   author    - dlib
*   license   - BSD 
*/

class MPTT extends ORM {
	
	public $children;
	
	public $parent;
	
	protected $left_column     =    'lft';
	
	protected $right_column    =    'rgt';
	
	protected $parent_column   =    'parent_id';
	
	protected $scope_column    =    'scope';
	
	protected $_scope           =    1;
	
	/**
	* Patch through to ORM construct
	*
	* @return void
	*/
	public function __construct($id = FALSE)
	{
		parent::__construct($id);
		
		if (!empty($id))
		{
			//Set the right scope on new objects
			$scope_column=$this->scope_column;
			$this->set_scope($this->$scope_column);
			$this->where='';
		}
		
		
	}

	//////////////////////////////////////////
	//  Lock functions
	//////////////////////////////////////////

	/**
	 * Locks tree table
	 * This is a straight write lock - the database blocks until the previous lock is released
	 */
	protected function lock_tree($aliases = array())
	{
		$sql = "LOCK TABLE " . $this->table . " WRITE";
		return self::$db->query($sql);
	}

	/**
	 * Unlocks tree table
	 * Releases previous lock
	 */
	protected function unlock_tree()
	{
		$sql = "UNLOCK TABLES";
		return self::$db->query($sql);
	}

	/**
	* Bit of an odd function since the node is supplied
	* supply a node object and it will return a whole bunch of descendants
	* @return tree object
	* @param $root_node mixed
	
	*/
	public function get_tree($root_node)
	{
		
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		
		if(is_object($root_node))
		{
			$root=$root_node;
		}   
		else
		{
			$root=$this->get_node($root_node);
		}    
		
		$children=$this->new_node();
		$children->where('`'.$lft_col.'`>'.$root->$lft_col.' AND `'.$rgt_col.'`<'.$root->$rgt_col);
		$children->where($this->scope_column,$this->get_scope());         
		$children->orderby($lft_col,'ASC');
		$children=$children->find_all();
		
		if(count($children)>0)
		{
			
			$parent=$root;
			$parent->children=array();

			foreach($children as $child_data)
			{
				
				$child=$child_data;
				$child->children=array();
				$child->parent=$parent;
				$child->scope=$this->get_scope();
				
					while(!$this->_is_descendant_of($child, $parent)) 
					{
						$parent = $parent->parent;
					}      
					
				$parent->children[]=$child;
				$parent=$child;
				
			}
		}
		
	return $root;
	}
	/*
	* *****************
	* Retrieval methods
	* *****************
	*/   
	/**
	* Current object will obtain its children
	* @see http://trac.symfony-project.com/wiki/sfPropelActAsNestedSetBehaviorPlugin
	* @see http://www.phpriot.com/articles/nested-trees-2/5
	* @return 
	*/
	public function get_children($return=false)
	{
		if(!$this->has_descendants())
			return false;
			
		$parent_id=$this->id;
		
		$this->children=array();
		$children=$this->new_node();
		$children->where($this->parent_column,$parent_id);
		$children->where($this->scope_column,$this->get_scope());   
		$children->orderby($this->left_column);
		
		foreach($children->find_all() as $child)
		{
			$child->parent=$this;
			
			$this->children[]=$child;
		}
		if($return== true)
		{
			return $this->children;
		}
	return $this;
	}
	
	/**
	* The current object will obtain all descendants
	* @return ORM object
	* @param $node_id Object
	*/
	public function get_descendants()
	{
		if($this->has_descendants())
			return $this->get_tree($this);
	}
	/**
	* Return an array of all leaves in the entire tree
	* 
	* @return array of ORM objects
	*/
	public function get_leaves()
	{
		
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
				
		$leaves=$this->new_node();
		$leaves->where($this->scope_column,$this->get_scope());          
		$leaves->where($rgt_col.' = '.$lft_col . ' + 1');
		
		return $leaves->find_all();
	}
	/**
	* Get the path leading up to the current node
	* @return array with ORM objects
	* 
	*/
	public function get_path()
	{

		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;

		$path=$this->new_node();
		$path->where($this->scope_column,$this->get_scope());          
		$path->where($lft_col . ' <= '.$this->$lft_col . ' AND ' . $rgt_col . ' >=' .$this->$rgt_col . ' ORDER BY '.$lft_col); 
	

		return $path->find_all();        

	}
	
	/**
	* Returns the root node
	* @return array $resultNode The node returned
	*/
	function get_root()
	{   
		return $this->get_node_where('`'.$this->left_column . '` = 1 ');
	} 
	/**
	* Returns a node by id
	* @return object
	* @param $node_id integer [optional]
	*/
	function get_node($node_id)
	{
		$scope_column=$this->scope_column;
		$class = get_class($this);
		$node=new $class();
		
		$node=$node->find($node_id,true);
		
		return $node;
	}
	/**
	* Returns a new empty node
	* @return object
	* @param $node_id integer [optional]
	*/
	function new_node(){
		
		$class = get_class($this);
		$node=new $class();
		return $node;
	}
	/**
	* Returns one node with where condition
	* @return object
	* @param $node_id integer [optional]
	*/    
	function get_node_where($where)
	{
		$scope_column=$this->scope_column;
		$class = get_class($this);
		$node  = new $class();
		$node->where($where);
		$node->where($this->scope_column,$this->get_scope());
		
		$node=$node->find(false,false);
		return $node;
	}
	/**
	* Returns the first child node of the given parentNode
	* 
	* @return array $resultNode The first child of the parent node supplied
	*/
	function get_first_child()
	{ 
		$lft_col = $this->left_column;

		return $this->get_node_where($this->left_column . " = " . ($this->$lft_col+1));
	}
	
	/**
	* Returns the last child node of the given parentNode
	* 
	* @return array $resultNode the last child of the parent node supplied
	*/
	function get_last_child()
	{ 
		$rgt_col = $this->right_column;
		
		return $this->get_node_where($this->right_column . " = " . ($this->$rgt_col-1));
	}
	
	/**
	* Returns the node that is the immediately prior sibling of the given node
	* 
	* @return array $resultNode The node returned
	*/
	function get_prev_sibling()
	{ 
		$lft_col = $this->left_column;

	return $this->get_node_where($this->right_column . " = " . ($this->$lft_col-1));
	}
	
	/**
	* Returns the node that is the next sibling of the given node
	* 
	* @return array $resultNode The node returned
	*/
	function get_next_sibling()
	{ 
		$rgt_col = $this->right_column;

		return $this->get_node_where($this->left_column . " = " . ($this->$rgt_col+1));
	}    
	/**
	* Returns the node that represents the parent of the given node
	* 
	* @return array $resultNode the node returned
	*/
	function get_parent()
	{ 
	
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		
		$whereArg = "           $leftcol    < " . $this->$leftcol . 
					" AND       $rightcol   > " . $this->$rightcol . 
					" ORDER BY  $rightcol ASC";
		return $this->get_node_where($whereArg);
	}   
/*
	* *****************
	* Modifying methods
	* *****************
	*/
	/**
	* Adds the first entry to the table (only call once in an empty table) else corruption will follow
	* @return    $node an array of left and right values
	*/ 
	function make_root()
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		$scp_col=$this->scope_column;
		
		if(is_numeric($this->$lft_col) )
		{
			Log::add('error', 'Cannot make existing node root' );
			//existing nodes cannot become root
			return false;
		}

		$this->$lft_col=1;
		$this->$rgt_col=2;
		$this->$scp_col=$this->get_scope();
		
		$this->save_node();
		
		return $this;
	}
	/**
	* Not yet implemented
	* 
	*/    
	function insert_as_parent_of($child_node)
	{
		
	}
	/**
	* inserts a the object node as the first child of the supplied parent node
	* @param array $parentNode The node array of the parent to use
	* 
	* @return 
	*/
	function insert_as_first_child_of($parent_node)
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		$scp_col=$this->scope_column;        
		$parent_column=$this->parent_column;

		//Set parent id (id of the parent, is childs parent id)                  
		$this->$parent_column=$parent_node->id;
		
		$this->$lft_col=$parent_node->$lft_col+1;
		$this->$rgt_col=$parent_node->$lft_col+2;
		//Get scope from current object (obsolete)
		$this->$scp_col=$this->get_scope();
		
		$this->lock_tree();
		$this->modify_node($this->$lft_col,2);
		$this->save_node();
		$this->unlock_tree();
		
		return $this;
	}
	/**
	* Same as insertNewChild except the new node is added as the last child
	* @param array $parentNode The node array of the parent to use
	* 
	* @return 
	*/
	function insert_as_last_child_of($parent_node) 
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		$scp_col=$this->scope_column;              
		$parent_column=$this->parent_column;        
		
		//Set parent id (id of the parent, is childs parent id)          
		$this->$parent_column=$parent_node->id;
		
		$this->$lft_col=$parent_node->$rgt_col;
		$this->$rgt_col=$parent_node->$rgt_col+1;
		$this->$scp_col=$this->get_scope();                
		
		$this->lock_tree();		
		$this->modify_node($this->$lft_col,2);
		$this->save_node();
		$this->unlock_tree();
		
		return $this;
	}   
	/**
	* Adds a new node to the left of the supplied focusNode
	* @param array $focusNode The node to use as the position marker
	* 
	* @return  
	*/
	function insert_as_prev_sibling_of($focus_node)
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		$parent_column=$this->parent_column;        
		$scp_col=$this->scope_column;  
						
		//Set parent id (siblings have the same parent)        
		$this->$parent_column=$focus_node->$parent_column;
		
		$this->$lft_col=$focus_node->$lft_col;
		$this->$rgt_col=$focus_node->$lft_col+1; 
		$this->$scp_col=$this->get_scope();        
		
		$this->lock_tree();		
		$this->modify_node($this->$lft_col,2);
		$this->save_node();
		$this->unlock_tree();		
			
		return $this;
	}     
	/**
	* Adds a new node to the right of the supplied focusNode
	* @param array $focusNode The node to use as the position marker
	* 
	* @return 
	*/
	function insert_as_next_sibling_of($focus_node)
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		$parent_column=$this->parent_column;  
		$scp_col=$this->scope_column;                
				
		//Set parent id (siblings have the same parent)        
		$this->$parent_column=$focus_node->$parent_column;
		
		$this->$lft_col=$focus_node->$rgt_col+1;
		$this->$rgt_col=$focus_node->$rgt_col+2; 
		$this->$scp_col=$this->get_scope();           

		$this->lock_tree();		
		$this->modify_node($this->$lft_col,2);
		$this->save_node();
		$this->unlock_tree();		
			
		return $this;
	}    
	/**
	* Why not, kill the entire tree
	* 
	* @return 
	*/
	function delete_tree()
	{
		$where=array($this->scope_column=>$this->get_scope());

		self::$db->delete($this->table, $where);
		return;
	} 
	/**
	*
	* overrides delete of ORM
	*/
	public function delete()
	{
		return $this->delete_node();   
	}
	/**
	* Deletes the given node ((itself)  from the tree table
	* @param children boolean set to false to not delete children 
	* and move them up the tree
	* @return boolean
	*/
	function delete_node($children=true)
	{
		
		$table              =       $this->table;
		$leftcol            =       $this->left_column;
		$rightcol           =       $this->right_column;
		$leftanchor         =       $this->$leftcol;
		$leftval            =       $this->$leftcol;
		$rightval           =       $this->$rightcol;
		$parent_column      =       $this->parent_column;  
		if($children==true)
		{

			$where=$leftcol . '>='.$leftval .' 
					AND '. $rightcol .' <= ' . $rightval . ' 
					AND '. $this->scope_column .' = '.$this->get_scope() ;  
			
			$this->lock_tree();
			//Delete node and children              
			self::$db->delete($this->table, $where);
			//Modify other nodes to restore tree integrity
			$this->modify_node($rightval+1, $leftval  - $rightval - 1);
			$this->unlock_tree();
		}
		else
		{
			if($this->is_root()){
				Log::add('error', 'Cannot delete root node without deleting its children' );
				return false;
			}
			$this->lock_tree();
			//Get children before the parent is gone
			//Set parent ids right again
			$parent_node=$this->get_node($this->$parent_column);
			
			$children=$this->get_children(true);
						
			//Delete the node
			$where=array('id'=>$this->id);
			self::$db->delete($this->table, $where);     
			
			//First update
			$sql = 'UPDATE `' . $this->table . '` 
					SET '. $this->left_column .'='.$this->left_column .'-1, '.
						$this->right_column.' = '.$this->right_column . '-1
					WHERE '. $this->left_column .' >= '.$this->$leftcol . '
					AND '.$this->right_column .' <= '.$this->$rightcol .'
					AND '.$this->scope_column.' = '.$this->get_scope().';';
			self::$db->query($sql);
	
			//Second update
			$sql = 'UPDATE `' . $this->table . '` 
					SET '. $this->left_column .'='.$this->left_column .' -2
					WHERE '. $this->right_column .' > '.$this->$rightcol . '-1
						AND '.$this->left_column .' > '.$this->$rightcol .'-1
						AND '.$this->scope_column.' = '.$this->get_scope().';';
			self::$db->query($sql);       
						
			//Third update              
			$sql = 'UPDATE `' . $this->table . '` 
					SET '. $this->right_column .'='.$this->right_column .'-2 
					WHERE '. $this->right_column .' > '.$this->$rightcol . '-1
						AND '.$this->scope_column.' = '.$this->get_scope().';';
			self::$db->query($sql);      
			
			//Set the parent ids
			if(is_array($children))
			{
				foreach($children as $child)
				{
					$child->$parent_column=$parent_node->id;
					$child->save_node();
				}
			}
			$this->unlock_tree();

		}

		
		return true;
	}         
	
	/**
	* Delete descendants but not node itself
	*  
	* @return 
	*/
	function delete_descendants()
	{
		$this->lock_tree();
		$this->get_children();
		foreach($this->children as $child)
		{
			$child->delete_node();
			
		}
		$this->unlock_tree();
		$this->children=array();
		return true;  
	}
	/**
	* Deletes children but not descendants, 
	* descendants move up the tree
	* @return 
	*/
	function delete_children()
	{
		$this->get_children();
		foreach($this->children as $child)
		{
			$child->delete_node(false);
			
		}
		$this->unlock_tree();
		$this->children=array();
		return true;          
	}

	// -------------------------------------------------------------------------
	//  MODIFY/REORGANISE TREE
	//
	//  Methods to move nodes around the tree. Method names should be 
	//  relatively self-explanatory! Hopefully ;)
	//
	// -------------------------------------------------------------------------

	//inter-scope moves might be something for later
	/**
	* Moves the given node to make it the next sibling of "target"
	* 
	* @param array $target The node to use as the position marker
	* @return array $newpos The new left and right values of the node moved
	*/
	function move_to_next_sibling_of( $target)
	{
		$rgt_col = $this->right_column;
		$parent_column=$this->parent_column;
		
		//Set parent id (siblings have the same parent) 
		$parent_id=$target->$parent_column;
		
		//only move when scopes are equal
		if($target->get_scope()==$this->get_scope())
		{
			$this->update_parent_id($parent_id);

			return $this->move_sub_tree( $target->$rgt_col+1);
		}
		return false;            
	}

	/** 
	* Moves the given node to make it the prior sibling of "target"
	* 
	* @param array $target The node to use as the position marker
	* @return array $newpos The new left and right values of the node moved
	*/
	function move_to_prev_sibling_of( $target)
	{
		$lft_col = $this->left_column;
		$parent_column=$this->parent_column;
		
		//Set parent id (siblings have the same parent)         
		$parent_id=$target->$parent_column;
		
		//only move when scopes are equal
		if($target->get_scope()==$this->get_scope())
		{
			$this->update_parent_id($parent_id);
	
			return $this->move_sub_tree( $target->$lft_col);   
		}
		return false;

	}

	/** 
	* Moves the given node to make it the first child of "target"
	* 
	* @param array $target The node to use as the position marker
	* @return array $newpos The new left and right values of the node moved
	*/    
	function move_to_first_child_of( $target)
	{
		$lft_col = $this->left_column;
		$parent_column=$this->parent_column;
		
		//Set parent id (id of the parent, is childs parent id)  
		$parent_id=$target->id;
		
		//only move when scopes are equal
		if($target->get_scope()==$this->get_scope())
		{
			$this->update_parent_id($parent_id);
	
			return $this->move_sub_tree( $target->$lft_col+1);
		}
		return false;
	}

	/** 
	* Moves the given node to make it the last child of "target"
	* 
	* @param array $target The node to use as the position marker
	* @return array $newpos The new left and right values of the node moved
	*/
	function move_to_last_child_of($target)
	{
		$rgt_col = $this->right_column;          
		$parent_column=$this->parent_column;

		//Set parent id (id of the parent, is childs parent id)  
		$parent_id=$target->id;
		
		//only move when scopes are equal
		if($target->get_scope()==$this->get_scope())
		{
			$this->update_parent_id($parent_id);
	
			return $this->move_sub_tree($target->$rgt_col);
		}
		return false;
	}
	
	/*
	* *****************
	* Check methods
	* *****************
	*/
	/**
	* Returns true or false 
	* (in reality, it checks to see if the given left and
	* right values _appear_ to be valid not necessarily that they _are_ valid)
	* 
	* @return boolean 
	*/
	function is_valid_node()
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		
		return ($this->$leftcol < $this->$rightcol);
	}
	
	/**
	* Tests whether the given node has an ancestor 
	* (effectively the opposite of isRoot yes|no)
	* 
	* @return boolean
	*/
	function has_parent()
	{ 
		return $this->is_valid_node($this->get_parent());
	}
	
	/**
	* Tests whether the given node has a prior sibling or not
	* 
	* @return boolean
	*/
	function has_prev_sibling()
	{ 
		return $this->is_valid_node($this->get_prev_sibling());
	}
	
	/**
	* Test to see if node has siblings after itself
	* 
	* @return boolean
	*/
	function has_next_sibling()
	{ 
		return $this->is_valid_node($this->get_next_sibling());
	}
	
	/**
	* Test to see if node has children
	* 
	* @return boolean
	*/
	function has_descendants()
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		return (($this->$rightcol - $this->$leftcol) > 1);
	}

	/**
	* Test to see if the given node is also the root node
	* 
	* @return boolean
	*/
	function is_root()
	{
		$leftcol        =       $this->left_column;
		return ($this->$leftcol == 1);
	}
	
	/**
	* Test to see if the given node is a leaf node (ie has no children)
	* 
	* @return boolean
	*/
	function is_leaf()
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		return (($this->$rightcol - $this->$leftcol) == 1);
	}
	
	/**
	* Test to see if the node is a descendant of the given node
	* @param array $control_node the node to use as the parent or ancestor
	* @return boolean
	*/
	function is_descendant_of($control_node)
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		
		return (        ($this->$leftcol  >   $control_node->$leftcol) 
					and ($this->$rightcol <   $control_node->$rightcol)
			);
	}
	
	/**
	* Test to see if the node is a descendant of the given node
	* @param array $control_node the node to use as the parent or ancestor
	* @return boolean
	*/
	function is_child_of($control_node)
	{   
		$child_id=$this->id;
		$parent_id=$control_node->id;
		
		self::$db->select('count(*) as is_child');
		self::$db->from($this->table);       
		self::$db->where('id',$child_id);          
		self::$db->where($this->parent_column,$parent_id);                 
		self::$db->where($this->scope_column, $this->get_scope());
		
		$result=self::$db->get(); 
		
		if ($row = $result->current()) 
		{
			return $row->is_child > 0;
		}

		return false;
		
	}    
	
	/**
	* Test to determine whether the node is infact also the $control_node (is A === B)
	* @param array $control_node The node prototype to use for the comparison
	* @return boolean
	*/
	function is_equal_to($control_node)
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		
		return (($this->$leftcol==$control_node->$leftcol) and ($this->$rightcol==$control_node->$rightcol));
	}

	/**
	* Combination method of is_descendant and is_equal
	* 
	* @param array $controlNode The node prototype to use for the comparison
	* @return boolean
	*/
	function is_descendant_or_equal_to($controlNode)
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		
		return (($this->$leftcol>=$control_node->$leftcol) and ($this->$rightcol<=$control_node->$rightcol));
	}
	/*
	* *****************
	* Informational methods
	* *****************
	*/
	/**
	* Returns the tree level for the given node (assuming root node is at level 0)
	* 
	* @return integer The level of the supplied node
	*/
	function get_level()
	{
		$leftval        = (int)  $this->$lft_col;
		$rightval       = (int)  $this->$rgt_col;
		
		self::$db->select('COUNT(*) AS level');
		self::$db->from($this->table);       
		self::$db->where($this->left_column.' < '.$leftval);
		self::$db->where($this->right_column.' < '.$rightval);                            
		self::$db->where($this->scope_column, $this->get_scope());
		
		$query=self::$db->get();        
		if($query->count() > 0) {
			$result = $query->current();
			return (int) $result->level;
		} else {
			return 0;
		}
	}    
	/**
	* Output number of descendants this node has
	* @return integer
	* 
	*/
	function get_number_of_descendants(){
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
		// Generate WHERE
	
	return (int) ( $this->$rgt_col - $this->$lft_col -1)/2; 
		
	}     
	/**
	* Output number of children of this node
	* @return integer 
	*/
	function get_number_of_children()
	{ 
	
		self::$db->select('count(*) as num_children');
		self::$db->from($this->table);       
		self::$db->where($this->parent_column,$this->id);          
		self::$db->where($this->scope_column, $this->get_scope());
		
		$result=self::$db->get();
		
		if($row = $result->current())
			return (int) $row->num_children;

		return -1;    
	}
	/**
	* Get current scope of the object
	* @return integer
	*/
	function get_scope()
	{ 
		return $this->_scope;
	}
	/**
	* Set scope of current object, retrieved objects calls this in constructor
	*/
	function set_scope($value)
	{
		$this->_scope=$value;
		return $this;
	}



	/* *****************************************
	* Print methods, more or less debug methods
	* *****************************************
	*/
	/**
	* Debug tree
	*/
	function debug_tree($tree,  $disp_col, $ind = '')
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
				$parent_column=$this->parent_column;
		
		echo $ind .'#'.$tree->id.' '.$tree->$lft_col.'- '.$tree->$disp_col .' p:'.$tree->$parent_column.' -'.$tree->$rgt_col.'<br>';
		if(is_array($tree->children))
		{
			foreach($tree->children as $child)
			{
				$this->debug_tree($child,$disp_col,'....'.$ind);
			}    
		}


	}
	/**
	* Will rebuild tree according to the parent_id column
	* Warning, the order of the tree might not exactly be maintained
	* Might be slow for big trees
	* Call this method only with the root_id and its left value.
	* @return 
	* @param $parent_id Object
	* @param $left Object
	*/
	function rebuild_tree($parent_id, $left) {
		$this->lock_tree();
		// the right value of this node is the left value + 1
		$right = $left+1;
	
		// get all children of this node
		self::$db->select('id');                            
		self::$db->where($this->parent_column, $parent_id);
		self::$db->where($this->scope_column, $this->get_scope());
		self::$db->from($this->table);
		$result=self::$db->get();
		
		foreach ($result as $row) {
			// recursive execution of this function for each
			// child of this node
			// $right is the current right value, which is
			// incremented by the rebuild_tree function
			$right = $this->rebuild_tree($row->id, $right);
		}
		
		// we've got the left value, and now that we've processed
		// the children of this node we also know the right value
			
		self::$db->set($this->left_column, $left);
		self::$db->set($this->right_column, $right);
		self::$db->where('id',$parent_id);
		self::$db->where($this->scope_column, $this->get_scope());
		self::$db->update($this->table);
		// return the right value of this node + 1
		return $right+1;
		
		$this->unlock_tree();
	} 
	
	/*
	*  Protected functions
	*  
	*/
	/**
	* check whether child is child of parent (internal)
	* 
	* @return 
	* @param $child Object
	* @param $parent Object
	*/     
	protected function _is_descendant_of($child, $parent)
	{
		$lft_col = $this->left_column;
		$rgt_col = $this->right_column;
				
		return ($child->$lft_col > $parent->$lft_col && $child->$rgt_col < $parent->$rgt_col);
	}     
	
		/** 
	* The method that performs moving/renumbering operations 
	* 
	* @param array $targetValue Position integer to use as the target
	* @return array $newpos The new left and right values of the node moved
	* @access private
	*/
	protected function move_sub_tree($targetValue)
	{ 
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
				
		$sizeOfTree = $this->$rightcol - $this->$leftcol + 1;
		$this->modify_node($targetValue, $sizeOfTree);
		
		
		if($this->$leftcol >= $targetValue)
		{
			$this->$leftcol  += $sizeOfTree;
			$this->$rightcol += $sizeOfTree;
		}

		$newpos = $this->modify_node_range($this->$leftcol, $this->$rightcol, $targetValue - $this->$leftcol);

		$this->modify_node($this->$rightcol+1, - $sizeOfTree);
		
		if($this->$leftcol <= $targetValue)
		{ 
			$newpos[$this->left_column] -= $sizeOfTree;
			$newpos[$this->right_column] -= $sizeOfTree;
		}

		return $newpos;
	}
	/**
	* _modifyNodeRange
	*
	* @param $lowerbound integer value of lowerbound of range to move
	* @param $upperbound integer value of upperbound of range to move
	* @param $changeVal unsigned integer of change amount
	* @access private
	*/
	
	protected function modify_node_range($lowerbound, $upperbound, $changeVal)
	{
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		$table          =       $this->table;
		$scope_col      =       $this->scope_column;
		
		$sql = "UPDATE      $table 
				SET         $leftcol    =   $leftcol    +   $changeVal 
				WHERE       $leftcol    >=  $lowerbound  
				AND         $leftcol    <=  $upperbound
				AND ".$this->scope_column.' = '.$this->$scope_col.';';
		
		self::$db->query($sql);
		
		$sql = "UPDATE      $table
				SET         $rightcol   =   $rightcol   +   $changeVal
				WHERE       $rightcol   >=  $lowerbound
				AND         $rightcol   <=  $upperbound
				AND ".$this->scope_column.' = '.$this->$scope_col.';';
		
		self::$db->query($sql);
		
		$retArray = array(
							$this->left_column  =>  $lowerbound+$changeVal, 
							$this->right_column =>  $upperbound+$changeVal
						); 
		return $retArray;
	} // END: Method _modifyNodeRange     
	
	/**
	* Update the parent id of this record, the ORM class handles
	* it when the parent id didn't change
	* @return 
	* @param $node Object
	* @param $parent_id Object
	*/
	protected function update_parent_id($parent_id)
	{
		$parent_column=$this->parent_column;
		$this->$parent_column=$parent_id;
		return $this->save_node();
	}
	/**
	* _modifyNode
	*
	* Adds $changeVal to all left and right values that are greater than or
	* equal to $node_int
	* 
	* @param  $node_int The value to start the shift from
	* @param  $changeVal unsigned integer value for change
	* @access private
	*/
	protected function modify_node($node_int, $changeVal)
	{
		$leftcol        =       $this->left_column;
		$rightcol       =       $this->right_column;
		$table          =       $this->table;
		$scope_col      =        $this->scope_column;
		
		$sql =  "UPDATE     $table " .
				"SET        $leftcol = $leftcol + $changeVal ".
				"WHERE      $leftcol >= $node_int
				AND ".$this->scope_column.' = '.$this->$scope_col.';';
		
		self::$db->query($sql);
		
		$sql =  "UPDATE     $table " .
				"SET        $rightcol = $rightcol + $changeVal ".
				"WHERE      $rightcol >= $node_int
				AND ".$this->scope_column.' = '.$this->$scope_col.';';
		
		self::$db->query($sql);
		
		return true;
	} // END: _modifyNode
	/**
	*  save_node
	* 
	*  Inserts a new node into the tree, or saves the current one
	*
	*  @return boolean True/False dependent upon the success of the operation
	*  @access private
	*/
	protected function save_node( )
	{
		if ($this->save()) {
			// Return true on success
			return TRUE;
		}
		
		return false;
	}         
}