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