|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\components\search; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use app\models\Object; |
|
7
|
|
|
use app\models\ObjectStaticValues; |
|
8
|
|
|
use app\models\PropertyStaticValues; |
|
9
|
|
|
use app\modules\shop\models\Product; |
|
10
|
|
|
use yii\db\Query; |
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
12
|
|
|
|
|
13
|
|
|
class SearchProductsByPropertyHandler implements SearchInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public static function editQuery(SearchEvent $event) |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \app\modules\shop\ShopModule $module */ |
|
18
|
|
|
$module = \Yii::$app->modules['shop']; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \app\modules\shop\ShopModule $module */ |
|
21
|
|
|
$properties = (new Query()) |
|
22
|
|
|
->select('`id`') |
|
23
|
|
|
->from(PropertyStaticValues::tableName()) |
|
24
|
|
|
->where('`name` LIKE :q') |
|
25
|
|
|
->addParams([':q' => '%' . $event->q . '%']) |
|
26
|
|
|
->all(); |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$event->activeQuery->select('{{%object_static_values}}.object_model_id') |
|
30
|
|
|
->distinct(true) |
|
31
|
|
|
->from(ObjectStaticValues::tableName()) |
|
32
|
|
|
->where('{{%object_static_values}}.object_id = :objectId') |
|
33
|
|
|
->addParams([':objectId' => Object::getForClass(Product::className())->id]) |
|
34
|
|
|
->andWhere([ |
|
35
|
|
|
'in', |
|
36
|
|
|
'{{%object_static_values}}.property_static_value_id', |
|
37
|
|
|
ArrayHelper::getColumn($properties, 'id') |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
if ($module->allowSearchGeneratedProducts != 1) { |
|
41
|
|
|
|
|
42
|
|
|
$event->activeQuery->innerJoin( |
|
43
|
|
|
'{{%product}}', |
|
44
|
|
|
'{{%product}}.id = {{%object_static_values}}.object_model_id' |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$event->activeQuery->andWhere( |
|
48
|
|
|
[ |
|
49
|
|
|
'{{%product}}.parent_id' => 0, |
|
50
|
|
|
'{{%product}}.active' => 1 |
|
51
|
|
|
] |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$event->setFunctionSearch(function ($activeQuery) { |
|
56
|
|
|
return ArrayHelper::getColumn($activeQuery->all(), 'object_model_id'); |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |