1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
8
|
|
|
use Yaro\Jarboe\Exceptions\PermissionDenied; |
9
|
|
|
use Yaro\Jarboe\Table\CRUD; |
10
|
|
|
use Yaro\Jarboe\Table\Fields\AbstractField; |
11
|
|
|
|
12
|
|
|
trait InlineHandlerTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Handle inline update action. |
16
|
|
|
* |
17
|
|
|
* @param Request $request |
18
|
|
|
* @return \Illuminate\Http\JsonResponse |
19
|
|
|
* @throws PermissionDenied |
20
|
|
|
* @throws \ReflectionException |
21
|
|
|
*/ |
22
|
3 |
|
public function handleInline(Request $request) |
23
|
|
|
{ |
24
|
3 |
|
$this->beforeInit(); |
25
|
3 |
|
$this->init(); |
26
|
3 |
|
$this->bound(); |
27
|
|
|
|
28
|
3 |
|
if (!$this->can('inline')) { |
29
|
2 |
|
throw UnauthorizedException::forPermissions(['inline']); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
$id = $request->get('_pk'); |
33
|
1 |
|
$value = $request->get('_value'); |
34
|
1 |
|
$locale = $request->get('_locale'); |
35
|
|
|
/** @var AbstractField $field */ |
36
|
1 |
|
$field = $this->crud()->getFieldByName($request->get('_name')); |
37
|
|
|
|
38
|
1 |
|
$request->replace( |
39
|
1 |
|
$request->all() + [$field->name() => $value] |
40
|
|
|
); |
41
|
|
|
|
42
|
1 |
|
$model = $this->crud()->repo()->find($id); |
43
|
1 |
|
if (!$field->isInline() || !$this->crud()->actions()->isAllowed('edit', $model) || $field->isReadonly()) { |
44
|
|
|
throw new PermissionDenied(); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if (method_exists($this, 'update')) { |
48
|
|
|
$keyName = $field->belongsToArray() ? $field->getDotPatternName() : $field->name(); |
49
|
|
|
list($rules, $messages, $attributes) = $this->getValidationDataForInlineField($request, $keyName); |
50
|
|
|
if ($rules) { |
51
|
|
|
$this->validate( |
|
|
|
|
52
|
|
|
$request, |
53
|
|
|
[$keyName => $rules], |
54
|
|
|
$messages, |
55
|
|
|
$attributes |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
// change app locale, so translatable model's column will be set properly |
61
|
|
|
if ($locale) { |
62
|
|
|
app()->setLocale($locale); |
63
|
|
|
} |
64
|
1 |
|
|
65
|
1 |
|
$fieldName = $field->name(); |
66
|
|
|
$fieldValue = $field->value($request); |
67
|
1 |
|
if ($field->belongsToArray()) { |
68
|
|
|
$fieldName = $field->getAncestorName(); |
69
|
1 |
|
$arrayValue = $field->getAttribute($model); |
70
|
|
|
$arrayValue = is_array($arrayValue) ? $arrayValue : []; |
71
|
1 |
|
$fieldValue = $arrayValue + [$field->getDescendantName() => $fieldValue]; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
$model = $this->crud()->repo()->update($id, [ |
75
|
|
|
$fieldName => $fieldValue, |
76
|
|
|
]); |
77
|
|
|
$field->afterUpdate($model, $request); |
78
|
|
|
|
79
|
|
|
$this->idEntity = $model->getKey(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return response()->json([ |
|
|
|
|
82
|
|
|
'value' => $field->getAttribute($model, $locale), |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get validation data for inline field. |
88
|
|
|
* |
89
|
|
|
* @param Request $request |
90
|
|
|
* @param $name |
91
|
|
|
* @return array |
92
|
|
|
* @throws \ReflectionException |
93
|
|
|
*/ |
94
|
|
|
protected function getValidationDataForInlineField(Request $request, $name) |
95
|
|
|
{ |
96
|
|
|
$rules = []; |
97
|
|
|
$messages = []; |
98
|
|
|
$attributes = []; |
99
|
|
|
|
100
|
|
|
$reflection = new \ReflectionClass(get_class($this)); |
101
|
|
|
$method = $reflection->getMethod('update'); |
102
|
|
|
$parameters = $method->getParameters(); |
103
|
|
|
$firstParam = $parameters[0] ?? null; |
104
|
|
|
$isRequestAsFirstParameter = $firstParam && $firstParam->getClass(); |
105
|
|
|
if ($isRequestAsFirstParameter) { |
106
|
|
|
$formRequestClass = $firstParam->getClass()->getName(); |
107
|
|
|
/** @var FormRequest $formRequest */ |
108
|
|
|
$formRequest = new $formRequestClass(); |
109
|
|
|
if (method_exists($formRequest, 'rules')) { |
110
|
|
|
foreach ($formRequest->rules() as $param => $paramRules) { |
111
|
|
|
if (preg_match('~^'. preg_quote($name) .'(\.\*)?$~', $param)) { |
112
|
|
|
return [ |
113
|
|
|
$paramRules, |
114
|
|
|
$formRequest->messages(), |
115
|
|
|
$formRequest->attributes(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return [$rules, $messages, $attributes]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
abstract protected function beforeInit(); |
|
|
|
|
126
|
|
|
abstract protected function init(); |
|
|
|
|
127
|
|
|
abstract protected function bound(); |
|
|
|
|
128
|
|
|
abstract protected function crud(): CRUD; |
129
|
|
|
abstract protected function can($action): bool; |
130
|
|
|
} |
131
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.