1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Laravel Datatables Collection Engine |
7
|
|
|
* |
8
|
|
|
* @package Laravel |
9
|
|
|
* @category Package |
10
|
|
|
* @author Arjay Angeles <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Closure; |
14
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
use Illuminate\Support\Collection; |
17
|
|
|
use Illuminate\Support\Str; |
18
|
|
|
use Yajra\Datatables\Contracts\DataTableEngineContract; |
19
|
|
|
use Yajra\Datatables\Request; |
20
|
|
|
|
21
|
|
|
class CollectionEngine extends BaseEngine implements DataTableEngineContract |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Collection object |
25
|
|
|
* |
26
|
|
|
* @var Collection |
27
|
|
|
*/ |
28
|
|
|
public $collection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Collection object |
32
|
|
|
* |
33
|
|
|
* @var Collection |
34
|
|
|
*/ |
35
|
|
|
public $original_collection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Collection $collection |
39
|
|
|
* @param \Yajra\Datatables\Request $request |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Collection $collection, Request $request) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->request = $request; |
44
|
|
|
$this->collection = $collection; |
45
|
|
|
$this->original_collection = $collection; |
46
|
|
|
$this->columns = array_keys($this->serialize($collection->first())); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Serialize collection |
51
|
|
|
* |
52
|
|
|
* @param mixed $collection |
53
|
|
|
* @return mixed|null |
54
|
|
|
*/ |
55
|
|
|
protected function serialize($collection) |
56
|
|
|
{ |
57
|
|
|
return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function filter(Closure $callback) |
64
|
|
|
{ |
65
|
|
|
$this->overrideGlobalSearch($callback, $this); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function showDebugger(array $output) |
74
|
|
|
{ |
75
|
|
|
$output["input"] = $this->request->all(); |
76
|
|
|
|
77
|
|
|
return $output; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function totalCount() |
84
|
|
|
{ |
85
|
|
|
return $this->count(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function count() |
92
|
|
|
{ |
93
|
|
|
return $this->collection->count(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function ordering() |
100
|
|
|
{ |
101
|
|
|
if ($this->orderCallback) { |
102
|
|
|
call_user_func($this->orderCallback, $this); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($this->request->orderableColumns() as $orderable) { |
108
|
|
|
$column = $this->getColumnName($orderable['column']); |
109
|
|
|
$this->collection = $this->collection->sortBy( |
110
|
|
|
function ($row) use ($column) { |
111
|
|
|
$data = $this->serialize($row); |
112
|
|
|
|
113
|
|
|
return Arr::get($data, $column); |
114
|
|
|
} |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
if ($orderable['direction'] == 'desc') { |
118
|
|
|
$this->collection = $this->collection->reverse(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function filtering() |
127
|
|
|
{ |
128
|
|
|
$columns = $this->request['columns']; |
129
|
|
|
$this->collection = $this->collection->filter( |
130
|
|
|
function ($row) use ($columns) { |
131
|
|
|
$data = $this->serialize($row); |
132
|
|
|
$this->isFilterApplied = true; |
133
|
|
|
$found = []; |
134
|
|
|
|
135
|
|
|
$keyword = $this->request->keyword(); |
136
|
|
|
foreach ($this->request->searchableColumnIndex() as $index) { |
137
|
|
|
$column = $this->getColumnName($index); |
138
|
|
|
if (! $value = Arr::get($data, $column)) { |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($this->isCaseInsensitive()) { |
143
|
|
|
$found[] = Str::contains(Str::lower($value), Str::lower($keyword)); |
144
|
|
|
} else { |
145
|
|
|
$found[] = Str::contains($value, $keyword); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return in_array(true, $found); |
150
|
|
|
} |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
public function columnSearch() |
158
|
|
|
{ |
159
|
|
|
$columns = $this->request->get('columns'); |
160
|
|
|
for ($i = 0, $c = count($columns); $i < $c; $i++) { |
161
|
|
|
if ($this->request->isColumnSearchable($i)) { |
162
|
|
|
$this->isFilterApplied = true; |
163
|
|
|
|
164
|
|
|
$column = $this->getColumnName($i); |
165
|
|
|
$keyword = $this->request->columnKeyword($i); |
166
|
|
|
|
167
|
|
|
$this->collection = $this->collection->filter( |
168
|
|
|
function ($row) use ($column, $keyword) { |
169
|
|
|
$data = $this->serialize($row); |
170
|
|
|
if ($this->isCaseInsensitive()) { |
171
|
|
|
return strpos(Str::lower($data[$column]), Str::lower($keyword)) !== false; |
172
|
|
|
} else { |
173
|
|
|
return strpos($data[$column], $keyword) !== false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
|
|
public function paging() |
185
|
|
|
{ |
186
|
|
|
$this->collection = $this->collection->slice( |
187
|
|
|
$this->request['start'], |
188
|
|
|
(int) $this->request['length'] > 0 ? $this->request['length'] : 10 |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function results() |
196
|
|
|
{ |
197
|
|
|
return $this->collection->all(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
public function make($mDataSupport = false, $orderFirst = true) |
204
|
|
|
{ |
205
|
|
|
return parent::make($mDataSupport, $orderFirst); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|