These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 = [ |
||
24 | 'create', |
||
25 | 'edit', |
||
26 | 'remove', |
||
27 | 'upload', |
||
28 | 'forceDelete', |
||
29 | 'restore', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @var \Illuminate\Database\Eloquent\Model |
||
34 | */ |
||
35 | protected $model = null; |
||
36 | |||
37 | /** |
||
38 | * Indicates if all mass assignment is enabled on model. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $unguarded = false; |
||
43 | |||
44 | /** |
||
45 | * Upload directory relative to storage path. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $uploadDir = 'editor'; |
||
50 | |||
51 | /** |
||
52 | * Flag to force delete a model. |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $forceDeleting = false; |
||
57 | |||
58 | /** |
||
59 | * Flag to restore a model from deleted state. |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $restoring = false; |
||
64 | |||
65 | /** |
||
66 | * Filesystem disk config to use for upload. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $disk = 'public'; |
||
71 | |||
72 | /** |
||
73 | * Process dataTables editor action request. |
||
74 | * |
||
75 | * @param Request $request |
||
76 | * @return JsonResponse|mixed |
||
77 | * @throws DataTablesEditorException |
||
78 | */ |
||
79 | public function process(Request $request) |
||
80 | { |
||
81 | $action = $request->get('action'); |
||
82 | |||
83 | if (! in_array($action, $this->actions)) { |
||
84 | throw new DataTablesEditorException('Requested action not supported!'); |
||
85 | } |
||
86 | |||
87 | return $this->{$action}($request); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Process create action request. |
||
92 | * |
||
93 | * @param Request $request |
||
94 | * @return JsonResponse |
||
95 | */ |
||
96 | public function create(Request $request) |
||
97 | { |
||
98 | $model = $this->resolveModel(); |
||
99 | $connection = $model->getConnection(); |
||
100 | $affected = []; |
||
101 | $errors = []; |
||
102 | |||
103 | $connection->beginTransaction(); |
||
104 | foreach ($request->get('data') as $data) { |
||
105 | $instance = $model->newInstance(); |
||
106 | $validator = $this->getValidationFactory() |
||
107 | ->make( |
||
108 | $data, |
||
109 | $this->createRules(), $this->messages() + $this->createMessages(), |
||
110 | $this->attributes() |
||
111 | ); |
||
112 | if ($validator->fails()) { |
||
113 | foreach ($this->formatErrors($validator) as $error) { |
||
114 | $errors[] = $error; |
||
115 | } |
||
116 | |||
117 | continue; |
||
118 | } |
||
119 | |||
120 | if (method_exists($this, 'creating')) { |
||
121 | $data = $this->creating($instance, $data); |
||
122 | } |
||
123 | |||
124 | if (method_exists($this, 'saving')) { |
||
125 | $data = $this->saving($instance, $data); |
||
126 | } |
||
127 | |||
128 | $instance->fill($data)->save(); |
||
129 | |||
130 | if (method_exists($this, 'created')) { |
||
131 | $instance = $this->created($instance, $data); |
||
132 | } |
||
133 | |||
134 | if (method_exists($this, 'saved')) { |
||
135 | $instance = $this->saved($instance, $data); |
||
136 | } |
||
137 | |||
138 | $instance->setAttribute('DT_RowId', $instance->getKey()); |
||
139 | $affected[] = $instance; |
||
140 | } |
||
141 | |||
142 | if (! $errors) { |
||
143 | $connection->commit(); |
||
144 | } else { |
||
145 | $connection->rollBack(); |
||
146 | } |
||
147 | |||
148 | return $this->toJson($affected, $errors); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Resolve model to used. |
||
153 | * |
||
154 | * @return Model|\Illuminate\Database\Eloquent\SoftDeletes |
||
155 | */ |
||
156 | protected function resolveModel() |
||
157 | { |
||
158 | if (! $this->model instanceof Model) { |
||
159 | $this->model = new $this->model; |
||
160 | } |
||
161 | |||
162 | $this->model->unguard($this->unguarded); |
||
163 | |||
164 | return $this->model; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get create action validation rules. |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | abstract public function createRules(); |
||
173 | |||
174 | /** |
||
175 | * Get validation messages. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function messages() |
||
180 | { |
||
181 | return []; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get create validation messages. |
||
186 | * |
||
187 | * @return array |
||
188 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
189 | */ |
||
190 | protected function createMessages() |
||
191 | { |
||
192 | return []; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get custom attributes for validator errors. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public function attributes() |
||
201 | { |
||
202 | return []; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param Validator $validator |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function formatErrors(Validator $validator) |
||
210 | { |
||
211 | $errors = []; |
||
212 | |||
213 | collect($validator->errors())->each(function ($error, $key) use (&$errors) { |
||
214 | $errors[] = [ |
||
215 | 'name' => $key, |
||
216 | 'status' => $error[0], |
||
217 | ]; |
||
218 | }); |
||
219 | |||
220 | return $errors; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Display success data in dataTables editor format. |
||
225 | * |
||
226 | * @param array $data |
||
227 | * @param array $errors |
||
228 | * @return JsonResponse |
||
229 | */ |
||
230 | protected function toJson(array $data, array $errors = []) |
||
231 | { |
||
232 | $response = ['data' => $data]; |
||
233 | if ($errors) { |
||
234 | $response['fieldErrors'] = $errors; |
||
235 | } |
||
236 | |||
237 | return new JsonResponse($response, 200); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Process restore action request. |
||
242 | * |
||
243 | * @param \Illuminate\Http\Request $request |
||
244 | * @return \Illuminate\Http\JsonResponse |
||
245 | */ |
||
246 | public function restore(Request $request) |
||
247 | { |
||
248 | $this->restoring = true; |
||
249 | |||
250 | return $this->edit($request); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Process edit action request. |
||
255 | * |
||
256 | * @param Request $request |
||
257 | * @return JsonResponse |
||
258 | */ |
||
259 | public function edit(Request $request) |
||
260 | { |
||
261 | $connection = $this->getBuilder()->getConnection(); |
||
262 | $affected = []; |
||
263 | $errors = []; |
||
264 | |||
265 | $connection->beginTransaction(); |
||
266 | foreach ($request->get('data') as $key => $data) { |
||
267 | $model = $this->getBuilder()->findOrFail($key); |
||
268 | $validator = $this->getValidationFactory() |
||
269 | ->make( |
||
270 | $data, |
||
271 | $this->editRules($model), $this->messages() + $this->editMessages(), |
||
0 ignored issues
–
show
The method
Yajra\DataTables\DataTablesEditor::editMessages() has been deprecated with message: deprecated since v1.12.0, please use messages() instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
272 | $this->attributes() |
||
273 | ); |
||
274 | if ($validator->fails()) { |
||
275 | foreach ($this->formatErrors($validator) as $error) { |
||
276 | $errors[] = $error; |
||
277 | } |
||
278 | |||
279 | continue; |
||
280 | } |
||
281 | |||
282 | if (method_exists($this, 'updating')) { |
||
283 | $data = $this->updating($model, $data); |
||
284 | } |
||
285 | |||
286 | if (method_exists($this, 'saving')) { |
||
287 | $data = $this->saving($model, $data); |
||
288 | } |
||
289 | |||
290 | $this->restoring ? $model->restore() : $model->fill($data)->save(); |
||
291 | |||
292 | if (method_exists($this, 'updated')) { |
||
293 | $model = $this->updated($model, $data); |
||
294 | } |
||
295 | |||
296 | if (method_exists($this, 'saved')) { |
||
297 | $model = $this->saved($model, $data); |
||
298 | } |
||
299 | |||
300 | $model->setAttribute('DT_RowId', $model->getKey()); |
||
301 | $affected[] = $model; |
||
302 | } |
||
303 | |||
304 | if (! $errors) { |
||
305 | $connection->commit(); |
||
306 | } else { |
||
307 | $connection->rollBack(); |
||
308 | } |
||
309 | |||
310 | return $this->toJson($affected, $errors); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Get elqouent builder of the model. |
||
315 | * |
||
316 | * @return \Illuminate\Database\Eloquent\Builder |
||
317 | */ |
||
318 | protected function getBuilder() |
||
319 | { |
||
320 | $model = $this->resolveModel(); |
||
321 | |||
322 | if (in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses($model))) { |
||
323 | return $model->newQuery()->withTrashed(); |
||
324 | } |
||
325 | |||
326 | return $model->newQuery(); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Get edit action validation rules. |
||
331 | * |
||
332 | * @param Model $model |
||
333 | * @return array |
||
334 | */ |
||
335 | abstract public function editRules(Model $model); |
||
336 | |||
337 | /** |
||
338 | * Get edit validation messages. |
||
339 | * |
||
340 | * @return array |
||
341 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
342 | */ |
||
343 | protected function editMessages() |
||
344 | { |
||
345 | return []; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Process force delete action request. |
||
350 | * |
||
351 | * @param \Illuminate\Http\Request $request |
||
352 | * @return \Illuminate\Http\JsonResponse |
||
353 | */ |
||
354 | public function forceDelete(Request $request) |
||
355 | { |
||
356 | $this->forceDeleting = true; |
||
357 | |||
358 | return $this->remove($request); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Process remove action request. |
||
363 | * |
||
364 | * @param Request $request |
||
365 | * @return JsonResponse |
||
366 | */ |
||
367 | public function remove(Request $request) |
||
368 | { |
||
369 | $connection = $this->getBuilder()->getConnection(); |
||
370 | $affected = []; |
||
371 | $errors = []; |
||
372 | |||
373 | $connection->beginTransaction(); |
||
374 | foreach ($request->get('data') as $key => $data) { |
||
375 | $model = $this->getBuilder()->findOrFail($key); |
||
376 | $validator = $this->getValidationFactory() |
||
377 | ->make( |
||
378 | $data, |
||
379 | $this->removeRules($model), $this->messages() + $this->removeMessages(), |
||
0 ignored issues
–
show
$model is of type object|null , but the function expects a object<Illuminate\Database\Eloquent\Model> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
The method
Yajra\DataTables\DataTab...ditor::removeMessages() has been deprecated with message: deprecated since v1.12.0, please use messages() instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
380 | $this->attributes() |
||
381 | ); |
||
382 | if ($validator->fails()) { |
||
383 | foreach ($this->formatErrors($validator) as $error) { |
||
384 | $errors[] = $error['status']; |
||
385 | } |
||
386 | |||
387 | continue; |
||
388 | } |
||
389 | |||
390 | try { |
||
391 | $deleted = clone $model; |
||
392 | if (method_exists($this, 'deleting')) { |
||
393 | $this->deleting($model, $data); |
||
394 | } |
||
395 | |||
396 | $this->forceDeleting ? $model->forceDelete() : $model->delete(); |
||
397 | |||
398 | if (method_exists($this, 'deleted')) { |
||
399 | $this->deleted($deleted, $data); |
||
400 | } |
||
401 | } catch (QueryException $exception) { |
||
402 | $error = config('app.debug') |
||
403 | ? $exception->errorInfo[2] |
||
404 | : $this->removeExceptionMessage($exception, $model); |
||
405 | |||
406 | $errors[] = $error; |
||
407 | } |
||
408 | |||
409 | $affected[] = $deleted; |
||
410 | } |
||
411 | |||
412 | if (! $errors) { |
||
413 | $connection->commit(); |
||
414 | } else { |
||
415 | $connection->rollBack(); |
||
416 | } |
||
417 | |||
418 | $response = ['data' => $affected]; |
||
419 | if ($errors) { |
||
420 | $response['error'] = implode("\n", $errors); |
||
421 | } |
||
422 | |||
423 | return new JsonResponse($response, 200); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Get remove action validation rules. |
||
428 | * |
||
429 | * @param Model $model |
||
430 | * @return array |
||
431 | */ |
||
432 | abstract public function removeRules(Model $model); |
||
433 | |||
434 | /** |
||
435 | * Get remove validation messages. |
||
436 | * |
||
437 | * @return array |
||
438 | * @deprecated deprecated since v1.12.0, please use messages() instead. |
||
439 | */ |
||
440 | protected function removeMessages() |
||
441 | { |
||
442 | return []; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Get remove query exception message. |
||
447 | * |
||
448 | * @param QueryException $exception |
||
449 | * @param Model $model |
||
450 | * @return string |
||
451 | */ |
||
452 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
453 | { |
||
454 | return "Record {$model->getKey()} is protected and cannot be deleted!"; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Get dataTables model. |
||
459 | * |
||
460 | * @return Model |
||
461 | */ |
||
462 | public function getModel() |
||
463 | { |
||
464 | return $this->model; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Set the dataTables model on runtime. |
||
469 | * |
||
470 | * @param Model|string $model |
||
471 | * @return DataTablesEditor |
||
472 | */ |
||
473 | public function setModel($model) |
||
474 | { |
||
475 | $this->model = $model; |
||
476 | |||
477 | return $this; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Set model unguard state. |
||
482 | * |
||
483 | * @param bool $state |
||
484 | * @return $this |
||
485 | */ |
||
486 | public function unguard($state = true) |
||
487 | { |
||
488 | $this->unguarded = $state; |
||
489 | |||
490 | return $this; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Handle uploading of file. |
||
495 | * |
||
496 | * @param \Illuminate\Http\Request $request |
||
497 | * @return \Illuminate\Http\JsonResponse |
||
498 | */ |
||
499 | public function upload(Request $request) |
||
500 | { |
||
501 | $field = $request->input('uploadField'); |
||
502 | $storage = Storage::disk($this->disk); |
||
503 | |||
504 | try { |
||
505 | $rules = $this->uploadRules(); |
||
506 | $fieldRules = ['upload' => data_get($rules, $field, [])]; |
||
507 | |||
508 | $this->validate($request, $fieldRules, $this->messages(), $this->attributes()); |
||
509 | |||
510 | $id = $storage->putFile($this->uploadDir, $request->file('upload')); |
||
511 | |||
512 | if (method_exists($this, 'uploaded')) { |
||
513 | $id = $this->uploaded($id); |
||
514 | } |
||
515 | |||
516 | return response()->json([ |
||
517 | 'data' => [], |
||
518 | 'files' => [ |
||
519 | 'files' => [ |
||
520 | $id => [ |
||
521 | 'filename' => $id, |
||
522 | 'size' => $request->file('upload')->getSize(), |
||
523 | 'directory' => $this->uploadDir, |
||
524 | 'disk' => $this->disk, |
||
525 | 'url' => $storage->url($id), |
||
526 | ], |
||
527 | ], |
||
528 | ], |
||
529 | 'upload' => [ |
||
530 | 'id' => $id, |
||
531 | ], |
||
532 | ]); |
||
533 | } catch (ValidationException $exception) { |
||
534 | return response()->json([ |
||
535 | 'data' => [], |
||
536 | 'fieldErrors' => [ |
||
537 | [ |
||
538 | 'name' => $field, |
||
539 | 'status' => str_replace('upload', $field, $exception->errors()['upload'][0]), |
||
540 | ], |
||
541 | ], |
||
542 | ]); |
||
543 | } |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Upload validation rules. |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | public function uploadRules() |
||
552 | { |
||
553 | return []; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Display dataTables editor validation errors. |
||
558 | * |
||
559 | * @param Validator $validator |
||
560 | * @return JsonResponse |
||
561 | */ |
||
562 | protected function displayValidationErrors(Validator $validator) |
||
563 | { |
||
564 | $errors = $this->formatErrors($validator); |
||
565 | |||
566 | return new JsonResponse([ |
||
567 | 'data' => [], |
||
568 | 'fieldErrors' => $errors, |
||
569 | ]); |
||
570 | } |
||
571 | } |
||
572 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: