|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\components\search\SearchEvent; |
|
6
|
|
|
use app\components\search\SearchInterface; |
|
7
|
|
|
use app\modules\core\CoreModule; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\Model; |
|
10
|
|
|
use yii\db\Query; |
|
11
|
|
|
|
|
12
|
|
|
class Search extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const QUERY_SEARCH_PRODUCTS_BY_DESCRIPTION = 'query_search_products_by_description'; |
|
16
|
|
|
const QUERY_SEARCH_PRODUCTS_BY_PROPERTY = 'query_search_products_by_property'; |
|
17
|
|
|
const QUERY_SEARCH_PAGES_BY_DESCRIPTION = 'query_search_pages_by_description'; |
|
18
|
|
|
|
|
19
|
|
|
public $q = ''; |
|
20
|
|
|
|
|
21
|
|
|
public function attributeLabels() |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
'q' => \Yii::t('app', 'Do Search') . '...' |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function rules() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
['q', 'string', 'min' => 3, 'skipOnEmpty' => false], |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function searchByKey($key) |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
$result = []; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var $module CoreModule; |
|
41
|
|
|
*/ |
|
42
|
|
|
$module = Yii::$app->getModule('core'); |
|
43
|
|
|
|
|
44
|
|
|
if (!empty($module->searchHandlers[$key])) { |
|
45
|
|
|
foreach ($module->searchHandlers[$key] as $class) { |
|
46
|
|
|
if (is_subclass_of($class, SearchInterface::class)) { |
|
|
|
|
|
|
47
|
|
|
$this->on($key, [$class, 'editQuery']); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
$event = new SearchEvent(); |
|
51
|
|
|
$event->q = $this->q; |
|
52
|
|
|
$event->activeQuery = (new Query()); |
|
53
|
|
|
$this->trigger($key, $event); |
|
54
|
|
|
$result = $event->getAll(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $result; |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function searchProductsByProperty() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->searchByKey(self::QUERY_SEARCH_PRODUCTS_BY_PROPERTY); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function searchProductsByDescription() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->searchByKey(self::QUERY_SEARCH_PRODUCTS_BY_DESCRIPTION); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function searchPagesByDescription() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->searchByKey(self::QUERY_SEARCH_PAGES_BY_DESCRIPTION); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|