1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use Request; |
7
|
|
|
use Route; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
11
|
|
|
use SleepingOwl\Admin\Display\Column\Email; |
12
|
|
|
use SleepingOwl\Admin\Display\Column\Link; |
13
|
|
|
use SleepingOwl\Admin\Display\Column\Text; |
14
|
|
|
use SleepingOwl\Admin\Display\Column\Custom; |
15
|
|
|
use SleepingOwl\Admin\Display\Column\NamedColumn; |
16
|
|
|
use SleepingOwl\Admin\Display\Column\Control; |
17
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
18
|
|
|
|
19
|
|
|
class DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Register display routes. |
23
|
|
|
* |
24
|
|
|
* @param Router $router |
25
|
|
|
*/ |
26
|
|
|
public static function registerRoutes(Router $router) |
27
|
|
|
{ |
28
|
|
|
$router->get('{adminModel}/async/{adminDisplayName?}', ['as' => 'admin.model.async', |
29
|
|
|
function (ModelConfigurationInterface $model, $name = null) { |
30
|
|
|
$display = $model->fireDisplay(); |
31
|
|
|
if ($display instanceof DisplayTabbed) { |
32
|
|
|
$display = static::findDatatablesAsyncByName($display, $name); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($display instanceof DisplayDatatablesAsync) { |
36
|
|
|
return $display->renderAsync(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
abort(404); |
40
|
|
|
}, |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Find DisplayDatatablesAsync in tabbed display by name. |
46
|
|
|
* |
47
|
|
|
* @param DisplayTabbed $display |
48
|
|
|
* @param string|null $name |
49
|
|
|
* |
50
|
|
|
* @return DisplayDatatablesAsync|null |
51
|
|
|
*/ |
52
|
|
|
protected static function findDatatablesAsyncByName(DisplayTabbed $display, $name) |
53
|
|
|
{ |
54
|
|
|
$tabs = $display->getTabs(); |
55
|
|
|
foreach ($tabs as $tab) { |
56
|
|
|
$content = $tab->getContent(); |
57
|
|
|
if ($content instanceof self && $content->getName() === $name) { |
58
|
|
|
return $content; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $name; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string|null $name |
70
|
|
|
*/ |
71
|
|
|
protected $distinct; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $searchableColumns = [ |
77
|
|
|
Text::class, |
78
|
|
|
Link::class, |
79
|
|
|
Email::class, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* DisplayDatatablesAsync constructor. |
84
|
|
|
* |
85
|
|
|
* @param string|null $name |
86
|
|
|
* @param string|null $distinct |
87
|
|
|
*/ |
88
|
|
|
public function __construct($name = null, $distinct = null) |
89
|
|
|
{ |
90
|
|
|
parent::__construct(); |
91
|
|
|
|
92
|
|
|
$this->setName($name); |
93
|
|
|
$this->setDistinct($distinct); |
94
|
|
|
|
95
|
|
|
$this->getColumns()->setView('display.extensions.columns_async'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Initialize display. |
100
|
|
|
*/ |
101
|
|
|
public function initialize() |
102
|
|
|
{ |
103
|
|
|
parent::initialize(); |
104
|
|
|
|
105
|
|
|
$attributes = Request::all(); |
106
|
|
|
array_unshift($attributes, $this->getName()); |
107
|
|
|
array_unshift($attributes, $this->getModelConfiguration()->getAlias()); |
108
|
|
|
|
109
|
|
|
$this->setHtmlAttribute('data-url', route('admin.model.async', $attributes)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getName() |
116
|
|
|
{ |
117
|
|
|
return $this->name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setName($name) |
126
|
|
|
{ |
127
|
|
|
$this->name = $name; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function getDistinct() |
136
|
|
|
{ |
137
|
|
|
return $this->distinct; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param mixed $distinct |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setDistinct($distinct) |
146
|
|
|
{ |
147
|
|
|
$this->distinct = $distinct; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Render async request. |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function renderAsync() |
157
|
|
|
{ |
158
|
|
|
$query = $this->getRepository()->getQuery(); |
159
|
|
|
$totalCount = $query->count(); |
160
|
|
|
$filteredCount = 0; |
161
|
|
|
|
162
|
|
|
if (! is_null($this->distinct)) { |
163
|
|
|
$filteredCount = $query->distinct()->count($this->getDistinct()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->modifyQuery($query); |
167
|
|
|
$this->applySearch($query); |
168
|
|
|
$this->applyColumnSearch($query); |
169
|
|
|
|
170
|
|
|
if (is_null($this->distinct)) { |
171
|
|
|
$filteredCount = $query->count(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->applyOrders($query); |
175
|
|
|
$this->applyOffset($query); |
176
|
|
|
$collection = $query->get(); |
177
|
|
|
|
178
|
|
|
return $this->prepareDatatablesStructure($collection, $totalCount, $filteredCount); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Apply offset and limit to the query. |
183
|
|
|
* |
184
|
|
|
* @param $query |
185
|
|
|
*/ |
186
|
|
|
protected function applyOffset($query) |
187
|
|
|
{ |
188
|
|
|
$offset = Request::input('start', 0); |
189
|
|
|
$limit = Request::input('length', 10); |
190
|
|
|
|
191
|
|
|
if ($limit == -1) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$query->offset($offset)->limit($limit); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Apply orders to the query. |
200
|
|
|
* |
201
|
|
|
* @param $query |
202
|
|
|
*/ |
203
|
|
|
protected function applyOrders($query) |
204
|
|
|
{ |
205
|
|
|
$orders = Request::input('order', []); |
206
|
|
|
|
207
|
|
|
foreach ($orders as $order) { |
|
|
|
|
208
|
|
|
$columnIndex = $order['column']; |
209
|
|
|
$orderDirection = $order['dir']; |
210
|
|
|
$column = $this->getColumns()->all()->get($columnIndex); |
211
|
|
|
|
212
|
|
|
if ($column instanceof NamedColumn && $column->isOrderable()) { |
213
|
|
|
$name = $column->getName(); |
214
|
|
|
$query->orderBy($name, $orderDirection); |
215
|
|
|
} elseif ($column instanceof Custom && $column->getOrderField()) { |
216
|
|
|
$name = $column->getOrderField(); |
217
|
|
|
$query->orderBy($name, $orderDirection); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Apply search to the query. |
224
|
|
|
* |
225
|
|
|
* @param Builder $query |
226
|
|
|
*/ |
227
|
|
|
protected function applySearch(Builder $query) |
228
|
|
|
{ |
229
|
|
|
$search = Request::input('search.value'); |
230
|
|
|
if (empty($search)) { |
231
|
|
|
return; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$query->where(function ($query) use ($search) { |
235
|
|
|
$columns = $this->getColumns()->all(); |
236
|
|
|
foreach ($columns as $column) { |
237
|
|
|
if (in_array(get_class($column), $this->searchableColumns)) { |
238
|
|
|
$name = $column->getName(); |
239
|
|
|
if ($this->repository->hasColumn($name)) { |
240
|
|
|
$query->orWhere($name, 'like', '%'.$search.'%'); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
}); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param Builder $query |
249
|
|
|
*/ |
250
|
|
|
protected function applyColumnSearch(Builder $query) |
251
|
|
|
{ |
252
|
|
|
$queryColumns = Request::input('columns', []); |
253
|
|
|
|
254
|
|
|
foreach ($queryColumns as $index => $queryColumn) { |
|
|
|
|
255
|
|
|
$search = array_get($queryColumn, 'search.value'); |
256
|
|
|
$fullSearch = array_get($queryColumn, 'search'); |
257
|
|
|
$column = $this->getColumns()->all()->get($index); |
258
|
|
|
$columnFilter = array_get($this->getColumnFilters()->all(), $index); |
259
|
|
|
|
260
|
|
|
if (! is_null($columnFilter) && ! is_null($column)) { |
261
|
|
|
$columnFilter->apply($this->repository, $column, $query, $search, $fullSearch); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Convert collection to the datatables structure. |
268
|
|
|
* |
269
|
|
|
* @param array|Collection $collection |
270
|
|
|
* @param int $totalCount |
271
|
|
|
* @param int $filteredCount |
272
|
|
|
* |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
protected function prepareDatatablesStructure(Collection $collection, $totalCount, $filteredCount) |
276
|
|
|
{ |
277
|
|
|
$columns = $this->getColumns(); |
278
|
|
|
|
279
|
|
|
$result = []; |
280
|
|
|
$result['draw'] = Request::input('draw', 0); |
281
|
|
|
$result['recordsTotal'] = $totalCount; |
282
|
|
|
$result['recordsFiltered'] = $filteredCount; |
283
|
|
|
$result['data'] = []; |
284
|
|
|
|
285
|
|
|
foreach ($collection as $instance) { |
286
|
|
|
$_row = []; |
287
|
|
|
|
288
|
|
|
foreach ($columns->all() as $column) { |
289
|
|
|
$column->setModel($instance); |
290
|
|
|
|
291
|
|
|
if ($column instanceof Control) { |
292
|
|
|
$column->initialize(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$_row[] = (string) $column; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$result['data'][] = $_row; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $result; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.