1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\widgets\filter; |
4
|
|
|
|
5
|
|
|
use app\models\BaseObject; |
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
|
|
|
public $currentObjects = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var null|array Array of group ids to display in filter, null to display all available for Object |
36
|
|
|
*/ |
37
|
|
|
public $onlyGroupsIds = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array Additional params passed to filter view |
41
|
|
|
*/ |
42
|
|
|
public $additionalViewParams = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function run() |
48
|
|
|
{ |
49
|
|
|
Yii::beginProfile("FilterWidget"); |
50
|
|
|
|
51
|
|
|
$view = $this->getView(); |
52
|
|
|
FilterWidgetAsset::register($view); |
53
|
|
|
$view->registerJs( |
54
|
|
|
"jQuery('#{$this->id}').getFilters();" |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
Yii::beginProfile("GetPossibleSelections"); |
58
|
|
|
$this->getPossibleSelections(); |
59
|
|
|
Yii::endProfile("GetPossibleSelections"); |
60
|
|
|
|
61
|
|
|
$result = $this->render( |
62
|
|
|
$this->viewFile, |
63
|
|
|
ArrayHelper::merge([ |
64
|
|
|
'id' => $this->id, |
65
|
|
|
'current_selections' => $this->currentSelections, |
66
|
|
|
'possible_selections' => $this->possibleSelections, |
67
|
|
|
'object_id' => $this->objectId, |
68
|
|
|
'title' => $this->title, |
69
|
|
|
'go_back_alignment' => $this->goBackAlignment, |
70
|
|
|
'route' => $this->route, |
71
|
|
|
'category_group_id' => $this->categoryGroupId, |
72
|
|
|
'disabled_ids' => $this->disabled_ids, |
73
|
|
|
'render_dynamic' => $this->render_dynamic, |
74
|
|
|
], $this->additionalViewParams) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
Yii::endProfile("FilterWidget"); |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getPossibleSelections() |
83
|
|
|
{ |
84
|
|
|
$data = [ |
85
|
|
|
'propertyIds' => [], |
86
|
|
|
'propertyStaticValueIds' => [], |
87
|
|
|
]; |
88
|
|
|
if ($this->onlyAvailableFilters) { |
89
|
|
|
Yii::beginProfile("onlyAvailableFilters"); |
90
|
|
|
$object = BaseObject::findById($this->objectId); |
91
|
|
|
if (!is_null($object) && isset($this->currentSelections['last_category_id'])) { |
92
|
|
|
|
93
|
|
|
$cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':' |
94
|
|
|
. Json::encode($this->currentSelections['properties']); |
95
|
|
|
$data = Yii::$app->cache->get($cacheKey); |
96
|
|
|
if ($data === false) { |
97
|
|
|
$data = []; |
98
|
|
|
Yii::beginProfile("ObjectIds for this category"); |
99
|
|
|
if (count($this->currentObjects) == 0) { |
100
|
|
|
$psv = []; |
101
|
|
|
array_walk_recursive($this->currentSelections['properties'], function ($item) use (&$psv) { |
102
|
|
|
if (is_array($item) === false) { |
103
|
|
|
$psv[] = $item; |
104
|
|
|
} |
105
|
|
|
}); |
106
|
|
|
$product = Yii::$container->get(Product::class); |
107
|
|
|
$query = (new Query) |
108
|
|
|
->select('pc.object_model_id') |
109
|
|
|
->from($object->categories_table_name . ' pc') |
110
|
|
|
->innerJoin($product::tableName() . ' product', 'product.id = pc.object_model_id') |
111
|
|
|
->where([ |
112
|
|
|
'pc.category_id' => $this->currentSelections['last_category_id'], |
113
|
|
|
'product.active' => 1, |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
if (count($this->currentSelections['properties'])) { |
117
|
|
|
$query->innerJoin([ |
118
|
|
|
'osvm' => (new Query) |
119
|
|
|
->select('object_model_id') |
120
|
|
|
->distinct() |
121
|
|
|
->from(ObjectStaticValues::tableName()) |
122
|
|
|
->where([ |
123
|
|
|
'object_id' => $object->id, |
124
|
|
|
'property_static_value_id' => $psv |
125
|
|
|
]) |
126
|
|
|
->groupBy('object_model_id') |
127
|
|
|
->having(['count(object_model_id)' => count($psv)]) |
128
|
|
|
], |
129
|
|
|
'osvm.object_model_id = pc.object_model_id' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
Yii::endProfile("ObjectIds for this category"); |
134
|
|
|
|
135
|
|
|
$ids = array_map('intval', $query->column()); |
136
|
|
|
$query = null; |
|
|
|
|
137
|
|
|
} else { |
138
|
|
|
$ids = ArrayHelper::getColumn($this->currentObjects, 'id'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
Yii::beginProfile("all PSV ids"); |
143
|
|
|
$data['propertyStaticValueIds'] = []; |
144
|
|
|
if (count($ids) !== 0) { |
145
|
|
|
$q4psv = (new Query()) |
146
|
|
|
->select('property_static_value_id') |
147
|
|
|
->from(ObjectStaticValues::tableName()) |
148
|
|
|
->distinct() |
149
|
|
|
->where(['object_id' => $object->id]) |
150
|
|
|
->andWhere('object_model_id in (' . implode(',', $ids) . ')'); |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
$data['propertyStaticValueIds'] = array_map('intval', $q4psv->column()); |
154
|
|
|
} |
155
|
|
|
Yii::endProfile("all PSV ids"); |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
$ids = null; |
|
|
|
|
159
|
|
|
|
160
|
|
|
Yii::beginProfile("Property ids from PSV ids"); |
161
|
|
|
$data['propertyIds'] = []; |
162
|
|
|
if (count($data['propertyStaticValueIds']) !== 0) { |
163
|
|
|
$data['propertyIds'] = PropertyStaticValues::find() |
164
|
|
|
->select('property_id') |
165
|
|
|
->distinct() |
166
|
|
|
->where(['dont_filter' => 0]) |
167
|
|
|
->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')') |
168
|
|
|
->asArray() |
169
|
|
|
->column(); |
170
|
|
|
} |
171
|
|
|
Yii::endProfile("Property ids from PSV ids"); |
172
|
|
|
|
173
|
|
|
Yii::$app->cache->set( |
174
|
|
|
$cacheKey, |
175
|
|
|
$data, |
176
|
|
|
86400, |
177
|
|
|
new TagDependency([ |
178
|
|
|
'tags' => [ |
179
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class) |
180
|
|
|
], |
181
|
|
|
]) |
182
|
|
|
); |
183
|
|
|
$object = null; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
Yii::endProfile("onlyAvailableFilters"); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->possibleSelections = []; |
190
|
|
|
|
191
|
|
|
$groups = PropertyGroup::getForObjectId($this->objectId); |
192
|
|
|
|
193
|
|
|
foreach ($groups as $group) { |
194
|
|
|
|
195
|
|
|
if ($this->onlyGroupsIds !== null) { |
196
|
|
|
if (in_array($group->id, $this->onlyGroupsIds) === false) { |
197
|
|
|
// skip this group |
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if ($group->is_internal) { |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
$this->possibleSelections[$group->id] = [ |
206
|
|
|
'group' => $group, |
207
|
|
|
'selections' => [], |
208
|
|
|
'static_selections' => [], |
209
|
|
|
'dynamic_selections' => [], |
210
|
|
|
]; |
211
|
|
|
$props = Property::getForGroupId($group->id); |
212
|
|
|
foreach ($props as $p) { |
213
|
|
|
|
214
|
|
|
if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) { |
215
|
|
|
if ($this->disableInsteadOfHide === false) { |
216
|
|
|
continue; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
if ($p->dont_filter) { |
220
|
|
|
continue; |
221
|
|
|
} |
222
|
|
|
if ($p->has_static_values) { |
223
|
|
|
$propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id); |
224
|
|
|
foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
225
|
|
|
|
226
|
|
|
if ($propertyStaticValue['dont_filter']) { |
227
|
|
|
unset($propertyStaticValues[$key]); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
if ($this->onlyAvailableFilters) { |
231
|
|
|
foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
232
|
|
|
|
233
|
|
|
if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) { |
234
|
|
|
if ($this->disableInsteadOfHide === true) { |
235
|
|
|
$this->disabled_ids[]=$propertyStaticValue['id']; |
236
|
|
|
} else { |
237
|
|
|
unset($propertyStaticValues[$key]); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues; |
244
|
|
|
} elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') { |
245
|
|
|
$this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
if (count($this->possibleSelections[$group->id]) === 0) { |
250
|
|
|
unset($this->possibleSelections[$group->id]); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.