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' => 'InlineCreate', |
23
|
|
|
]); |
24
|
|
|
Route::post($segment.'/inline/create', [ |
25
|
|
|
'as' => $segment.'-inline-create-save', |
26
|
|
|
'uses' => $controller.'@storeInlineCreate', |
27
|
|
|
'operation' => 'InlineCreate', |
28
|
|
|
]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setup operation default settings. We run setup() and setupCreateOperation() because those are run in middleware |
33
|
|
|
* and to get the fields we need them earlier in application lifecycle. |
34
|
|
|
*/ |
35
|
|
|
protected function setupInlineCreateDefaults() |
36
|
|
|
{ |
37
|
|
|
$inlineFound = false; |
|
|
|
|
38
|
|
|
foreach (class_uses(self::class) as $key => $value) { |
39
|
|
|
$inlineFound = strpos($value, "\InlineCreateOperation") !== false; |
40
|
|
|
if ($inlineFound && strpos($value, "\CreateOperation") !== false) { |
41
|
|
|
abort(500, 'Inline Create Operation trait should be loaded AFTER Create Operation.'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (method_exists($this, 'setup')) { |
46
|
|
|
$this->setup(); |
47
|
|
|
} |
48
|
|
|
if (method_exists($this, 'setupCreateOperation')) { |
49
|
|
|
$this->setupCreateOperation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->crud->applyConfigurationFromSettings('create'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the HTML of the create form. It's used by the CreateInline operation, to show that form |
57
|
|
|
* inside a popup (aka modal). |
58
|
|
|
*/ |
59
|
|
|
public function getInlineCreateModal() |
60
|
|
|
{ |
61
|
|
|
if (! request()->has('entity')) { |
62
|
|
|
abort(400, 'No "entity" inside the request.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return view( |
66
|
|
|
'crud::fields.relationship.inline_create_modal', |
67
|
|
|
[ |
68
|
|
|
'fields' => $this->crud->getCreateFields(), |
69
|
|
|
'action' => 'create', |
70
|
|
|
'crud' => $this->crud, |
71
|
|
|
'entity' => request()->get('entity'), |
72
|
|
|
'modalClass' => request()->get('modal_class'), |
73
|
|
|
'parentLoadedFields' => request()->get('parent_loaded_fields'), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Runs the store() function in controller like a regular crud create form. |
80
|
|
|
* Developer might overwrite this if he wants some custom save behaviour when added on the fly. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function storeInlineCreate() |
85
|
|
|
{ |
86
|
|
|
$result = $this->store(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// do not carry over the flash messages from the Create operation |
89
|
|
|
Alert::flush(); |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.