1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Extension; |
4
|
|
|
|
5
|
|
|
use Request; |
6
|
|
|
use KodiComponents\Support\HtmlAttributes; |
7
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
8
|
|
|
use SleepingOwl\Admin\Contracts\Display\Placable; |
9
|
|
|
use SleepingOwl\Admin\Display\Column\Filter\Control; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface; |
12
|
|
|
|
13
|
|
|
class ColumnFilters extends Extension implements Initializable, Placable |
14
|
|
|
{ |
15
|
|
|
use HtmlAttributes; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ColumnFilterInterface[] |
19
|
|
|
*/ |
20
|
|
|
protected $columnFilters = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|\Illuminate\View\View |
24
|
|
|
*/ |
25
|
|
|
protected $view = 'display.extensions.columns_filters_table'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $placement = 'table.footer'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array|ColumnFilterInterface $columnFilters |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function set($columnFilters) |
38
|
|
|
{ |
39
|
|
|
if (! is_array($columnFilters)) { |
40
|
|
|
$columnFilters = func_get_args(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->columnFilters = []; |
44
|
|
|
|
45
|
|
|
foreach ($columnFilters as $filter) { |
46
|
|
|
$this->push($filter); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ColumnFilterInterface[] |
54
|
|
|
*/ |
55
|
|
|
public function all() |
56
|
|
|
{ |
57
|
|
|
return $this->columnFilters; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ColumnFilterInterface $filter |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function push(ColumnFilterInterface $filter = null) |
66
|
|
|
{ |
67
|
|
|
$this->columnFilters[] = $filter; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove last element. |
74
|
|
|
*/ |
75
|
|
|
public function pop() |
76
|
|
|
{ |
77
|
|
|
array_pop($this->columnFilters); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string|\Illuminate\View\View |
82
|
|
|
*/ |
83
|
|
|
public function getView() |
84
|
|
|
{ |
85
|
|
|
return $this->view; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string|\Illuminate\View\View $view |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setView($view) |
94
|
|
|
{ |
95
|
|
|
$this->view = $view; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getPlacement() |
104
|
|
|
{ |
105
|
|
|
return $this->placement; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $placement |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setPlacement($placement) |
114
|
|
|
{ |
115
|
|
|
$this->placement = $placement; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @deprecated use getPlacement() |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getPosition() |
125
|
|
|
{ |
126
|
|
|
return $this->getPlacement(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @deprecated use setPlacement(string $placement) |
131
|
|
|
* @param string $position |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function setPosition($position) |
136
|
|
|
{ |
137
|
|
|
return $this->setPlacement($position); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the instance as an array. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function toArray() |
146
|
|
|
{ |
147
|
|
|
$this->setHtmlAttribute('data-display', class_basename($this->getDisplay())); |
148
|
|
|
|
149
|
|
|
return [ |
150
|
|
|
'filters' => $this->columnFilters, |
151
|
|
|
'attributes' => $this->htmlAttributesToString(), |
152
|
|
|
'tag' => $this->getPlacement() == 'table.header' ? 'thead' : 'tfoot', |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Initialize class. |
158
|
|
|
*/ |
159
|
|
|
public function initialize() |
160
|
|
|
{ |
161
|
|
|
if (empty($this->columnFilters)) { |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->validNumberOfFilters(); |
166
|
|
|
|
167
|
|
|
foreach ($this->all() as $filter) { |
168
|
|
|
if ($filter instanceof Initializable) { |
169
|
|
|
$filter->initialize(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->validNumberOfFilters(); |
174
|
|
|
|
175
|
|
|
$filters = collect($this->columnFilters); |
176
|
|
|
|
177
|
|
|
if ($filters->last() === null) { |
178
|
|
|
$filters->pop(); |
179
|
|
|
$filters->push(new Control()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->columnFilters = $filters; |
|
|
|
|
183
|
|
|
|
184
|
|
|
$this->prepareView(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
189
|
|
|
*/ |
190
|
|
|
public function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
191
|
|
|
{ |
192
|
|
|
$search = Request::input('columns', []); |
193
|
|
|
|
194
|
|
|
$display = $this->getDisplay(); |
195
|
|
|
|
196
|
|
|
if (! $display->getExtensions()->has('columns')) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$columns = $display->getColumns()->all(); |
201
|
|
|
|
202
|
|
|
if (! is_int(key($search))) { |
203
|
|
|
$search = [$search]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ($search as $index => $columnData) { |
207
|
|
|
$column = $columns->get($index); |
208
|
|
|
$columnFilter = array_get($this->all(), $index); |
209
|
|
|
|
210
|
|
|
if ($column && $column instanceof NamedColumnInterface && $columnFilter) { |
211
|
|
|
$columnFilter->apply( |
212
|
|
|
$column, |
213
|
|
|
$query, |
214
|
|
|
array_get($columnData, 'search.value'), |
215
|
|
|
array_get($columnData, 'search') |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function validNumberOfFilters() |
222
|
|
|
{ |
223
|
|
|
$display = $this->getDisplay(); |
224
|
|
|
|
225
|
|
|
if ($display->getExtensions()->has('columns')) { |
226
|
|
|
$totalColumns = count($display->getColumns()->all()); |
227
|
|
|
$totalFilters = count($this->all()); |
228
|
|
|
$missedFilters = $totalColumns - $totalFilters; |
229
|
|
|
|
230
|
|
|
while ($missedFilters > 0) { |
231
|
|
|
$this->push(null); |
232
|
|
|
$missedFilters--; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function prepareView() |
238
|
|
|
{ |
239
|
|
|
if (! in_array($this->getPlacement(), ['table.footer', 'table.header']) && $this->view == 'display.extensions.columns_filters_table') { |
240
|
|
|
$this->view = 'display.extensions.columns_filters'; |
241
|
|
|
$this->setHtmlAttribute('class', 'table table-default'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$this->setHtmlAttribute('class', 'display-filters'); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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..