-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeTreeBehavior.php
More file actions
149 lines (128 loc) · 3.93 KB
/
Copy pathChangeTreeBehavior.php
File metadata and controls
149 lines (128 loc) · 3.93 KB
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
<?php
namespace ale10257\ext;
use creocoder\nestedsets\NestedSetsBehavior;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;
class ChangeTreeBehavior extends Behavior
{
public $rootSite;
/**
* @param array $post
* @return mixed
*/
public function updateTree($post)
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
$one = $owner->find()->where(['id' => $post['first']])->one();
$two = $owner->find()->where(['id' => $post['two']])->one();
/**
* @var $one NestedSetsBehavior
* @var $two NestedSetsBehavior
*/
if ($one && $two) {
$parent_one = $one->parents(1)->one();
$parent_two = $two->parents(1)->one();
//if ($parent_one->id == $parent_two->id) {
/**
* @var $two ActiveRecord
*/
if ($post['action'] == 'before') $one->insertBefore($two);
if ($post['action'] == 'after') $one->insertAfter($two);
//}
}
return $this->getTree();
}
/**
* @return bool
*/
public function getTree()
{
/**
* @var $root NestedSetsBehavior
*/
if ($root = $this->getRoot()) {
return $root->children()->all();
}
return false;
}
/**
* @return bool|array
*/
public function getTreeAsArray()
{
/**
* @var $root NestedSetsBehavior
*/
if ($root = $this->getRoot()) {
return $root->children()->asArray()->all();
}
return false;
}
public function createItem($parent)
{
/* @var $owner BaseActiveRecord */
if ($parent) {
$result['parents'] = $parent;
$result['parent_id'] = $parent->id;
} else {
$root = $this->getRoot();
$result['parents'] = $root;
$result['parent_id'] = $root->id;
}
$result['parents'] = $this->setRootName($result['parents']);
return $result;
}
public function updateItem($node, $nameFieldForType)
{
/* @var $owner BaseActiveRecord */
/* @var $node BaseActiveRecord */
/* @var $item BaseActiveRecord */
/* @var $parent BaseActiveRecord */
/* @var $root NestedSetsBehavior */
/* @var $parent NestedSetsBehavior */
/* @var $node NestedSetsBehavior */
$root = $this->getRoot();
$parents = $root->children()->andWhere([$nameFieldForType => $node->$nameFieldForType])->all();
array_unshift($parents, $root);
$parent = $node->parents(1)->one();
$childs = [];
foreach ($node->children()->all() as $item) {
$childs[$item->primaryKey] = $item;
}
$result['parent_id'] = $parent->primaryKey;
foreach ($parents as $key => $item) {
if (array_key_exists($item->primaryKey, $childs)) {
unset($parents[$key]);
}
if ($item->primaryKey == $node->primaryKey) {
unset($parents[$key]);
}
}
$parents[0] = $this->setRootName($parents[0]);
$result['parents'] = $parents;
return $result;
}
public function getRoot (){
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
return $owner->findOne(['name' => $this->rootSite]);
}
public function checkRoot (){
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
return $owner->find()->where(['name' => $this->rootSite])->count();
}
private function setRootName($parent)
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if (!empty($owner->root_name)) {
if ($parent->name == $this->rootSite) {
$parent->name = $owner->root_name;
}
}
return $parent;
}
}