1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Filters |
6
|
|
|
{ |
7
|
|
|
public $filters = []; |
8
|
|
|
|
9
|
|
|
public function filtersEnabled() |
10
|
|
|
{ |
11
|
|
|
return ! is_array($this->filters); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function filtersDisabled() |
15
|
|
|
{ |
16
|
|
|
return is_array($this->filters); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function enableFilters() |
20
|
|
|
{ |
21
|
|
|
if ($this->filtersDisabled()) { |
22
|
|
|
$this->filters = new FiltersCollection; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function disableFilters() |
27
|
|
|
{ |
28
|
|
|
$this->filters = []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function clearFilters() |
32
|
|
|
{ |
33
|
|
|
$this->filters = new FiltersCollection; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a filter to the CRUD table view. |
38
|
|
|
* |
39
|
|
|
* @param array $options Name, type, label, etc. |
40
|
|
|
* @param array/closure $values The HTML for the filter. |
|
|
|
|
41
|
|
|
* @param closure $filter_logic Query modification (filtering) logic when filter is active. |
|
|
|
|
42
|
|
|
* @param closure $fallback_logic Query modification (filtering) logic when filter is not active. |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function addFilter($options, $values = false, $filter_logic = false, $fallback_logic = false) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
// if a closure was passed as "values" |
47
|
|
|
if (is_callable($values)) { |
48
|
|
|
// get its results |
49
|
|
|
$values = $values(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// enable the filters functionality |
53
|
|
|
$this->enableFilters(); |
54
|
|
|
|
55
|
|
|
// check if another filter with the same name exists |
56
|
|
|
if (! isset($options['name'])) { |
57
|
|
|
abort(500, 'All your filters need names.'); |
58
|
|
|
} |
59
|
|
|
if ($this->filters->contains('name', $options['name'])) { |
60
|
|
|
abort(500, "Sorry, you can't have two filters with the same name."); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// add a new filter to the interface |
64
|
|
|
$filter = new CrudFilter($options, $values, $filter_logic); |
65
|
|
|
$this->filters->push($filter); |
66
|
|
|
|
67
|
|
|
// if a closure was passed as "filter_logic" |
68
|
|
|
if ($this->doingListOperation()) { |
69
|
|
|
if ($this->request->has($options['name'])) { |
70
|
|
|
if (is_callable($filter_logic)) { |
71
|
|
|
// apply it |
72
|
|
|
$filter_logic($this->request->input($options['name'])); |
|
|
|
|
73
|
|
|
} else { |
74
|
|
|
$this->addDefaultFilterLogic($filter->name, $filter_logic); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
//if the filter is not active, but fallback logic was supplied |
78
|
|
|
if (is_callable($fallback_logic)) { |
79
|
|
|
// apply the fallback logic |
80
|
|
|
$fallback_logic(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addDefaultFilterLogic($name, $operator) |
87
|
|
|
{ |
88
|
|
|
$input = \Request::all(); |
89
|
|
|
|
90
|
|
|
// if this filter is active (the URL has it as a GET parameter) |
91
|
|
|
switch ($operator) { |
92
|
|
|
// if no operator was passed, just use the equals operator |
93
|
|
|
case false: |
94
|
|
|
$this->addClause('where', $name, $input[$name]); |
|
|
|
|
95
|
|
|
break; |
96
|
|
|
|
97
|
|
|
case 'scope': |
98
|
|
|
$this->addClause($operator); |
|
|
|
|
99
|
|
|
break; |
100
|
|
|
|
101
|
|
|
// TODO: |
102
|
|
|
// whereBetween |
103
|
|
|
// whereNotBetween |
104
|
|
|
// whereIn |
105
|
|
|
// whereNotIn |
106
|
|
|
// whereNull |
107
|
|
|
// whereNotNull |
108
|
|
|
// whereDate |
109
|
|
|
// whereMonth |
110
|
|
|
// whereDay |
111
|
|
|
// whereYear |
112
|
|
|
// whereColumn |
113
|
|
|
// like |
114
|
|
|
|
115
|
|
|
// sql comparison operators |
116
|
|
|
case '=': |
117
|
|
|
case '<=>': |
118
|
|
|
case '<>': |
119
|
|
|
case '!=': |
120
|
|
|
case '>': |
121
|
|
|
case '>=': |
122
|
|
|
case '<': |
123
|
|
|
case '<=': |
124
|
|
|
$this->addClause('where', $name, $operator, $input[$name]); |
|
|
|
|
125
|
|
|
break; |
126
|
|
|
|
127
|
|
|
default: |
128
|
|
|
abort(500, 'Unknown filter operator.'); |
129
|
|
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function filters() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $this->filters; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function removeFilter($name) |
139
|
|
|
{ |
140
|
|
|
$this->filters = $this->filters->reject(function ($filter) use ($name) { |
141
|
|
|
return $filter->name == $name; |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function removeAllFilters() |
146
|
|
|
{ |
147
|
|
|
$this->filters = collect([]); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine if the current CRUD action is a list operation (using standard or ajax DataTables). |
152
|
|
|
* @return bool |
|
|
|
|
153
|
|
|
*/ |
154
|
|
|
public function doingListOperation() |
155
|
|
|
{ |
156
|
|
|
$route = $this->route; |
|
|
|
|
157
|
|
|
|
158
|
|
|
switch ($this->request->url()) { |
159
|
|
|
case url($this->route): |
160
|
|
|
if ($this->request->getMethod() == 'POST' || |
|
|
|
|
161
|
|
|
$this->request->getMethod() == 'PATCH') { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
break; |
|
|
|
|
167
|
|
|
|
168
|
|
|
case url($this->route.'/search'): |
169
|
|
|
return true; |
170
|
|
|
break; |
|
|
|
|
171
|
|
|
|
172
|
|
|
default: |
173
|
|
|
return false; |
174
|
|
|
break; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
class FiltersCollection extends \Illuminate\Support\Collection |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
public function removeFilter($name) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// public function count() |
|
|
|
|
186
|
|
|
// { |
187
|
|
|
// dd($this); |
|
|
|
|
188
|
|
|
// } |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
class CrudFilter |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
public $name; // the name of the filtered variable (db column name) |
194
|
|
|
public $type = 'select'; // the name of the filter view that will be loaded |
195
|
|
|
public $label; |
196
|
|
|
public $placeholder; |
197
|
|
|
public $values; |
198
|
|
|
public $options; |
199
|
|
|
public $currentValue; |
200
|
|
|
public $view; |
201
|
|
|
|
202
|
|
|
public function __construct($options, $values, $filter_logic) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$this->checkOptionsIntegrity($options); |
205
|
|
|
|
206
|
|
|
$this->name = $options['name']; |
207
|
|
|
$this->type = $options['type']; |
208
|
|
|
$this->label = $options['label']; |
209
|
|
|
|
210
|
|
|
if (! isset($options['placeholder'])) { |
211
|
|
|
$this->placeholder = ''; |
212
|
|
|
} else { |
213
|
|
|
$this->placeholder = $options['placeholder']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->values = $values; |
217
|
|
|
$this->options = $options; |
218
|
|
|
$this->setView(); |
219
|
|
|
|
220
|
|
|
if (\Request::has($this->name)) { |
221
|
|
|
$this->currentValue = \Request::input($this->name); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function setView() |
226
|
|
|
{ |
227
|
|
|
if (isset($this->options['view_namespace'])) { |
228
|
|
|
$this->view = $this->options['view_namespace'] . '.' . $this->type; |
229
|
|
|
} else { |
230
|
|
|
$this->view = 'crud::filters.' . $this->type; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function checkOptionsIntegrity($options) |
235
|
|
|
{ |
236
|
|
|
if (! isset($options['name'])) { |
237
|
|
|
abort(500, 'Please make sure all your filters have names.'); |
238
|
|
|
} |
239
|
|
|
if (! isset($options['type'])) { |
240
|
|
|
abort(500, 'Please make sure all your filters have types.'); |
241
|
|
|
} |
242
|
|
|
if (! \View::exists('crud::filters.'.$options['type'])) { |
243
|
|
|
abort(500, 'No filter view named "'.$options['type'].'.blade.php" was found.'); |
244
|
|
|
} |
245
|
|
|
if (! isset($options['label'])) { |
246
|
|
|
abort(500, 'Please make sure all your filters have labels.'); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function isActive() |
251
|
|
|
{ |
252
|
|
|
if (\Request::has($this->name)) { |
|
|
|
|
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..