|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace devgroup\JsTreeWidget; |
|
4
|
|
|
|
|
5
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\base\Action; |
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
use yii\caching\TagDependency; |
|
10
|
|
|
use yii\web\Response; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Helper action for retrieving tree data for jstree by ajax. |
|
14
|
|
|
* Example use in controller: |
|
15
|
|
|
* |
|
16
|
|
|
* ``` php |
|
17
|
|
|
* public function actions() |
|
18
|
|
|
* { |
|
19
|
|
|
* return [ |
|
20
|
|
|
* 'getTree' => [ |
|
21
|
|
|
* 'class' => AdjacencyFullTreeDataAction::className(), |
|
22
|
|
|
* 'class_name' => Category::className(), |
|
23
|
|
|
* 'model_label_attribute' => 'name', |
|
24
|
|
|
* |
|
25
|
|
|
* ], |
|
26
|
|
|
* 'upload' => [ |
|
27
|
|
|
* 'class' => UploadAction::className(), |
|
28
|
|
|
* 'upload' => 'theme/resources/product-images', |
|
29
|
|
|
* ], |
|
30
|
|
|
* 'remove' => [ |
|
31
|
|
|
* 'class' => RemoveAction::className(), |
|
32
|
|
|
* 'uploadDir' => 'theme/resources/product-images', |
|
33
|
|
|
* ], |
|
34
|
|
|
* 'save-info' => [ |
|
35
|
|
|
* 'class' => SaveInfoAction::className(), |
|
36
|
|
|
* ], |
|
37
|
|
|
* ]; |
|
38
|
|
|
* } |
|
39
|
|
|
* ``` |
|
40
|
|
|
*/ |
|
41
|
|
|
class AdjacencyFullTreeDataAction extends Action |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
public $class_name = null; |
|
45
|
|
|
|
|
46
|
|
|
public $model_id_attribute = 'id'; |
|
47
|
|
|
|
|
48
|
|
|
public $model_label_attribute = 'name'; |
|
49
|
|
|
|
|
50
|
|
|
public $model_parent_attribute = 'parent_id'; |
|
51
|
|
|
|
|
52
|
|
|
public $vary_by_type_attribute = null; |
|
53
|
|
|
|
|
54
|
|
|
public $query_parent_attribute = 'id'; |
|
55
|
|
|
|
|
56
|
|
|
public $query_sort_order = 'sort_order'; |
|
57
|
|
|
|
|
58
|
|
|
public $query_selected_attribute = 'selected_id'; |
|
59
|
|
|
/** |
|
60
|
|
|
* Additional conditions for retrieving tree(ie. don't display nodes marked as deleted) |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
public $whereCondition = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Cache key prefix. Should be unique if you have multiple actions with different $whereCondition |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
public $cacheKey = 'FullTree'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Cache lifetime for the full tree |
|
73
|
|
|
* @var int |
|
74
|
|
|
*/ |
|
75
|
|
|
public $cacheLifeTime = 86400; |
|
76
|
|
|
|
|
77
|
|
|
public function init() |
|
78
|
|
|
{ |
|
79
|
|
|
if (!isset($this->class_name)) { |
|
80
|
|
|
throw new InvalidConfigException("Model name should be set in controller actions"); |
|
81
|
|
|
} |
|
82
|
|
|
if (!class_exists($this->class_name)) { |
|
83
|
|
|
throw new InvalidConfigException("Model class does not exists"); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function run() |
|
88
|
|
|
{ |
|
89
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
90
|
|
|
|
|
91
|
|
|
$class = $this->class_name; |
|
92
|
|
|
|
|
93
|
|
|
if (null === $current_selected_id = Yii::$app->request->get($this->query_selected_attribute)) { |
|
94
|
|
|
$current_selected_id = Yii::$app->request->get($this->query_parent_attribute); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$cacheKey = "AdjacencyFullTreeData:{$this->cacheKey}:{$class}:{$this->query_sort_order}"; |
|
98
|
|
|
|
|
99
|
|
|
if (false === $result = Yii::$app->cache->get($cacheKey)) { |
|
100
|
|
|
$query = $class::find() |
|
101
|
|
|
->orderBy([$this->query_sort_order => SORT_ASC]); |
|
102
|
|
|
|
|
103
|
|
|
if (count($this->whereCondition) > 0) { |
|
104
|
|
|
$query = $query->where($this->whereCondition); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (null === $rows = $query->asArray()->all()) { |
|
108
|
|
|
return []; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$result = []; |
|
112
|
|
|
|
|
113
|
|
|
foreach ($rows as $row) { |
|
114
|
|
|
$item = [ |
|
115
|
|
|
'id' => $row[$this->model_id_attribute], |
|
116
|
|
|
'parent' => ($row[$this->model_parent_attribute] > 0) ? $row[$this->model_parent_attribute] : '#', |
|
117
|
|
|
'text' => $row[$this->model_label_attribute], |
|
118
|
|
|
'a_attr' => ['data-id'=>$row[$this->model_id_attribute], 'data-parent_id'=>$row[$this->model_parent_attribute]], |
|
|
|
|
|
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
if (null !== $this->vary_by_type_attribute) { |
|
122
|
|
|
$item['type'] = $row[$this->vary_by_type_attribute]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$result[$row[$this->model_id_attribute]] = $item; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
Yii::$app->cache->set( |
|
129
|
|
|
$cacheKey, |
|
130
|
|
|
$result, |
|
131
|
|
|
86400, |
|
132
|
|
|
new TagDependency([ |
|
133
|
|
|
'tags' => [ |
|
134
|
|
|
ActiveRecordHelper::getCommonTag($class), |
|
135
|
|
|
], |
|
136
|
|
|
]) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (array_key_exists($current_selected_id, $result)) { |
|
141
|
|
|
$result[$current_selected_id] = array_merge($result[$current_selected_id], ['state' => ['opened' => true, 'selected' => true]]); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return array_values($result); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.