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