1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\QueryException; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use Illuminate\Contracts\Validation\Validator; |
11
|
|
|
use Illuminate\Validation\ValidationException; |
12
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
13
|
|
|
|
14
|
|
|
abstract class DataTablesEditor |
15
|
|
|
{ |
16
|
|
|
use ValidatesRequests; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Allowed dataTables editor actions. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $actions = ['create', 'edit', 'remove', 'upload', 'forceDelete']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
27
|
|
|
*/ |
28
|
|
|
protected $model = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Indicates if all mass assignment is enabled on model. |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $unguarded = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Upload directory relative to storage path. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $uploadDir = 'editor'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Flag to force delete a model. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $forceDeleting = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Filesystem disk config to use for upload. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $disk = 'public'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process dataTables editor action request. |
60
|
|
|
* |
61
|
|
|
* @param Request $request |
62
|
|
|
* @return JsonResponse|mixed |
63
|
|
|
* @throws DataTablesEditorException |
64
|
|
|
*/ |
65
|
|
|
public function process(Request $request) |
66
|
|
|
{ |
67
|
|
|
$action = $request->get('action'); |
68
|
|
|
|
69
|
|
|
if (! in_array($action, $this->actions)) { |
70
|
|
|
throw new DataTablesEditorException('Requested action not supported!'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->{$action}($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Process create action request. |
78
|
|
|
* |
79
|
|
|
* @param Request $request |
80
|
|
|
* @return JsonResponse |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function create(Request $request) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$instance = $this->resolveModel(); |
85
|
|
|
$connection = $instance->getConnection(); |
86
|
|
|
$affected = []; |
87
|
|
|
$errors = []; |
88
|
|
|
|
89
|
|
|
$connection->beginTransaction(); |
90
|
|
|
foreach ($request->get('data') as $data) { |
91
|
|
|
$validator = $this->getValidationFactory() |
92
|
|
|
->make($data, $this->createRules(), $this->messages() + $this->createMessages(), $this->attributes()); |
|
|
|
|
93
|
|
|
if ($validator->fails()) { |
94
|
|
|
foreach ($this->formatErrors($validator) as $error) { |
95
|
|
|
$errors[] = $error; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (method_exists($this, 'creating')) { |
102
|
|
|
$data = $this->creating($instance, $data); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (method_exists($this, 'saving')) { |
106
|
|
|
$data = $this->saving($instance, $data); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$instance->fill($data)->save(); |
110
|
|
|
|
111
|
|
|
if (method_exists($this, 'created')) { |
112
|
|
|
$instance = $this->created($instance, $data); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (method_exists($this, 'saved')) { |
116
|
|
|
$instance = $this->saved($instance, $data); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$instance->setAttribute('DT_RowId', $instance->getKey()); |
120
|
|
|
$affected[] = $instance; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (! $errors) { |
|
|
|
|
124
|
|
|
$connection->commit(); |
125
|
|
|
} else { |
126
|
|
|
$connection->rollBack(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->toJson($affected, $errors); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Resolve model to used. |
134
|
|
|
* |
135
|
|
|
* @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
136
|
|
|
*/ |
137
|
|
|
protected function resolveModel() |
138
|
|
|
{ |
139
|
|
|
if (! $this->model instanceof Model) { |
140
|
|
|
$this->model = new $this->model; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->model->unguard($this->unguarded); |
144
|
|
|
|
145
|
|
|
return $this->model; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get create action validation rules. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
abstract public function createRules(); |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get create validation messages. |
157
|
|
|
* |
158
|
|
|
* @deprecated deprecated since v1.12.0, please use messages() instead. |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function createMessages() |
162
|
|
|
{ |
163
|
|
|
return []; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get custom attributes for validator errors. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function attributes() |
172
|
|
|
{ |
173
|
|
|
return []; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Validator $validator |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
protected function formatErrors(Validator $validator) |
181
|
|
|
{ |
182
|
|
|
$errors = []; |
183
|
|
|
|
184
|
|
|
collect($validator->errors())->each(function ($error, $key) use (&$errors) { |
185
|
|
|
$errors[] = [ |
186
|
|
|
'name' => $key, |
187
|
|
|
'status' => $error[0], |
188
|
|
|
]; |
189
|
|
|
}); |
190
|
|
|
|
191
|
|
|
return $errors; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Display success data in dataTables editor format. |
196
|
|
|
* |
197
|
|
|
* @param array $data |
198
|
|
|
* @param array $errors |
199
|
|
|
* @return JsonResponse |
200
|
|
|
*/ |
201
|
|
|
protected function toJson(array $data, array $errors = []) |
202
|
|
|
{ |
203
|
|
|
$response = ['data' => $data]; |
204
|
|
|
if ($errors) { |
|
|
|
|
205
|
|
|
$response['fieldErrors'] = $errors; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return new JsonResponse($response, 200); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Process edit action request. |
213
|
|
|
* |
214
|
|
|
* @param Request $request |
215
|
|
|
* @return JsonResponse |
216
|
|
|
*/ |
217
|
|
View Code Duplication |
public function edit(Request $request) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$connection = $this->getBuilder()->getConnection(); |
220
|
|
|
$affected = []; |
221
|
|
|
$errors = []; |
222
|
|
|
|
223
|
|
|
$connection->beginTransaction(); |
224
|
|
|
foreach ($request->get('data') as $key => $data) { |
225
|
|
|
$model = $this->getBuilder()->findOrFail($key); |
226
|
|
|
$validator = $this->getValidationFactory() |
227
|
|
|
->make($data, $this->editRules($model), $this->messages() + $this->editMessages(), $this->attributes()); |
|
|
|
|
228
|
|
|
if ($validator->fails()) { |
229
|
|
|
foreach ($this->formatErrors($validator) as $error) { |
230
|
|
|
$errors[] = $error; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
continue; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (method_exists($this, 'updating')) { |
237
|
|
|
$data = $this->updating($model, $data); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if (method_exists($this, 'saving')) { |
241
|
|
|
$data = $this->saving($model, $data); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$model->fill($data)->save(); |
245
|
|
|
|
246
|
|
|
if (method_exists($this, 'updated')) { |
247
|
|
|
$model = $this->updated($model, $data); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (method_exists($this, 'saved')) { |
251
|
|
|
$model = $this->saved($model, $data); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$model->setAttribute('DT_RowId', $model->getKey()); |
255
|
|
|
$affected[] = $model; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if (! $errors) { |
|
|
|
|
259
|
|
|
$connection->commit(); |
260
|
|
|
} else { |
261
|
|
|
$connection->rollBack(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $this->toJson($affected, $errors); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get elqouent builder of the model. |
269
|
|
|
* |
270
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
271
|
|
|
*/ |
272
|
|
|
protected function getBuilder() |
273
|
|
|
{ |
274
|
|
|
$model = $this->resolveModel(); |
275
|
|
|
|
276
|
|
|
if (in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses($model))) { |
277
|
|
|
return $model->newQuery()->withTrashed(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $model->newQuery(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get edit action validation rules. |
285
|
|
|
* |
286
|
|
|
* @param Model $model |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
abstract public function editRules(Model $model); |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get edit validation messages. |
293
|
|
|
* |
294
|
|
|
* @deprecated deprecated since v1.12.0, please use messages() instead. |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
protected function editMessages() |
298
|
|
|
{ |
299
|
|
|
return []; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Process force delete action request. |
304
|
|
|
* |
305
|
|
|
* @param \Illuminate\Http\Request $request |
306
|
|
|
* @return \Illuminate\Http\JsonResponse |
307
|
|
|
*/ |
308
|
|
|
public function forceDelete(Request $request) |
309
|
|
|
{ |
310
|
|
|
$this->forceDeleting = true; |
311
|
|
|
|
312
|
|
|
return $this->remove($request); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Process remove action request. |
317
|
|
|
* |
318
|
|
|
* @param Request $request |
319
|
|
|
* @return JsonResponse |
320
|
|
|
*/ |
321
|
|
|
public function remove(Request $request) |
322
|
|
|
{ |
323
|
|
|
$connection = $this->getBuilder()->getConnection(); |
324
|
|
|
$affected = []; |
325
|
|
|
$errors = []; |
326
|
|
|
|
327
|
|
|
$connection->beginTransaction(); |
328
|
|
|
foreach ($request->get('data') as $key => $data) { |
329
|
|
|
$model = $this->getBuilder()->findOrFail($key); |
330
|
|
|
$validator = $this->getValidationFactory() |
331
|
|
|
->make($data, $this->removeRules($model), $this->messages() + $this->removeMessages(), $this->attributes()); |
|
|
|
|
332
|
|
|
if ($validator->fails()) { |
333
|
|
|
foreach ($this->formatErrors($validator) as $error) { |
334
|
|
|
$errors[] = $error['status']; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
continue; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
try { |
341
|
|
|
$deleted = clone $model; |
342
|
|
|
if (method_exists($this, 'deleting')) { |
343
|
|
|
$this->deleting($model, $data); |
|
|
|
|
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$this->forceDeleting ? $model->forceDelete() : $model->delete(); |
347
|
|
|
|
348
|
|
|
if (method_exists($this, 'deleted')) { |
349
|
|
|
$this->deleted($deleted, $data); |
|
|
|
|
350
|
|
|
} |
351
|
|
|
} catch (QueryException $exception) { |
352
|
|
|
$error = config('app.debug') |
353
|
|
|
? $exception->errorInfo[2] |
354
|
|
|
: $this->removeExceptionMessage($exception, $model); |
|
|
|
|
355
|
|
|
|
356
|
|
|
$errors[] = $error; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$affected[] = $deleted; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if (! $errors) { |
|
|
|
|
363
|
|
|
$connection->commit(); |
364
|
|
|
} else { |
365
|
|
|
$connection->rollBack(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$response = ['data' => $affected]; |
369
|
|
|
if ($errors) { |
|
|
|
|
370
|
|
|
$response['error'] = implode("\n", $errors); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return new JsonResponse($response, 200); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get remove action validation rules. |
378
|
|
|
* |
379
|
|
|
* @param Model $model |
380
|
|
|
* @return array |
381
|
|
|
*/ |
382
|
|
|
abstract public function removeRules(Model $model); |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Get remove validation messages. |
386
|
|
|
* |
387
|
|
|
* @deprecated deprecated since v1.12.0, please use messages() instead. |
388
|
|
|
* @return array |
389
|
|
|
*/ |
390
|
|
|
protected function removeMessages() |
391
|
|
|
{ |
392
|
|
|
return []; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Get remove query exception message. |
397
|
|
|
* |
398
|
|
|
* @param QueryException $exception |
399
|
|
|
* @param Model $model |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
protected function removeExceptionMessage(QueryException $exception, Model $model) |
|
|
|
|
403
|
|
|
{ |
404
|
|
|
return "Record {$model->getKey()} is protected and cannot be deleted!"; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Get dataTables model. |
409
|
|
|
* |
410
|
|
|
* @return Model |
411
|
|
|
*/ |
412
|
|
|
public function getModel() |
413
|
|
|
{ |
414
|
|
|
return $this->model; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Set the dataTables model on runtime. |
419
|
|
|
* |
420
|
|
|
* @param Model|string $model |
421
|
|
|
* @return DataTablesEditor |
422
|
|
|
*/ |
423
|
|
|
public function setModel($model) |
424
|
|
|
{ |
425
|
|
|
$this->model = $model; |
|
|
|
|
426
|
|
|
|
427
|
|
|
return $this; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Set model unguard state. |
432
|
|
|
* |
433
|
|
|
* @param bool $state |
434
|
|
|
* @return $this |
435
|
|
|
*/ |
436
|
|
|
public function unguard($state = true) |
437
|
|
|
{ |
438
|
|
|
$this->unguarded = $state; |
439
|
|
|
|
440
|
|
|
return $this; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Handle uploading of file. |
445
|
|
|
* |
446
|
|
|
* @param \Illuminate\Http\Request $request |
447
|
|
|
* @return \Illuminate\Http\JsonResponse |
448
|
|
|
*/ |
449
|
|
|
public function upload(Request $request) |
450
|
|
|
{ |
451
|
|
|
$field = $request->input('uploadField'); |
452
|
|
|
$storage = Storage::disk($this->disk); |
453
|
|
|
|
454
|
|
|
try { |
455
|
|
|
$rules = $this->uploadRules(); |
456
|
|
|
$fieldRules = ['upload' => data_get($rules, $field, [])]; |
|
|
|
|
457
|
|
|
|
458
|
|
|
$this->validate($request, $fieldRules, $this->messages(), $this->attributes()); |
459
|
|
|
|
460
|
|
|
$id = $storage->putFile($this->uploadDir, $request->file('upload')); |
461
|
|
|
|
462
|
|
|
if (method_exists($this, 'uploaded')) { |
463
|
|
|
$id = $this->uploaded($id); |
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return response()->json([ |
|
|
|
|
467
|
|
|
'data' => [], |
468
|
|
|
'files' => [ |
469
|
|
|
'files' => [ |
470
|
|
|
$id => [ |
471
|
|
|
'filename' => $id, |
472
|
|
|
'size' => $request->file('upload')->getSize(), |
473
|
|
|
'directory' => $this->uploadDir, |
474
|
|
|
'disk' => $this->disk, |
475
|
|
|
'url' => $storage->url($id), |
476
|
|
|
] |
477
|
|
|
] |
478
|
|
|
], |
479
|
|
|
'upload' => [ |
480
|
|
|
'id' => $id, |
481
|
|
|
], |
482
|
|
|
]); |
483
|
|
|
} catch (ValidationException $exception) { |
484
|
|
|
return response()->json([ |
485
|
|
|
'data' => [], |
486
|
|
|
'fieldErrors' => [ |
487
|
|
|
[ |
488
|
|
|
'name' => $field, |
489
|
|
|
'status' => str_replace('upload', $field, $exception->errors()['upload'][0]), |
490
|
|
|
], |
491
|
|
|
], |
492
|
|
|
]); |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Upload validation rules. |
498
|
|
|
* |
499
|
|
|
* @return array |
500
|
|
|
*/ |
501
|
|
|
public function uploadRules() |
502
|
|
|
{ |
503
|
|
|
return []; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Get validation messages. |
508
|
|
|
* |
509
|
|
|
* @return array |
510
|
|
|
*/ |
511
|
|
|
protected function messages() |
512
|
|
|
{ |
513
|
|
|
return []; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Display dataTables editor validation errors. |
518
|
|
|
* |
519
|
|
|
* @param Validator $validator |
520
|
|
|
* @return JsonResponse |
521
|
|
|
*/ |
522
|
|
|
protected function displayValidationErrors(Validator $validator) |
523
|
|
|
{ |
524
|
|
|
$errors = $this->formatErrors($validator); |
525
|
|
|
|
526
|
|
|
return new JsonResponse([ |
527
|
|
|
'data' => [], |
528
|
|
|
'fieldErrors' => $errors, |
529
|
|
|
]); |
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.