3f2bc3d0
Administrator
first commit
|
1
2
3
4
|
<?php
namespace frontend\models;
|
a0be9a4d
Karnovsky A
30062016
|
5
|
use common\modules\product\helpers\ProductHelper;
|
3f2bc3d0
Administrator
first commit
|
6
|
use common\modules\product\models\Brand;
|
8724ec1f
Karnovsky A
-
|
7
8
|
use common\modules\product\models\BrandName;
use common\modules\product\models\CategoryName;
|
3f2bc3d0
Administrator
first commit
|
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
|
use common\modules\product\models\ProductCategory;
use common\modules\product\models\ProductOption;
use common\modules\product\models\ProductSearch;
use common\modules\rubrication\models\TaxGroup;
use common\modules\rubrication\models\TaxOption;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\web\NotFoundHttpException;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
class ProductFrontendSearch extends Product {
/**
* @inheritdoc
*/
public function rules()
{
return [
[['price_interval', 'brands'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied for frontend
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($category = null, $params = []) {
if (!empty($category)) {
/** @var ActiveQuery $query */
|
8724ec1f
Karnovsky A
-
|
53
54
|
// $query = $category->getRelations('product_categories');
$query = $category->getProducts();
|
3f2bc3d0
Administrator
first commit
|
55
56
57
|
} else {
$query = Product::find();
}
|
8e0b0623
Karnovsky A
Sort by price
|
58
|
$query->select(['product.*']);
|
d48d8bc0
Karnovsky A
-
|
59
|
$query->joinWith(['variant', 'image', 'brand', 'brand.brandName', 'category', 'category.categoryName']);
|
4902c747
Karnovsky A
-
|
60
|
|
8e0b0623
Karnovsky A
Sort by price
|
61
|
$query->groupBy(['product.product_id', 'product_variant.price']);
|
4902c747
Karnovsky A
-
|
62
|
|
3f2bc3d0
Administrator
first commit
|
63
64
65
|
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
|
4902c747
Karnovsky A
-
|
66
|
'pageSize' => 16,
|
3f2bc3d0
Administrator
first commit
|
67
68
69
70
71
72
73
74
75
76
77
78
79
|
],
'sort' => [
'attributes' => [
'name' => [
'asc' => ['name' => SORT_ASC],
'desc' => ['name' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'имени',
],
'price' => [
'asc' => [ProductVariant::tableName() .'.price' => SORT_ASC],
'desc' => [ProductVariant::tableName() .'.price' => SORT_DESC],
'default' => SORT_DESC,
|
49c47c76
Karnovsky A
-
|
80
|
'label' => 'по цене',
|
3f2bc3d0
Administrator
first commit
|
81
82
83
84
85
86
87
88
89
|
],
],
]
]);
if (!$this->validate()) {
return $dataProvider;
}
|
a0be9a4d
Karnovsky A
30062016
|
90
|
ProductHelper::_setQueryParams($query, $params);
|
3f2bc3d0
Administrator
first commit
|
91
|
|
3147aab1
Karnovsky A
Fix search with s...
|
92
93
|
$query->andWhere(['!=', ProductVariant::tableName() .'.stock', 0]);
|
3f2bc3d0
Administrator
first commit
|
94
95
96
97
|
return $dataProvider;
}
public function optionsForCategory($category = null, $params = []) {
|
3f2bc3d0
Administrator
first commit
|
98
99
|
$query = TaxOption::find()
->select([
|
9d539ac3
Karnovsky A
Brands and option...
|
100
101
|
TaxOption::tableName() .'.*',
'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'
|
3f2bc3d0
Administrator
first commit
|
102
103
104
105
|
])
->innerJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id')
->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. ProductOption::tableName() .'.product_id')
->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id')
|
9d539ac3
Karnovsky A
Brands and option...
|
106
107
108
109
110
111
112
|
->where([TaxGroup::tableName() .'.is_filter' => true]);
$query->leftJoin('product_variant', 'product_variant.product_id = '. ProductCategory::tableName() .'.product_id');
$query->andWhere(['!=', 'product_variant.stock', 0]);
$query->groupBy(TaxOption::tableName() .'.tax_option_id');
// $query->having(['>', 'COUNT(product_variant.product_variant_id)', 0]);
|
3f2bc3d0
Administrator
first commit
|
113
114
115
|
if (!empty($category)) {
$query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]);
}
|
9d539ac3
Karnovsky A
Brands and option...
|
116
|
|
163914fd
Karnovsky A
-
|
117
|
$query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC);
|
c34b3aa0
Karnovsky A
-
|
118
|
$query->limit(null);
|
3f2bc3d0
Administrator
first commit
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
if (!empty($params['prices'])) {
if ($params['prices']['min'] > 0 || $params['prices']['max'] > 0) {
$query->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id='. ProductCategory::tableName() .'.product_id');
}
if ($params['prices']['min'] > 0) {
$query->andWhere(['>=', ProductVariant::tableName() .'.price', $params['prices']['min']]);
}
if ($params['prices']['max'] > 0) {
$query->andWhere(['<=', ProductVariant::tableName() .'.price', $params['prices']['max']]);
}
}
if (!empty($params['brands'])) {
$query->innerJoin(Product::tableName(), Product::tableName() .'.product_id='. ProductCategory::tableName() .'.product_id');
$query->andWhere([Product::tableName() .'.brand_id' => $params['brands']]);
}
|
8724ec1f
Karnovsky A
-
|
135
|
return $query;
|
3f2bc3d0
Administrator
first commit
|
136
137
138
139
140
|
}
public function priceLimits($category = null, $params = []) {
if (!empty($category)) {
/** @var ActiveQuery $query */
|
8724ec1f
Karnovsky A
-
|
141
142
|
// $query = $category->getRelations('product_categories');
$query = $category->getProducts();
|
3f2bc3d0
Administrator
first commit
|
143
144
145
146
147
|
} else {
$query = Product::find();
}
$query->joinWith('variant');
|
a0be9a4d
Karnovsky A
30062016
|
148
|
ProductHelper::_setQueryParams($query, $params, false);
|
3f2bc3d0
Administrator
first commit
|
149
150
151
152
153
154
155
156
157
158
159
|
// $query->select([
// 'MIN('. ProductVariant::tableName() .'.price) AS priceMIN',
// 'MAX('. ProductVariant::tableName() .'.price) AS priceMAX',
// ]);
return [
'min' => $query->min(ProductVariant::tableName() .'.price'),
'max' => $query->max(ProductVariant::tableName() .'.price'),
];
}
|
3f2bc3d0
Administrator
first commit
|
160
|
}
|