|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
class CollectionDataTable extends DataTableAbstract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Collection object |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Illuminate\Support\Collection |
|
16
|
|
|
*/ |
|
17
|
|
|
public $collection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Collection object |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Illuminate\Support\Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
public $original; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* CollectionEngine constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Illuminate\Support\Collection $collection |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Collection $collection) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->request = resolve('datatables.request'); |
|
34
|
|
|
$this->config = resolve('datatables.config'); |
|
35
|
|
|
$this->collection = $collection; |
|
36
|
|
|
$this->original = $collection; |
|
37
|
|
|
$this->columns = array_keys($this->serialize($collection->first())); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Serialize collection |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed $collection |
|
44
|
|
|
* @return mixed|null |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function serialize($collection) |
|
47
|
|
|
{ |
|
48
|
|
|
return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Append debug parameters on output. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $output |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function showDebugger(array $output) |
|
58
|
|
|
{ |
|
59
|
|
|
$output["input"] = $this->request->all(); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return $output; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Count results. |
|
66
|
|
|
* |
|
67
|
|
|
* @return integer |
|
68
|
|
|
*/ |
|
69
|
|
|
public function count() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->collection->count() > $this->totalRecords ? $this->totalRecords : $this->collection->count(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Perform column search. |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function columnSearch() |
|
80
|
|
|
{ |
|
81
|
|
|
$columns = $this->request->get('columns'); |
|
|
|
|
|
|
82
|
|
|
for ($i = 0, $c = count($columns); $i < $c; $i++) { |
|
83
|
|
|
if ($this->request->isColumnSearchable($i)) { |
|
84
|
|
|
$this->isFilterApplied = true; |
|
85
|
|
|
|
|
86
|
|
|
$regex = $this->request->isRegex($i); |
|
87
|
|
|
$column = $this->getColumnName($i); |
|
88
|
|
|
$keyword = $this->request->columnKeyword($i); |
|
89
|
|
|
|
|
90
|
|
|
$this->collection = $this->collection->filter( |
|
91
|
|
|
function ($row) use ($column, $keyword, $regex) { |
|
92
|
|
|
$data = $this->serialize($row); |
|
93
|
|
|
|
|
94
|
|
|
$value = Arr::get($data, $column); |
|
95
|
|
|
|
|
96
|
|
|
if ($this->config->isCaseInsensitive()) { |
|
97
|
|
View Code Duplication |
if ($regex) { |
|
|
|
|
|
|
98
|
|
|
return preg_match('/' . $keyword . '/i', $value) == 1; |
|
99
|
|
|
} else { |
|
100
|
|
|
return strpos(Str::lower($value), Str::lower($keyword)) !== false; |
|
101
|
|
|
} |
|
102
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
103
|
|
|
if ($regex) { |
|
104
|
|
|
return preg_match('/' . $keyword . '/', $value) == 1; |
|
105
|
|
|
} else { |
|
106
|
|
|
return strpos($value, $keyword) !== false; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Perform pagination. |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
public function paging() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->collection = $this->collection->slice( |
|
123
|
|
|
$this->request->input('start'), |
|
|
|
|
|
|
124
|
|
|
(int) $this->request->input('length') > 0 ? $this->request->input('length') : 10 |
|
|
|
|
|
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Organizes works. |
|
130
|
|
|
* |
|
131
|
|
|
* @param bool $mDataSupport |
|
132
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
133
|
|
|
*/ |
|
134
|
|
|
public function make($mDataSupport = true) |
|
135
|
|
|
{ |
|
136
|
|
|
try { |
|
137
|
|
|
$this->totalRecords = $this->totalCount(); |
|
138
|
|
|
|
|
139
|
|
|
if ($this->totalRecords) { |
|
140
|
|
|
$results = $this->results(); |
|
141
|
|
|
$processed = $this->processResults($results, $mDataSupport); |
|
142
|
|
|
$output = $this->transform($results, $processed); |
|
143
|
|
|
|
|
144
|
|
|
$this->collection = collect($output); |
|
145
|
|
|
$this->ordering(); |
|
146
|
|
|
$this->filterRecords(); |
|
147
|
|
|
$this->paginate(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->render($this->collection->values()->all()); |
|
151
|
|
|
} catch (\Exception $exception) { |
|
152
|
|
|
return $this->errorResponse($exception); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Count total items. |
|
158
|
|
|
* |
|
159
|
|
|
* @return integer |
|
160
|
|
|
*/ |
|
161
|
|
|
public function totalCount() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->totalRecords ? $this->totalRecords : $this->collection->count(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get results. |
|
168
|
|
|
* |
|
169
|
|
|
* @return mixed |
|
170
|
|
|
*/ |
|
171
|
|
|
public function results() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->collection->all(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Perform global search for the given keyword. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $keyword |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function globalSearch($keyword) |
|
182
|
|
|
{ |
|
183
|
|
|
$columns = $this->request->columns(); |
|
184
|
|
|
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword; |
|
185
|
|
|
|
|
186
|
|
|
$this->collection = $this->collection->filter(function ($row) use ($columns, $keyword) { |
|
187
|
|
|
$this->isFilterApplied = true; |
|
188
|
|
|
|
|
189
|
|
|
$data = $this->serialize($row); |
|
190
|
|
|
foreach ($this->request->searchableColumnIndex() as $index) { |
|
191
|
|
|
$column = $this->getColumnName($index); |
|
192
|
|
|
$value = Arr::get($data, $column); |
|
193
|
|
|
if (!$value || is_array($value)) { |
|
194
|
|
|
continue; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$value = $this->config->isCaseInsensitive() ? Str::lower($value) : $value; |
|
198
|
|
|
if (Str::contains($value, $keyword)) { |
|
199
|
|
|
return true; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return false; |
|
204
|
|
|
}); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Perform default query orderBy clause. |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function defaultOrdering() |
|
211
|
|
|
{ |
|
212
|
|
|
$criteria = $this->request->orderableColumns(); |
|
213
|
|
|
if (! empty($criteria)) { |
|
214
|
|
|
$sorter = function ($a, $b) use ($criteria) { |
|
215
|
|
|
foreach ($criteria as $orderable) { |
|
216
|
|
|
$column = $this->getColumnName($orderable['column']); |
|
217
|
|
|
$direction = $orderable['direction']; |
|
218
|
|
|
if ($direction === 'desc') { |
|
219
|
|
|
$first = $b; |
|
220
|
|
|
$second = $a; |
|
221
|
|
|
} else { |
|
222
|
|
|
$first = $a; |
|
223
|
|
|
$second = $b; |
|
224
|
|
|
} |
|
225
|
|
|
if ($this->config->isCaseInsensitive()) { |
|
226
|
|
|
$cmp = strnatcasecmp($first[$column], $second[$column]); |
|
227
|
|
|
} else { |
|
228
|
|
|
$cmp = strnatcmp($first[$column], $second[$column]); |
|
229
|
|
|
} |
|
230
|
|
|
if ($cmp != 0) { |
|
231
|
|
|
return $cmp; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
// all elements were equal |
|
235
|
|
|
return 0; |
|
236
|
|
|
}; |
|
237
|
|
|
$this->collection = $this->collection->sort($sorter); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Resolve callback parameter instance. |
|
243
|
|
|
* |
|
244
|
|
|
* @return $this |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function resolveCallbackParameter() |
|
247
|
|
|
{ |
|
248
|
|
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: