1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\DataTables; |
6
|
|
|
|
7
|
|
|
use Yajra\DataTables\Services\DataTable; |
8
|
|
|
|
9
|
|
|
abstract class AbstractDataTable extends DataTable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The model class. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The transformer class. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $transformer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get columns. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
abstract protected function getColumns(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the query object to be processed by dataTables. |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function query() |
38
|
|
|
{ |
39
|
|
|
$query = app($this->model)->query(); |
40
|
|
|
|
41
|
|
|
return $this->applyScopes($query); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display ajax response. |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Http\JsonResponse |
48
|
|
|
*/ |
49
|
|
|
public function ajax() |
50
|
|
|
{ |
51
|
|
|
$transformer = app($this->transformer); |
52
|
|
|
|
53
|
|
|
return datatables()->eloquent($this->query()) |
|
|
|
|
54
|
|
|
->setTransformer($transformer) |
55
|
|
|
->make(true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Optional method if you want to use html builder. |
60
|
|
|
* |
61
|
|
|
* @return \Yajra\DataTables\Html\Builder |
62
|
|
|
*/ |
63
|
|
|
public function html() |
64
|
|
|
{ |
65
|
|
|
return $this->builder() |
66
|
|
|
->minifiedAjax() |
67
|
|
|
->columns($this->getColumns()) |
68
|
|
|
->parameters($this->getBuilderParameters()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Process DataTables needed render output. |
73
|
|
|
* |
74
|
|
|
* @param string $view |
75
|
|
|
* @param array $data |
76
|
|
|
* @param array $mergeData |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
79
|
|
|
*/ |
80
|
|
|
public function render($view, $data = [], $mergeData = []) |
81
|
|
|
{ |
82
|
|
|
if ($this->request()->ajax() && $this->request()->wantsJson()) { |
83
|
|
|
return app()->call([$this, 'ajax']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (($action = $this->request()->get('action')) && in_array($action, $this->actions)) { |
87
|
|
|
if ($action === 'print') { |
88
|
|
|
return app()->call([$this, 'printPreview']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return app()->call([$this, $action]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return view($view, array_merge($this->attributes, $data), $mergeData)->with($this->dataTableVariable, $this->getHtmlBuilder()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get default builder parameters. |
99
|
|
|
* |
100
|
|
|
* @return array |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
protected function getBuilderParameters() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'keys' => true, |
106
|
|
|
'autoWidth' => false, |
107
|
|
|
'dom' => "<'row'<'col-sm-6'B><'col-sm-6'f>> <'row'r><'row'<'col-sm-12't>> <'row'<'col-sm-5'i><'col-sm-7'p>>", |
|
|
|
|
108
|
|
|
'buttons' => [ |
109
|
|
|
['extend' => 'create', 'text' => '<i class="fa fa-plus"></i> '.trans('cortex/foundation::common.new')], 'print', 'reset', 'reload', 'export', |
|
|
|
|
110
|
|
|
['extend' => 'colvis', 'text' => '<i class="fa fa-columns"></i> '.trans('cortex/foundation::common.columns').' <span class="caret"/>'], |
|
|
|
|
111
|
|
|
['extend' => 'pageLength', 'text' => '<i class="fa fa-list-ol"></i> '.trans('cortex/foundation::common.limit').' <span class="caret"/>'], |
|
|
|
|
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get filename for export. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function filename() |
122
|
|
|
{ |
123
|
|
|
$resource = str_plural(mb_strtolower(array_last(str_replace('Contract', '', explode(class_exists($this->model) ? '\\' : '.', $this->model))))); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $resource.'-export-'.date('Y-m-d').'-'.time(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.