|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\formgenerator\roa\models; |
|
4
|
|
|
|
|
5
|
|
|
use roaresearch\yii2\roa\ResourceSearch; |
|
6
|
|
|
use yii\data\ActiveDataProvider; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Contract to filter and sort collections of `Solicitude` records. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Angel (Faryshta) Guevara <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $value |
|
14
|
|
|
*/ |
|
15
|
|
|
class SolicitudeSearch extends Solicitude implements ResourceSearch |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @inhertidoc |
|
19
|
|
|
*/ |
|
20
|
2 |
|
protected function slugBehaviorConfig(): array |
|
21
|
|
|
{ |
|
22
|
|
|
return [ |
|
23
|
2 |
|
'idAttribute' => [], |
|
24
|
|
|
'parentSlugRelation' => 'form', |
|
25
|
|
|
'resourceName' => 'form', |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inhertidoc |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function attributes() |
|
33
|
|
|
{ |
|
34
|
2 |
|
return array_merge(parent::attributes(), ['value']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inhertidoc |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function rules() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
2 |
|
[['form_id'], 'required'], |
|
44
|
|
|
[['form_id', 'created_by'], 'integer'], |
|
45
|
|
|
[['value'], 'default', 'value' => []], |
|
46
|
|
|
[['value'], 'validateValues'], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function validateValues($attribute, $params, $validator) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$values = $this->$attribute; |
|
53
|
1 |
|
if (!is_array($values)) { |
|
54
|
|
|
$validator->addError( |
|
55
|
|
|
$this, |
|
56
|
|
|
$attribute, |
|
57
|
|
|
'{attribute} must receive an array' |
|
58
|
|
|
); |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
1 |
|
foreach ($values as $field_id => $value) { |
|
62
|
1 |
|
if (is_string($field_id)) { |
|
63
|
|
|
$validator->addError( |
|
64
|
|
|
$this, |
|
65
|
|
|
$attribute, |
|
66
|
|
|
'`value` only accepts key integers to filter by `field_id`.' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
1 |
|
if (empty($value)) { |
|
70
|
|
|
$validator->addError( |
|
71
|
|
|
$this, |
|
72
|
|
|
$attribute, |
|
73
|
1 |
|
'`value` must not be empty.' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inhertidoc |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function search( |
|
83
|
|
|
array $params, |
|
84
|
|
|
?string $formName = '' |
|
85
|
|
|
): ?ActiveDataProvider { |
|
86
|
2 |
|
$this->load($params, $formName); |
|
87
|
2 |
|
if (!$this->validate()) { |
|
88
|
1 |
|
return null; |
|
89
|
|
|
} |
|
90
|
2 |
|
$class = get_parent_class(); |
|
91
|
2 |
|
$query = $class::find()->andFilterWhere([ |
|
92
|
2 |
|
'form_id' => $this->form_id, |
|
93
|
2 |
|
'created_by' => $this->created_by, |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
2 |
|
foreach ($this->value as $field_id => $value) { |
|
|
|
|
|
|
97
|
1 |
|
$alias = "value_$field_id"; |
|
|
|
|
|
|
98
|
1 |
|
$query->andWhere([ |
|
99
|
1 |
|
'exists', |
|
100
|
1 |
|
SolicitudeValue::find()->andWhere([ |
|
101
|
1 |
|
'and', |
|
102
|
1 |
|
'solicitude_id = id', |
|
103
|
1 |
|
['field_id' => $field_id], |
|
104
|
|
|
[ |
|
105
|
1 |
|
'like', |
|
106
|
1 |
|
'value', |
|
107
|
1 |
|
$value, |
|
108
|
|
|
], |
|
109
|
|
|
]) |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
return new ActiveDataProvider(['query' => $query]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|