1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Lib;
13:
14: use Nette\Database\Table\ActiveRow,
15: App\Model\Database,
16: App\Interfaces as I,
17: App\Lib\Statics\Cons;
18:
19: 20: 21:
22: class TreeRebuild extends Tree implements I\IData, I\ITree {
23:
24: 25: 26: 27: 28: 29:
30: public function __construct($tableName, Database $database, $masterNode) {
31: parent::__construct($tableName, $database, false, $masterNode);
32: }
33:
34: 35: 36: 37: 38:
39: public function rebuildTree() {
40: return ($this->masterNode) ? $this->rebuildTreeWorker($this->masterNode, 1) / 2 : false ;
41: }
42:
43: 44: 45: 46: 47: 48: 49:
50: private function rebuildTreeWorker(ActiveRow $parent, $left, $depth = 0) {
51: $right = $left + 1;
52: $childs = $this->getCopy()->where(Cons::COLUMN_PARENT, $parent->id);
53: foreach ($childs as $row) {
54: $right = $this->rebuildTreeWorker($row, $right, $depth + 1);
55: }
56: $update = array(Cons::COLUMN_LEFT => $left, Cons::COLUMN_RIGHT => $right, Cons::COLUMN_DEPTH => $depth);
57: $parent->update($update);
58: return $right + 1;
59: }
60: }