forked from wbraganca/yii2-nested-set-behavior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NestedSetQueryBehavior.php
150 lines (137 loc) · 5.36 KB
/
NestedSetQueryBehavior.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
<?php
/**
* @link https://github.com/wbraganca/yii2-nested-set-behavior
* @copyright Copyright (c) 2014 Wanderson Bragança
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace wbraganca\behaviors;
use yii\base\Behavior;
/**
* @author Wanderson Bragança <wanderson.wbc@gmail.com>
*/
class NestedSetQueryBehavior extends Behavior
{
/**
* @var ActiveQuery the owner of this behavior.
*/
public $owner;
/**
* Gets root node(s).
* @return ActiveRecord the owner.
*/
public function roots()
{
/** @var $modelClass ActiveRecord */
$modelClass = $this->owner->modelClass;
$model = new $modelClass;
$this->owner->andWhere($modelClass::getDb()->quoteColumnName($model->leftAttribute) . '=1');
unset($model);
return $this->owner;
}
public function options($root = 0, $level = null)
{
$res = [];
if (is_object($root)) {
$res[$root->{$root->idAttribute}] = str_repeat('—', $root->{$root->levelAttribute} - 1)
. ((($root->{$root->levelAttribute}) > 1) ? '›': '')
. $root->{$root->titleAttribute};
if ($level) {
foreach ($root->children()->all() as $childRoot) {
$res += $this->options($childRoot, $level - 1);
}
} elseif (is_null($level)) {
foreach ($root->children()->all() as $childRoot) {
$res += $this->options($childRoot, null);
}
}
} elseif (is_scalar($root)) {
if ($root == 0) {
foreach ($this->roots()->all() as $rootItem) {
if ($level) {
$res += $this->options($rootItem, $level - 1);
} elseif (is_null($level)) {
$res += $this->options($rootItem, null);
}
}
} else {
$modelClass = $this->owner->modelClass;
$model = new $modelClass;
$root = $modelClass::find()->andWhere([$model->idAttribute => $root])->one();
if ($root) {
$res += $this->options($root, $level);
}
unset($model);
}
}
return $res;
}
public function dataFancytree($root = 0, $level = null)
{
$data = array_values($this->prepareData2Fancytree($root, $level));
return $this->makeData2Fancytree($data);
}
private function prepareData2Fancytree($root = 0, $level = null)
{
$res = [];
if (is_object($root)) {
$res[$root->{$root->idAttribute}] = [
'key' => $root->{$root->idAttribute},
'title' => $root->{$root->titleAttribute}
];
if ($level) {
foreach ($root->children()->all() as $childRoot) {
$aux = $this->prepareData2Fancytree($childRoot, $level - 1);
if (isset($res[$root->{$root->idAttribute}]['children']) && !empty($aux)) {
$res[$root->{$root->idAttribute}]['folder'] = true;
$res[$root->{$root->idAttribute}]['children'] += $aux;
} elseif(!empty($aux)) {
$res[$root->{$root->idAttribute}]['folder'] = true;
$res[$root->{$root->idAttribute}]['children'] = $aux;
}
}
} elseif (is_null($level)) {
foreach ($root->children()->all() as $childRoot) {
$aux = $this->prepareData2Fancytree($childRoot, null);
if (isset($res[$root->{$root->idAttribute}]['children']) && !empty($aux)) {
$res[$root->{$root->idAttribute}]['folder'] = true;
$res[$root->{$root->idAttribute}]['children'] += $aux;
} elseif(!empty($aux)) {
$res[$root->{$root->idAttribute}]['folder'] = true;
$res[$root->{$root->idAttribute}]['children'] = $aux;
}
}
}
} elseif (is_scalar($root)) {
if ($root == 0) {
foreach ($this->roots()->all() as $rootItem) {
if ($level) {
$res += $this->prepareData2Fancytree($rootItem, $level - 1);
} elseif (is_null($level)) {
$res += $this->prepareData2Fancytree($rootItem, null);
}
}
} else {
$modelClass = $this->owner->modelClass;
$model = new $modelClass;
$root = $modelClass::find()->andWhere([$model->idAttribute => $root])->one();
if ($root) {
$res += $this->prepareData2Fancytree($root, $level);
}
unset($model);
}
}
return $res;
}
private function makeData2Fancytree(&$data)
{
$tree = [];
foreach ($data as $key => &$item) {
if (isset($item['children'])) {
$item['children'] = array_values($item['children']);
$tree[$key] = $this->makeData2Fancytree($item['children']);
}
$tree[$key] = $item;
}
return $tree;
}
}