1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\View\Filters; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Sco\Admin\Contracts\View\Filters\FilterInterface; |
7
|
|
|
|
8
|
|
|
abstract class Filter implements FilterInterface |
9
|
|
|
{ |
10
|
|
|
protected $type; |
11
|
|
|
|
12
|
|
|
protected $value; |
13
|
|
|
|
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
protected $title; |
17
|
|
|
|
18
|
|
|
protected $defaultValue; |
19
|
|
|
|
20
|
|
|
protected $operator = '='; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Apply filter to the query. |
24
|
|
|
* |
25
|
|
|
* @param Builder $query |
26
|
|
|
*/ |
27
|
|
|
public function apply(Builder $query) |
28
|
|
|
{ |
29
|
|
|
$name = $this->getName(); |
30
|
|
|
if (strpos($name, '.') !== false) { |
31
|
|
|
list($relation, $name) = explode('.', $name, 2); |
32
|
|
|
$query->whereHas($relation, function ($q) use ($name) { |
33
|
|
|
$this->buildQuery($q, $name); |
34
|
|
|
}); |
35
|
|
|
} else { |
36
|
|
|
$this->buildQuery($query, $this->getName()); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function buildQuery(Builder $query, $name) |
41
|
|
|
{ |
42
|
|
|
$op = $this->getOperator(); |
43
|
|
|
$value = $this->getValue(); |
44
|
|
|
|
45
|
|
|
switch ($op) { |
46
|
|
|
case 'in': |
47
|
|
|
$query->whereIn($name, (array)$value); |
48
|
|
|
break; |
49
|
|
|
case 'between': |
50
|
|
|
$query->whereBetween($name, (array)$value); |
51
|
|
|
break; |
52
|
|
|
case 'like': |
|
|
|
|
53
|
|
|
$value .= '%'; |
54
|
|
|
default: |
55
|
|
|
$query->where($name, $op, $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __construct($name, $title) |
61
|
|
|
{ |
62
|
|
|
$this->setName($name)->setTitle($title); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function initialize() |
66
|
|
|
{ |
67
|
|
|
if (is_null($value = $this->getValue())) { |
68
|
|
|
$value = $this->getRequestInputValue(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->setValue($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getRequestInputValue() |
75
|
|
|
{ |
76
|
|
|
$name = $this->getRequestName(); |
77
|
|
|
return request()->input($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getRequestName() |
81
|
|
|
{ |
82
|
|
|
return str_replace('.', '_', $this->getName()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getValue() |
89
|
|
|
{ |
90
|
|
|
return $this->value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getViewValue() |
97
|
|
|
{ |
98
|
|
|
$value = $this->getValue(); |
99
|
|
|
if (is_null($value)) { |
100
|
|
|
return $this->getDefaultValue(); |
101
|
|
|
} |
102
|
|
|
return $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function setValue($value) |
109
|
|
|
{ |
110
|
|
|
$this->value = $value; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getName() |
119
|
|
|
{ |
120
|
|
|
return $this->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function setName($name) |
127
|
|
|
{ |
128
|
|
|
$this->name = $name; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getTitle() |
137
|
|
|
{ |
138
|
|
|
return $this->title; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function setTitle($title) |
145
|
|
|
{ |
146
|
|
|
$this->title = $title; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function getDefaultValue() |
155
|
|
|
{ |
156
|
|
|
return $this->defaultValue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function setDefaultValue($defaultValue) |
163
|
|
|
{ |
164
|
|
|
$this->defaultValue = $defaultValue; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function getOperator() |
173
|
|
|
{ |
174
|
|
|
return $this->operator; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function setOperator(string $operator) |
181
|
|
|
{ |
182
|
|
|
$this->operator = $operator; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function isActive() |
188
|
|
|
{ |
189
|
|
|
return !is_null($this->value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function toArray() |
193
|
|
|
{ |
194
|
|
|
return [ |
195
|
|
|
'name' => $this->getName(), |
196
|
|
|
'title' => $this->getTitle(), |
197
|
|
|
'type' => $this->type, |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function jsonSerialize() |
202
|
|
|
{ |
203
|
|
|
return $this->toArray(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function toJson($options = 0) |
207
|
|
|
{ |
208
|
|
|
return json_encode($this->jsonSerialize(), $options); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function __toString() |
212
|
|
|
{ |
213
|
|
|
return $this->toJson(); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|