1
|
|
|
<?php namespace Distilleries\DatatableBuilder; |
2
|
|
|
|
3
|
|
|
use \Datatable; |
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use \ReflectionClass; |
6
|
|
|
use \FormBuilder; |
7
|
|
|
use \Request; |
8
|
|
|
|
9
|
|
|
abstract class EloquentDatatable |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $model; |
13
|
|
|
protected $colomns; |
14
|
|
|
protected $form = null; |
15
|
|
|
protected $colomnsDisplay = []; |
16
|
|
|
protected $datatableOptions = []; |
17
|
|
|
|
18
|
|
|
// 0 can be an integer to represents the column's number or it can be a string that references the column's name |
19
|
|
|
protected $defaultOrder = [[0, 'desc']]; |
20
|
|
|
|
21
|
|
|
// ------------------------------------------------------------------------------------------------ |
22
|
8 |
|
|
23
|
|
|
public function __construct(Model $model = null) |
24
|
8 |
|
{ |
25
|
6 |
|
$this->model = $model; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Eloquent $model |
30
|
4 |
|
*/ |
31
|
|
|
public function setModel(Model $model) |
32
|
4 |
|
{ |
33
|
3 |
|
$this->model = $model; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
// ------------------------------------------------------------------------------------------------ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @param \Closure $closure |
42
|
8 |
|
*/ |
43
|
|
|
public function add($name, $closure = null, $translation = '') |
44
|
8 |
|
{ |
45
|
6 |
|
if (!empty($closure)) { |
46
|
8 |
|
$this->colomns[] = [ |
47
|
8 |
|
$name, |
48
|
2 |
|
$closure |
49
|
6 |
|
]; |
50
|
6 |
|
} else { |
51
|
|
|
$this->colomns[] = $name; |
52
|
8 |
|
} |
53
|
|
|
|
54
|
|
|
$this->addTranslation($name, $translation); |
55
|
8 |
|
|
56
|
|
|
return $this; |
57
|
8 |
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $translation |
62
|
|
|
* @param string $name |
63
|
|
|
*/ |
64
|
|
|
public function addTranslation($name, $translation) |
65
|
8 |
|
{ |
66
|
|
|
$this->colomnsDisplay[] = (!empty($translation)) ? $translation : ucfirst($name); |
67
|
8 |
|
} |
68
|
6 |
|
|
69
|
|
|
// ------------------------------------------------------------------------------------------------ |
70
|
|
|
|
71
|
|
|
public function applyFilters() |
72
|
4 |
|
{ |
73
|
|
|
$allInput = Request::all(); |
74
|
4 |
|
$columns = \Schema::getColumnListing($this->model->getTable()); |
75
|
4 |
|
|
76
|
|
|
foreach ($allInput as $name => $input) { |
77
|
4 |
|
if (in_array($name, $columns) && $input != '') { |
78
|
|
|
|
79
|
4 |
|
$this->model = $this->model->where($name, '=', $input); |
80
|
3 |
|
} |
81
|
|
|
} |
82
|
4 |
|
} |
83
|
3 |
|
|
84
|
3 |
|
// ------------------------------------------------------------------------------------------------ |
85
|
3 |
|
|
86
|
|
|
public function generateColomns() |
87
|
|
|
{ |
88
|
|
|
$this->applyFilters(); |
89
|
4 |
|
|
90
|
|
|
$datatable = Datatable::query($this->model); |
91
|
4 |
|
$colSearchAndSort = []; |
92
|
|
|
$sortOnly = []; |
93
|
4 |
|
|
94
|
4 |
|
if (!empty($this->colomns)) { |
95
|
4 |
|
foreach ($this->colomns as $key => $value) { |
96
|
|
|
|
97
|
4 |
|
if (is_string($value)) { |
98
|
3 |
|
$datatable->showColumns($value); |
99
|
4 |
|
$colSearchAndSort[] = $value; |
100
|
|
|
|
101
|
|
|
} else { |
102
|
4 |
|
if (is_array($value) && count($value) == 2) { |
103
|
3 |
|
$datatable->addColumn($value[0], $value[1]); |
104
|
4 |
|
$sortOnly[] = $value[0]; |
105
|
4 |
|
} |
106
|
|
|
} |
107
|
4 |
|
|
108
|
3 |
|
} |
109
|
4 |
|
} |
110
|
4 |
|
|
111
|
3 |
|
$datatable = $this->setClassRow($datatable); |
112
|
|
|
$datatable->orderColumns(array_merge($colSearchAndSort, $sortOnly)); |
113
|
3 |
|
$datatable->searchColumns($colSearchAndSort); |
114
|
3 |
|
|
115
|
|
|
return $datatable->make(); |
116
|
4 |
|
} |
117
|
4 |
|
|
118
|
4 |
|
public function setClassRow($datatable) |
119
|
|
|
{ |
120
|
4 |
|
//DT_RowClass |
121
|
|
|
$datatable->setRowClass(function ($row) { |
122
|
|
|
return (isset($row->status) && empty($row->status)) ? 'danger' : ''; |
123
|
4 |
|
}); |
124
|
|
|
|
125
|
|
|
return $datatable; |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
// ------------------------------------------------------------------------------------------------ |
129
|
4 |
|
public function generateHtmlRender($template = 'datatable-builder::part.datatable', $route = '') |
130
|
|
|
{ |
131
|
4 |
|
return view($template, [ |
132
|
|
|
'colomns_display' => $this->colomnsDisplay, |
133
|
|
|
'datatable_options' => $this->addOptions(), |
134
|
|
|
'id' => strtolower(str_replace('\\','_',get_class($this))), |
135
|
8 |
|
'route' => !empty($route) ? $route : $this->getControllerNameForAction() . '@getDatatable', |
136
|
|
|
'filters' => $this->addFilter(), |
137
|
8 |
|
]); |
138
|
8 |
|
} |
139
|
8 |
|
|
140
|
8 |
|
// ------------------------------------------------------------------------------------------------ |
141
|
8 |
|
public function addDefaultAction($template = 'datatable-builder::form.components.datatable.actions', $route = '') |
142
|
6 |
|
{ |
143
|
|
|
|
144
|
|
|
$reflection = new ReflectionClass(get_class($this)); |
145
|
|
|
|
146
|
8 |
|
$this->add('actions', function ($model) use ($template, $reflection, $route) { |
147
|
|
|
return view($template, [ |
|
|
|
|
148
|
|
|
'data' => $model->toArray(), |
149
|
8 |
|
'route' => !empty($route) ? $route . '@' : $this->getControllerNameForAction() . '@' |
150
|
|
|
])->render(); |
151
|
8 |
|
}); |
152
|
|
|
} |
153
|
4 |
|
|
154
|
4 |
|
// ------------------------------------------------------------------------------------------------ |
155
|
4 |
|
// ------------------------------------------------------------------------------------------------ |
156
|
4 |
|
// ------------------------------------------------------------------------------------------------ |
157
|
8 |
|
|
158
|
6 |
|
|
159
|
|
|
protected function addFilter($template = 'datatable-builder::form.components.datatable.filter') |
160
|
|
|
{ |
161
|
|
|
$this->form = FormBuilder::plain(); |
162
|
|
|
$this->filters(); |
163
|
|
|
|
164
|
|
|
$filter_content = view($template, [ |
|
|
|
|
165
|
8 |
|
'form' => $this->form |
166
|
|
|
])->render(); |
167
|
8 |
|
|
168
|
8 |
|
|
169
|
|
|
return $filter_content; |
170
|
8 |
|
|
171
|
8 |
|
} |
172
|
8 |
|
|
173
|
|
|
// ------------------------------------------------------------------------------------------------ |
174
|
|
|
// ------------------------------------------------------------------------------------------------ |
175
|
8 |
|
// ------------------------------------------------------------------------------------------------ |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
protected function addOptions() |
179
|
|
|
{ |
180
|
|
|
if (!array_key_exists('order', $this->datatableOptions) && !empty($this->defaultOrder)) { |
181
|
|
|
if (is_array($this->defaultOrder)) { |
182
|
|
|
foreach ($this->defaultOrder as $keyOrder => $order) { |
183
|
|
|
if (is_string($order[0])) { |
184
|
8 |
|
foreach ($this->colomns as $key => $colomn) { |
185
|
|
|
if (is_array($colomn)) { |
186
|
8 |
|
$colomn = $colomn[0]; |
187
|
8 |
|
} |
188
|
8 |
|
if ($colomn == $order[0]) { |
189
|
8 |
|
$this->defaultOrder[$keyOrder][0] = $key; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
if (is_string($this->defaultOrder[$keyOrder][0])) { |
193
|
|
|
$this->defaultOrder[$keyOrder][0] = 0; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
$this->datatableOptions['order'] = $this->defaultOrder; |
198
|
|
|
} |
199
|
2 |
|
} |
200
|
|
|
|
201
|
|
|
return $this->datatableOptions; |
202
|
6 |
|
} |
203
|
8 |
|
|
204
|
6 |
|
// ------------------------------------------------------------------------------------------------ |
205
|
6 |
|
|
206
|
8 |
|
protected function getControllerNameForAction() |
207
|
|
|
{ |
208
|
|
|
|
209
|
|
|
$action = explode('@', \Route::currentRouteAction()); |
210
|
|
|
|
211
|
8 |
|
return '\\' . $action[0]; |
212
|
|
|
} |
213
|
8 |
|
|
214
|
8 |
|
// ------------------------------------------------------------------------------------------------ |
215
|
|
|
|
216
|
|
|
public function filters() |
217
|
|
|
{ |
218
|
|
|
|
219
|
3 |
|
//Add the fileds of the filter form here |
220
|
3 |
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// ------------------------------------------------------------------------------------------------ |
224
|
3 |
|
// ------------------------------------------------------------------------------------------------ |
225
|
|
|
// ------------------------------------------------------------------------------------------------ |
226
|
|
|
|
227
|
|
|
abstract public function build(); |
228
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: