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