1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\shop\controllers; |
4
|
|
|
|
5
|
|
|
use app\components\Controller; |
6
|
|
|
use app\extensions\DefaultTheme\components\BaseWidget; |
7
|
|
|
use app\extensions\DefaultTheme\models\ThemeActiveWidgets; |
8
|
|
|
use app\extensions\DefaultTheme\models\ThemeWidgets; |
9
|
|
|
use app\extensions\DefaultTheme\widgets\FilterSets\Widget; |
10
|
|
|
use app\models\PropertyStaticValues; |
11
|
|
|
use app\modules\core\helpers\EventTriggeringHelper; |
12
|
|
|
use app\modules\shop\events\ProductPageShowed; |
13
|
|
|
use app\modules\shop\models\Category; |
14
|
|
|
use app\models\Object; |
15
|
|
|
use app\modules\shop\models\Product; |
16
|
|
|
use app\models\Search; |
17
|
|
|
use app\traits\DynamicContentTrait; |
18
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\caching\TagDependency; |
21
|
|
|
use yii\data\Pagination; |
22
|
|
|
use yii\helpers\ArrayHelper; |
23
|
|
|
use yii\helpers\Json; |
24
|
|
|
use yii\helpers\Url; |
25
|
|
|
use yii\web\ForbiddenHttpException; |
26
|
|
|
use yii\web\NotFoundHttpException; |
27
|
|
|
use yii\web\Response; |
28
|
|
|
use yii\web\ServerErrorHttpException; |
29
|
|
|
|
30
|
|
|
class ProductController extends Controller |
31
|
|
|
{ |
32
|
|
|
use DynamicContentTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Products listing by category with filtration support. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
* @throws \Exception |
39
|
|
|
* @throws NotFoundHttpException |
40
|
|
|
* @throws ServerErrorHttpException |
41
|
|
|
*/ |
42
|
|
|
public function actionList() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
$request = Yii::$app->request; |
46
|
|
|
|
47
|
|
|
if (null === $request->get('category_group_id')) { |
48
|
|
|
throw new NotFoundHttpException; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (null === $object = Object::getForClass(Product::className())) { |
52
|
|
|
throw new ServerErrorHttpException('Object not found.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$category_group_id = intval($request->get('category_group_id', 0)); |
56
|
|
|
|
57
|
|
|
$title_append = $request->get('title_append', ''); |
58
|
|
|
if (!empty($title_append)) { |
59
|
|
|
$title_append = is_array($title_append) ? implode(' ', $title_append) : $title_append; |
60
|
|
|
unset($_GET['title_append']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$values_by_property_id = $request->get('properties', []); |
64
|
|
|
if (!is_array($values_by_property_id)) { |
65
|
|
|
$values_by_property_id = [$values_by_property_id]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (Yii::$app->request->isPost && isset($_POST['properties'])) { |
69
|
|
|
if (is_array($_POST['properties'])) { |
70
|
|
|
foreach ($_POST['properties'] as $key => $value) { |
71
|
|
|
if (isset($values_by_property_id[$key])) { |
72
|
|
|
$values_by_property_id[$key] = array_unique( |
73
|
|
|
ArrayHelper::merge( |
74
|
|
|
$values_by_property_id[$key], |
75
|
|
|
$value |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
$values_by_property_id[$key] = array_unique($value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$selected_category_ids = $request->get('categories', []); |
86
|
|
|
if (!is_array($selected_category_ids)) { |
87
|
|
|
$selected_category_ids = [$selected_category_ids]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (null !== $selected_category_id = $request->get('last_category_id')) { |
91
|
|
|
$selected_category_id = intval($selected_category_id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = Product::filteredProducts( |
95
|
|
|
$category_group_id, |
96
|
|
|
$values_by_property_id, |
97
|
|
|
$selected_category_id, |
98
|
|
|
false, |
99
|
|
|
null, |
100
|
|
|
true, |
101
|
|
|
false |
102
|
|
|
); |
103
|
|
|
/** @var Pagination $pages */ |
104
|
|
|
$pages = $result['pages']; |
105
|
|
|
if (Yii::$app->response->is_prefiltered_page) { |
106
|
|
|
$pages->route = '/' . Yii::$app->request->pathInfo; |
107
|
|
|
$pages->params = [ |
108
|
|
|
|
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
$allSorts = $result['allSorts']; |
112
|
|
|
$products = $result['products']; |
113
|
|
|
|
114
|
|
|
// throw 404 if we are at filtered page without any products |
115
|
|
|
if ( !empty($values_by_property_id) && empty($products)) { |
116
|
|
|
throw new NotFoundHttpException(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (null !== $selected_category = $selected_category_id) { |
120
|
|
|
if ($selected_category_id > 0) { |
121
|
|
|
if (null !== $selected_category = Category::findById($selected_category_id, null)) { |
122
|
|
View Code Duplication |
if (!empty($selected_category->meta_description)) { |
123
|
|
|
$this->view->registerMetaTag( |
|
|
|
|
124
|
|
|
[ |
125
|
|
|
'name' => 'description', |
126
|
|
|
'content' => $selected_category->meta_description, |
127
|
|
|
], |
128
|
|
|
'meta_description' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
$this->view->title = $selected_category->title; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
if (is_null($selected_category) || !$selected_category->active) { |
136
|
|
|
throw new NotFoundHttpException; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!empty($title_append)) { |
140
|
|
|
$this->view->title .= " " . $title_append; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->view->blocks['h1'] = $selected_category->h1; |
144
|
|
|
$this->view->blocks['announce'] = $selected_category->announce; |
145
|
|
|
$this->view->blocks['content'] = $selected_category->content; |
146
|
|
|
|
147
|
|
|
$this->loadDynamicContent($object->id, 'shop/product/list', $request->get()); |
148
|
|
|
|
149
|
|
|
$params = [ |
150
|
|
|
'model' => $selected_category, |
151
|
|
|
'selected_category' => $selected_category, |
152
|
|
|
'selected_category_id' => $selected_category_id, |
153
|
|
|
'selected_category_ids' => $selected_category_ids, |
154
|
|
|
'values_by_property_id' => $values_by_property_id, |
155
|
|
|
'products' => $products, |
156
|
|
|
'object' => $object, |
157
|
|
|
'category_group_id' => $category_group_id, |
158
|
|
|
'pages' => $pages, |
159
|
|
|
'title_append' => $title_append, |
160
|
|
|
'selections' => $request->get(), |
161
|
|
|
'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, null, $values_by_property_id), |
162
|
|
|
'allSorts' => $allSorts, |
163
|
|
|
]; |
164
|
|
|
$viewFile = $this->computeViewFile($selected_category, 'list'); |
165
|
|
|
|
166
|
|
|
if (Yii::$app->request->isAjax) { |
167
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
168
|
|
|
|
169
|
|
|
$content = $this->renderAjax( |
170
|
|
|
$viewFile, |
171
|
|
|
$params |
172
|
|
|
); |
173
|
|
|
$filters = ''; |
174
|
|
|
$activeWidgets = ThemeActiveWidgets::getActiveWidgets(); |
175
|
|
|
foreach ($activeWidgets as $activeWidget) { |
176
|
|
|
if ($activeWidget->widget->widget == Widget::className()) { |
177
|
|
|
/** @var ThemeWidgets $widgetModel */ |
178
|
|
|
$widgetModel = $activeWidget->widget; |
179
|
|
|
/** @var BaseWidget $widgetClassName */ |
180
|
|
|
$widgetClassName = $widgetModel->widget; |
181
|
|
|
$widgetConfiguration = Json::decode($widgetModel->configuration_json, true); |
182
|
|
|
if (!is_array($widgetConfiguration)) { |
183
|
|
|
$widgetConfiguration = []; |
184
|
|
|
} |
185
|
|
|
$activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true); |
186
|
|
|
if (!is_array($activeWidgetConfiguration)) { |
187
|
|
|
$activeWidgetConfiguration = []; |
188
|
|
|
} |
189
|
|
|
$config = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration); |
190
|
|
|
$config['themeWidgetModel'] = $widgetModel; |
191
|
|
|
$config['partRow'] = $activeWidget->part; |
192
|
|
|
$config['activeWidget'] = $activeWidget; |
193
|
|
|
$filters = $widgetClassName::widget($config); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
return [ |
|
|
|
|
197
|
|
|
'content' => $content, |
198
|
|
|
'filters' => $filters, |
199
|
|
|
'title' => $this->view->title, |
200
|
|
|
'url' => Url::to( |
201
|
|
|
[ |
202
|
|
|
'/shop/product/list', |
203
|
|
|
'last_category_id' => $selected_category_id, |
204
|
|
|
'properties' => $values_by_property_id |
205
|
|
|
] |
206
|
|
|
), |
207
|
|
|
]; |
208
|
|
|
} else { |
209
|
|
|
return $this->render( |
210
|
|
|
$viewFile, |
211
|
|
|
$params |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Product page view |
218
|
|
|
* |
219
|
|
|
* @param null $model_id |
220
|
|
|
* @return string |
221
|
|
|
* @throws NotFoundHttpException |
222
|
|
|
* @throws ServerErrorHttpException |
223
|
|
|
*/ |
224
|
|
|
public function actionShow($model_id = null) |
225
|
|
|
{ |
226
|
|
|
if (null === $object = Object::getForClass(Product::className())) { |
227
|
|
|
throw new ServerErrorHttpException('Object not found.'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$cacheKey = 'Product:' . $model_id; |
231
|
|
|
if (false === $product = Yii::$app->cache->get($cacheKey)) { |
232
|
|
|
if (null === $product = Product::findById($model_id)) { |
233
|
|
|
throw new NotFoundHttpException; |
234
|
|
|
} |
235
|
|
|
Yii::$app->cache->set( |
236
|
|
|
$cacheKey, |
237
|
|
|
$product, |
238
|
|
|
86400, |
239
|
|
|
new TagDependency( |
240
|
|
|
[ |
241
|
|
|
'tags' => [ |
242
|
|
|
ActiveRecordHelper::getObjectTag(Product::className(), $model_id), |
243
|
|
|
] |
244
|
|
|
] |
245
|
|
|
) |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$request = Yii::$app->request; |
250
|
|
|
|
251
|
|
|
$values_by_property_id = $request->get('properties', []); |
252
|
|
|
if (!is_array($values_by_property_id)) { |
253
|
|
|
$values_by_property_id = [$values_by_property_id]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$selected_category_id = $request->get('last_category_id'); |
257
|
|
|
|
258
|
|
|
$selected_category_ids = $request->get('categories', []); |
259
|
|
|
if (!is_array($selected_category_ids)) { |
260
|
|
|
$selected_category_ids = [$selected_category_ids]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$category_group_id = intval($request->get('category_group_id', 0)); |
264
|
|
|
|
265
|
|
|
// trigger that we are to show product to user! |
266
|
|
|
// wow! such product! very events! |
267
|
|
|
$specialEvent = new ProductPageShowed([ |
268
|
|
|
'product_id' => $product->id, |
269
|
|
|
]); |
270
|
|
|
EventTriggeringHelper::triggerSpecialEvent($specialEvent); |
271
|
|
|
|
272
|
|
View Code Duplication |
if (!empty($product->meta_description)) { |
273
|
|
|
$this->view->registerMetaTag( |
|
|
|
|
274
|
|
|
[ |
275
|
|
|
'name' => 'description', |
276
|
|
|
'content' => $product->meta_description, |
277
|
|
|
], |
278
|
|
|
'meta_description' |
279
|
|
|
); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$selected_category = ($selected_category_id > 0) ? Category::findById($selected_category_id) : null; |
283
|
|
|
|
284
|
|
|
$this->view->title = $product->title; |
285
|
|
|
$this->view->blocks['announce'] = $product->announce; |
286
|
|
|
$this->view->blocks['content'] = $product->content; |
287
|
|
|
$this->view->blocks['title'] = $product->title; |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
return $this->render( |
291
|
|
|
$this->computeViewFile($product, 'show'), |
292
|
|
|
[ |
293
|
|
|
'model' => $product, |
294
|
|
|
'category_group_id' => $category_group_id, |
295
|
|
|
'values_by_property_id' => $values_by_property_id, |
296
|
|
|
'selected_category_id' => $selected_category_id, |
297
|
|
|
'selected_category' => $selected_category, |
298
|
|
|
'selected_category_ids' => $selected_category_ids, |
299
|
|
|
'object' => $object, |
300
|
|
|
'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, $product) |
|
|
|
|
301
|
|
|
] |
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Search handler |
307
|
|
|
* @return array |
308
|
|
|
* @throws ForbiddenHttpException |
309
|
|
|
*/ |
310
|
|
|
public function actionSearch() |
311
|
|
|
{ |
312
|
|
|
$headers = Yii::$app->response->getHeaders(); |
313
|
|
|
$headers->set('X-Robots-Tag', 'none'); |
314
|
|
|
$headers->set('X-Frame-Options', 'SAMEORIGIN'); |
315
|
|
|
$headers->set('X-Content-Type-Options', 'nosniff'); |
316
|
|
|
if (!Yii::$app->request->isAjax) { |
317
|
|
|
throw new ForbiddenHttpException(); |
318
|
|
|
} |
319
|
|
|
$model = new Search(); |
320
|
|
|
$model->load(Yii::$app->request->get()); |
321
|
|
|
$cacheKey = 'ProductSearchIds: ' . $model->q; |
322
|
|
|
$ids = Yii::$app->cache->get($cacheKey); |
323
|
|
|
if ($ids === false) { |
324
|
|
|
$ids = ArrayHelper::merge( |
325
|
|
|
$model->searchProductsByDescription(), |
326
|
|
|
$model->searchProductsByProperty() |
327
|
|
|
); |
328
|
|
|
Yii::$app->cache->set( |
329
|
|
|
$cacheKey, |
330
|
|
|
$ids, |
331
|
|
|
86400, |
332
|
|
|
new TagDependency( |
333
|
|
|
[ |
334
|
|
|
'tags' => ActiveRecordHelper::getCommonTag(Product::className()), |
335
|
|
|
] |
336
|
|
|
) |
337
|
|
|
); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** @var \app\modules\shop\ShopModule $module */ |
341
|
|
|
$module = Yii::$app->modules['shop']; |
342
|
|
|
|
343
|
|
|
$pages = new Pagination( |
344
|
|
|
[ |
345
|
|
|
'defaultPageSize' => $module->searchResultsLimit, |
346
|
|
|
'forcePageParam' => false, |
347
|
|
|
'totalCount' => count($ids), |
348
|
|
|
] |
349
|
|
|
); |
350
|
|
|
$cacheKey .= ' : ' . $pages->offset; |
351
|
|
|
$products = Yii::$app->cache->get($cacheKey); |
352
|
|
View Code Duplication |
if ($products === false) { |
353
|
|
|
$products = Product::find()->where( |
354
|
|
|
[ |
355
|
|
|
'in', |
356
|
|
|
'`id`', |
357
|
|
|
array_slice( |
358
|
|
|
$ids, |
359
|
|
|
$pages->offset, |
360
|
|
|
$pages->limit |
361
|
|
|
) |
362
|
|
|
] |
363
|
|
|
)->addOrderBy('sort_order')->with('images')->all(); |
364
|
|
|
Yii::$app->cache->set( |
365
|
|
|
$cacheKey, |
366
|
|
|
$products, |
367
|
|
|
86400, |
368
|
|
|
new TagDependency( |
369
|
|
|
[ |
370
|
|
|
'tags' => ActiveRecordHelper::getCommonTag(Product::className()), |
371
|
|
|
] |
372
|
|
|
) |
373
|
|
|
); |
374
|
|
|
} |
375
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
376
|
|
|
return [ |
377
|
|
|
'view' => $this->renderAjax( |
378
|
|
|
'search', |
379
|
|
|
[ |
380
|
|
|
'model' => $model, |
381
|
|
|
'pages' => $pages, |
382
|
|
|
'products' => $products, |
383
|
|
|
] |
384
|
|
|
), |
385
|
|
|
'totalCount' => count($ids), |
386
|
|
|
]; |
387
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* This function build array for widget "Breadcrumbs" |
392
|
|
|
* @param Category $selCat - model of current category |
393
|
|
|
* @param Product|null $product - model of product, if current page is a page of product |
394
|
|
|
* @param array $properties - array of properties and static values |
395
|
|
|
* Return an array for widget or empty array |
396
|
|
|
*/ |
397
|
|
|
private function buildBreadcrumbsArray($selCat, $product = null, $properties = []) |
398
|
|
|
{ |
399
|
|
|
if ($selCat === null) { |
400
|
|
|
return []; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
// init |
404
|
|
|
$breadcrumbs = []; |
405
|
|
|
if ($product !== null) { |
406
|
|
|
$crumbs[$product->slug] = !empty($product->breadcrumbs_label) ? $product->breadcrumbs_label : ''; |
|
|
|
|
407
|
|
|
} |
408
|
|
|
$crumbs[$selCat->slug] = $selCat->breadcrumbs_label; |
|
|
|
|
409
|
|
|
|
410
|
|
|
// get basic data |
411
|
|
|
$parent = $selCat->parent_id > 0 ? Category::findById($selCat->parent_id) : null; |
412
|
|
View Code Duplication |
while ($parent !== null) { |
413
|
|
|
$crumbs[$parent->slug] = $parent->breadcrumbs_label; |
414
|
|
|
$parent = $parent->parent; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
// build array for widget |
418
|
|
|
$url = ''; |
419
|
|
|
$crumbs = array_reverse($crumbs, true); |
420
|
|
View Code Duplication |
foreach ($crumbs as $slug => $label) { |
421
|
|
|
$url .= '/' . $slug; |
422
|
|
|
$breadcrumbs[] = [ |
423
|
|
|
'label' => $label, |
424
|
|
|
'url' => $url |
425
|
|
|
]; |
426
|
|
|
} |
427
|
|
|
if (is_null($product) && $this->module->showFiltersInBreadcrumbs && !empty($properties)) { |
428
|
|
|
$route = [ |
429
|
|
|
'@category', |
430
|
|
|
'last_category_id' => $selCat->id, |
431
|
|
|
'category_group_id' => $selCat->category_group_id, |
432
|
|
|
]; |
433
|
|
|
$params = []; |
434
|
|
|
foreach ($properties as $propertyId => $propertyStaticValues) { |
435
|
|
|
$localParams = $params; |
436
|
|
|
foreach ($propertyStaticValues as $propertyStaticValue) { |
437
|
|
|
$psv = PropertyStaticValues::findById($propertyStaticValue); |
438
|
|
|
if (is_null($psv)) { |
439
|
|
|
continue; |
440
|
|
|
} |
441
|
|
|
$localParams[$propertyId][] = $propertyStaticValue; |
442
|
|
|
$breadcrumbs[] = [ |
443
|
|
|
'label' => $psv['name'], |
444
|
|
|
'url' => array_merge($route, ['properties' => $localParams]), |
445
|
|
|
]; |
446
|
|
|
} |
447
|
|
|
$params[$propertyId] = $propertyStaticValues; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
unset($breadcrumbs[count($breadcrumbs) - 1]['url']); // last item is not a link |
451
|
|
|
|
452
|
|
|
if (isset(Yii::$app->response->blocks['breadcrumbs_label'])) { |
453
|
|
|
// last item label rewrited through prefiltered page or something similar |
454
|
|
|
$breadcrumbs[count($breadcrumbs) - 1]['label'] = Yii::$app->response->blocks['breadcrumbs_label']; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return $breadcrumbs; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: