1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait HasForm |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Set up a GET and POST route, for an operation that contains a form. |
12
|
|
|
*/ |
13
|
|
|
protected function formRoutes(string $operationName, bool $routesHaveIdSegment, string $segment, string $routeName, string $controller): void |
14
|
|
|
{ |
15
|
|
|
$secondSegment = $routesHaveIdSegment ? '/{id}/' : '/'; |
16
|
|
|
$thirdSegment = Str::of($operationName)->kebab(); |
17
|
|
|
$getFormMethod = 'get'.$operationName.'Form'; |
18
|
|
|
$postFormMethod = 'post'.$operationName.'Form'; |
19
|
|
|
|
20
|
|
|
Route::get($segment.$secondSegment.$thirdSegment, [ |
21
|
|
|
'as' => $routeName.'.'.$getFormMethod, |
22
|
|
|
'uses' => $controller.'@'.$getFormMethod, |
23
|
|
|
'operation' => $operationName, |
24
|
|
|
]); |
25
|
|
|
Route::post($segment.$secondSegment.$thirdSegment, [ |
26
|
|
|
'as' => $routeName.'.'.$postFormMethod, |
27
|
|
|
'uses' => $controller.'@'.$postFormMethod, |
28
|
|
|
'operation' => $operationName, |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set up the default configurations, save actions and buttons |
34
|
|
|
* for a standard operation that contains a form. |
35
|
|
|
*/ |
36
|
|
|
protected function formDefaults(string $operationName, string $buttonStack = 'line', array $buttonMeta = []): void |
37
|
|
|
{ |
38
|
|
|
// Access |
39
|
|
|
$this->crud->allowAccess($operationName); |
40
|
|
|
|
41
|
|
|
// Config |
42
|
|
|
$this->crud->operation($operationName, function () use ($operationName) { |
43
|
|
|
// if the backpack.operations.{operationName} config exists, use that one |
44
|
|
|
// otherwise, use the generic backpack.operations.form config |
45
|
|
|
if (config()->has('backpack.operations.'.$operationName)) { |
46
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
47
|
|
|
} else { |
48
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig('backpack.operations.form'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// add a reasonable "save and back" save action |
52
|
|
|
$this->crud->addSaveAction([ |
53
|
|
|
'name' => 'save_and_back', |
54
|
|
|
'visible' => function ($crud) use ($operationName) { |
55
|
|
|
return $crud->hasAccess($operationName); |
56
|
|
|
}, |
57
|
|
|
'redirect' => function ($crud, $request, $itemId = null) { |
|
|
|
|
58
|
|
|
return $request->request->has('_http_referrer') ? $request->request->get('_http_referrer') : $crud->route; |
59
|
|
|
}, |
60
|
|
|
'button_text' => trans('backpack::crud.save_action_save_and_back'), |
61
|
|
|
]); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
// Default Button |
65
|
|
|
$this->crud->operation(['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) { |
66
|
|
|
$this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Method to handle the GET request and display the View with a Backpack form. |
72
|
|
|
*/ |
73
|
|
|
public function formView(?int $id = null): \Illuminate\Contracts\View\View |
74
|
|
|
{ |
75
|
|
|
if ($id) { |
|
|
|
|
76
|
|
|
// Get entry ID from Request (makes sure its the last ID for nested resources) |
77
|
|
|
$this->data['id'] = $this->crud->getCurrentEntryId() ?? $id; |
|
|
|
|
78
|
|
|
$this->data['entry'] = $this->crud->getEntryWithLocale($this->data['id']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->crud->setOperationSetting('fields', $this->crud->fields()); |
82
|
|
|
|
83
|
|
|
$this->data['crud'] = $this->crud; |
84
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? Str::of($this->crud->getCurrentOperation())->headline(); |
85
|
|
|
$this->data['operation'] = $this->crud->getCurrentOperation(); |
86
|
|
|
|
87
|
|
|
if ($this->crud->getOperationSetting('save_actions')) { |
88
|
|
|
$this->data['saveAction'] = $this->crud->getSaveAction(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->data['formAction'] = $this->crud->getOperationSetting('form_action'); |
92
|
|
|
$this->data['formMethod'] = $this->crud->getOperationSetting('form_method'); |
93
|
|
|
|
94
|
|
|
return view($this->crud->getOperationSetting('view') ?? 'crud::inc.form_page', $this->data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Method to handle the POST request and save the form. |
99
|
|
|
* It performs the validation, authorization and redirect according to the save action. |
100
|
|
|
* But it does NOT save the data. That should be done in the given $formLogic callback. |
101
|
|
|
* |
102
|
|
|
* @return array|\Illuminate\Http\RedirectResponse |
103
|
|
|
*/ |
104
|
|
|
public function formAction(?int $id = null, callable $formLogic) |
105
|
|
|
{ |
106
|
|
|
if ($id) { |
|
|
|
|
107
|
|
|
// Get entry ID from Request (makes sure its the last ID for nested resources) |
108
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
109
|
|
|
$entry = $this->crud->getEntryWithLocale($id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// execute the FormRequest authorization and validation, if one is required |
113
|
|
|
$request = $this->crud->validateRequest(); |
114
|
|
|
$request = $this->crud->getStrippedSaveRequest($request); |
115
|
|
|
|
116
|
|
|
// register any Model Events defined on fields |
117
|
|
|
$this->crud->registerFieldEvents(); |
118
|
|
|
|
119
|
|
|
// perform the actual logic, that developers give in a callback |
120
|
|
|
($formLogic)($request ?? null, $entry ?? null); |
121
|
|
|
|
122
|
|
|
// save the redirect choice for next time |
123
|
|
|
$this->crud->setSaveAction(); |
124
|
|
|
|
125
|
|
|
return $this->crud->performSaveAction($id); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.