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