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
|
|
|
* The datatable dom parameter. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $dom = "<'row'<'col-sm-8'B><'col-sm-4'f>> <'row'r><'row'<'col-sm-12't>> <'row'<'col-sm-5'i><'col-sm-7'p>>"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The datatable select parameter. |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $select = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The datatable keys parameter. |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $keys = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The datatable mark parameter. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $mark = true; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The datatable order parameter. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $order = [[0, 'asc']]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The datatable retrieve parameter. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $retrieve = true; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The datatable autoWidth parameter. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $autoWidth = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The datatable fixedHeader parameter. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $fixedHeader = true; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The datatable create parameter. |
83
|
|
|
* |
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
protected $createButton = true; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* The datatable builder parameters. |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
protected $builderParameters = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get columns. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
abstract protected function getColumns(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the query object to be processed by dataTables. |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
public function query() |
108
|
|
|
{ |
109
|
|
|
$query = app($this->model)->query(); |
110
|
|
|
|
111
|
|
|
return $this->applyScopes($query); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Display ajax response. |
116
|
|
|
* |
117
|
|
|
* @return \Illuminate\Http\JsonResponse |
118
|
|
|
*/ |
119
|
|
|
public function ajax() |
120
|
|
|
{ |
121
|
|
|
return datatables($this->query()) |
|
|
|
|
122
|
|
|
->setTransformer(app($this->transformer)) |
123
|
|
|
->orderColumn('name', 'name->"$.'.app()->getLocale().'" $1') |
124
|
|
|
->make(true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Optional method if you want to use html builder. |
129
|
|
|
* |
130
|
|
|
* @return \Yajra\DataTables\Html\Builder |
131
|
|
|
*/ |
132
|
|
|
public function html() |
133
|
|
|
{ |
134
|
|
|
return $this->builder() |
135
|
|
|
->minifiedAjax() |
136
|
|
|
->columns($this->getColumns()) |
137
|
|
|
->parameters($this->getBuilderParameters()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Process DataTables needed render output. |
142
|
|
|
* |
143
|
|
|
* @param string $view |
144
|
|
|
* @param array $data |
145
|
|
|
* @param array $mergeData |
146
|
|
|
* |
147
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
148
|
|
|
*/ |
149
|
|
|
public function render($view, $data = [], $mergeData = []) |
150
|
|
|
{ |
151
|
|
|
if ($this->request()->ajax() && $this->request()->wantsJson()) { |
152
|
|
|
return app()->call([$this, 'ajax']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (($action = $this->request()->get('action')) && in_array($action, $this->actions)) { |
156
|
|
|
if ($action === 'print') { |
157
|
|
|
return app()->call([$this, 'printPreview']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return app()->call([$this, $action]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return view($view, array_merge($this->attributes, $data), $mergeData)->with($this->dataTableVariable, $this->getHtmlBuilder()); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get default builder parameters. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
protected function getBuilderParameters(): array |
172
|
|
|
{ |
173
|
|
|
$createButton = ['extend' => 'create', 'text' => '<i class="fa fa-plus"></i> '.trans('cortex/foundation::common.new')]; |
174
|
|
|
$columnsButton = ['extend' => 'colvis', 'text' => '<i class="fa fa-columns"></i> '.trans('cortex/foundation::common.columns').' <span class="caret"/>']; |
175
|
|
|
$lengthButton = ['extend' => 'pageLength', 'text' => '<i class="fa fa-list-ol"></i> '.trans('cortex/foundation::common.limit').' <span class="caret"/>']; |
176
|
|
|
|
177
|
|
|
return array_merge([ |
178
|
|
|
'dom' => $this->dom, |
179
|
|
|
'keys' => $this->keys, |
180
|
|
|
'mark' => $this->mark, |
181
|
|
|
'order' => $this->order, |
182
|
|
|
'select' => $this->select, |
183
|
|
|
'retrieve' => $this->retrieve, |
184
|
|
|
'autoWidth' => $this->autoWidth, |
185
|
|
|
'fixedHeader' => $this->fixedHeader, |
186
|
|
|
'buttons' => $this->createButton |
187
|
|
|
? [$createButton, 'print', 'reset', 'reload', 'import', 'export', $columnsButton, $lengthButton] |
188
|
|
|
: ['print', 'reset', 'reload', 'export', $columnsButton, $lengthButton], |
189
|
|
|
], $this->builderParameters); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get filename for export. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
protected function filename(): string |
198
|
|
|
{ |
199
|
|
|
$model = $this->model ?? trim(str_replace('DataTable', '', mb_strrchr(static::class, '\\')), " \t\n\r\0\x0B\\"); |
200
|
|
|
|
201
|
|
|
$resource = str_plural(mb_strtolower(array_last(explode(class_exists($model) ? '\\' : '.', $model)))); |
|
|
|
|
202
|
|
|
|
203
|
|
|
return $resource.'-export-'.date('Y-m-d').'-'.time(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
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.