forked from stefangabos/Zebra_Mptt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zebra_Mptt.php
1631 lines (1234 loc) · 65.7 KB
/
Zebra_Mptt.php
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
<?php
/**
* A PHP library that provides an implementation of the modified preorder tree traversal algorithm making it easy to
* implement the MPTT algorithm in your PHP applications.
*
* Read more {@link https://github.com/stefangabos/Zebra_Mptt/ here}
*
* @author Stefan Gabos <[email protected]>
* @version 2.3.5 (last revision: July 14, 2017)
* @copyright (c) 2009 - 2017 Stefan Gabos
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE
* @package Zebra_Mptt
*/
class Zebra_Mptt {
/**
* Constructor of the class.
*
* <i>Make sure that before you instantiate the class you import or execute the SQL code found in the in the
* "install/mptt.sql" file, using the command line or your preferred MySQL manager.</i>
*
* <code>
* // include the php file
* require 'path/to/Zebra_Mptt.php';
*
* // instantiate the class
* $mptt = new Zebra_Mptt($db_link);
* </code>
*
* @param resource &$link An object representing the connection to a MySQL Server, as returned by
* {@link http://www.php.net/manual/en/mysqli.construct.php mysqli_connect}.
*
* If you use {@link http://stefangabos.ro/php-libraries/zebra-database/ Zebra_Database}
* to connect to the database, you can get the connection to the MySQL server
* via Zebra_Database's {@link http://stefangabos.ro/wp-content/docs/Zebra_Database/Zebra_Database/Zebra_Database.html#methodget_link get_link}
* method.
*
* @param string $table_name (Optional) MySQL table name to be used for storing items.
*
* Default is <i>mptt</i>
*
* @param string $id_column (Optional) Name of the column that uniquely identifies items in the table
*
* Default is <i>id</i>
*
* @param string $title_column (Optional) Name of the column that stores item names
*
* Default is <i>title</i>
*
* @param string $left_column (Optional) Name of the column that stores "left" values
*
* Default is <i>lft</i> ("left" is a reserved word in MySQL)
*
* @param string $right_column (Optional) Name of the column that stores "right" values
*
* Default is <i>rgt</i> ("right" is a reserved word in MySQL)
*
* @param string $parent_column (Optional) Name of the column that stores the IDs of parent items
*
* Default is <i>parent</i>
*
* @return void
*/
public function __construct(&$link, $table_name = 'mptt', $id_column = 'id', $title_column = 'title', $left_column = 'lft', $right_column = 'rgt', $parent_column = 'parent') {
// stop if required PHP version is not available
if (version_compare(phpversion(), '5.0.0') < 0) trigger_error('PHP 5.0.0 or greater required', E_USER_ERROR);
// stop if the mysqli extension is not loaded
if (!extension_loaded('mysqli')) trigger_error('mysqli extension is required', E_USER_ERROR);
// store the connection link
$this->link = $link;
// continue only if there is an active MySQL connection
if (@mysqli_ping($this->link))
// initialize properties
$this->properties = array(
'table_name' => $table_name,
'id_column' => $id_column,
'title_column' => $title_column,
'left_column' => $left_column,
'right_column' => $right_column,
'parent_column' => $parent_column,
);
// if no MySQL connections could be found
// trigger a fatal error message and stop execution
else trigger_error('no MySQL connection', E_USER_ERROR);
}
/**
* Adds a new node as the child of a given parent node.
*
* <code>
* // add a new topmost node
* $node = $mptt->add(0, 'Main');
*
* // add a child node
* $mptt->add($node, 'Child 1');
*
* // add another child node
* $mptt->add($node, 'Child 2');
*
* // insert a third child node
* // notice the "1" as the last argument, instructing the script to insert the child node
* // as the second child node, after "Child 1"
* // remember that the trees are 0-based, meaning that the first node in a tree has the index 0!
* $mptt->add($node, 'Child 3', 1);
*
* // and finally, insert a fourth child node
* // notice the "0" as the last argument, instructing the script to insert the child node
* // as the very first child node of the parent node
* // remember that the trees are 0-based, meaning that the first node in a tree has the index 0!
* $mptt->add($node, 'Child 4', 0);
* </code>
*
* @param integer $parent The ID of the parent node.
*
* Use "0" to add a topmost node.
*
* @param string $title The title of the node.
*
* @param integer $position (Optional) The position the node will have among the parent node's children nodes.
*
* When parent node is given as "0", this refers to the position the node will have
* among the topmost nodes.
*
* The values are 0-based, meaning that if you want the node to be inserted as
* the first child of the target node, you have to use "0", if you want it to
* be second, use "1", and so on.
*
* If not given (or given as boolean FALSE), the node will be inserted as the last
* of the parent node's children nodes.
*
* @return mixed Returns the ID of the newly inserted node or FALSE on error.
*/
public function add($parent, $title, $position = false) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// make sure parent ID is an integer
$parent = (int)$parent;
// continue only if
if (
// we are adding a topmost node OR
$parent == 0 ||
// parent node exists in the lookup array
isset($this->lookup[$parent])
) {
// get parent's descendant nodes (no deeper than the first level)
$descendants = $this->get_descendants($parent);
// if node is to be inserted in the default position (as the last of the parent node's children)
// give a numerical value to the position
if ($position === false) $position = count($descendants);
// if a custom position was specified
else {
// make sure that position is an integer value
$position = (int)$position;
// if position is a bogus number
// use the default position (as the last of the parent node's children)
if ($position > count($descendants) || $position < 0) $position = count($descendants);
}
// if parent has no descendants OR the node is to be inserted as the parent node's first child
if (empty($descendants) || $position == 0)
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
// if parent is not found (meaning that we're inserting a topmost node) set the boundary to 0
$boundary = isset($this->lookup[$parent]) ? $this->lookup[$parent][$this->properties['left_column']] : 0;
// if parent node has descendant nodes and/or the node needs to be inserted at a specific position
else {
// find the child node that currently exists at the position where the new node needs to be inserted to
$slice = array_slice($descendants, $position - 1, 1);
$descendants = array_shift($slice);
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
$boundary = $descendants[$this->properties['right_column']];
}
// iterate through all the records in the lookup array
foreach ($this->lookup as $id => $properties) {
// if the node's "left" value is outside the boundary
if ($properties[$this->properties['left_column']] > $boundary)
// increment it with 2
$this->lookup[$id][$this->properties['left_column']] += 2;
// if the node's "right" value is outside the boundary
if ($properties[$this->properties['right_column']] > $boundary)
// increment it with 2
$this->lookup[$id][$this->properties['right_column']] += 2;
}
// lock table to prevent other sessions from modifying the data and thus preserving data integrity
mysqli_query($this->link, 'LOCK TABLE `' . $this->properties['table_name'] . '` WRITE');
// update the nodes in the database having their "left"/"right" values outside the boundary
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['left_column'] . '` = `' . $this->properties['left_column'] . '` + 2
WHERE
`' . $this->properties['left_column'] . '` > ' . $boundary . '
');
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['right_column'] . '` = `' . $this->properties['right_column'] . '` + 2
WHERE
`' . $this->properties['right_column'] . '` > ' . $boundary . '
');
// insert the new node into the database
mysqli_query($this->link, '
INSERT INTO
`' . $this->properties['table_name'] . '`
(
`' . $this->properties['title_column'] . '`,
`' . $this->properties['left_column'] . '`,
`' . $this->properties['right_column'] . '`,
`' . $this->properties['parent_column'] . '`
)
VALUES
(
"' . mysqli_real_escape_string($this->link, $title) . '",
' . ($boundary + 1) . ',
' . ($boundary + 2) . ',
' . $parent . '
)
');
// get the ID of the newly inserted node
$node_id = mysqli_insert_id($this->link);
// release table lock
mysqli_query($this->link, 'UNLOCK TABLES');
// add the node to the lookup array
$this->lookup[$node_id] = array(
$this->properties['id_column'] => $node_id,
$this->properties['title_column'] => $title,
$this->properties['left_column'] => $boundary + 1,
$this->properties['right_column'] => $boundary + 2,
$this->properties['parent_column'] => $parent,
);
// reorder the lookup array
$this->_reorder_lookup_array();
// return the ID of the newly inserted node
return $node_id;
}
// if script gets this far, something must've went wrong so we return false
return false;
}
/**
* Creates a copy of a node (including its descendant nodes) as the child node of a given node.
*
* <code>
* // insert a topmost node
* $node = $mptt->add(0, 'Main');
*
* // add a child node
* $child1 = $mptt->add($node, 'Child 1');
*
* // add another child node
* $child2 = $mptt->add($node, 'Child 2');
*
* // create a copy of "Child 2" node and put it as "Child 1"'s child
* $mptt->copy($child2, $child1);
* </code>
*
* @param integer $source The ID of a node to copy.
*
* <i>Remember that the node will be copied together with all its descendant nodes!</i>
*
* @param integer $target The ID of a node which will become the copy's parent node.
*
* Use "0" to make the copy a topmost node.
*
* @param integer $position (Optional) The position the node will have among the target node's children
* nodes.
*
* When target node is "0", this refers to the position the node will have among
* the topmost nodes.
*
* The values are 0-based, meaning that if you want the node to be inserted as
* the first child of the target node, you have to use "0", if you want it to
* be second, use "1", and so on.
*
* If not given (or given as boolean FALSE), the node will be inserted as the last
* of the target node's children nodes.
*
* @return mixed Returns the ID of the newly created copy, or FALSE on error.
*/
public function copy($source, $target, $position = false) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// continue only if
if (
// source node exists in the lookup array AND
isset($this->lookup[$source]) &&
// target node exists in the lookup array OR is 0 (indicating a topmost node)
(isset($this->lookup[$target]) || $target == 0)
) {
// get the source's children nodes (if any)
$source_children = $this->get_descendants($source, false);
// this array will hold the items we need to copy
// by default we add the source item to it
$sources = array($this->lookup[$source]);
// the copy's parent will be the target node
$sources[0][$this->properties['parent_column']] = $target;
// iterate through source node's children
foreach ($source_children as $child)
// save them for later use
$sources[] = $this->lookup[$child[$this->properties['id_column']]];
// the value with which items outside the boundary set below, are to be updated with
$source_rl_difference =
$this->lookup[$source][$this->properties['right_column']] -
$this->lookup[$source][$this->properties['left_column']]
+ 1;
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
$source_boundary = $this->lookup[$source][$this->properties['left_column']];
// get target node's children (no deeper than the first level)
$target_children = $this->get_descendants($target);
// if copy is to be inserted in the default position (as the last of the target node's children)
if ($position === false)
// give a numerical value to the position
$position = count($target_children);
// if a custom position was specified
else {
// make sure given position is an integer value
$position = (int)$position;
// if position is a bogus number
if ($position > count($target_children) || $position < 0)
// use the default position (the last of the target node's children)
$position = count($target_children);
}
// we are about to do an insert and some nodes need to be updated first
// if target has no children nodes OR the copy is to be inserted as the target node's first child node
if (empty($target_children) || $position == 0)
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
// if parent is not found (meaning that we're inserting a topmost node) set the boundary to 0
$target_boundary = isset($this->lookup[$target]) ? $this->lookup[$target][$this->properties['left_column']] : 0;
// if target has children nodes and/or the copy needs to be inserted at a specific position
else {
// find the target's child node that currently exists at the position where the new node needs to be inserted to
$slice = array_slice($target_children, $position - 1, 1);
$target_children = array_shift($slice);
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
$target_boundary = $target_children[$this->properties['right_column']];
}
// iterate through the nodes in the lookup array
foreach ($this->lookup as $id => $properties) {
// if the "left" value of node is outside the boundary
if ($properties[$this->properties['left_column']] > $target_boundary)
// increment it
$this->lookup[$id][$this->properties['left_column']] += $source_rl_difference;
// if the "right" value of node is outside the boundary
if ($properties[$this->properties['right_column']] > $target_boundary)
// increment it
$this->lookup[$id][$this->properties['right_column']] += $source_rl_difference;
}
// lock table to prevent other sessions from modifying the data and thus preserving data integrity
mysqli_query($this->link, 'LOCK TABLE `' . $this->properties['table_name'] . '` WRITE');
// update the nodes in the database having their "left"/"right" values outside the boundary
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['left_column'] . '` = `' . $this->properties['left_column'] . '` + ' . $source_rl_difference . '
WHERE
`' . $this->properties['left_column'] . '` > ' . $target_boundary . '
');
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['right_column'] . '` = `' . $this->properties['right_column'] . '` + ' . $source_rl_difference . '
WHERE
`' . $this->properties['right_column'] . '` > ' . $target_boundary . '
');
// finally, the nodes that are to be inserted need to have their "left" and "right" values updated
$shift = $target_boundary - $source_boundary + 1;
// iterate through the nodes that are to be inserted
foreach ($sources as $id => &$properties) {
// update "left" value
$properties[$this->properties['left_column']] += $shift;
// update "right" value
$properties[$this->properties['right_column']] += $shift;
// insert into the database
mysqli_query($this->link, '
INSERT INTO
`' . $this->properties['table_name'] . '`
(
`' . $this->properties['title_column'] . '`,
`' . $this->properties['left_column'] . '`,
`' . $this->properties['right_column'] . '`,
`' . $this->properties['parent_column'] . '`
)
VALUES
(
"' . mysqli_real_escape_string($this->link, $properties[$this->properties['title_column']]) . '",
' . $properties[$this->properties['left_column']] . ',
' . $properties[$this->properties['right_column']] . ',
' . $properties[$this->properties['parent_column']] . '
)
');
// get the ID of the newly inserted node
$node_id = mysqli_insert_id($this->link);
// because the node may have children nodes and its ID just changed
// we need to find its children and update the reference to the parent ID
foreach ($sources as $key => $value)
// if a child node was found
if ($value[$this->properties['parent_column']] == $properties[$this->properties['id_column']])
// update the reference to the parent ID
$sources[$key][$this->properties['parent_column']] = $node_id;
// update the node's properties with the ID
$properties[$this->properties['id_column']] = $node_id;
// update the array of inserted items
$sources[$id] = $properties;
}
// a reference of a $properties and the last array element remain even after the foreach loop
// we have to destroy it
unset($properties);
// release table lock
mysqli_query($this->link, 'UNLOCK TABLES');
// at this point, we have the nodes in the database but we need to also update the lookup array
$parents = array();
// iterate through the inserted nodes
foreach ($sources as $id => $properties) {
// if the node has any parents
if (count($parents) > 0)
// iterate through the array of parent nodes
while ($parents[count($parents) - 1]['right'] < $properties[$this->properties['right_column']])
// and remove those which are not parents of the current node
array_pop($parents);
// if there are any parents left
if (count($parents) > 0)
// the last node in the $parents array is the current node's parent
$properties[$this->properties['parent_column']] = $parents[count($parents) - 1]['id'];
// update the lookup array
$this->lookup[$properties[$this->properties['id_column']]] = $properties;
// add current node to the stack
$parents[] = array(
'id' => $properties[$this->properties['id_column']],
'right' => $properties[$this->properties['right_column']]
);
}
// reorder the lookup array
$this->_reorder_lookup_array();
// return the ID of the copy
return $sources[0][$this->properties['id_column']];
}
// if scripts gets this far, return false as something must've went wrong
return false;
}
/**
* Deletes a node, including the node's descendant nodes.
*
* <code>
* // add a topmost node
* $node = $mptt->add(0, 'Main');
*
* // add child node
* $child1 = $mptt->add($node, 'Child 1');
*
* // add another child node
* $child2 = $mptt->add($node, 'Child 2');
*
* // delete the "Child 2" node
* $mptt->delete($child2);
* </code>
*
* @param integer $node The ID of the node to delete.
*
* @return boolean TRUE on success or FALSE on error.
*/
public function delete($node) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// continue only if target node exists in the lookup array
if (isset($this->lookup[$node])) {
// get target node's descendant nodes (if any)
$descendants = $this->get_descendants($node, false);
// iterate through target node's descendant nodes
foreach ($descendants as $descendant)
// remove node from the lookup array
unset($this->lookup[$descendant[$this->properties['id_column']]]);
// lock table to prevent other sessions from modifying the data and thus preserving data integrity
mysqli_query($this->link, 'LOCK TABLE `' . $this->properties['table_name'] . '` WRITE');
// also remove nodes from the database
mysqli_query($this->link, '
DELETE
FROM
`' . $this->properties['table_name'] . '`
WHERE
`' . $this->properties['left_column'] . '` >= ' . $this->lookup[$node][$this->properties['left_column']] . ' AND
`' . $this->properties['right_column'] . '` <= ' . $this->lookup[$node][$this->properties['right_column']] . '
');
// the value with which items outside the boundary set below, are to be updated with
$target_rl_difference =
$this->lookup[$node][$this->properties['right_column']] -
$this->lookup[$node][$this->properties['left_column']]
+ 1;
// set the boundary - nodes having their "left"/"right" values outside this boundary will be affected by
// the insert, and will need to be updated
$boundary = $this->lookup[$node][$this->properties['left_column']];
// remove the target node from the lookup array
unset($this->lookup[$node]);
// iterate through nodes in the lookup array
foreach ($this->lookup as $id => $properties) {
// if the "left" value of node is outside the boundary
if ($this->lookup[$id][$this->properties['left_column']] > $boundary)
// decrement it
$this->lookup[$id][$this->properties['left_column']] -= $target_rl_difference;
// if the "right" value of node is outside the boundary
if ($this->lookup[$id][$this->properties['right_column']] > $boundary)
// decrement it
$this->lookup[$id][$this->properties['right_column']] -= $target_rl_difference;
}
// update the nodes in the database having their "left"/"right" values outside the boundary
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['left_column'] . '` = `' . $this->properties['left_column'] . '` - ' . $target_rl_difference . '
WHERE
`' . $this->properties['left_column'] . '` > ' . $boundary . '
');
mysqli_query($this->link, '
UPDATE
`' . $this->properties['table_name'] . '`
SET
`' . $this->properties['right_column'] . '` = `' . $this->properties['right_column'] . '` - ' . $target_rl_difference . '
WHERE
`' . $this->properties['right_column'] . '` > ' . $boundary . '
');
// release table lock
mysqli_query($this->link, 'UNLOCK TABLES');
// return true as everything went well
return true;
}
// if script gets this far, something must've went wrong so we return false
return false;
}
/**
* Returns an unidimensional (flat) array with the descendant nodes of a given parent node.
*
* <i>For a multidimensional array use the {@link get_tree()} method.</i>
*
* @param integer $node (Optional) The ID of a node for which to return the descendant nodes.
*
* When not specified or given as "0", the "root" node is implied.
*
* @param boolean $direct_descendants_only (Optional) Set this to FALSE if you want <b>all the descendants</b>
* (including descendants of descendants), and not just the <b>direct
* descendants</b> (children) of the node.
*
* Default is TRUE
*
* @return array Returns an unidimensional array with the descendant nodes of a
* given parent node.
*/
public function get_descendants($node = 0, $direct_descendants_only = true) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// if parent node exists in the lookup array OR we're looking for the topmost nodes
if (isset($this->lookup[$node]) || $node === 0) {
$descendants = array();
// get the keys in the lookup array
$keys = array_keys($this->lookup);
// iterate through the available keys
foreach ($keys as $item)
// if
if (
// node's "left" is higher than parent node's "left" (or, if parent is 0, if it is higher than 0)
$this->lookup[$item][$this->properties['left_column']] > ($node !== 0 ? $this->lookup[$node][$this->properties['left_column']] : 0) &&
// node's "left" is smaller than parent node's "right" (or, if parent is 0, if it is smaller than PHP's maximum integer value)
$this->lookup[$item][$this->properties['left_column']] < ($node !== 0 ? $this->lookup[$node][$this->properties['right_column']] : PHP_INT_MAX) &&
// if we only need the first level children, check if children node's parent node is the parent given as argument
(!$direct_descendants_only || $this->lookup[$item][$this->properties['parent_column']] == $node)
// save to array
) $descendants[$this->lookup[$item][$this->properties['id_column']]] = $this->lookup[$item];
// return children nodes
return $descendants;
}
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns the number of descendant nodes of a given node.
*
* @param integer $node The ID of the node for which to return the number of direct
* descendant nodes.
*
* @param boolean $direct_descendants_only (Optional) Specifies whether to count <b>direct descendants only</b>,
* or to count <b>all the descendants</b> (including descendants of
* descendants)
*
* Default is TRUE
*
* @return integer Returns the number of direct descendant nodes of a parent node,
* or FALSE on error.
*
* <i>Since this method may return both "0" and FALSE, make sure you
* use === to verify the returned result!</i>
*/
public function get_descendant_count($node, $direct_descendants_only = true) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// if parent node exists in the lookup array
if (isset($this->lookup[$node]))
// if we require all the descendants (not direct only)
if (!$direct_descendants_only)
// return the total number of descendant nodes
return ($this->lookup[$node][$this->properties['right_column']] - $this->lookup[$node][$this->properties['left_column']] - 1) / 2;
// if we require direct descendants only
else {
$result = 0;
// iterate through all the records in the lookup array
foreach ($this->lookup as $id => $properties)
// if node is a direct descendant of the parent node
if ($this->lookup[$id][$this->properties['parent_column']] == $node)
// increment the number of direct descendant nodes
$result++;
// return the number of direct descendant nodes
return $result;
}
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns the next sibling of a node.
*
* @param integer $node The ID of a node for which to return the next sibling node.
*
* @return mixed Returns a node's next sibling node, "0" if a next sibling doesn't exist, or
* FALSE on error (if the node doesn't exist).
*
* <i>Since this method may return both "0" and FALSE, make sure you use === to
* verify the returned result!</i>
*
* @since 2.2.6
*/
public function get_next_sibling($node) {
// if node exists, get its siblings
// (if $node exists this will never be an empty array as it will contain at least $node)
if ($siblings = $this->get_siblings($node, true)) {
// get the node's position among the siblings
$node_position = array_search($node, array_keys($siblings));
// get next node
$sibling = array_slice($siblings, $node_position + 1, 1);
// return result
return !empty($sibling) ? array_pop($sibling) : 0;
}
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns an array containing a node's direct parent node if the node has a parent node, or "0" if the node is a
* topmost node.
*
* @param integer $node The ID of a node for which to return the parent node.
*
* @return mixed Returns an array containing a node's direct parent node if the node has a
* parent node, or "0" if the node is a topmost node.
*
* <i>Since this method may return both "0" and FALSE, make sure you use ===
* to verify the returned result!</>
*/
public function get_parent($node) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
// if node exists in the lookup array
if (isset($this->lookup[$node]))
// if node has a parent node, return the parent node's properties
// also, return 0 if the node is a topmost node
return isset($this->lookup[$this->lookup[$node][$this->properties['parent_column']]]) ? $this->lookup[$this->lookup[$node][$this->properties['parent_column']]] : 0;
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns an unidimensional (flat) array with the path to the given node (including the node itself).
*
* @param integer $node The ID of a node for which to return the path.
*
* @return array Returns an unidimensional array with the path to the given node.
*/
public function get_path($node) {
// lazy connection: touch the database only when the data is required for the first time and not at object instantiation
$this->_init();
$parents = array();
// if node exists in the lookup array
if (isset($this->lookup[$node]))
// iterate through all the nodes in the lookup array
foreach ($this->lookup as $id => $properties)
// if
if (
// node is a parent node
$properties[$this->properties['left_column']] < $this->lookup[$node][$this->properties['left_column']] &&
$properties[$this->properties['right_column']] > $this->lookup[$node][$this->properties['right_column']]
// save the parent node's information
) $parents[$properties[$this->properties['id_column']]] = $properties;
// add also the node given as argument
$parents[$node] = $this->lookup[$node];
// return the path to the node
return $parents;
}
/**
* Returns the previous sibling of a node.
*
* @param integer $node The ID of a node for which to return the previous sibling node.
*
* @return mixed Returns a node's previous sibling node, "0" if a previous sibling doesn't
* exist, or FALSE on error (if the node doesn't exist).
*
* <i>Since this method may return both "0" and FALSE, make sure you use === to
* verify the returned result!</i>
*
* @since 2.2.6
*/
public function get_previous_sibling($node) {
// if node exists, get its siblings
// (if $node exists this will never be an empty array as it will contain at least $node)
if ($siblings = $this->get_siblings($node, true)) {
// get the node's position among the siblings
$node_position = array_search($node, array_keys($siblings));
// get previous node
$sibling = $node_position > 0 ? array_slice($siblings, $node_position - 1, 1) : array();
// return result
return !empty($sibling) ? array_pop($sibling) : 0;
}
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns an array with a node's sibling nodes.
*
* @param integer $node The ID of a node for which to return the node's sibling nodes.
*
* @param boolean $include_self Whether the node given as argument should also be present in the returned
* array.
*
* @return mixed Returns an array with a node's sibling nodes, an empty array if the node has
* no siblings, or FALSE on error (if the node doesn't exist)
*
* @since 2.2.6
*/
public function get_siblings($node, $include_self = false) {
// if parent node exists in the lookup array OR we're looking for the topmost nodes
if (isset($this->lookup[$node])) {
// properties of the node
$properties = $this->lookup[$node];
// get node's siblings
$siblings = $this->get_descendants($properties['parent']);
// remove self, if required so
if (!$include_self) unset($siblings[$node]);
// return siblings
return $siblings;
}
// if script gets this far, return false as something must've went wrong
return false;
}
/**
* Returns a multidimensional array with all the descendant nodes (including children nodes of children nodes of
* children nodes and so on) of a given node.
*
* @param integer $node (Optional) The ID of a node for which to return all descendant nodes, as
* a multidimensional array.
*
* Not given or given as "0", will return all the nodes.
*
* @return array Returns a multi dimensional array with all the descendant nodes (including
* children nodes of children nodes of children nodes and so on) of a given
* node.
*/