forked from SteJaySulli/data-table-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTableController.php
More file actions
181 lines (156 loc) · 5.53 KB
/
DataTableController.php
File metadata and controls
181 lines (156 loc) · 5.53 KB
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
<?php
namespace Team383\LaravelDataTable\Http\Controllers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Team383\LaravelDataTable\Http\Resources\DataTableResource;
abstract class DataTableController extends Controller
{
// In the simplest use case, set these up in your derived class
protected $hidden_fields = [];
protected $sortable_fields = [];
protected $searchable_fields = [];
protected $filterable_fields = [];
protected $field_labels = [];
protected $resource_class = null;
protected $page_options = [5, 10, 25, 50, 100];
// You must provide this, which should return the base eloquent query object for your datatable
abstract public function dataQuery(Request $request): Builder;
public function authorize(Request $request)
{
return true;
}
public function unauthorizedResponse(Request $request)
{
return response()->json(['message' => 'Unauthorized'], 403);
}
// You can override these functions in your derived class to provide custom search and sort functionality
public function getHiddenFields(): array
{
return $this->hidden_fields;
}
public function getSortableFields(): array
{
return $this->sortable_fields;
}
public function getSearchableFields(): array
{
return $this->searchable_fields;
}
public function getFilterableFields(array $fields = []): array
{
$labels = $this->filterable_fields;
foreach ($fields as $field) {
if (!in_array($field, $labels)) {
$labels[$field] = Str::title(
preg_replace('/\s{2,}/', ' ', preg_replace("/[^a-z0-9]/", " ", Str::snake($field)))
);
}
}
return $labels;
}
public function getFieldLabels(): array
{
return $this->field_labels;
}
public function getPageOptions(): array
{
return $this->page_options;
}
public function getResourceClass(): string
{
if (!is_null($this->resource_class)) {
return $this->resource_class;
}
return DataTableResource::class;
}
public function dataSearch($query, $search): void
{
$query->where(function ($query) use ($search) {
foreach ($this->getSearchableFields() as $field) {
$functionName = Str::camel('search_' . $field);
if (method_exists($this, $functionName)) {
$this->{$functionName}($query, $search);
} else {
$query->orWhere($field, 'like', '%' . $search . '%');
}
}
});
}
public function defaultFilter($query, $field, $value): void
{
if (is_array($value)) {
$query->whereIn($field, $value);
} else {
$query->where($field, $value);
}
}
public function dataFilter($query, $filters): void
{
$query->where(function ($query) use ($filters) {
$filterableFields = $this->getFilterableFields();
foreach ($filters as $field => $value) {
if (!in_array($field, $filterableFields)) {
continue;
}
$functionName = Str::camel('filter_' . $field);
if (method_exists($this, $functionName)) {
$this->{$functionName}($query, $value);
} else {
$this->defaultFilter($query, $field, $value);
}
}
});
}
public function dataSort($query, $sort, $direction = "ASC"): void
{
if (in_array($sort, $this->getSortableFields())) {
$functionName = Str::camel('sort_by_' . $sort);
if (method_exists($this, $functionName)) {
$this->{$functionName}($query, $direction);
} else {
$query->orderBy($sort, $direction);
}
}
}
public function __invoke(Request $request)
{
if (!$this->authorize($request)) {
return $this->unauthorizedResponse($request);
}
$per_page = $request->input('per_page', 5);
$data = $this->dataQuery($request);
if ($search = $request->input('search')) {
$this->dataSearch($data, $search);
}
if ($filters = $request->input('filters')) {
$this->dataFilter($data, $filters);
}
$sort = $request->input('sort', count($this->getSortableFields()) > 0 ? $this->getSortableFields()[0] : null);
if (strtoupper($request->input('direction', "ASC")) == "DESC") {
$direction = "DESC";
} else {
$direction = "ASC";
}
if ($sort) {
$this->dataSort($data, $sort, $direction);
}
return $this->getResourceClass()::collection(
$data
->paginate($per_page)
)
->additional([
"meta" => [
"hidden_fields" => $this->getHiddenFields(),
"field_labels" => $this->getFieldLabels(),
"sortable_fields" => $this->getSortableFields(),
"searchable_fields" => $this->getSearchableFields(),
"filterable_fields" => $this->getFilterableFields(),
"sort_by" => $sort,
"direction" => strtolower($direction),
"page_options" => $this->getPageOptions(),
]
]);
}
}