CollectionController.php
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace frontend\controllers;
use common\models\Project;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Article controller
*/
class CollectionController extends Controller
{
/**
* Displays article list.
*
* @return string
*/
public function actionIndex($id)
{
/**
* @var ProductVariant $variant
*/
$collection = $this->findCollection($id);
$variants = $collection->variants;
$variant = current($variants);
return $this->redirect([
'view',
'collection_id' => $collection->product_id,
'variant_id' => $variant->product_variant_id,
]);
}
public function actionView($collection_id, $variant_id)
{
$collection = $this->findCollection($collection_id, $variant_id);
$variants = $collection->variants;
$variants = ArrayHelper::index($variants, 'product_variant_id');
$variant = $variants[ $variant_id ];
$projects = Project::find()
->joinWith('productToProject')
->joinWith('lang', true, 'INNER JOIN')
->with('image')
->where([ 'product_to_project.product_variant_id' => $variant->product_variant_id ])
->all();
$referer = $this->checkReferer() ? \Yii::$app->request->referrer : NULL;
return $this->render('view', [
'collection' => $collection,
'variants' => $variants,
'variant' => $variant,
'projects' => $projects,
'referer' => $referer,
]);
}
/**
* @param $id
*
* @return Product
* @throws NotFoundHttpException
*/
private function findCollection($id, $variant_id = NULL)
{
$model = Product::find()
->joinWith('variants.lang', true, 'INNER JOIN')
->joinWith('lang', true, 'INNER JOIN')
->joinWith('brand.lang', true, 'INNER JOIN')
->with('productSpec.lang')
->where([ 'product.product_id' => $id ])
->andFilterWhere([ 'product_variant.product_variant_id' => $variant_id ])
->one();
if(empty( $model )) {
throw new NotFoundHttpException();
}
return $model;
}
/**
* @param string|NULL $referer
*
* @return bool
*/
private function checkReferer(string $referer = NULL)
{
if($referer === NULL) {
$referer = \Yii::$app->request->referrer;
}
if(!empty( $referer )) {
$host = \Yii::$app->request->hostInfo;
if(strpos($referer, $host) === 0) {
return true;
}
}
return false;
}
}