|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
|
6
|
|
|
use Prologue\Alerts\Facades\Alert; |
|
7
|
|
|
|
|
8
|
|
|
trait InlineCreateOperation |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Define which routes are needed for this operation. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
|
14
|
|
|
* @param string $routeName Prefix of the route name. |
|
15
|
|
|
* @param string $controller Name of the current CrudController. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function setupInlineCreateRoutes($segment, $routeName, $controller) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
Route::post($segment.'/inline/create/modal', [ |
|
20
|
|
|
'as' => $segment.'-inline-create', |
|
21
|
|
|
'uses' => $controller.'@getInlineCreateModal', |
|
22
|
|
|
'operation' => 'DefaultInlineCreate', |
|
23
|
|
|
]); |
|
24
|
|
|
Route::post($segment.'/inline/create', [ |
|
25
|
|
|
'as' => $segment.'-inline-create-save', |
|
26
|
|
|
'uses' => $controller.'@storeInlineCreate', |
|
27
|
|
|
'operation' => 'DefaultInlineCreate', |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This operation have some known quirks that can only be addressed using this setup instead of `setupInlineCreateDefaults` |
|
33
|
|
|
* 1 - InlineCreateOperation must be added AFTER CreateOperation trait. |
|
34
|
|
|
* 2 - setup() in controllers that have the InlineCreateOperations need to be called twice (technically we are re-creating a new crud). |
|
35
|
|
|
* |
|
36
|
|
|
* We use this setup to make this operation behaviour similar to other operations, so developer could still override |
|
37
|
|
|
* the defaults with `setupInlineCreateOperation` as with any other operation. |
|
38
|
|
|
* |
|
39
|
|
|
* `setupInlineCreateDefaults` is not used because of the time it is called in the stack. The "defaults" are applied in backpack |
|
40
|
|
|
* to all operations, before applying operation specific setups. As this operation directly need to call other |
|
41
|
|
|
* methods in the CrudController, it should only be "initialized" when it's requested. |
|
42
|
|
|
* |
|
43
|
|
|
* To solve the mentioned problems we initialize the operation defaults at the same time we apply developer setup. Developer settings |
|
44
|
|
|
* are applied after the defaults to prevail. |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function setupDefaultInlineCreateOperation() |
|
47
|
|
|
{ |
|
48
|
|
|
if (method_exists($this, 'setup')) { |
|
49
|
|
|
$this->setup(); |
|
50
|
|
|
} |
|
51
|
|
|
if (method_exists($this, 'setupCreateOperation')) { |
|
52
|
|
|
$this->setupCreateOperation(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->crud->applyConfigurationFromSettings('create'); |
|
56
|
|
|
|
|
57
|
|
|
if (method_exists($this, 'setupInlineCreateOperation')) { |
|
58
|
|
|
$this->setupInlineCreateOperation(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the HTML of the create form. It's used by the CreateInline operation, to show that form |
|
64
|
|
|
* inside a popup (aka modal). |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getInlineCreateModal() |
|
67
|
|
|
{ |
|
68
|
|
|
if (! request()->has('entity')) { |
|
69
|
|
|
abort(400, 'No "entity" inside the request.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return view( |
|
73
|
|
|
'crud::fields.relationship.inline_create_modal', |
|
74
|
|
|
[ |
|
75
|
|
|
'fields' => $this->crud->getCreateFields(), |
|
76
|
|
|
'action' => 'create', |
|
77
|
|
|
'crud' => $this->crud, |
|
78
|
|
|
'entity' => request()->get('entity'), |
|
79
|
|
|
'modalClass' => request()->get('modal_class'), |
|
80
|
|
|
'parentLoadedFields' => request()->get('parent_loaded_fields'), |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Runs the store() function in controller like a regular crud create form. |
|
87
|
|
|
* Developer might overwrite this if he wants some custom save behaviour when added on the fly. |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function storeInlineCreate() |
|
92
|
|
|
{ |
|
93
|
|
|
$result = $this->store(); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
// do not carry over the flash messages from the Create operation |
|
96
|
|
|
Alert::flush(); |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.