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
|
|
|
$query->where($this->getName(), $this->getOperator(), $this->getValue() . '%'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __construct($name, $title) |
33
|
|
|
{ |
34
|
|
|
$this->setName($name)->setTitle($title); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function initialize() |
38
|
|
|
{ |
39
|
|
|
if (is_null($value = $this->getValue())) { |
40
|
|
|
$value = $this->getRequestInputValue(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->setValue($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getRequestInputValue() |
47
|
|
|
{ |
48
|
|
|
$name = $this->getName(); |
49
|
|
|
return request()->input($name); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function getValue() |
56
|
|
|
{ |
57
|
|
|
return $this->value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getViewValue() |
64
|
|
|
{ |
65
|
|
|
$value = $this->getValue(); |
66
|
|
|
if (is_null($value)) { |
67
|
|
|
return $this->getDefaultValue(); |
68
|
|
|
} |
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function setValue($value) |
76
|
|
|
{ |
77
|
|
|
$this->value = $value; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function setName($name) |
94
|
|
|
{ |
95
|
|
|
$this->name = $name; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getTitle() |
104
|
|
|
{ |
105
|
|
|
return $this->title; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function setTitle($title) |
112
|
|
|
{ |
113
|
|
|
$this->title = $title; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getDefaultValue() |
122
|
|
|
{ |
123
|
|
|
return $this->defaultValue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function setDefaultValue($defaultValue) |
130
|
|
|
{ |
131
|
|
|
$this->defaultValue = $defaultValue; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getOperator() |
140
|
|
|
{ |
141
|
|
|
return $this->operator; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function setOperator(string $operator) |
148
|
|
|
{ |
149
|
|
|
$this->operator = $operator; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function isActive() |
155
|
|
|
{ |
156
|
|
|
return !is_null($this->value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function toArray() |
160
|
|
|
{ |
161
|
|
|
return [ |
162
|
|
|
'name' => $this->getName(), |
163
|
|
|
'title' => $this->getTitle(), |
164
|
|
|
'type' => $this->type, |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function jsonSerialize() |
169
|
|
|
{ |
170
|
|
|
return $this->toArray(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function toJson($options = 0) |
174
|
|
|
{ |
175
|
|
|
return json_encode($this->jsonSerialize(), $options); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function __toString() |
179
|
|
|
{ |
180
|
|
|
return $this->toJson(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|