1
|
|
|
<?php namespace Distilleries\FormBuilder\States; |
2
|
|
|
|
3
|
|
|
use \FormBuilder; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
|
6
|
|
|
trait FormStateTrait { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var \Kris\LaravelFormBuilder\Form $form |
10
|
|
|
* Injected by the constructor |
11
|
|
|
*/ |
12
|
|
|
protected $form; |
13
|
|
|
protected $model; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------------------------------ |
17
|
|
|
// ------------------------------------------------------------------------------------------------ |
18
|
|
|
// ------------------------------------------------------------------------------------------------ |
19
|
|
|
|
20
|
4 |
|
public function getEdit($id = '') |
21
|
|
|
{ |
22
|
4 |
|
$model = (!empty($id)) ? $this->model->findOrFail($id) : $this->model; |
23
|
4 |
|
$form = FormBuilder::create(get_class($this->form), [ |
24
|
4 |
|
'model' => $model |
25
|
|
|
]); |
26
|
|
|
|
27
|
4 |
|
$form_content = view('form-builder::form.components.formgenerator.full', [ |
28
|
4 |
|
'form' => $form |
29
|
|
|
]); |
30
|
|
|
|
31
|
4 |
|
return view('form-builder::form.state.form', [ |
32
|
4 |
|
'form'=>$form_content |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------------------------------ |
37
|
|
|
|
38
|
18 |
|
public function postEdit(Request $request) |
39
|
|
|
{ |
40
|
18 |
|
$form = FormBuilder::create(get_class($this->form), [ |
41
|
18 |
|
'model' => $this->model |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
|
45
|
18 |
|
if ($form->hasError()) |
46
|
|
|
{ |
47
|
6 |
|
return $form->validateAndRedirectBack(); |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
$result = $this->beforeSave(); |
|
|
|
|
51
|
|
|
|
52
|
12 |
|
if ($result != null) |
|
|
|
|
53
|
|
|
{ |
54
|
2 |
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
10 |
|
$result = $this->save($this->dataToSave($request), $request); |
|
|
|
|
58
|
|
|
|
59
|
10 |
|
if ($result != null) |
|
|
|
|
60
|
|
|
{ |
61
|
2 |
|
return $result; |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
return redirect(action($this->getControllerNameForAction().'@getIndex')); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// ------------------------------------------------------------------------------------------------ |
69
|
|
|
|
70
|
12 |
|
protected function dataToSave(Request $request) |
71
|
|
|
{ |
72
|
12 |
|
return $request->only($this->model->getFillable()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// ------------------------------------------------------------------------------------------------ |
76
|
|
|
|
77
|
12 |
|
protected function beforeSave() |
78
|
|
|
{ |
79
|
12 |
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// ------------------------------------------------------------------------------------------------ |
83
|
|
|
|
84
|
10 |
|
protected function afterSave() |
85
|
|
|
{ |
86
|
10 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
// ------------------------------------------------------------------------------------------------ |
91
|
|
|
|
92
|
12 |
|
protected function save($data, Request $request) |
93
|
|
|
{ |
94
|
|
|
|
95
|
12 |
|
$primary = $request->get($this->model->getKeyName()); |
96
|
12 |
|
if (empty($primary)) |
97
|
|
|
{ |
98
|
8 |
|
$this->model = $this->model->create($data); |
99
|
|
|
} else |
100
|
|
|
{ |
101
|
4 |
|
$this->model = $this->model->find($primary); |
102
|
4 |
|
$this->model->update($data); |
103
|
|
|
} |
104
|
|
|
|
105
|
12 |
|
return $this->afterSave(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
// ------------------------------------------------------------------------------------------------ |
110
|
|
|
|
111
|
2 |
|
public function getView($id) |
112
|
|
|
{ |
113
|
2 |
|
$model = (!empty($id)) ? $this->model->findOrFail($id) : $this->model; |
114
|
2 |
|
$form = FormBuilder::create(get_class($this->form), [ |
115
|
2 |
|
'model' => $model |
116
|
|
|
]); |
117
|
|
|
|
118
|
2 |
|
$form_content = view('form-builder::form.components.formgenerator.info', [ |
119
|
2 |
|
'form' => $form, |
120
|
2 |
|
'id' => $id, |
121
|
2 |
|
'route' => $this->getControllerNameForAction().'@', |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
|
125
|
2 |
|
return view('form-builder::form.state.form', [ |
126
|
2 |
|
'form'=>$form_content |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// ------------------------------------------------------------------------------------------------ |
132
|
|
|
|
133
|
18 |
|
protected function getControllerNameForAction() { |
134
|
|
|
|
135
|
18 |
|
$action = explode('@', \Route::currentRouteAction()); |
136
|
|
|
|
137
|
18 |
|
return '\\'.$action[0]; |
138
|
|
|
} |
139
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.