1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Laravel\Scout\Builder; |
8
|
|
|
use Yajra\DataTables\DataTableAbstract; |
9
|
|
|
|
10
|
|
|
class ScoutDataTable extends DataTableAbstract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Model |
14
|
|
|
*/ |
15
|
|
|
protected $model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Builder |
19
|
|
|
*/ |
20
|
|
|
protected $builder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Collection |
24
|
|
|
*/ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ScoutDataTable constructor. |
29
|
|
|
* |
30
|
|
|
* @param Model $model |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Model $model) |
33
|
|
|
{ |
34
|
|
|
$this->model = $model; |
35
|
|
|
$this->request = resolve('datatables.request'); |
36
|
|
|
$this->config = resolve('datatables.config'); |
37
|
|
|
$this->builder = new Builder($this->model, $this->request->keyword()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get results. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function results() |
46
|
|
|
{ |
47
|
|
|
// Intentionally left blank. |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Count results. |
52
|
|
|
* |
53
|
|
|
* @return integer |
54
|
|
|
*/ |
55
|
|
|
public function count() |
56
|
|
|
{ |
57
|
|
|
// Intentionally left blank. |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Count total items. |
62
|
|
|
* |
63
|
|
|
* @return integer |
64
|
|
|
*/ |
65
|
|
|
public function totalCount() |
66
|
|
|
{ |
67
|
|
|
// Intentionally left blank. |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Perform column search. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function columnSearch() |
76
|
|
|
{ |
77
|
|
|
// Intentionally left blank. |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Perform pagination. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function paging() |
86
|
|
|
{ |
87
|
|
|
// Intentionally left blank. |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Organizes works. |
92
|
|
|
* |
93
|
|
|
* @param bool $mDataSupport |
94
|
|
|
* @return \Illuminate\Http\JsonResponse |
95
|
|
|
*/ |
96
|
|
|
public function make($mDataSupport = true) |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$limit = $this->request->get('length', 10); |
100
|
|
|
$start = $this->request->get('start', 0); |
101
|
|
|
$page = ($start / $limit) + 1; |
102
|
|
|
$results = $this->builder->paginate($limit, 'page', $page); |
103
|
|
|
$this->totalRecords = $results->total(); |
104
|
|
|
$this->filteredRecords = $this->totalRecords; |
105
|
|
|
|
106
|
|
|
$processed = $this->processResults($results->items(), $mDataSupport); |
107
|
|
|
$output = $this->transform($results, $processed); |
108
|
|
|
|
109
|
|
|
$this->collection = collect($output); |
110
|
|
|
$this->ordering(); |
111
|
|
|
|
112
|
|
|
return $this->render($this->collection->values()->all()); |
113
|
|
|
} catch (\Exception $exception) { |
114
|
|
|
return $this->errorResponse($exception); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $keyword |
120
|
|
|
*/ |
121
|
|
|
protected function globalSearch($keyword) |
122
|
|
|
{ |
123
|
|
|
// Intentionally left blank. |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Append debug parameters on output. |
128
|
|
|
* |
129
|
|
|
* @param array $output |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function showDebugger(array $output) |
133
|
|
|
{ |
134
|
|
|
$output['input'] = $this->request->all(); |
135
|
|
|
|
136
|
|
|
return $output; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Resolve callback parameter instance. |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
protected function resolveCallbackParameter() |
145
|
|
|
{ |
146
|
|
|
return $this->builder; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Perform default query orderBy clause. |
151
|
|
|
*/ |
152
|
|
|
protected function defaultOrdering() |
153
|
|
|
{ |
154
|
|
|
$criteria = $this->request->orderableColumns(); |
155
|
|
|
if (! empty($criteria)) { |
156
|
|
|
$sorter = function ($a, $b) use ($criteria) { |
157
|
|
|
foreach ($criteria as $orderable) { |
158
|
|
|
$column = $this->getColumnName($orderable['column']); |
159
|
|
|
$direction = $orderable['direction']; |
160
|
|
|
if ($direction === 'desc') { |
161
|
|
|
$first = $b; |
162
|
|
|
$second = $a; |
163
|
|
|
} else { |
164
|
|
|
$first = $a; |
165
|
|
|
$second = $b; |
166
|
|
|
} |
167
|
|
|
if ($this->config->isCaseInsensitive()) { |
168
|
|
|
$cmp = strnatcasecmp($first[$column], $second[$column]); |
169
|
|
|
} else { |
170
|
|
|
$cmp = strnatcmp($first[$column], $second[$column]); |
171
|
|
|
} |
172
|
|
|
if ($cmp != 0) { |
173
|
|
|
return $cmp; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
// all elements were equal |
177
|
|
|
return 0; |
178
|
|
|
}; |
179
|
|
|
$this->collection = $this->collection->sort($sorter); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|