1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
trait SaveActions |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get save actions, with pre-selected action from stored session variable or config fallback. |
9
|
|
|
* |
10
|
|
|
* TODO: move this to the CrudPanel object; They don't belong in controllers; |
11
|
|
|
* |
12
|
|
|
* @return array |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
public function getSaveAction() |
15
|
|
|
{ |
16
|
|
|
$saveAction = session('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
17
|
|
|
|
18
|
|
|
// Permissions and their related actions. |
19
|
|
|
$permissions = [ |
20
|
|
|
'list' => 'save_and_back', |
21
|
|
|
'update' => 'save_and_edit', |
22
|
|
|
'create' => 'save_and_new', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
$saveOptions = collect($permissions) |
26
|
|
|
// Restrict list to allowed actions. |
27
|
|
|
->filter(function ($action, $permission) { |
28
|
|
|
return $this->crud->hasAccess($permission); |
|
|
|
|
29
|
|
|
}) |
30
|
|
|
// Generate list of possible actions. |
31
|
|
|
->mapWithKeys(function ($action, $permission) { |
|
|
|
|
32
|
|
|
return [$action => $this->getSaveActionButtonName($action)]; |
33
|
|
|
}) |
34
|
|
|
->all(); |
35
|
|
|
|
36
|
|
|
// Set current action if it exist, or first available option. |
37
|
|
|
if (isset($saveOptions[$saveAction])) { |
38
|
|
|
$saveCurrent = [ |
39
|
|
|
'value' => $saveAction, |
40
|
|
|
'label' => $saveOptions[$saveAction], |
41
|
|
|
]; |
42
|
|
|
} else { |
43
|
|
|
$saveCurrent = [ |
44
|
|
|
'value' => key($saveOptions), |
45
|
|
|
'label' => reset($saveOptions), |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Remove active action from options. |
50
|
|
|
unset($saveOptions[$saveCurrent['value']]); |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
'active' => $saveCurrent, |
54
|
|
|
'options' => $saveOptions, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Change the session variable that remembers what to do after the "Save" action. |
60
|
|
|
* |
61
|
|
|
* @param string|null $forceSaveAction |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function setSaveAction($forceSaveAction = null) |
65
|
|
|
{ |
66
|
|
|
$saveAction = $forceSaveAction ?: |
67
|
|
|
\Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
68
|
|
|
|
69
|
|
|
if (config('backpack.crud.show_save_action_change', true) && |
70
|
|
|
session('save_action', 'save_and_back') !== $saveAction) { |
71
|
|
|
\Alert::info(trans('backpack::crud.save_action_changed_notification'))->flash(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
session(['save_action' => $saveAction]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Redirect to the correct URL, depending on which save action has been selected. |
79
|
|
|
* |
80
|
|
|
* @param string $itemId |
|
|
|
|
81
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function performSaveAction($itemId = null) |
84
|
|
|
{ |
85
|
|
|
$saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
86
|
|
|
$itemId = $itemId ?: \Request::input('id'); |
87
|
|
|
|
88
|
|
|
switch ($saveAction) { |
89
|
|
|
case 'save_and_new': |
90
|
|
|
$redirectUrl = $this->crud->route.'/create'; |
91
|
|
|
break; |
92
|
|
|
case 'save_and_edit': |
93
|
|
|
$redirectUrl = $this->crud->route.'/'.$itemId.'/edit'; |
94
|
|
|
if (\Request::has('current_tab')) { |
95
|
|
|
$redirectUrl = $redirectUrl.'#'.\Request::get('current_tab'); |
96
|
|
|
} |
97
|
|
|
if (\Request::has('locale')) { |
98
|
|
|
$redirectUrl .= '?locale='.\Request::input('locale'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
break; |
102
|
|
|
case 'save_and_back': |
103
|
|
|
default: |
104
|
|
|
$redirectUrl = $this->crud->route; |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// if the request is AJAX, return a JSON response |
109
|
|
|
if ($this->request->ajax()) { |
|
|
|
|
110
|
|
|
return [ |
111
|
|
|
'success' => true, |
112
|
|
|
'data' => $this->crud->entry, |
113
|
|
|
'redirect_url' => $redirectUrl, |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return \Redirect::to($redirectUrl); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the translated text for the Save button. |
122
|
|
|
* |
123
|
|
|
* @param string $actionValue |
124
|
|
|
* @return string |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
private function getSaveActionButtonName($actionValue = 'save_and_back') |
127
|
|
|
{ |
128
|
|
|
switch ($actionValue) { |
129
|
|
|
case 'save_and_edit': |
130
|
|
|
return trans('backpack::crud.save_action_save_and_edit'); |
131
|
|
|
break; |
|
|
|
|
132
|
|
|
case 'save_and_new': |
133
|
|
|
return trans('backpack::crud.save_action_save_and_new'); |
134
|
|
|
break; |
|
|
|
|
135
|
|
|
case 'save_and_back': |
136
|
|
|
default: |
137
|
|
|
return trans('backpack::crud.save_action_save_and_back'); |
138
|
|
|
break; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.