1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Yajra\Datatables\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CollectionEngine. |
13
|
|
|
* |
14
|
|
|
* @package Yajra\Datatables\Engines |
15
|
|
|
* @author Arjay Angeles <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class CollectionEngine extends BaseEngine |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Collection object |
21
|
|
|
* |
22
|
|
|
* @var \Illuminate\Support\Collection |
23
|
|
|
*/ |
24
|
|
|
public $collection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Collection object |
28
|
|
|
* |
29
|
|
|
* @var \Illuminate\Support\Collection |
30
|
|
|
*/ |
31
|
|
|
public $original_collection; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CollectionEngine constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Support\Collection $collection |
37
|
|
|
* @param \Yajra\Datatables\Request $request |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Collection $collection, Request $request) |
40
|
|
|
{ |
41
|
|
|
$this->request = $request; |
42
|
|
|
$this->collection = $collection; |
43
|
|
|
$this->original_collection = $collection; |
44
|
|
|
$this->columns = array_keys($this->serialize($collection->first())); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Serialize collection |
49
|
|
|
* |
50
|
|
|
* @param mixed $collection |
51
|
|
|
* @return mixed|null |
52
|
|
|
*/ |
53
|
|
|
protected function serialize($collection) |
54
|
|
|
{ |
55
|
|
|
return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set auto filter off and run your own filter. |
60
|
|
|
* Overrides global search. |
61
|
|
|
* |
62
|
|
|
* @param callable $callback |
63
|
|
|
* @param bool $globalSearch |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function filter(callable $callback, $globalSearch = false) |
67
|
|
|
{ |
68
|
|
|
$this->overrideGlobalSearch($callback, $this, $globalSearch); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Append debug parameters on output. |
75
|
|
|
* |
76
|
|
|
* @param array $output |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function showDebugger(array $output) |
80
|
|
|
{ |
81
|
|
|
$output["input"] = $this->request->all(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $output; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Count total items. |
88
|
|
|
* |
89
|
|
|
* @return integer |
90
|
|
|
*/ |
91
|
|
|
public function totalCount() |
92
|
|
|
{ |
93
|
|
|
return $this->totalRecords ? $this->totalRecords : $this->collection->count(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Count results. |
98
|
|
|
* |
99
|
|
|
* @return integer |
100
|
|
|
*/ |
101
|
|
|
public function count() |
102
|
|
|
{ |
103
|
|
|
return $this->collection->count() > $this->totalRecords ? $this->totalRecords : $this->collection->count(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Perform sorting of columns. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function ordering() |
112
|
|
|
{ |
113
|
|
|
if ($this->orderCallback) { |
114
|
|
|
call_user_func($this->orderCallback, $this); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach ($this->request->orderableColumns() as $orderable) { |
120
|
|
|
$column = $this->getColumnName($orderable['column']); |
121
|
|
|
|
122
|
|
|
$options = SORT_NATURAL; |
123
|
|
|
if ($this->isCaseInsensitive()) { |
124
|
|
|
$options = SORT_NATURAL | SORT_FLAG_CASE; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->collection = $this->collection->sortBy(function ($row) use ($column) { |
128
|
|
|
$data = $this->serialize($row); |
129
|
|
|
|
130
|
|
|
return Arr::get($data, $column); |
131
|
|
|
}, $options); |
132
|
|
|
|
133
|
|
|
if ($orderable['direction'] == 'desc') { |
134
|
|
|
$this->collection = $this->collection->reverse(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Perform global search. |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function filtering() |
145
|
|
|
{ |
146
|
|
|
$keyword = $this->request->keyword(); |
147
|
|
|
|
148
|
|
|
if ($this->isSmartSearch()) { |
149
|
|
|
$this->smartGlobalSearch($keyword); |
|
|
|
|
150
|
|
|
|
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->globalSearch($keyword); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Perform multi-term search by splitting keyword into |
159
|
|
|
* individual words and searches for each of them. |
160
|
|
|
* |
161
|
|
|
* @param string $keyword |
162
|
|
|
*/ |
163
|
|
|
private function smartGlobalSearch($keyword) |
164
|
|
|
{ |
165
|
|
|
$keywords = array_filter(explode(' ', $keyword)); |
166
|
|
|
|
167
|
|
|
foreach ($keywords as $keyword) { |
168
|
|
|
$this->globalSearch($keyword); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Perform global search for the given keyword. |
174
|
|
|
* |
175
|
|
|
* @param string $keyword |
176
|
|
|
*/ |
177
|
|
|
private function globalSearch($keyword) |
178
|
|
|
{ |
179
|
|
|
if ($this->isCaseInsensitive()) { |
180
|
|
|
$keyword = Str::lower($keyword); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$columns = $this->request->columns(); |
184
|
|
|
$this->collection = $this->collection->filter( |
185
|
|
|
function ($row) use ($columns, $keyword) { |
186
|
|
|
$data = $this->serialize($row); |
187
|
|
|
$this->isFilterApplied = true; |
188
|
|
|
|
189
|
|
|
foreach ($this->request->searchableColumnIndex() as $index) { |
190
|
|
|
$column = $this->getColumnName($index); |
191
|
|
|
if (!$value = Arr::get($data, $column)) { |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (is_array($value)) { |
196
|
|
|
continue; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($this->isCaseInsensitive()) { |
200
|
|
|
$value = Str::lower($value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (Str::contains($value, $keyword)) { |
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Perform column search. |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public function columnSearch() |
219
|
|
|
{ |
220
|
|
|
$columns = $this->request->get('columns'); |
|
|
|
|
221
|
|
|
for ($i = 0, $c = count($columns); $i < $c; $i++) { |
222
|
|
|
if ($this->request->isColumnSearchable($i)) { |
223
|
|
|
$this->isFilterApplied = true; |
224
|
|
|
$regex = $this->request->isRegex($i); |
225
|
|
|
|
226
|
|
|
$column = $this->getColumnName($i); |
227
|
|
|
$keyword = $this->request->columnKeyword($i); |
228
|
|
|
|
229
|
|
|
$this->collection = $this->collection->filter( |
230
|
|
|
function ($row) use ($column, $keyword, $regex) { |
231
|
|
|
$data = $this->serialize($row); |
232
|
|
|
|
233
|
|
|
$value = Arr::get($data, $column); |
234
|
|
|
|
235
|
|
|
if ($this->isCaseInsensitive()) { |
236
|
|
View Code Duplication |
if ($regex) { |
|
|
|
|
237
|
|
|
return preg_match('/' . $keyword . '/i', $value) == 1; |
238
|
|
|
} else { |
239
|
|
|
return strpos(Str::lower($value), Str::lower($keyword)) !== false; |
240
|
|
|
} |
241
|
|
View Code Duplication |
} else { |
|
|
|
|
242
|
|
|
if ($regex) { |
243
|
|
|
return preg_match('/' . $keyword . '/', $value) == 1; |
244
|
|
|
} else { |
245
|
|
|
return strpos($value, $keyword) !== false; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Perform pagination. |
256
|
|
|
* |
257
|
|
|
* @return void |
258
|
|
|
*/ |
259
|
|
|
public function paging() |
260
|
|
|
{ |
261
|
|
|
$this->collection = $this->collection->slice( |
262
|
|
|
$this->request->input('start'), |
263
|
|
|
(int) $this->request->input('length') > 0 ? $this->request->input('length') : 10 |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get results. |
269
|
|
|
* |
270
|
|
|
* @return mixed |
271
|
|
|
*/ |
272
|
|
|
public function results() |
273
|
|
|
{ |
274
|
|
|
return $this->collection->all(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Organizes works. |
279
|
|
|
* |
280
|
|
|
* @param bool $mDataSupport |
281
|
|
|
* @return \Illuminate\Http\JsonResponse |
282
|
|
|
*/ |
283
|
|
|
public function make($mDataSupport = false) |
284
|
|
|
{ |
285
|
|
|
try { |
286
|
|
|
$this->totalRecords = $this->totalCount(); |
287
|
|
|
|
288
|
|
|
if ($this->totalRecords) { |
289
|
|
|
$data = $this->getProcessedData($mDataSupport); |
290
|
|
|
$output = $this->transform($data); |
291
|
|
|
|
292
|
|
|
$this->collection = collect($output); |
293
|
|
|
$this->ordering(); |
294
|
|
|
$this->filterRecords(); |
295
|
|
|
$this->paginate(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $this->render($this->collection->values()->all()); |
299
|
|
|
} catch (\Exception $exception) { |
300
|
|
|
return $this->errorResponse($exception); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: