|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\widgets\filter; |
|
4
|
|
|
|
|
5
|
|
|
use app\models\Object; |
|
6
|
|
|
use app\models\ObjectStaticValues; |
|
7
|
|
|
use app\models\Property; |
|
8
|
|
|
use app\models\PropertyGroup; |
|
9
|
|
|
use app\models\PropertyStaticValues; |
|
10
|
|
|
use app\modules\Shop\models\Product; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\Widget; |
|
13
|
|
|
use yii\caching\TagDependency; |
|
14
|
|
|
use yii\db\Query; |
|
15
|
|
|
use yii\helpers\ArrayHelper; |
|
16
|
|
|
use yii\helpers\Json; |
|
17
|
|
|
|
|
18
|
|
|
class FilterWidget extends Widget |
|
19
|
|
|
{ |
|
20
|
|
|
protected $possibleSelections = null; |
|
21
|
|
|
public $categoryGroupId = 0; |
|
22
|
|
|
public $currentSelections = []; |
|
23
|
|
|
public $goBackAlignment = 'left'; |
|
24
|
|
|
public $objectId = null; |
|
25
|
|
|
public $onlyAvailableFilters = true; |
|
26
|
|
|
public $disableInsteadOfHide = false; |
|
27
|
|
|
public $route = '/shop/product/list'; |
|
28
|
|
|
public $title = 'Filter'; |
|
29
|
|
|
public $viewFile = 'filterWidget'; |
|
30
|
|
|
protected $disabled_ids = []; |
|
31
|
|
|
public $render_dynamic = true; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var null|array Array of group ids to display in filter, null to display all available for Object |
|
35
|
|
|
*/ |
|
36
|
|
|
public $onlyGroupsIds = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array Additional params passed to filter view |
|
40
|
|
|
*/ |
|
41
|
|
|
public $additionalViewParams = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function run() |
|
47
|
|
|
{ |
|
48
|
|
|
Yii::beginProfile("FilterWidget"); |
|
49
|
|
|
|
|
50
|
|
|
$view = $this->getView(); |
|
51
|
|
|
FilterWidgetAsset::register($view); |
|
52
|
|
|
$view->registerJs( |
|
53
|
|
|
"jQuery('#{$this->id}').getFilters();" |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
Yii::beginProfile("GetPossibleSelections"); |
|
57
|
|
|
$this->getPossibleSelections(); |
|
58
|
|
|
Yii::endProfile("GetPossibleSelections"); |
|
59
|
|
|
|
|
60
|
|
|
$result = $this->render( |
|
61
|
|
|
$this->viewFile, |
|
62
|
|
|
ArrayHelper::merge([ |
|
63
|
|
|
'id' => $this->id, |
|
64
|
|
|
'current_selections' => $this->currentSelections, |
|
65
|
|
|
'possible_selections' => $this->possibleSelections, |
|
66
|
|
|
'object_id' => $this->objectId, |
|
67
|
|
|
'title' => $this->title, |
|
68
|
|
|
'go_back_alignment' => $this->goBackAlignment, |
|
69
|
|
|
'route' => $this->route, |
|
70
|
|
|
'category_group_id' => $this->categoryGroupId, |
|
71
|
|
|
'disabled_ids' => $this->disabled_ids, |
|
72
|
|
|
'render_dynamic' => $this->render_dynamic, |
|
73
|
|
|
], $this->additionalViewParams) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
Yii::endProfile("FilterWidget"); |
|
77
|
|
|
|
|
78
|
|
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getPossibleSelections() |
|
82
|
|
|
{ |
|
83
|
|
|
$data = [ |
|
84
|
|
|
'propertyIds' => [], |
|
85
|
|
|
'propertyStaticValueIds' => [], |
|
86
|
|
|
]; |
|
87
|
|
|
if ($this->onlyAvailableFilters) { |
|
88
|
|
|
Yii::beginProfile("onlyAvailableFilters"); |
|
89
|
|
|
$object = Object::findById($this->objectId); |
|
90
|
|
|
if (!is_null($object) && isset($this->currentSelections['last_category_id'])) { |
|
91
|
|
|
|
|
92
|
|
|
$cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':' |
|
93
|
|
|
. Json::encode($this->currentSelections['properties']); |
|
94
|
|
|
$data = Yii::$app->cache->get($cacheKey); |
|
95
|
|
|
if ($data === false) { |
|
96
|
|
|
$data = []; |
|
97
|
|
|
Yii::beginProfile("ObjectIds for this category"); |
|
98
|
|
|
$query = new Query(); |
|
99
|
|
|
$query = $query->select($object->categories_table_name . '.object_model_id') |
|
100
|
|
|
->distinct() |
|
101
|
|
|
->from($object->categories_table_name) |
|
102
|
|
|
->join( |
|
103
|
|
|
"JOIN", |
|
104
|
|
|
Product::tableName(), |
|
105
|
|
|
sprintf("%s.`id` = %s.`object_model_id`", |
|
106
|
|
|
Product::tableName(), |
|
107
|
|
|
$object->categories_table_name |
|
108
|
|
|
) |
|
109
|
|
|
) |
|
110
|
|
|
->andWhere([Product::tableName() . ".`active`" => 1]) |
|
111
|
|
|
->andWhere(['category_id' => $this->currentSelections['last_category_id']]); |
|
112
|
|
|
|
|
113
|
|
|
if (count($this->currentSelections['properties']) > 0) { |
|
114
|
|
|
Yii::beginProfile("Apply currency selections(properties)"); |
|
115
|
|
|
foreach ($this->currentSelections['properties'] as $property_id => $values) { |
|
116
|
|
|
|
|
117
|
|
|
$joinTableName = 'OSVJoinTable'.$property_id; |
|
118
|
|
|
$query->join( |
|
119
|
|
|
'JOIN', |
|
120
|
|
|
ObjectStaticValues::tableName() . ' '.$joinTableName, |
|
121
|
|
|
$joinTableName.'.object_id = :objectId AND ' |
|
122
|
|
|
. $joinTableName.'.object_model_id = ' . $object->categories_table_name . '.object_model_id ', |
|
|
|
|
|
|
123
|
|
|
[ |
|
124
|
|
|
':objectId' => $object->id, |
|
125
|
|
|
] |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$query->andWhere(['in', '`'.$joinTableName.'`.`property_static_value_id`', $values]); |
|
130
|
|
|
} |
|
131
|
|
|
Yii::endProfile("Apply currency selections(properties)"); |
|
132
|
|
|
} |
|
133
|
|
|
Yii::endProfile("ObjectIds for this category"); |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$ids = array_map('intval', $query->column()); |
|
137
|
|
|
|
|
138
|
|
|
$query = null; |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
Yii::beginProfile("all PSV ids"); |
|
142
|
|
|
$data['propertyStaticValueIds'] = []; |
|
143
|
|
|
if (count($ids) !== 0) { |
|
144
|
|
|
$q4psv = (new Query()) |
|
145
|
|
|
->select('property_static_value_id') |
|
146
|
|
|
->from(ObjectStaticValues::tableName()) |
|
147
|
|
|
->distinct() |
|
148
|
|
|
->where(['object_id' => $object->id]) |
|
149
|
|
|
->andWhere('object_model_id in (' . implode(',', $ids) . ')'); |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
$data['propertyStaticValueIds'] = array_map('intval', $q4psv->column()); |
|
153
|
|
|
} |
|
154
|
|
|
Yii::endProfile("all PSV ids"); |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$ids = null; |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
Yii::beginProfile("Property ids from PSV ids"); |
|
160
|
|
|
$data['propertyIds'] = []; |
|
161
|
|
|
if (count($data['propertyStaticValueIds']) !== 0) { |
|
162
|
|
|
$data['propertyIds'] = PropertyStaticValues::find() |
|
163
|
|
|
->select('property_id') |
|
164
|
|
|
->distinct() |
|
165
|
|
|
->where(['dont_filter' => 0]) |
|
166
|
|
|
->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')') |
|
167
|
|
|
->asArray() |
|
168
|
|
|
->column(); |
|
169
|
|
|
} |
|
170
|
|
|
Yii::endProfile("Property ids from PSV ids"); |
|
171
|
|
|
|
|
172
|
|
|
Yii::$app->cache->set( |
|
173
|
|
|
$cacheKey, |
|
174
|
|
|
$data, |
|
175
|
|
|
86400, |
|
176
|
|
|
new TagDependency([ |
|
177
|
|
|
'tags' => [ |
|
178
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class) |
|
179
|
|
|
], |
|
180
|
|
|
]) |
|
181
|
|
|
); |
|
182
|
|
|
$object = null; |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
Yii::endProfile("onlyAvailableFilters"); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->possibleSelections = []; |
|
189
|
|
|
|
|
190
|
|
|
$groups = PropertyGroup::getForObjectId($this->objectId); |
|
191
|
|
|
|
|
192
|
|
|
foreach ($groups as $group) { |
|
193
|
|
|
|
|
194
|
|
|
if ($this->onlyGroupsIds !== null) { |
|
195
|
|
|
if (in_array($group->id, $this->onlyGroupsIds) === false) { |
|
196
|
|
|
// skip this group |
|
197
|
|
|
continue; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if ($group->is_internal) { |
|
202
|
|
|
continue; |
|
203
|
|
|
} |
|
204
|
|
|
$this->possibleSelections[$group->id] = [ |
|
205
|
|
|
'group' => $group, |
|
206
|
|
|
'selections' => [], |
|
207
|
|
|
'static_selections' => [], |
|
208
|
|
|
'dynamic_selections' => [], |
|
209
|
|
|
]; |
|
210
|
|
|
$props = Property::getForGroupId($group->id); |
|
211
|
|
|
foreach ($props as $p) { |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) { |
|
214
|
|
|
if ($this->disableInsteadOfHide === false) { |
|
215
|
|
|
continue; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
if ($p->dont_filter) { |
|
219
|
|
|
continue; |
|
220
|
|
|
} |
|
221
|
|
|
if ($p->has_static_values) { |
|
222
|
|
|
$propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id); |
|
223
|
|
|
foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
|
224
|
|
|
|
|
225
|
|
|
if ($propertyStaticValue['dont_filter']) { |
|
226
|
|
|
unset($propertyStaticValues[$key]); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
if ($this->onlyAvailableFilters) { |
|
230
|
|
|
foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
|
231
|
|
|
|
|
232
|
|
|
if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) { |
|
233
|
|
|
if ($this->disableInsteadOfHide === true) { |
|
234
|
|
|
$this->disabled_ids[]=$propertyStaticValue['id']; |
|
235
|
|
|
} else { |
|
236
|
|
|
unset($propertyStaticValues[$key]); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues; |
|
243
|
|
|
} elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') { |
|
244
|
|
|
$this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
if (count($this->possibleSelections[$group->id]) === 0) { |
|
249
|
|
|
unset($this->possibleSelections[$group->id]); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.