|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\components\search\SearchEvent; |
|
6
|
|
|
use app\components\search\SearchInterface; |
|
7
|
|
|
use app\modules\page\models\Page; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\Model; |
|
10
|
|
|
use yii\db\Query; |
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
12
|
|
|
|
|
13
|
|
|
class Search extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
const QUERY_SEARCH_BY_DESCRIPTION = 'query_search_by_description'; |
|
17
|
|
|
public $q = ''; |
|
18
|
|
|
|
|
19
|
|
|
public function attributeLabels() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'q' => \Yii::t('app', 'Do Search') . '...' |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function rules() |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
['q', 'string', 'min' => 3, 'skipOnEmpty' => false], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function searchProductsByProperty() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$result = (new Query()) |
|
36
|
|
|
->select('`id`') |
|
37
|
|
|
->from(PropertyStaticValues::tableName()) |
|
38
|
|
|
->where('`name` LIKE :q') |
|
39
|
|
|
->addParams([':q' => '%' . $this->q . '%']) |
|
40
|
|
|
->all(); |
|
41
|
|
|
$result = (new Query()) |
|
42
|
|
|
->select('`object_model_id`') |
|
43
|
|
|
->distinct(true) |
|
44
|
|
|
->from(ObjectStaticValues::tableName()) |
|
45
|
|
|
->where('`object_id` = :objectId') |
|
46
|
|
|
->addParams([':objectId' => 1]) |
|
47
|
|
|
->andWhere(['in', '`property_static_value_id`', ArrayHelper::getColumn($result, 'id')]) |
|
48
|
|
|
->all(); |
|
49
|
|
|
return ArrayHelper::getColumn($result, 'object_model_id'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function searchProductsByDescription() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
$module = Yii::$app->getModule('core'); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($module->handlersSearchProductByDescription as $class) { |
|
58
|
|
|
if (is_subclass_of($class, SearchInterface::class)) { |
|
|
|
|
|
|
59
|
|
|
$this->on(self::QUERY_SEARCH_BY_DESCRIPTION, [$class, 'editQuery']); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$event = new SearchEvent(); |
|
64
|
|
|
$event->q = $this->q; |
|
65
|
|
|
$event->activeQuery = (new Query()); |
|
66
|
|
|
$this->trigger(self::QUERY_SEARCH_BY_DESCRIPTION, $event); |
|
67
|
|
|
|
|
68
|
|
|
return ArrayHelper::getColumn($event->activeQuery->all(), 'id'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function searchPagesByDescription() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$result = (new Query()) |
|
74
|
|
|
->select('`id`') |
|
75
|
|
|
->from(Page::tableName()) |
|
76
|
|
|
->orWhere('`title` LIKE :q') |
|
77
|
|
|
->orWhere('`h1` LIKE :q') |
|
78
|
|
|
->orWhere('`content` LIKE :q') |
|
79
|
|
|
->addParams([':q' => '%' . $this->q . '%']) |
|
80
|
|
|
->andWhere('published=1') |
|
81
|
|
|
->andWhere('searchable=1') |
|
82
|
|
|
->all(); |
|
83
|
|
|
return ArrayHelper::getColumn($result, 'id'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.