1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\View\Factory; |
6
|
|
|
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet; |
7
|
|
|
use Maatwebsite\Excel\Writers\LaravelExcelWriter; |
8
|
|
|
use Yajra\Datatables\Contracts\DataTableButtonsContract; |
9
|
|
|
use Yajra\Datatables\Contracts\DataTableContract; |
10
|
|
|
use Yajra\Datatables\Contracts\DataTableScopeContract; |
11
|
|
|
use Yajra\Datatables\Datatables; |
12
|
|
|
use Yajra\Datatables\Transformers\DataTransformer; |
13
|
|
|
|
14
|
|
|
abstract class DataTable implements DataTableContract, DataTableButtonsContract |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Yajra\Datatables\Datatables |
18
|
|
|
*/ |
19
|
|
|
protected $datatables; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Illuminate\Contracts\View\Factory |
23
|
|
|
*/ |
24
|
|
|
protected $viewFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Datatables print preview view. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $printPreview; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* List of columns to be exported. |
35
|
|
|
* |
36
|
|
|
* @var string|array |
37
|
|
|
*/ |
38
|
|
|
protected $exportColumns = '*'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List of columns to be printed. |
42
|
|
|
* |
43
|
|
|
* @var string|array |
44
|
|
|
*/ |
45
|
|
|
protected $printColumns = '*'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Query scopes. |
49
|
|
|
* |
50
|
|
|
* @var \Yajra\Datatables\Contracts\DataTableScopeContract[] |
51
|
|
|
*/ |
52
|
|
|
protected $scopes = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Export filename. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $filename = ''; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \Yajra\Datatables\Datatables $datatables |
63
|
|
|
* @param \Illuminate\Contracts\View\Factory $viewFactory |
64
|
|
|
*/ |
65
|
|
|
public function __construct(Datatables $datatables, Factory $viewFactory) |
66
|
|
|
{ |
67
|
|
|
$this->datatables = $datatables; |
68
|
|
|
$this->viewFactory = $viewFactory; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Render view. |
73
|
|
|
* |
74
|
|
|
* @param $view |
75
|
|
|
* @param array $data |
76
|
|
|
* @param array $mergeData |
77
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
78
|
|
|
*/ |
79
|
|
|
public function render($view, $data = [], $mergeData = []) |
80
|
|
|
{ |
81
|
|
|
if ($this->request()->ajax() && $this->request()->wantsJson()) { |
82
|
|
|
return $this->ajax(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
switch ($this->datatables->getRequest()->get('action')) { |
86
|
|
|
case 'excel': |
87
|
|
|
return $this->excel(); |
88
|
|
|
|
89
|
|
|
case 'csv': |
90
|
|
|
return $this->csv(); |
91
|
|
|
|
92
|
|
|
case 'pdf': |
93
|
|
|
return $this->pdf(); |
94
|
|
|
|
95
|
|
|
case 'print': |
96
|
|
|
return $this->printPreview(); |
97
|
|
|
|
98
|
|
|
default: |
99
|
|
|
return $this->viewFactory->make($view, $data, $mergeData)->with('dataTable', $this->html()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Datatables Request instance. |
105
|
|
|
* |
106
|
|
|
* @return \Yajra\Datatables\Request |
107
|
|
|
*/ |
108
|
|
|
public function request() |
109
|
|
|
{ |
110
|
|
|
return $this->datatables->getRequest(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Export results to Excel file. |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function excel() |
119
|
|
|
{ |
120
|
|
|
return $this->buildExcelFile()->download('xls'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Build excel file and prepare for export. |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
protected function buildExcelFile() |
129
|
|
|
{ |
130
|
|
|
return app('excel')->create($this->getFilename(), function (LaravelExcelWriter $excel) { |
131
|
|
|
$excel->sheet('exported-data', function (LaravelExcelWorksheet $sheet) { |
132
|
|
|
$sheet->fromArray($this->getDataForExport()); |
133
|
|
|
}); |
134
|
|
|
}); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get filename for export. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function filename() |
143
|
|
|
{ |
144
|
|
|
return 'export_' . time(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get mapped columns versus final decorated output. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
protected function getDataForExport() |
153
|
|
|
{ |
154
|
|
|
$columns = $this->exportColumns(); |
155
|
|
|
|
156
|
|
|
return $this->mapResponseToColumns($columns, 'exportable'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get export columns definition. |
161
|
|
|
* |
162
|
|
|
* @return array|string |
163
|
|
|
*/ |
164
|
|
|
private function exportColumns() |
165
|
|
|
{ |
166
|
|
|
return is_array($this->exportColumns) ? $this->exportColumns : $this->getColumnsFromBuilder(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get columns definition from html builder. |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
protected function getColumnsFromBuilder() |
175
|
|
|
{ |
176
|
|
|
return $this->html()->getColumns(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Optional method if you want to use html builder. |
181
|
|
|
* |
182
|
|
|
* @return \Yajra\Datatables\Html\Builder |
183
|
|
|
*/ |
184
|
|
|
public function html() |
185
|
|
|
{ |
186
|
|
|
return $this->builder(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get Datatables Html Builder instance. |
191
|
|
|
* |
192
|
|
|
* @return \Yajra\Datatables\Html\Builder |
193
|
|
|
*/ |
194
|
|
|
public function builder() |
195
|
|
|
{ |
196
|
|
|
return $this->datatables->getHtmlBuilder(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Map ajax response to columns definition. |
201
|
|
|
* |
202
|
|
|
* @param mixed $columns |
203
|
|
|
* @param string $type |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function mapResponseToColumns($columns, $type) |
207
|
|
|
{ |
208
|
|
|
return array_map(function ($row) use ($columns, $type) { |
209
|
|
|
if ($columns) { |
210
|
|
|
return (new DataTransformer())->transform($row, $columns, $type); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $row; |
214
|
|
|
}, $this->getAjaxResponseData()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get decorated data as defined in datatables ajax response. |
219
|
|
|
* |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
protected function getAjaxResponseData() |
223
|
|
|
{ |
224
|
|
|
$this->datatables->getRequest()->merge(['length' => -1]); |
225
|
|
|
|
226
|
|
|
$response = $this->ajax(); |
227
|
|
|
$data = $response->getData(true); |
228
|
|
|
|
229
|
|
|
return $data['data']; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Export results to CSV file. |
234
|
|
|
* |
235
|
|
|
* @return mixed |
236
|
|
|
*/ |
237
|
|
|
public function csv() |
238
|
|
|
{ |
239
|
|
|
return $this->buildExcelFile()->download('csv'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Export results to PDF file. |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public function pdf() |
248
|
|
|
{ |
249
|
|
|
return $this->buildExcelFile()->download('pdf'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Display printable view of datatables. |
254
|
|
|
* |
255
|
|
|
* @return \Illuminate\Contracts\View\View |
256
|
|
|
*/ |
257
|
|
|
public function printPreview() |
258
|
|
|
{ |
259
|
|
|
$data = $this->getDataForPrint(); |
260
|
|
|
$view = $this->printPreview ?: 'datatables::print'; |
261
|
|
|
|
262
|
|
|
return $this->viewFactory->make($view, compact('data')); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get mapped columns versus final decorated output. |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
protected function getDataForPrint() |
271
|
|
|
{ |
272
|
|
|
$columns = $this->printColumns(); |
273
|
|
|
|
274
|
|
|
return $this->mapResponseToColumns($columns, 'printable'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get printable columns. |
279
|
|
|
* |
280
|
|
|
* @return array|string |
281
|
|
|
*/ |
282
|
|
|
protected function printColumns() |
283
|
|
|
{ |
284
|
|
|
return is_array($this->printColumns) ? $this->printColumns : $this->getColumnsFromBuilder(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Add basic array query scopes. |
289
|
|
|
* |
290
|
|
|
* @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
public function addScope(DataTableScopeContract $scope) |
294
|
|
|
{ |
295
|
|
|
$this->scopes[] = $scope; |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Get export filename. |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function getFilename() |
306
|
|
|
{ |
307
|
|
|
return $this->filename ?: $this->filename(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set export filename. |
312
|
|
|
* |
313
|
|
|
* @param string $filename |
314
|
|
|
* @return DataTable |
315
|
|
|
*/ |
316
|
|
|
public function setFilename($filename) |
317
|
|
|
{ |
318
|
|
|
$this->filename = $filename; |
319
|
|
|
|
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Apply query scopes. |
325
|
|
|
* |
326
|
|
|
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query |
327
|
|
|
* @return mixed |
328
|
|
|
*/ |
329
|
|
|
protected function applyScopes($query) |
330
|
|
|
{ |
331
|
|
|
foreach ($this->scopes as $scope) { |
332
|
|
|
$scope->apply($query); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $query; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get default builder parameters. |
340
|
|
|
* |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
|
|
protected function getBuilderParameters() |
344
|
|
|
{ |
345
|
|
|
return [ |
346
|
|
|
'order' => [[0, 'desc']], |
347
|
|
|
'buttons' => [ |
348
|
|
|
'create', |
349
|
|
|
[ |
350
|
|
|
'extend' => 'collection', |
351
|
|
|
'text' => '<i class="fa fa-download"></i> Export', |
352
|
|
|
'buttons' => [ |
353
|
|
|
'csv', |
354
|
|
|
'excel', |
355
|
|
|
'pdf', |
356
|
|
|
], |
357
|
|
|
], |
358
|
|
|
'print', |
359
|
|
|
'reset', |
360
|
|
|
'reload', |
361
|
|
|
], |
362
|
|
|
]; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|