1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backend\actions; |
4
|
|
|
|
5
|
|
|
use app; |
6
|
|
|
use yii; |
7
|
|
|
use yii\base\Action; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
|
10
|
|
|
class JSSelectableTreeGetTree extends Action |
11
|
|
|
{ |
12
|
|
|
public $modelName = null; |
13
|
|
|
|
14
|
|
|
public $label_attribute = 'name'; |
15
|
|
|
public $parent_attribute = 'parent_id'; |
16
|
|
|
public $additional_search_conditions = []; |
17
|
|
|
public $query_parent_attribute = 'id'; |
18
|
|
|
public $vary_by_type_attribute = 'show_type'; |
19
|
|
|
public $order = []; |
20
|
|
|
|
21
|
|
|
|
22
|
|
View Code Duplication |
public function init() |
23
|
|
|
{ |
24
|
|
|
if (!isset($this->modelName)) { |
25
|
|
|
throw new InvalidConfigException("Model name should be set in controller actions"); |
26
|
|
|
} |
27
|
|
|
if (!class_exists($this->modelName)) { |
28
|
|
|
throw new InvalidConfigException("Model class does not exists"); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function run() |
33
|
|
|
{ |
34
|
|
|
if (!isset($_GET[$this->query_parent_attribute])) { |
35
|
|
|
throw new yii\web\NotFoundHttpException; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$modelName = $this->modelName; |
39
|
|
|
$selectedItems = Yii::$app->request->get('selectedItems', []); |
40
|
|
|
if (!empty($selectedItems)) { |
41
|
|
|
$selectedItems = explode(',', $selectedItems); |
42
|
|
|
} |
43
|
|
|
$parents = [0]; |
44
|
|
|
$ids = $selectedItems; |
45
|
|
|
$q = new yii\db\Query; |
46
|
|
|
$q->select('parent_id') |
47
|
|
|
->from($modelName::tableName()); |
48
|
|
|
while (!empty($ids)) { |
49
|
|
|
$q->where(['id' => $ids]); |
50
|
|
|
$result = $q->all(); |
51
|
|
|
$ids = []; |
52
|
|
|
foreach ($result as $row) { |
53
|
|
|
if (!in_array($row['parent_id'], $parents)) { |
54
|
|
|
$parents[] = $row['parent_id']; |
55
|
|
|
} |
56
|
|
|
if ($row['parent_id'] != 0 && !in_array($row['parent_id'], $ids)) { |
57
|
|
|
$ids[] = $row['parent_id']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$q = null; |
|
|
|
|
62
|
|
|
$ids = null; |
|
|
|
|
63
|
|
|
// |
64
|
|
|
Yii::$app->response->format = yii\web\Response::FORMAT_JSON; |
65
|
|
|
|
66
|
|
|
$query = new yii\db\Query; |
67
|
|
|
|
68
|
|
|
$fields = 'id, '.$this->label_attribute.', '.$this->parent_attribute; |
69
|
|
|
if (isset($this->vary_by_type_attribute)) { |
70
|
|
|
$fields .= ', '.$this->vary_by_type_attribute; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (isset($this->expand_in_admin_attribute)) { |
74
|
|
|
$fields .= ', '.$this->expand_in_admin_attribute; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$fields .= ', ('; |
78
|
|
|
$fields .= 'SELECT count(id) FROM '.$modelName::tableName().' counter '; |
79
|
|
|
$fields .= 'WHERE counter.parent_id = '.$modelName::tableName().'.id)'; |
80
|
|
|
$fields .= " AS 'children_count'"; |
81
|
|
|
|
82
|
|
|
$query->select($fields) |
83
|
|
|
->from($modelName::tableName()) |
84
|
|
|
->where([$this->parent_attribute => $_GET[$this->query_parent_attribute]]) |
85
|
|
|
->andWhere($this->additional_search_conditions) |
86
|
|
|
->orderBy($this->order); |
87
|
|
|
|
88
|
|
|
$rows = $query |
89
|
|
|
->all(); |
90
|
|
|
$result = []; |
91
|
|
|
foreach ($rows as $row) { |
92
|
|
|
$item = [ |
93
|
|
|
'id' => $row['id'], |
94
|
|
|
'text' => $row[$this->label_attribute], |
95
|
|
|
]; |
96
|
|
|
if (isset($this->vary_by_type_attribute)) { |
97
|
|
|
$item['type'] = $row[$this->vary_by_type_attribute]; |
98
|
|
|
} |
99
|
|
|
$item['children'] = $row['children_count'] > 0; |
100
|
|
|
$item['a_attr'] = ['data-id'=>$row['id'], 'data-parent-id'=>$row['parent_id']]; |
101
|
|
|
$item['state'] = []; |
102
|
|
|
|
103
|
|
|
if (in_array($row['id'], $parents)) { |
104
|
|
|
$item['state']['opened'] = true; |
105
|
|
|
} |
106
|
|
|
if (is_array($selectedItems) && in_array($row['id'], $selectedItems)) { |
107
|
|
|
$item['state']['selected'] = true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (isset($this->expand_in_admin_attribute)) { |
111
|
|
|
if ($row[$this->expand_in_admin_attribute]) { |
|
|
|
|
112
|
|
|
$item['state']['opened'] = true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
if (isset($_GET['selected_id']) && $item['id'] == $_GET['selected_id']) { |
116
|
|
|
$item['state']['selected'] = true; |
117
|
|
|
} |
118
|
|
|
if (count($item['state']) == 0) { |
119
|
|
|
unset($item['state']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
$result[] = $item; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $result; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.