1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; |
6
|
|
|
use Illuminate\Support\Facades\Route; |
7
|
|
|
|
8
|
|
|
trait CreateOperation |
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 setupCreateRoutes($segment, $routeName, $controller) |
18
|
|
|
{ |
19
|
|
|
Route::get($segment.'/create', [ |
20
|
|
|
'as' => $routeName.'.create', |
21
|
|
|
'uses' => $controller.'@create', |
22
|
|
|
'operation' => 'create', |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
Route::post($segment, [ |
26
|
|
|
'as' => $routeName.'.store', |
27
|
|
|
'uses' => $controller.'@store', |
28
|
|
|
'operation' => 'create', |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
34
|
|
|
*/ |
35
|
|
|
protected function setupCreateDefaults() |
36
|
|
|
{ |
37
|
|
|
$this->crud->allowAccess('create'); |
38
|
|
|
|
39
|
|
|
LifecycleHook::hookInto('create:before_setup', function () { |
|
|
|
|
40
|
|
|
$this->crud->setupDefaultSaveActions(); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
LifecycleHook::hookInto('list:before_setup', function () { |
44
|
|
|
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create'); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Show the form for creating inserting a new row. |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Contracts\View\View |
52
|
|
|
*/ |
53
|
|
|
public function create() |
54
|
|
|
{ |
55
|
|
|
$this->crud->hasAccessOrFail('create'); |
56
|
|
|
|
57
|
|
|
// prepare the fields you need to show |
58
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
59
|
|
|
$this->data['saveAction'] = $this->crud->getSaveAction(); |
60
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name; |
61
|
|
|
|
62
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
63
|
|
|
return request()->ajax() ? |
64
|
|
|
view('crud::components.form.form_ajax_view', $this->data) : |
65
|
|
|
view($this->crud->getCreateView(), $this->data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function createForm() |
69
|
|
|
{ |
70
|
|
|
$this->crud->hasAccessOrFail('create'); |
71
|
|
|
|
72
|
|
|
// if the request isn't an AJAX request, return a 404 |
73
|
|
|
if (! request()->ajax()) { |
74
|
|
|
abort(404); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return view( |
78
|
|
|
$this->crud->getFirstFieldView('form.create_form'), |
79
|
|
|
[ |
80
|
|
|
'fields' => $this->crud->getCreateFields(), |
81
|
|
|
'action' => 'create', |
82
|
|
|
'crud' => $this->crud, |
83
|
|
|
'modalClass' => request()->get('modal_class'), |
84
|
|
|
'parentLoadedAssets' => request()->get('parent_loaded_assets'), |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Store a newly created resource in the database. |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Http\RedirectResponse |
93
|
|
|
*/ |
94
|
|
|
public function store() |
95
|
|
|
{ |
96
|
|
|
$this->crud->hasAccessOrFail('create'); |
97
|
|
|
|
98
|
|
|
// execute the FormRequest authorization and validation, if one is required |
99
|
|
|
$request = $this->crud->validateRequest(); |
100
|
|
|
|
101
|
|
|
// register any Model Events defined on fields |
102
|
|
|
$this->crud->registerFieldEvents(); |
103
|
|
|
|
104
|
|
|
// insert item in the db |
105
|
|
|
$item = $this->crud->create($this->crud->getStrippedSaveRequest($request)); |
106
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
|
|
|
|
107
|
|
|
|
108
|
|
|
// show a success message |
109
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
// save the redirect choice for next time |
112
|
|
|
$this->crud->setSaveAction(); |
113
|
|
|
|
114
|
|
|
return $this->crud->performSaveAction($item->getKey()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|