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
|
|
|
public function getSaveAction() |
9
|
|
|
{ |
10
|
|
|
$saveAction = session('save_action', config('backback.crud.default_save_action', 'save_and_back')); |
11
|
|
|
$saveOptions = []; |
12
|
|
|
$saveCurrent = [ |
13
|
|
|
'value' => $saveAction, |
14
|
|
|
'label' => $this->getSaveActionButtonName($saveAction), |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
switch ($saveAction) { |
18
|
|
View Code Duplication |
case 'save_and_edit': |
|
|
|
|
19
|
|
|
$saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back'); |
20
|
|
|
$saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new'); |
21
|
|
|
break; |
22
|
|
View Code Duplication |
case 'save_and_new': |
|
|
|
|
23
|
|
|
$saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back'); |
24
|
|
|
$saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit'); |
25
|
|
|
break; |
26
|
|
|
case 'save_and_black': |
27
|
|
View Code Duplication |
default: |
|
|
|
|
28
|
|
|
$saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit'); |
29
|
|
|
$saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new'); |
30
|
|
|
break; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return [ |
34
|
|
|
'active' => $saveCurrent, |
35
|
|
|
'options' => $saveOptions, |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setSaveAction($forceSaveAction = null) |
40
|
|
|
{ |
41
|
|
|
if ($forceSaveAction) { |
42
|
|
|
$saveAction = $forceSaveAction; |
43
|
|
|
} else { |
44
|
|
|
$saveAction = \Request::input('save_action', config('backback.crud.default_save_action', 'save_and_back')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (session('save_action', 'save_and_back') !== $saveAction) { |
48
|
|
|
\Alert::info(trans('backpack::crud.save_action_changed_notification'))->flash(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
session(['save_action' => $saveAction]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function performSaveAction($itemId = null) |
55
|
|
|
{ |
56
|
|
|
$saveAction = \Request::input('save_action', config('backback.crud.default_save_action', 'save_and_back')); |
57
|
|
|
$itemId = $itemId ? $itemId : \Request::input('id'); |
58
|
|
|
|
59
|
|
|
switch ($saveAction) { |
60
|
|
|
case 'save_and_new': |
61
|
|
|
$redirectUrl = $this->crud->route.'/create'; |
|
|
|
|
62
|
|
|
break; |
63
|
|
|
case 'save_and_edit': |
64
|
|
|
$redirectUrl = $this->crud->route.'/'.$itemId.'/edit'; |
65
|
|
|
break; |
66
|
|
|
case 'save_and_back': |
67
|
|
|
default: |
68
|
|
|
$redirectUrl = $this->crud->route; |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return \Redirect::to($redirectUrl); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getSaveActionButtonName($actionValue = 'save_and_black') |
76
|
|
|
{ |
77
|
|
|
switch ($actionValue) { |
78
|
|
|
case 'save_and_edit': |
79
|
|
|
return trans('backpack::crud.save_action_save_and_edit'); |
80
|
|
|
break; |
|
|
|
|
81
|
|
|
case 'save_and_new': |
82
|
|
|
return trans('backpack::crud.save_action_save_and_new'); |
83
|
|
|
break; |
|
|
|
|
84
|
|
|
case 'save_and_back': |
85
|
|
|
default: |
86
|
|
|
return trans('backpack::crud.save_action_save_and_back'); |
87
|
|
|
break; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.