|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\modules\shop\widgets; |
|
3
|
|
|
|
|
4
|
|
|
use yii\db\Query; |
|
5
|
|
|
use yii\helpers\ArrayHelper; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class PriceSliderRangeWidget extends SliderRangeWidget |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public $attributeName = 'Цена'; |
|
13
|
|
|
|
|
14
|
|
|
public $minAttribute = 'price_min'; |
|
15
|
|
|
public $maxAttribute = 'price_max'; |
|
16
|
|
|
public $changeFlagAttribute = 'price_change_flag'; |
|
17
|
|
|
|
|
18
|
|
|
public $categoryId; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
public function init() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$cacheKey = 'priceRangeCategory' . $this->categoryId; |
|
24
|
|
|
|
|
25
|
|
|
if (!$data = Yii::$app->cache->get($cacheKey)) { |
|
26
|
|
|
$dataMin = (new Query())->select([ |
|
27
|
|
|
'MIN(product.price / currency.convert_nominal * currency.convert_rate) AS min_price', |
|
28
|
|
|
'product.currency_id AS min_currency', |
|
29
|
|
|
]) |
|
30
|
|
|
->from(['product', 'product_category', 'currency']) |
|
31
|
|
|
->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)') |
|
32
|
|
|
->andWhere( |
|
33
|
|
|
[ |
|
34
|
|
|
'product.active' => 1, |
|
35
|
|
|
'product_category.category_id' => $this->categoryId |
|
36
|
|
|
] |
|
37
|
|
|
)->one(); |
|
38
|
|
|
|
|
39
|
|
|
$dataMax = (new Query())->select([ |
|
40
|
|
|
'MAX(product.price / currency.convert_nominal * currency.convert_rate) AS max_price', |
|
41
|
|
|
'product.currency_id AS max_currency', |
|
42
|
|
|
]) |
|
43
|
|
|
->from(['product', 'product_category', 'currency']) |
|
44
|
|
|
->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)') |
|
45
|
|
|
->andWhere( |
|
46
|
|
|
[ |
|
47
|
|
|
'product.active' => 1, |
|
48
|
|
|
'product_category.category_id' => $this->categoryId |
|
49
|
|
|
] |
|
50
|
|
|
)->one(); |
|
51
|
|
|
|
|
52
|
|
|
$data = ArrayHelper::merge($dataMax, $dataMin); |
|
53
|
|
|
if ($data) { |
|
54
|
|
|
$data['min_price'] = (int) $data['min_price']; |
|
55
|
|
|
$data['max_price'] = (int) $data['max_price']; |
|
56
|
|
|
Yii::$app->cache->set($cacheKey, $data, 86400); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
if ($data && isset($data['min_price']) && isset($data['max_price'])) { |
|
61
|
|
|
$this->minValue = $data['min_price']; |
|
62
|
|
|
$this->maxValue = $data['max_price']; |
|
63
|
|
|
$get = ArrayHelper::merge(Yii::$app->request->get(), Yii::$app->request->post()); |
|
64
|
|
|
|
|
65
|
|
|
if (isset($get[$this->minAttribute]) && is_numeric($get[$this->minAttribute])) { |
|
66
|
|
|
$this->changeFlagDefaultValue = 1; |
|
67
|
|
|
$this->minValueNow = $get[$this->minAttribute]; |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->minValueNow = $this->minValue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (isset($get[$this->maxAttribute]) && is_numeric($get[$this->maxAttribute])) { |
|
73
|
|
|
$this->changeFlagDefaultValue = 1; |
|
74
|
|
|
$this->maxValueNow = $get[$this->maxAttribute]; |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->maxValueNow = $this->maxValue; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
return parent::init(); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.