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