-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
DataTable.php
436 lines (381 loc) · 9.78 KB
/
DataTable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
namespace Yajra\Datatables\Services;
use Illuminate\Contracts\View\Factory;
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
use Maatwebsite\Excel\Writers\LaravelExcelWriter;
use Yajra\Datatables\Contracts\DataTableButtonsContract;
use Yajra\Datatables\Contracts\DataTableContract;
use Yajra\Datatables\Contracts\DataTableScopeContract;
use Yajra\Datatables\Datatables;
use Yajra\Datatables\Transformers\DataTransformer;
/**
* Class DataTable.
*
* @package Yajra\Datatables\Services
* @author Arjay Angeles <aqangeles@gmail.com>
*/
abstract class DataTable implements DataTableContract, DataTableButtonsContract
{
/**
* @var \Yajra\Datatables\Datatables
*/
protected $datatables;
/**
* @var \Illuminate\Contracts\View\Factory
*/
protected $viewFactory;
/**
* Datatables print preview view.
*
* @var string
*/
protected $printPreview = 'datatables::print';
/**
* List of columns to be exported.
*
* @var string|array
*/
protected $exportColumns = '*';
/**
* List of columns to be printed.
*
* @var string|array
*/
protected $printColumns = '*';
/**
* Query scopes.
*
* @var \Yajra\Datatables\Contracts\DataTableScopeContract[]
*/
protected $scopes = [];
/**
* Html builder.
*
* @var \Yajra\Datatables\Html\Builder
*/
protected $htmlBuilder;
/**
* Export filename.
*
* @var string
*/
protected $filename = '';
/**
* Custom attributes set on the class.
*
* @var array
*/
protected $attributes = [];
/**
* DataTable constructor.
*
* @param \Yajra\Datatables\Datatables $datatables
* @param \Illuminate\Contracts\View\Factory $viewFactory
*/
public function __construct(Datatables $datatables, Factory $viewFactory)
{
$this->datatables = $datatables;
$this->viewFactory = $viewFactory;
}
/**
* Process dataTables needed render output.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function render($view, $data = [], $mergeData = [])
{
if ($this->request()->ajax() && $this->request()->wantsJson()) {
return $this->ajax();
}
if ($action = $this->request()->get('action') AND in_array($action, ['print', 'csv', 'excel', 'pdf'])) {
if ($action == 'print') {
return $this->printPreview();
}
return call_user_func_array([$this, $action], []);
}
return $this->viewFactory->make($view, $data, $mergeData)->with('dataTable', $this->html());
}
/**
* Get Datatables Request instance.
*
* @return \Yajra\Datatables\Request
*/
public function request()
{
return $this->datatables->getRequest();
}
/**
* Display printable view of datatables.
*
* @return \Illuminate\Contracts\View\View
*/
public function printPreview()
{
$data = $this->getDataForPrint();
return $this->viewFactory->make($this->printPreview, compact('data'));
}
/**
* Get mapped columns versus final decorated output.
*
* @return array
*/
protected function getDataForPrint()
{
$columns = $this->printColumns();
return $this->mapResponseToColumns($columns, 'printable');
}
/**
* Get printable columns.
*
* @return array|string
*/
protected function printColumns()
{
return is_array($this->printColumns) ? $this->printColumns : $this->getColumnsFromBuilder();
}
/**
* Get columns definition from html builder.
*
* @return array
*/
protected function getColumnsFromBuilder()
{
return $this->html()->getColumns();
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder();
}
/**
* Get Datatables Html Builder instance.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function builder()
{
return $this->htmlBuilder ?: $this->htmlBuilder = app('datatables.html');
}
/**
* Map ajax response to columns definition.
*
* @param mixed $columns
* @param string $type
* @return array
*/
protected function mapResponseToColumns($columns, $type)
{
return array_map(function ($row) use ($columns, $type) {
if ($columns) {
return (new DataTransformer())->transform($row, $columns, $type);
}
return $row;
}, $this->getAjaxResponseData());
}
/**
* Get decorated data as defined in datatables ajax response.
*
* @return array
*/
protected function getAjaxResponseData()
{
$this->datatables->getRequest()->merge(['length' => -1]);
$response = $this->ajax();
$data = $response->getData(true);
return $data['data'];
}
/**
* Export results to Excel file.
*
* @return void
*/
public function excel()
{
$this->buildExcelFile()->download('xls');
}
/**
* Build excel file and prepare for export.
*
* @return \Maatwebsite\Excel\Writers\LaravelExcelWriter
*/
protected function buildExcelFile()
{
/** @var \Maatwebsite\Excel\Excel $excel */
$excel = app('excel');
return $excel->create($this->getFilename(), function (LaravelExcelWriter $excel) {
$excel->sheet('exported-data', function (LaravelExcelWorksheet $sheet) {
$sheet->fromArray($this->getDataForExport());
});
});
}
/**
* Get export filename.
*
* @return string
*/
public function getFilename()
{
return $this->filename ?: $this->filename();
}
/**
* Set export filename.
*
* @param string $filename
* @return DataTable
*/
public function setFilename($filename)
{
$this->filename = $filename;
return $this;
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'export_' . time();
}
/**
* Get mapped columns versus final decorated output.
*
* @return array
*/
protected function getDataForExport()
{
$columns = $this->exportColumns();
return $this->mapResponseToColumns($columns, 'exportable');
}
/**
* Get export columns definition.
*
* @return array|string
*/
private function exportColumns()
{
return is_array($this->exportColumns) ? $this->exportColumns : $this->getColumnsFromBuilder();
}
/**
* Export results to CSV file.
*
* @return void
*/
public function csv()
{
$this->buildExcelFile()->download('csv');
}
/**
* Export results to PDF file.
*
* @return mixed
*/
public function pdf()
{
if ('snappy' == config('datatables-buttons.pdf_generator', 'excel')) {
return $this->snappyPdf();
} else {
$this->buildExcelFile()->download('pdf');
}
}
/**
* PDF version of the table using print preview blade template.
*
* @return mixed
*/
public function snappyPdf()
{
/** @var \Barryvdh\Snappy\PdfWrapper $snappy */
$snappy = app('snappy.pdf.wrapper');
$options = config('datatables-buttons.snappy.options', [
'no-outline' => true,
'margin-left' => '0',
'margin-right' => '0',
'margin-top' => '10mm',
'margin-bottom' => '10mm',
]);
$orientation = config('datatables-buttons.snappy.orientation', 'landscape');
$snappy->setOptions($options)
->setOrientation($orientation);
return $snappy->loadHTML($this->printPreview())
->download($this->getFilename() . ".pdf");
}
/**
* Add basic array query scopes.
*
* @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope
* @return $this
*/
public function addScope(DataTableScopeContract $scope)
{
$this->scopes[] = $scope;
return $this;
}
/**
* Set a custom class attribute.
*
* @param mixed $key
* @param mixed|null $value
* @return $this
*/
public function with($key, $value = null)
{
if (is_array($key)) {
$this->attributes = array_merge($this->attributes, $key);
} else {
$this->attributes[$key] = $value;
}
return $this;
}
/**
* Dynamically retrieve the value of an attribute.
*
* @param string $key
* @return mixed|null
*/
public function __get($key)
{
if (array_key_exists($key, $this->attributes)) {
return $this->attributes[$key];
}
return null;
}
/**
* Apply query scopes.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
* @return mixed
*/
protected function applyScopes($query)
{
foreach ($this->scopes as $scope) {
$scope->apply($query);
}
return $query;
}
/**
* Get default builder parameters.
*
* @return array
*/
protected function getBuilderParameters()
{
return [
'order' => [[0, 'desc']],
'buttons' => [
'create',
'export',
'print',
'reset',
'reload',
],
];
}
}