-
Notifications
You must be signed in to change notification settings - Fork 17
/
SearchModelTrait.php
135 lines (115 loc) · 3.68 KB
/
SearchModelTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* HiPanel core package
*
* @link https://hipanel.com/
* @package hipanel-core
* @license BSD-3-Clause
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
*/
namespace hipanel\base;
use hiqdev\hiart\ActiveQuery;
use hiqdev\hiart\ActiveRecord;
use hiqdev\hiart\ActiveDataProvider;
use yii\helpers\ArrayHelper;
/**
* Trait SearchModelTrait
* Basic behavior for search models.
*/
trait SearchModelTrait
{
public static $filterConditions = ['in', 'ne', 'like', 'ilike', 'gt', 'ge', 'lt', 'le', 'ni'];
public function attributes()
{
return $this->searchAttributes();
}
protected function searchAttributes()
{
static $attributes = [];
if ($attributes === []) {
foreach (parent::attributes() as $attribute) {
$attributes = array_merge($attributes, $this->buildAttributeConditions($attribute));
}
}
return $attributes;
}
/**
* Builds all possible $attribute names using [[$filterConditions]].
*
* @param $attribute
* @return array
*/
protected function buildAttributeConditions($attribute)
{
$attributes = [];
foreach (array_merge([''], static::$filterConditions) as $condition) {
$attributes[] = $attribute . ($condition === '' ? '' : "_$condition");
}
return $attributes;
}
public function rules()
{
$rules = parent::rules();
$rules[] = [$this->searchAttributes(), 'safe'];
return $rules;
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return \yii\base\Model::scenarios();
}
/**
* Creates data provider instance with search query applied.
* @param $params
* @param array $dataProviderConfig
* @throws \yii\base\InvalidConfigException
* @return ActiveDataProvider
*/
public function search($params, $dataProviderConfig = [])
{
/**
* @var ActiveRecord
* @var ActiveRecord $class
* @var ActiveQuery $query
*/
$class = get_parent_class();
$query = $class::find(); // $class::find()->orderBy($sort->orders)->all(); if $sort is Sort
$dataProvider = new ActiveDataProvider(ArrayHelper::merge(['query' => $query], $dataProviderConfig));
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
foreach ($this->attributes() as $attribute) {
if (($value = $this->{$attribute}) === null) {
continue;
}
/*
* Extracts underscore suffix from the key.
*
* Examples:
* client_id -> 0 - client_id, 1 - client, 2 - _id, 3 - id
* server_owner_like -> 0 - server_owner_like, 1 - server_owner, 2 - _like, 3 - like
*/
preg_match('/^(.*?)(_((?:.(?!_))+))?$/', $attribute, $matches);
/// If the suffix is in the list of acceptable suffix filer conditions
if ($matches[3] && in_array($matches[3], static::$filterConditions, true)) {
$cmp = $matches[3];
$attribute = $matches[1];
} else {
$cmp = 'eq';
}
$query->andFilterWhere([$cmp, $attribute, $value]);
}
return $dataProvider;
}
/**
* Returns default labels.
* Solves infinite recursion problem.
* @return array
*/
public function defaultAttributeLabels()
{
/// XXX parent::attributeLabels() creates infinite recursion
$class = parent::class;
return (new $class())->attributeLabels();
}
}