1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\shop\controllers; |
4
|
|
|
|
5
|
|
|
use app\backend\actions\PropertyHandler; |
6
|
|
|
use app\backend\components\BackendController; |
7
|
|
|
use app\backend\events\BackendEntityEditEvent; |
8
|
|
|
use app\modules\image\widgets\views\AddImageAction; |
9
|
|
|
use app\modules\shop\models\Category; |
10
|
|
|
use app\models\Object; |
11
|
|
|
use app\models\ViewObject; |
12
|
|
|
use app\properties\HasProperties; |
13
|
|
|
use app\modules\image\widgets\RemoveAction; |
14
|
|
|
use app\modules\image\widgets\SaveInfoAction; |
15
|
|
|
use app\modules\image\widgets\UploadAction; |
16
|
|
|
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction; |
17
|
|
|
use devgroup\JsTreeWidget\TreeNodeMoveAction; |
18
|
|
|
use devgroup\JsTreeWidget\TreeNodesReorderAction; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\db\Query; |
21
|
|
|
use yii\filters\AccessControl; |
22
|
|
|
use yii\helpers\Json; |
23
|
|
|
use yii\helpers\Url; |
24
|
|
|
use yii\web\NotFoundHttpException; |
25
|
|
|
use yii\web\ServerErrorHttpException; |
26
|
|
|
|
27
|
|
|
class BackendCategoryController extends BackendController |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
const BACKEND_CATEGORY_EDIT = 'backend-category-edit'; |
31
|
|
|
const BACKEND_CATEGORY_EDIT_SAVE = 'backend-category-edit-save'; |
32
|
|
|
const BACKEND_CATEGORY_EDIT_FORM = 'backend-category-edit-form'; |
33
|
|
|
const BACKEND_CATEGORY_AFTER_SAVE = 'backend-category-after-save'; |
34
|
|
|
|
35
|
|
|
public function behaviors() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'access' => [ |
39
|
|
|
'class' => AccessControl::className(), |
40
|
|
|
'rules' => [ |
41
|
|
|
[ |
42
|
|
|
'allow' => true, |
43
|
|
|
'roles' => ['category manage'], |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function actions() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'getTree' => [ |
54
|
|
|
'class' => AdjacencyFullTreeDataAction::className(), |
55
|
|
|
'class_name' => Category::className(), |
56
|
|
|
'model_label_attribute' => 'name', |
57
|
|
|
], |
58
|
|
|
'move' => [ |
59
|
|
|
'class' => TreeNodeMoveAction::className(), |
60
|
|
|
'className' => Category::className(), |
61
|
|
|
], |
62
|
|
|
'reorder' => [ |
63
|
|
|
'class' => TreeNodesReorderAction::className(), |
64
|
|
|
'className' => Category::className(), |
65
|
|
|
], |
66
|
|
|
'addImage' => [ |
67
|
|
|
'class' => AddImageAction::className(), |
68
|
|
|
], |
69
|
|
|
'upload' => [ |
70
|
|
|
'class' => UploadAction::className(), |
71
|
|
|
'upload' => 'theme/resources/product-images', |
72
|
|
|
], |
73
|
|
|
'remove' => [ |
74
|
|
|
'class' => RemoveAction::className(), |
75
|
|
|
'uploadDir' => 'theme/resources/product-images', |
76
|
|
|
], |
77
|
|
|
'save-info' => [ |
78
|
|
|
'class' => SaveInfoAction::className(), |
79
|
|
|
], |
80
|
|
|
'property-handler' => [ |
81
|
|
|
'class' => PropertyHandler::className(), |
82
|
|
|
'modelName' => Category::className() |
83
|
|
|
] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function actionIndex($parent_id = 0) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$searchModel = new Category(); |
90
|
|
|
$searchModel->parent_id = $parent_id; |
91
|
|
|
|
92
|
|
|
$params = Yii::$app->request->get(); |
93
|
|
|
|
94
|
|
|
$dataProvider = $searchModel->search($params); |
95
|
|
|
|
96
|
|
|
$model = null; |
97
|
|
|
if ($parent_id > 0) { |
98
|
|
|
$model = Category::findOne($parent_id); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->render( |
102
|
|
|
'index', |
103
|
|
|
[ |
104
|
|
|
'dataProvider' => $dataProvider, |
105
|
|
|
'searchModel' => $searchModel, |
106
|
|
|
'model' => $model, |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function actionEdit($parent_id = null, $id = null) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if (null === $parent_id) { |
114
|
|
|
throw new NotFoundHttpException; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (null === $object = Object::getForClass(Category::className())) { |
118
|
|
|
throw new ServerErrorHttpException; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @var null|Category|HasProperties $model */ |
122
|
|
|
$model = null; |
123
|
|
|
if (null !== $id) { |
124
|
|
|
$model = Category::findById($id, null, null); |
|
|
|
|
125
|
|
|
} else { |
126
|
|
|
$parent = Category::findById($parent_id, null, null); |
|
|
|
|
127
|
|
|
if ($parent_id === '0' || !is_null($parent)) { |
128
|
|
|
$model = new Category; |
129
|
|
|
$model->loadDefaultValues(); |
130
|
|
|
$model->parent_id = $parent_id; |
131
|
|
|
if ($parent_id !== '0') { |
132
|
|
|
$model->category_group_id = $parent->category_group_id; |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
throw new ServerErrorHttpException; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (null === $model) { |
140
|
|
|
throw new ServerErrorHttpException; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$event = new BackendEntityEditEvent($model); |
144
|
|
|
$this->trigger(self::BACKEND_CATEGORY_EDIT, $event); |
145
|
|
|
|
146
|
|
|
$post = \Yii::$app->request->post(); |
147
|
|
|
if ($event->isValid && $model->load($post) && $model->validate()) { |
148
|
|
|
$saveStateEvent = new BackendEntityEditEvent($model); |
149
|
|
|
$this->trigger(self::BACKEND_CATEGORY_EDIT_SAVE, $saveStateEvent); |
150
|
|
|
|
151
|
|
|
$save_result = $model->save(); |
152
|
|
|
$model->saveProperties($post); |
|
|
|
|
153
|
|
|
|
154
|
|
View Code Duplication |
if (null !== $view_object = ViewObject::getByModel($model, true)) { |
155
|
|
|
if ($view_object->load($post, 'ViewObject')) { |
156
|
|
|
if ($view_object->view_id <= 0) { |
157
|
|
|
$view_object->delete(); |
158
|
|
|
} else { |
159
|
|
|
$view_object->save(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($save_result) { |
165
|
|
|
$modelAfterSaveEvent = new BackendEntityEditEvent($model); |
166
|
|
|
$this->trigger(self::BACKEND_CATEGORY_AFTER_SAVE, $modelAfterSaveEvent); |
167
|
|
|
|
168
|
|
|
$this->runAction('save-info', ['model_id'=>$model->id]); |
169
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
170
|
|
|
$returnUrl = Yii::$app->request->get('returnUrl', ['index']); |
171
|
|
|
switch (Yii::$app->request->post('action', 'save')) { |
172
|
|
|
case 'next': |
173
|
|
|
return $this->redirect( |
174
|
|
|
[ |
175
|
|
|
'edit', |
176
|
|
|
'returnUrl' => $returnUrl, |
177
|
|
|
'parent_id' => Yii::$app->request->get('parent_id', null) |
178
|
|
|
] |
179
|
|
|
); |
180
|
|
|
case 'back': |
181
|
|
|
return $this->redirect($returnUrl); |
182
|
|
|
default: |
183
|
|
|
return $this->redirect( |
184
|
|
|
Url::toRoute( |
185
|
|
|
[ |
186
|
|
|
'edit', |
187
|
|
|
'id' => $model->id, |
188
|
|
|
'returnUrl' => $returnUrl, |
189
|
|
|
'parent_id' => $model->parent_id |
190
|
|
|
] |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} else { |
195
|
|
|
throw new ServerErrorHttpException; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->render( |
200
|
|
|
'category-form', |
201
|
|
|
[ |
202
|
|
|
'model' => $model, |
203
|
|
|
'object' => $object, |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function actionDelete($id = null, $parent_id = null, $mode = null) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
|
211
|
|
|
if ((null === $id) || (null === $model = Category::findById($id, null, null))) { |
|
|
|
|
212
|
|
|
throw new NotFoundHttpException; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$model->deleteMode = $mode; |
216
|
|
|
if (!$model->delete()) { |
|
|
|
|
217
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'The object is placed in the cart')); |
218
|
|
|
} else { |
219
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Object has been removed')); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->redirect( |
223
|
|
|
Yii::$app->request->get( |
224
|
|
|
'returnUrl', |
225
|
|
|
Url::to(['index', 'parent_id' => $model->parent_id]) |
226
|
|
|
) |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function actionRemoveAll($parent_id, $mode = null) |
232
|
|
|
{ |
233
|
|
|
$items = Yii::$app->request->post('items', []); |
234
|
|
|
if (!empty($items)) { |
235
|
|
|
$items = Category::findAll(['id' => $items]); |
236
|
|
|
/** @var Category[] $items */ |
237
|
|
|
foreach ($items as $item) { |
238
|
|
|
$item->deleteMode = $mode; |
239
|
|
|
$item->delete(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
return $this->redirect(['index', 'parent_id' => $parent_id]); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function actionAutocomplete() |
246
|
|
|
{ |
247
|
|
|
/** |
248
|
|
|
* @todo Добавить отображение вложенности |
|
|
|
|
249
|
|
|
*/ |
250
|
|
|
$search = Yii::$app->request->get('search'); |
251
|
|
|
$out = ['more' => false]; |
252
|
|
|
if (!is_null($search['term'])) { |
253
|
|
|
$query = new Query; |
254
|
|
|
$query->select('id, name AS text')->from(Category::tableName())->andWhere(['like', 'name', $search['term']])->limit( |
|
|
|
|
255
|
|
|
100 |
256
|
|
|
); |
257
|
|
|
$command = $query->createCommand(); |
258
|
|
|
$data = $command->queryAll(); |
259
|
|
|
$out['results'] = array_values($data); |
260
|
|
|
}/* elseif ($id > 0) { |
|
|
|
|
261
|
|
|
$out['results'] = ['id' => $id, 'text' => Category::findOne($id)->name]; |
262
|
|
|
} else { |
263
|
|
|
$out['results'] = ['id' => 0, 'text' => Yii::t('app', 'No matching records found')]; |
264
|
|
|
}*/ |
265
|
|
|
echo Json::encode($out); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.