1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\page\backend; |
4
|
|
|
|
5
|
|
|
use app\backend\actions\PropertyHandler; |
6
|
|
|
use app\backend\events\BackendEntityEditEvent; |
7
|
|
|
use app\models\Object; |
8
|
|
|
use app\models\ObjectPropertyGroup; |
9
|
|
|
use app\models\Property; |
10
|
|
|
use app\modules\image\models\Image; |
11
|
|
|
use app\modules\image\widgets\views\AddImageAction; |
12
|
|
|
use app\modules\page\models\Page; |
13
|
|
|
use app\models\ViewObject; |
14
|
|
|
use app\properties\HasProperties; |
15
|
|
|
use app\modules\image\widgets\RemoveAction; |
16
|
|
|
use app\modules\image\widgets\SaveInfoAction; |
17
|
|
|
use app\modules\image\widgets\UploadAction; |
18
|
|
|
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction; |
19
|
|
|
use devgroup\JsTreeWidget\TreeNodeMoveAction; |
20
|
|
|
use devgroup\JsTreeWidget\TreeNodesReorderAction; |
21
|
|
|
use Yii; |
22
|
|
|
use yii\db\Query; |
23
|
|
|
use yii\filters\AccessControl; |
24
|
|
|
use yii\helpers\ArrayHelper; |
25
|
|
|
use yii\helpers\Url; |
26
|
|
|
use yii\web\NotFoundHttpException; |
27
|
|
|
|
28
|
|
|
class PageController extends \app\backend\components\BackendController |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
const BACKEND_PAGE_EDIT = 'backend-page-edit'; |
32
|
|
|
const BACKEND_PAGE_EDIT_SAVE = 'backend-page-edit-save'; |
33
|
|
|
const BACKEND_PAGE_EDIT_FORM = 'backend-page-edit-form'; |
34
|
|
|
const BACKEND_PAGE_AFTER_SAVE = 'backend-page-after-save'; |
35
|
|
|
|
36
|
|
|
public function behaviors() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
'access' => [ |
40
|
|
|
'class' => AccessControl::className(), |
41
|
|
|
'rules' => [ |
42
|
|
|
[ |
43
|
|
|
'allow' => true, |
44
|
|
|
'roles' => ['content manage'], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function actions() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'getTree' => [ |
55
|
|
|
'class' => AdjacencyFullTreeDataAction::className(), |
56
|
|
|
'class_name' => Page::className(), |
57
|
|
|
'model_label_attribute' => 'name', |
58
|
|
|
], |
59
|
|
|
'addImage' => [ |
60
|
|
|
'class' => AddImageAction::className(), |
61
|
|
|
], |
62
|
|
|
'upload' => [ |
63
|
|
|
'class' => UploadAction::className(), |
64
|
|
|
'upload' => 'theme/resources/product-images', |
65
|
|
|
], |
66
|
|
|
'remove' => [ |
67
|
|
|
'class' => RemoveAction::className(), |
68
|
|
|
'uploadDir' => 'theme/resources/product-images', |
69
|
|
|
], |
70
|
|
|
'save-info' => [ |
71
|
|
|
'class' => SaveInfoAction::className(), |
72
|
|
|
], |
73
|
|
|
'property-handler' => [ |
74
|
|
|
'class' => PropertyHandler::className(), |
75
|
|
|
'modelName' => Page::className() |
76
|
|
|
], |
77
|
|
|
'move' => [ |
78
|
|
|
'class' => TreeNodeMoveAction::className(), |
79
|
|
|
'className' => Page::className(), |
80
|
|
|
'saveAttributes' => ['slug_compiled'], |
81
|
|
|
], |
82
|
|
|
'reorder' => [ |
83
|
|
|
'class' => TreeNodesReorderAction::className(), |
84
|
|
|
'className' => Page::className(), |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function actionIndex($parent_id = 1) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$searchModel = new Page(); |
92
|
|
|
$searchModel->parent_id = $parent_id; |
93
|
|
|
|
94
|
|
|
$params = Yii::$app->request->get(); |
95
|
|
|
$dataProvider = $searchModel->search($params); |
96
|
|
|
|
97
|
|
|
$model = null; |
98
|
|
|
if ($parent_id > 0) { |
99
|
|
|
$model = Page::findOne($parent_id); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->render( |
103
|
|
|
'index', |
104
|
|
|
[ |
105
|
|
|
'dataProvider' => $dataProvider, |
106
|
|
|
'searchModel' => $searchModel, |
107
|
|
|
'model' => $model, |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function actionEdit($parent_id, $id = null) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$object = Object::getForClass(Page::className()); |
115
|
|
|
|
116
|
|
|
/** @var null|Page|HasProperties $model */ |
117
|
|
|
$model = new Page; |
118
|
|
|
$model->published = 1; |
119
|
|
|
if ($id !== null) { |
120
|
|
|
$model = Page::findOne($id); |
121
|
|
|
if ($model === null) { |
122
|
|
|
throw new NotFoundHttpException; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$model->parent_id = $parent_id; |
126
|
|
|
|
127
|
|
|
$event = new BackendEntityEditEvent($model); |
128
|
|
|
$this->trigger(self::BACKEND_PAGE_EDIT, $event); |
129
|
|
|
|
130
|
|
|
$post = \Yii::$app->request->post(); |
131
|
|
|
if ($event->isValid && $model->load($post)) { |
|
|
|
|
132
|
|
|
$saveStateEvent = new BackendEntityEditEvent($model); |
133
|
|
|
$this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent); |
134
|
|
|
|
135
|
|
|
if ($saveStateEvent->isValid && $model->validate()) { |
|
|
|
|
136
|
|
|
$save_result = $model->save(); |
|
|
|
|
137
|
|
|
$model->saveProperties($post); |
|
|
|
|
138
|
|
|
|
139
|
|
View Code Duplication |
if (null !== $view_object = ViewObject::getByModel($model, true)) { |
140
|
|
|
if ($view_object->load($post, 'ViewObject')) { |
141
|
|
|
if ($view_object->view_id <= 0) { |
142
|
|
|
$view_object->delete(); |
143
|
|
|
} else { |
144
|
|
|
$view_object->save(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($save_result) { |
150
|
|
|
$modelAfterSaveEvent = new BackendEntityEditEvent($model); |
151
|
|
|
$this->trigger(self::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent); |
152
|
|
|
|
153
|
|
|
$this->runAction('save-info', ['model_id' => $model->id]); |
154
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); |
155
|
|
|
$returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']); |
156
|
|
|
switch (Yii::$app->request->post('action', 'save')) { |
157
|
|
|
case 'next': |
158
|
|
|
return $this->redirect( |
159
|
|
|
[ |
160
|
|
|
'/page/backend/edit', |
161
|
|
|
'returnUrl' => $returnUrl, |
162
|
|
|
'parent_id' => Yii::$app->request->get('parent_id', null) |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
case 'back': |
166
|
|
|
return $this->redirect($returnUrl); |
167
|
|
|
default: |
168
|
|
|
return $this->redirect( |
169
|
|
|
Url::toRoute( |
170
|
|
|
[ |
171
|
|
|
'/page/backend/edit', |
172
|
|
|
'id' => $model->id, |
173
|
|
|
'returnUrl' => $returnUrl, |
174
|
|
|
'parent_id' => $model->parent_id |
175
|
|
|
] |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} else { |
180
|
|
|
\Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->render( |
186
|
|
|
'page-form', |
187
|
|
|
[ |
188
|
|
|
'model' => $model, |
189
|
|
|
'object' => $object, |
190
|
|
|
] |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/* |
195
|
|
|
* |
196
|
|
|
*/ |
197
|
|
|
public function actionDelete($id = null) |
198
|
|
|
{ |
199
|
|
|
if ((null === $id) || (null === $model = Page::findOne($id))) { |
200
|
|
|
throw new NotFoundHttpException; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (!$model->delete()) { |
|
|
|
|
204
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'The object is placed in the cart')); |
205
|
|
|
} else { |
206
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed')); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->redirect( |
210
|
|
|
Yii::$app->request->get( |
211
|
|
|
'returnUrl', |
212
|
|
|
Url::toRoute(['index', 'parent_id' => $model->parent_id]) |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function actionRemoveAll($parent_id) |
218
|
|
|
{ |
219
|
|
|
$items = Yii::$app->request->post('items', []); |
220
|
|
|
if (!empty($items)) { |
221
|
|
|
$items = Page::find()->where(['in', 'id', $items])->all(); |
222
|
|
|
foreach ($items as $item) { |
223
|
|
|
$item->delete(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->redirect(['index', 'parent_id' => $parent_id]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/* |
231
|
|
|
* |
232
|
|
|
*/ |
233
|
|
|
public function actionRestore($id = null, $parent_id = null) |
234
|
|
|
{ |
235
|
|
|
if (null === $id) { |
236
|
|
|
new NotFoundHttpException(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (null === $model = Page::findOne(['id' => $id])) { |
240
|
|
|
new NotFoundHttpException(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$model->restoreFromTrash(); |
|
|
|
|
244
|
|
|
|
245
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Object successfully restored')); |
246
|
|
|
|
247
|
|
|
return $this->redirect( |
248
|
|
|
Yii::$app->request->get( |
249
|
|
|
'returnUrl', |
250
|
|
|
Url::toRoute(['edit', 'id' => $id, 'parent_id' => $parent_id]) |
251
|
|
|
) |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Clone page action. |
258
|
|
|
* @param integer $id |
259
|
|
|
* @param array|string $returnUrl |
260
|
|
|
* @throws \yii\web\NotFoundHttpException |
261
|
|
|
*/ |
262
|
|
|
public function actionClone($id, $returnUrl = ['index']) |
263
|
|
|
{ |
264
|
|
|
/** @var Page|HasProperties $model */ |
265
|
|
|
$model = Page::findOne($id); |
266
|
|
|
if ($model === null) { |
267
|
|
|
throw new NotFoundHttpException; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** @var Page|HasProperties $newModel */ |
271
|
|
|
$newModel = new Page; |
272
|
|
|
$newModel->setAttributes($model->attributes, false); |
|
|
|
|
273
|
|
|
$time = time(); |
274
|
|
|
$newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')'; |
275
|
|
|
$newModel->slug .= '-copy-' . date('Ymdhis', $time); |
276
|
|
|
$newModel->title .= '-copy-' . date('Ymdhis', $time); |
277
|
|
|
$newModel->id = null; |
278
|
|
View Code Duplication |
if ($newModel->validate() === false) { |
|
|
|
|
279
|
|
|
$newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80); |
280
|
|
|
} |
281
|
|
|
if ($newModel->save()) { |
|
|
|
|
282
|
|
|
$object = Object::getForClass(get_class($newModel)); |
283
|
|
|
$query = new Query(); |
284
|
|
|
|
285
|
|
|
// save images bindings |
286
|
|
|
$params = $query->select( |
287
|
|
|
['object_id', 'filename', 'image_title', 'image_alt', 'sort_order'] |
288
|
|
|
)->from(Image::tableName())->where( |
289
|
|
|
[ |
290
|
|
|
'object_id' => $object->id, |
291
|
|
|
'object_model_id' => $model->id |
292
|
|
|
] |
293
|
|
|
)->all(); |
294
|
|
View Code Duplication |
if (!empty($params)) { |
295
|
|
|
$rows = []; |
296
|
|
|
foreach ($params as $param) { |
297
|
|
|
$rows[] = [ |
298
|
|
|
$param['object_id'], |
299
|
|
|
$newModel->id, |
300
|
|
|
$param['filename'], |
301
|
|
|
$param['image_title'], |
302
|
|
|
$param['image_alt'], |
303
|
|
|
$param['sort_order'], |
304
|
|
|
]; |
305
|
|
|
} |
306
|
|
|
Yii::$app->db->createCommand()->batchInsert( |
307
|
|
|
Image::tableName(), |
308
|
|
|
[ |
309
|
|
|
'object_id', |
310
|
|
|
'object_model_id', |
311
|
|
|
'filename', |
312
|
|
|
'image_title', |
313
|
|
|
'image_alt', |
314
|
|
|
'sort_order', |
315
|
|
|
], |
316
|
|
|
$rows |
317
|
|
|
)->execute(); |
318
|
|
|
} |
319
|
|
|
$newModelProps = []; |
320
|
|
View Code Duplication |
foreach (array_keys($model->propertyGroups) as $key) { |
321
|
|
|
$opg = new ObjectPropertyGroup(); |
322
|
|
|
$opg->attributes = [ |
323
|
|
|
'object_id' => $object->id, |
324
|
|
|
'object_model_id' => $newModel->id, |
325
|
|
|
'property_group_id' => $key, |
326
|
|
|
]; |
327
|
|
|
$opg->save(); |
328
|
|
|
$props = Property::getForGroupId($key); |
329
|
|
|
foreach ($props as $prop) { |
|
|
|
|
330
|
|
|
$propValues = $model->getPropertyValuesByPropertyId($prop->id); |
|
|
|
|
331
|
|
|
if ($propValues !== null) { |
332
|
|
|
foreach ($propValues->values as $val) { |
333
|
|
|
$valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']); |
334
|
|
|
$newModelProps[$prop->key][] = $valueToSave; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
$newModel->saveProperties( |
|
|
|
|
340
|
|
|
[ |
341
|
|
|
'Properties_Page_' . $newModel->id => $newModelProps, |
342
|
|
|
] |
343
|
|
|
); |
344
|
|
|
$view = ViewObject::findOne(['object_id' => $model->object->id, 'object_model_id' => $model->id]); |
345
|
|
|
if ($view !== null) { |
346
|
|
|
$newView = new ViewObject; |
347
|
|
|
$newView->setAttributes($view->attributes, false); |
348
|
|
|
$newView->id = null; |
349
|
|
|
$newView->object_model_id = $newModel->id; |
350
|
|
|
$newView->save(); |
351
|
|
|
} |
352
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Page has been cloned successfully.')); |
353
|
|
|
$this->redirect( |
354
|
|
|
['edit', 'id' => $newModel->id, 'parent_id' => $newModel->parent_id, 'returnUrl' => $returnUrl] |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
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.