1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures; |
4
|
|
|
|
5
|
|
|
//save_and_back save_and_edit save_and_new |
6
|
|
|
trait SaveActions |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Get the save configured save action or the one stored in a session variable. |
10
|
|
|
* @return [type] [description] |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
public function getSaveAction() |
13
|
|
|
{ |
14
|
|
|
$cantCreate = ! $this->crud->hasAccess('create'); |
|
|
|
|
15
|
|
|
$saveAction = session('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
16
|
|
|
if ($saveAction == 'save_and_new' && $cantCreate) { |
17
|
|
|
$saveAction = 'save_and_back'; |
18
|
|
|
} |
19
|
|
|
$saveOptions = []; |
20
|
|
|
$saveCurrent = [ |
21
|
|
|
'value' => $saveAction, |
22
|
|
|
'label' => $this->getSaveActionButtonName($saveAction), |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
switch ($saveAction) { |
26
|
|
|
case 'save_and_edit': |
27
|
|
|
$saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back'); |
28
|
|
|
$saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new'); |
29
|
|
|
break; |
30
|
|
View Code Duplication |
case 'save_and_new': |
|
|
|
|
31
|
|
|
$saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back'); |
32
|
|
|
$saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit'); |
33
|
|
|
break; |
34
|
|
|
case 'save_and_back': |
35
|
|
View Code Duplication |
default: |
|
|
|
|
36
|
|
|
$saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit'); |
37
|
|
|
$saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new'); |
38
|
|
|
break; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($cantCreate) { |
42
|
|
|
unset($saveOptions['save_and_new']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return [ |
46
|
|
|
'active' => $saveCurrent, |
47
|
|
|
'options' => $saveOptions, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Change the session variable that remembers what to do after the "Save" action. |
53
|
|
|
* @param [type] $forceSaveAction [description] |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function setSaveAction($forceSaveAction = null) |
56
|
|
|
{ |
57
|
|
|
if ($forceSaveAction) { |
58
|
|
|
$saveAction = $forceSaveAction; |
59
|
|
|
} else { |
60
|
|
|
$saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (session('save_action', 'save_and_back') !== $saveAction && config('backpack.crud.show_save_action_change', true)) { |
|
|
|
|
64
|
|
|
\Alert::info(trans('backpack::crud.save_action_changed_notification'))->flash(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
session(['save_action' => $saveAction]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Redirect to the correct URL, depending on which save action has been selected. |
72
|
|
|
* @param [type] $itemId [description] |
|
|
|
|
73
|
|
|
* @return [type] [description] |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function performSaveAction($itemId = null) |
76
|
|
|
{ |
77
|
|
|
$saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back')); |
78
|
|
|
$itemId = $itemId ? $itemId : \Request::input('id'); |
79
|
|
|
|
80
|
|
|
switch ($saveAction) { |
81
|
|
|
case 'save_and_new': |
82
|
|
|
$redirectUrl = $this->crud->route.'/create'; |
83
|
|
|
break; |
84
|
|
|
case 'save_and_edit': |
85
|
|
|
$redirectUrl = $this->crud->route.'/'.$itemId.'/edit'; |
86
|
|
|
if (\Request::has('locale')) { |
87
|
|
|
$redirectUrl .= '?locale='.\Request::input('locale'); |
88
|
|
|
} |
89
|
|
|
break; |
90
|
|
|
case 'save_and_back': |
91
|
|
|
default: |
92
|
|
|
$redirectUrl = $this->crud->route; |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return \Redirect::to($redirectUrl); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the translated text for the Save button. |
101
|
|
|
* @param string $actionValue [description] |
102
|
|
|
* @return [type] [description] |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
private function getSaveActionButtonName($actionValue = 'save_and_back') |
105
|
|
|
{ |
106
|
|
|
switch ($actionValue) { |
107
|
|
|
case 'save_and_edit': |
108
|
|
|
return trans('backpack::crud.save_action_save_and_edit'); |
109
|
|
|
break; |
|
|
|
|
110
|
|
|
case 'save_and_new': |
111
|
|
|
return trans('backpack::crud.save_action_save_and_new'); |
112
|
|
|
break; |
|
|
|
|
113
|
|
|
case 'save_and_back': |
114
|
|
|
default: |
115
|
|
|
return trans('backpack::crud.save_action_save_and_back'); |
116
|
|
|
break; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.