|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backend\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\backend\models\BackendMenu; |
|
6
|
|
|
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\filters\AccessControl; |
|
9
|
|
|
use yii\helpers\Url; |
|
10
|
|
|
use yii\web\Controller; |
|
11
|
|
|
use yii\web\Cookie; |
|
12
|
|
|
use yii\web\HttpException; |
|
13
|
|
|
use yii\web\NotFoundHttpException; |
|
14
|
|
|
use yii\web\ServerErrorHttpException; |
|
15
|
|
|
|
|
16
|
|
|
class BackendMenuController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
View Code Duplication |
public function behaviors() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'access' => [ |
|
23
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
|
|
24
|
|
|
'rules' => [ |
|
25
|
|
|
[ |
|
26
|
|
|
'allow' => true, |
|
27
|
|
|
'roles' => ['setting manage'], |
|
28
|
|
|
], |
|
29
|
|
|
], |
|
30
|
|
|
], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
public function actions() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
'getTree' => [ |
|
38
|
|
|
'class' => AdjacencyFullTreeDataAction::className(), |
|
|
|
|
|
|
39
|
|
|
'class_name' => BackendMenu::className(), |
|
|
|
|
|
|
40
|
|
|
'model_label_attribute' => 'name', |
|
41
|
|
|
], |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
public function actionIndex($parent_id = 1) |
|
46
|
|
|
{ |
|
47
|
|
|
$searchModel = new BackendMenu(); |
|
48
|
|
|
$searchModel->parent_id = $parent_id; |
|
49
|
|
|
|
|
50
|
|
|
$params = Yii::$app->request->get(); |
|
51
|
|
|
|
|
52
|
|
|
$dataProvider = $searchModel->search($params); |
|
53
|
|
|
|
|
54
|
|
|
$model = null; |
|
55
|
|
|
if ($parent_id > 0) { |
|
56
|
|
|
$model = BackendMenu::findOne($parent_id); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->render( |
|
60
|
|
|
'index', |
|
61
|
|
|
[ |
|
62
|
|
|
'dataProvider' => $dataProvider, |
|
63
|
|
|
'searchModel' => $searchModel, |
|
64
|
|
|
'model' => $model, |
|
65
|
|
|
] |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function actionEdit($parent_id = null, $id = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $parent_id) { |
|
72
|
|
|
throw new NotFoundHttpException; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var null|BackendMenu|HasProperties $model */ |
|
76
|
|
|
$model = null; |
|
77
|
|
|
if (null !== $id) { |
|
78
|
|
|
$model = BackendMenu::findById($id); |
|
79
|
|
|
} else { |
|
80
|
|
|
if (null !== $parent = BackendMenu::findById($parent_id)) { |
|
81
|
|
|
$model = new BackendMenu; |
|
82
|
|
|
$model->loadDefaultValues(); |
|
83
|
|
|
$model->parent_id = $parent_id; |
|
84
|
|
|
|
|
85
|
|
|
} else { |
|
86
|
|
|
$model = new BackendMenu; |
|
87
|
|
|
$model->loadDefaultValues(); |
|
88
|
|
|
$model->parent_id = 0; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (null === $model) { |
|
93
|
|
|
throw new ServerErrorHttpException; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$post = \Yii::$app->request->post(); |
|
97
|
|
|
if ($model->load($post) && $model->validate()) { |
|
98
|
|
|
if ($model->save()) { |
|
99
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
|
100
|
|
|
$returnUrl = Yii::$app->request->get('returnUrl', ['/backend/backend-menu/index']); |
|
101
|
|
|
switch (Yii::$app->request->post('action', 'save')) { |
|
102
|
|
|
case 'next': |
|
103
|
|
|
return $this->redirect( |
|
104
|
|
|
[ |
|
105
|
|
|
'/backend/backend-menu/edit', |
|
106
|
|
|
'returnUrl' => $returnUrl, |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
case 'back': |
|
110
|
|
|
return $this->redirect($returnUrl); |
|
111
|
|
|
default: |
|
112
|
|
|
return $this->redirect( |
|
113
|
|
|
Url::toRoute( |
|
114
|
|
|
[ |
|
115
|
|
|
'/backend/backend-menu/edit', |
|
116
|
|
|
'id' => $model->id, |
|
117
|
|
|
'returnUrl' => $returnUrl, |
|
118
|
|
|
'parent_id' => $model->parent_id |
|
119
|
|
|
] |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
throw new ServerErrorHttpException; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->render( |
|
130
|
|
|
'form', |
|
131
|
|
|
[ |
|
132
|
|
|
'model' => $model, |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function actionDelete($id = null, $parent_id = null) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
|
|
140
|
|
|
if ((null === $id) || (null === $model = BackendMenu::findById($id))) { |
|
141
|
|
|
throw new NotFoundHttpException; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (!$model->delete()) { |
|
|
|
|
|
|
145
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'The object is placed in the cart')); |
|
146
|
|
|
} else { |
|
147
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Object has been removed')); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->redirect(Url::to(['index', 'parent_id' => $model->parent_id])); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function actionRemoveAll($parent_id) |
|
154
|
|
|
{ |
|
155
|
|
|
$items = Yii::$app->request->post('items', []); |
|
156
|
|
|
if (!empty($items)) { |
|
157
|
|
|
$items = BackendMenu::find()->where(['in', 'id', $items])->all(); |
|
158
|
|
|
foreach ($items as $item) { |
|
159
|
|
|
$item->delete(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->redirect(['index', 'parent_id' => $parent_id]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
public function actionAjaxToggle($status) |
|
168
|
|
|
{ |
|
169
|
|
|
if (!Yii::$app->request->isAjax) { |
|
170
|
|
|
throw new HttpException(403); |
|
171
|
|
|
} |
|
172
|
|
|
$currentStatus = Yii::$app->request->cookies->getValue('backend_menu', 'normal'); |
|
173
|
|
|
$cookieData =[ |
|
174
|
|
|
'name' => 'backend_menu', |
|
175
|
|
|
'value' => 'normal', |
|
176
|
|
|
'expire' => time() + 86400 * 365, |
|
177
|
|
|
]; |
|
178
|
|
|
if ($status !== $currentStatus) { |
|
179
|
|
|
$cookieData['value'] = $status; |
|
180
|
|
|
} |
|
181
|
|
|
Yii::$app->response->cookies->add(new Cookie($cookieData)); |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.