6fa713ca
Karnovsky A
Catalog v 1.2
|
1
2
3
4
5
6
7
|
<?php
namespace frontend\models;
use common\modules\product\models\Brand;
use common\modules\product\models\ProductCategory;
use common\modules\product\models\ProductOption;
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
8
|
use common\modules\product\models\ProductSearch;
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
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
|
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
*/
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
47
48
49
50
51
52
53
|
public function search($category = null, $params = []) {
if (!empty($category)) {
/** @var ActiveQuery $query */
$query = $category->getRelations('product_categories');
} else {
$query = Product::find();
}
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
54
55
|
$query->joinWith('variant');
$query->joinWith('brand');
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
56
|
$query->joinWith('image');
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
57
|
$query->joinWith('categories');
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
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
|
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 24,
],
'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,
'label' => 'цене',
],
],
]
]);
if (!$this->validate()) {
return $dataProvider;
}
$this->_setParams($query, $params);
return $dataProvider;
}
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
91
|
public function optionsForCategory($category = null, $params = []) {
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
92
93
94
95
96
97
98
99
|
$query = TaxOption::find()
->select([
TaxOption::tableName() .'.*',
'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'
])
->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')
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
100
101
102
103
104
|
->andWhere([TaxGroup::tableName() .'.is_filter' => true]);
if (!empty($category)) {
$query->andWhere([ProductCategory::tableName() .'.category_id' => $category->category_id]);
}
$query->groupBy(TaxOption::tableName() .'.tax_option_id');
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
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']]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
129
130
131
132
133
134
135
|
public function priceLimits($category = null, $params = []) {
if (!empty($category)) {
/** @var ActiveQuery $query */
$query = $category->getRelations('product_categories');
} else {
$query = Product::find();
}
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
$query->joinWith('variant');
$this->_setParams($query, $params, false);
// $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'),
];
}
protected function _setParams(&$query, $params, $setPriceLimits = true) {
|
ad9b9ca9
Karnovsky A
Karnovsky-2904201...
|
152
153
154
155
156
157
158
159
|
if (!empty($params['keywords'])) {
if (!is_array($params['keywords'])) {
$params['keywords'] = [$params['keywords']];
}
foreach ($params['keywords'] as $keyword) {
$query->andFilterWhere(['ilike', Product::tableName() .'.name', $keyword]);
}
}
|
6fa713ca
Karnovsky A
Catalog v 1.2
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
if (!empty($params['brands'])) {
$query->andFilterWhere([Product::tableName() .'.brand_id' => $params['brands']]);
}
if (!empty($params['options'])) {
foreach ($params['options'] as $group => $options) {
foreach ($options as &$option) {
$option = "'$option'";
}
$query->andWhere(
Product::tableName() . '.product_id IN (SELECT product_id AS products FROM product_option INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id WHERE tax_group.alias LIKE \''. $group .'\' AND tax_option.alias IN (' . implode(',', $options) . '))'
);
}
}
if ($setPriceLimits && !empty($params['prices'])) {
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']]);
}
}
}
}
|