1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MedianetDev\BackpackForm\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
|
7
|
|
|
trait CreatePopupOperation |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Define which routes are needed for this operation. |
11
|
|
|
* |
12
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
13
|
|
|
* @param string $routeName Prefix of the route name. |
14
|
|
|
* @param string $controller Name of the current CrudController. |
15
|
|
|
*/ |
16
|
|
|
protected function setupCreatePopupRoutes($segment, $routeName, $controller) |
17
|
|
|
{ |
18
|
|
|
Route::get($segment.'/create', [ |
19
|
|
|
'as' => $routeName.'.create', |
20
|
|
|
'uses' => $controller.'@create', |
21
|
|
|
'operation' => 'create', |
22
|
|
|
]); |
23
|
|
|
|
24
|
|
|
Route::post($segment, [ |
25
|
|
|
'as' => $routeName.'.store', |
26
|
|
|
'uses' => $controller.'@store', |
27
|
|
|
'operation' => 'create', |
28
|
|
|
]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
33
|
|
|
*/ |
34
|
|
|
protected function setupCreatePopupDefaults() |
35
|
|
|
{ |
36
|
|
|
$this->crud->allowAccess('create'); |
37
|
|
|
|
38
|
|
|
$this->crud->operation('create', function () { |
39
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
40
|
|
|
$this->crud->setupDefaultSaveActions(); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$this->crud->operation('list', 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 createPopup() |
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
|
|
|
|
61
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name; |
|
|
|
|
62
|
|
|
|
63
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
64
|
|
|
return view("vendor/backpack/crud/create_popup", $this->data); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Store a newly created resource in the database. |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Http\RedirectResponse |
71
|
|
|
*/ |
72
|
|
|
public function storePopup() |
73
|
|
|
{ |
74
|
|
|
$this->crud->hasAccessOrFail('create'); |
75
|
|
|
|
76
|
|
|
// execute the FormRequest authorization and validation, if one is required |
77
|
|
|
$request = $this->crud->validateRequest(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// insert item in the db |
80
|
|
|
$item = $this->crud->create($this->crud->getStrippedSaveRequest()); |
81
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
|
|
|
|
82
|
|
|
|
83
|
|
|
// show a success message |
84
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// save the redirect choice for next time |
87
|
|
|
$this->crud->setSaveAction(); |
88
|
|
|
|
89
|
|
|
return $this->crud->performSaveAction($item->getKey()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|