3f2bc3d0
Administrator
first commit
|
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
|
<?php
namespace frontend\models;
use common\modules\product\models\Brand;
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 */
$query = $category->getRelations('product_categories');
} else {
$query = Product::find();
}
|
c34b3aa0
Karnovsky A
-
|
54
|
$query->joinWith('variant');
|
3f2bc3d0
Administrator
first commit
|
55
|
$query->joinWith('brand');
|
c34b3aa0
Karnovsky A
-
|
56
|
$query->joinWith('image');
|
3f2bc3d0
Administrator
first commit
|
57
|
$query->joinWith('categories');
|
dc2cd017
Karnovsky A
-
|
58
59
60
|
if (empty($_GET['sort']) || ($_GET['sort'] != 'price' && $_GET['sort'] != '-price')) {
$query->groupBy('product.product_id');
}
|
3f2bc3d0
Administrator
first commit
|
61
62
63
64
|
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
|
ccc7a9d3
Karnovsky A
Karnovsky 12052016
|
65
|
'pageSize' => 15,
|
3f2bc3d0
Administrator
first commit
|
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
|
],
'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;
}
public function optionsForCategory($category = null, $params = []) {
|
c34b3aa0
Karnovsky A
-
|
95
96
|
// ,
// 'COUNT('. ProductOption::tableName() .'.product_id) AS _items_count'
|
3f2bc3d0
Administrator
first commit
|
97
98
|
$query = TaxOption::find()
->select([
|
c34b3aa0
Karnovsky A
-
|
99
|
TaxOption::tableName() .'.*'
|
3f2bc3d0
Administrator
first commit
|
100
101
102
103
104
105
106
107
108
|
])
->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')
->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');
|
c34b3aa0
Karnovsky A
-
|
109
|
$query->limit(null);
|
3f2bc3d0
Administrator
first commit
|
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
155
156
157
158
159
160
161
162
163
|
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;
}
public function priceLimits($category = null, $params = []) {
if (!empty($category)) {
/** @var ActiveQuery $query */
$query = $category->getRelations('product_categories');
} else {
$query = Product::find();
}
$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) {
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]);
}
}
|
e2651dcd
Karnovsky A
Special-product-f...
|
164
165
166
167
168
|
if (!empty($params['special'])) {
foreach($params['special'] as $key => $value) {
$query->orFilterWhere([Product::tableName() .'.'. $key => $value]);
}
}
|
3f2bc3d0
Administrator
first commit
|
169
170
171
172
173
174
175
176
177
178
179
180
181
|
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) . '))'
);
}
}
|
e2651dcd
Karnovsky A
Special-product-f...
|
182
183
184
|
|
3f2bc3d0
Administrator
first commit
|
185
186
187
188
189
190
191
192
193
194
|
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']]);
}
}
}
}
|