ProductsController.php
5.93 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\web\HttpException;
use yii\data\Pagination;
use yii\web\Session;
use common\models\Catalog;
use common\models\Products;
use common\models\Mod;
use common\models\Filters;
use common\models\ProductsFilters;
use common\models\ProductsType;
use common\models\ProductsBrends;
use common\models\ViewProduct;
class ProductsController extends Controller
{
public function actionIndex ()
{
$modelProducts = new Products;
$modelProducts->load ($_POST);
if (! $catalog = Catalog::find ()->where (['translit' => $_GET['translit']])->with ('parent')->one ())
throw new HttpException(404, 'Данной странице не существует!');
$query = Products::find ()->where ('catalog_id=:catalog_id OR catalog_parent_id=:catalog_parent_id', [':catalog_id' => $catalog->id, ':catalog_parent_id' => $catalog->id])->with (['catalog'])->innerJoinWith (['cost']);
if (! empty($_POST['Products']['minCost']) && ! empty($_POST['Products']['maxCost'])) $query->andWhere ('(cost>=:minCost and cost<=:maxCost)', [':minCost' => $_POST['Products']['minCost'], ':maxCost' => $_POST['Products']['maxCost']]);
if (! empty($_GET['brends']))
{
$b = explode (';', $_GET['brends']);
$query->andWhere (['brend_id' => $b]);
}
if (! empty($_GET['filters']))
{
$l = explode (';', $_GET['filters']);
$items = Filters::find ()->where (['parent_id' => 0])->all ();
foreach ($items as $key => $it)
{
$f = [];
foreach ($it->childs as $c)
{
if (in_array ($c['id'], $l)) $f[] = $c['id'];
}
if (count ($f) > 0)
$query->innerJoin ('productsFilters as filter_' . $key, 'filter_' . $key . '.product_id=products.id')->andWhere (['filter_' . $key . '.filter_id' => $f]);
// $childs->leftJoin('productsFilters as pf_'.$key, 'pf_'.$key.'.product_id = productsFilters.product_id')->andWhere(['pf_'.$key.'.filter_id'=>$f]);
}
}
if (! empty($modelProducts->fasovka))
{
$query->innerJoinWith (['fasovka'])->andWhere ([ProductsFasovka::tableName () . '.fasovka_id' => $modelProducts->fasovka]);
}
if (! empty($modelProducts->type))
{
$query->innerJoinWith (['type'])->andWhere ([ProductsType::tableName () . '.type_id' => $modelProducts->type]);
}
if (! empty($modelProducts->brends))
{
$query->innerJoinWith (['brends'])->andWhere ([ProductsBrends::tableName () . '.brend_id' => $modelProducts->brends]);
}
$query->groupBy (['id']);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count (), 'pageSize' => 15]);
$pages->forcePageParam = false;
$pages->pageSizeParam = false;
$products = $query->offset ($pages->offset)
->limit ($pages->limit)
->all ();
return $this->render ('index', [
'modelProducts' => $modelProducts,
'catalog' => $catalog,
'pages' => $pages,
'products' => $products,
]);
}
public function actionSearch ()
{
$query = Products::find ()->innerJoinWith (['catalog'])->innerJoinWith (['cost'])->innerJoinWith (['brend']);
if (! empty($_GET['search_str']))
{
$query->andWhere (['like', 'products.name', $_GET['search_str']]);
$query->orWhere (['like', 'catalog.name', $_GET['search_str']]);
$query->orWhere (['like', 'catalog_brends.name', $_GET['search_str']]);
$query->orWhere (['like', 'mod.art', $_GET['search_str']]);
}
$query->groupBy (['id']);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count (), 'pageSize' => 20]);
$pages->forcePageParam = false;
$pages->pageSizeParam = false;
$products = $query->offset ($pages->offset)
->limit ($pages->limit)
->all ();
return $this->render ('search', [
'pages' => $pages,
'products' => $products,
]);
}
public function actionShow ()
{
if (! $catalog = Catalog::find ()->where (['translit' => $_GET['translit_rubric']])->with ('parent')->one ())
throw new HttpException(404, 'Данной странице не существует!');
if (! $product = Products::find ()->where (['id' => $_GET['id']])->one ())
throw new HttpException(404, 'Данной странице не существует!');
ViewProduct::add ($product->id);
return $this->render ('show', [
'catalog' => $catalog,
'product' => $product,
]);
}
public function actionCompare ()
{
$session = new Session;
$session->open ();
if (! empty($_GET['id']))
{
$i = 0;
if (isset($session['compare']))
{
foreach ($session['compare'] as $key => $compare)
{
if ($_GET['id'] == $compare)
{
$i++;
}
}
}
if ($i == 0)
{
$data[] = $_GET['id'];
$session['compare'] = $data;
}
Yii::$app->getSession ()->setFlash ('success', 'Этот товар добавлен к сравнению!');
return $this->redirect (Yii::$app->request->referrer);
}
else
{
//print_r($session['compare']);
$products = Products::find ()->where (['id' => $session['compare']])->all ();
return $this->render ('compare', [
'products' => $products,
]);
}
}
}