1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Request; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use SleepingOwl\Admin\Display\Column\Link; |
10
|
|
|
use SleepingOwl\Admin\Display\Column\Text; |
11
|
|
|
use SleepingOwl\Admin\Display\Column\Email; |
12
|
|
|
use Illuminate\Http\Request as InlineRequest; |
13
|
|
|
use SleepingOwl\Admin\Display\Column\Control; |
14
|
|
|
use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
15
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
16
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
17
|
|
|
|
18
|
|
|
class DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Register display routes. |
22
|
|
|
* |
23
|
|
|
* @param Router $router |
24
|
|
|
*/ |
25
|
|
|
public static function registerRoutes(Router $router) |
26
|
|
|
{ |
27
|
|
|
$router->get('{adminModel}/async/{adminDisplayName?}', [ |
28
|
|
|
'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
|
|
|
$router->post('{adminModel}/async/{adminDisplayName?}', [ |
43
|
|
|
'as' => 'admin.model.async.inline', |
44
|
|
|
function (ModelConfigurationInterface $model, InlineRequest $request) { |
45
|
|
|
$field = $request->input('name'); |
46
|
|
|
$value = $request->input('value'); |
47
|
|
|
$id = $request->input('pk'); |
48
|
|
|
|
49
|
|
|
$display = $model->fireDisplay(); |
50
|
|
|
|
51
|
|
|
/** @var ColumnEditableInterface|null $column */ |
52
|
|
|
$column = $display->getColumns()->all()->filter(function ($column) use ($field) { |
53
|
|
|
return ($column instanceof ColumnEditableInterface) and $field == $column->getName(); |
|
|
|
|
54
|
|
|
})->first(); |
55
|
|
|
|
56
|
|
|
if (is_null($column)) { |
57
|
|
|
abort(404); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$model->saveColumn($column, $value, $id); |
|
|
|
|
61
|
|
|
|
62
|
|
|
if ($display instanceof DisplayDatatablesAsync) { |
63
|
|
|
return $display->renderAsync(); |
64
|
|
|
} |
65
|
|
|
}, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Find DisplayDatatablesAsync in tabbed display by name. |
71
|
|
|
* |
72
|
|
|
* @param DisplayTabbed $display |
73
|
|
|
* @param string|null $name |
74
|
|
|
* |
75
|
|
|
* @return DisplayDatatablesAsync|null |
76
|
|
|
*/ |
77
|
|
|
protected static function findDatatablesAsyncByName(DisplayTabbed $display, $name) |
78
|
|
|
{ |
79
|
|
|
$tabs = $display->getTabs(); |
80
|
|
|
foreach ($tabs as $tab) { |
81
|
|
|
$content = $tab->getContent(); |
82
|
|
|
if ($content instanceof self && $content->getName() === $name) { |
83
|
|
|
return $content; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $name; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string|null $name |
95
|
|
|
*/ |
96
|
|
|
protected $distinct; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
protected $searchableColumns = [ |
102
|
|
|
Text::class, |
103
|
|
|
Link::class, |
104
|
|
|
Email::class, |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* DisplayDatatablesAsync constructor. |
109
|
|
|
* |
110
|
|
|
* @param string|null $name |
111
|
|
|
* @param string|null $distinct |
112
|
|
|
*/ |
113
|
|
|
public function __construct($name = null, $distinct = null) |
114
|
|
|
{ |
115
|
|
|
parent::__construct(); |
116
|
|
|
|
117
|
|
|
$this->setName($name); |
118
|
|
|
$this->setDistinct($distinct); |
119
|
|
|
|
120
|
|
|
$this->getColumns()->setView('display.extensions.columns_async'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Initialize display. |
125
|
|
|
*/ |
126
|
|
|
public function initialize() |
127
|
|
|
{ |
128
|
|
|
parent::initialize(); |
129
|
|
|
|
130
|
|
|
$attributes = Request::all(); |
131
|
|
|
array_unshift($attributes, $this->getName()); |
132
|
|
|
array_unshift($attributes, $this->getModelConfiguration()->getAlias()); |
133
|
|
|
|
134
|
|
|
$this->setHtmlAttribute('data-url', route('admin.model.async', $attributes)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getName() |
141
|
|
|
{ |
142
|
|
|
return $this->name; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $name |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setName($name) |
151
|
|
|
{ |
152
|
|
|
$this->name = $name; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
public function getDistinct() |
161
|
|
|
{ |
162
|
|
|
return $this->distinct; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param mixed $distinct |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function setDistinct($distinct) |
171
|
|
|
{ |
172
|
|
|
$this->distinct = $distinct; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Render async request. |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function renderAsync() |
182
|
|
|
{ |
183
|
|
|
$query = $this->getRepository()->getQuery(); |
184
|
|
|
$totalCount = $query->count(); |
185
|
|
|
$filteredCount = 0; |
186
|
|
|
|
187
|
|
|
if (! is_null($this->distinct)) { |
188
|
|
|
$filteredCount = $query->distinct()->count($this->getDistinct()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->modifyQuery($query); |
192
|
|
|
$this->applySearch($query); |
193
|
|
|
$this->applyColumnSearch($query); |
194
|
|
|
|
195
|
|
|
if (is_null($this->distinct)) { |
196
|
|
|
$filteredCount = $query->count(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->applyOrders($query); |
200
|
|
|
$this->applyOffset($query); |
201
|
|
|
$collection = $query->get(); |
202
|
|
|
|
203
|
|
|
return $this->prepareDatatablesStructure($collection, $totalCount, $filteredCount); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Apply offset and limit to the query. |
208
|
|
|
* |
209
|
|
|
* @param $query |
210
|
|
|
*/ |
211
|
|
|
protected function applyOffset($query) |
212
|
|
|
{ |
213
|
|
|
$offset = Request::input('start', 0); |
214
|
|
|
$limit = Request::input('length', 10); |
215
|
|
|
|
216
|
|
|
if ($limit == -1) { |
217
|
|
|
return; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$query->offset($offset)->limit($limit); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Apply search to the query. |
225
|
|
|
* |
226
|
|
|
* @param Builder $query |
227
|
|
|
*/ |
228
|
|
|
protected function applySearch(Builder $query) |
229
|
|
|
{ |
230
|
|
|
$search = Request::input('search.value'); |
231
|
|
|
if (empty($search)) { |
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$query->where(function ($query) use ($search) { |
236
|
|
|
$columns = $this->getColumns()->all(); |
237
|
|
|
foreach ($columns as $column) { |
238
|
|
|
if (in_array(get_class($column), $this->searchableColumns)) { |
239
|
|
|
$name = $column->getName(); |
240
|
|
|
if ($this->repository->hasColumn($name)) { |
241
|
|
|
$query->orWhere($name, 'like', '%'.$search.'%'); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
}); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param Builder $query |
250
|
|
|
*/ |
251
|
|
|
protected function applyColumnSearch(Builder $query) |
252
|
|
|
{ |
253
|
|
|
$queryColumns = Request::input('columns', []); |
254
|
|
|
|
255
|
|
|
foreach ($queryColumns as $index => $queryColumn) { |
|
|
|
|
256
|
|
|
$search = array_get($queryColumn, 'search.value'); |
257
|
|
|
$fullSearch = array_get($queryColumn, 'search'); |
258
|
|
|
$column = $this->getColumns()->all()->get($index); |
259
|
|
|
$columnFilter = array_get($this->getColumnFilters()->all(), $index); |
260
|
|
|
|
261
|
|
|
if (! is_null($columnFilter) && ! is_null($column)) { |
262
|
|
|
$columnFilter->apply($this->repository, $column, $query, $search, $fullSearch); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Convert collection to the datatables structure. |
269
|
|
|
* |
270
|
|
|
* @param array|Collection $collection |
271
|
|
|
* @param int $totalCount |
272
|
|
|
* @param int $filteredCount |
273
|
|
|
* |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
protected function prepareDatatablesStructure(Collection $collection, $totalCount, $filteredCount) |
277
|
|
|
{ |
278
|
|
|
$columns = $this->getColumns(); |
279
|
|
|
|
280
|
|
|
$result = []; |
281
|
|
|
$result['draw'] = Request::input('draw', 0); |
282
|
|
|
$result['recordsTotal'] = $totalCount; |
283
|
|
|
$result['recordsFiltered'] = $filteredCount; |
284
|
|
|
$result['data'] = []; |
285
|
|
|
|
286
|
|
|
foreach ($collection as $instance) { |
287
|
|
|
$_row = []; |
288
|
|
|
|
289
|
|
|
foreach ($columns->all() as $column) { |
290
|
|
|
$column->setModel($instance); |
291
|
|
|
|
292
|
|
|
if ($column instanceof Control) { |
293
|
|
|
$column->initialize(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$_row[] = (string) $column; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$result['data'][] = $_row; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $result; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.