d8c1a2e0
Yarik
Big commit artbox
|
1
|
<?php
|
8af13427
Yarik
For leha commit.
|
2
3
4
5
|
namespace common\components\artboxtree;
trait ArtboxTreeQueryTrait
|
d8c1a2e0
Yarik
Big commit artbox
|
6
|
{
|
8af13427
Yarik
For leha commit.
|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static $cacheTree = [];
/** @var \yii\db\ActiveQuery $this */
static $model;
/*
* @return \yii\db\ActiveQuery
*/
private function getModel()
{
if(empty( self::$model )) {
$class = $this->modelClass;
self::$model = new $class;
}
return self::$model;
|
d8c1a2e0
Yarik
Big commit artbox
|
23
|
}
|
8af13427
Yarik
For leha commit.
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public function getTree($group = NULL, $with = NULL)
{
$model = $this->getModel();
if($group !== NULL) {
$this->andWhere([ $model->keyNameGroup => $group ]);
}
if($with) {
$this->with($with);
}
$data = $this->all();
if(empty( $data )) {
return [];
}
return $this->buildTree($data);
|
d8c1a2e0
Yarik
Big commit artbox
|
40
|
}
|
8af13427
Yarik
For leha commit.
|
41
42
43
44
45
46
47
48
49
50
51
52
53
|
private function recursiveRebuild($tree, $parentPath = NULL, $depth = 0)
{
$model = $this->getModel();
foreach($tree as $row) {
$path = ( is_null($parentPath) ? '' : $parentPath . $model->delimiter ) . $row[ 'item' ]->getAttribute($model->keyNameId);
$row[ 'item' ]->setAttribute($model->keyNamePath, $path);
$row[ 'item' ]->setAttribute($model->keyNameDepth, $depth);
$row[ 'item' ]->save();
if(!empty( $row[ 'children' ] )) {
$this->recursiveRebuild($row[ 'children' ], $path, $depth + 1);
}
|
d8c1a2e0
Yarik
Big commit artbox
|
54
55
|
}
}
|
8af13427
Yarik
For leha commit.
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/**
* @param int $group
*/
public function rebuildMP($group, $with = NULL)
{
$tree = $this->getTree($group, $with);
$this->recursiveRebuild($tree);
}
protected function buildTree(array $data, $parentId = 0)
{
$model = $this->getModel();
$result = [];
foreach($data as $element) {
if($element[ $model->keyNameParentId ] == $parentId) {
$children = $this->buildTree($data, $element[ $model->keyNameId ]);
$result[] = [
'item' => $element,
'children' => $children,
];
}
|
d8c1a2e0
Yarik
Big commit artbox
|
80
|
}
|
8af13427
Yarik
For leha commit.
|
81
|
return $result;
|
d8c1a2e0
Yarik
Big commit artbox
|
82
|
}
|
8af13427
Yarik
For leha commit.
|
83
84
85
86
87
88
89
90
91
92
93
94
95
|
public function normalizeTreeData(array $data, $parentId = NULL)
{
$model = $this->getModel();
$result = [];
foreach($data as $element) {
if($element[ $model->keyNameParentId ] == $parentId) {
$result[] = $element;
$children = $this->normalizeTreeData($data, $element[ $model->keyNameId ]);
if($children) {
$result = array_merge($result, $children);
}
|
d8c1a2e0
Yarik
Big commit artbox
|
96
97
|
}
}
|
8af13427
Yarik
For leha commit.
|
98
|
return $result;
|
d8c1a2e0
Yarik
Big commit artbox
|
99
|
}
|
8af13427
Yarik
For leha commit.
|
100
|
}
|