1
|
|
|
<?php namespace Distilleries\Expendable\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
|
|
|
|
14
|
|
|
|
15
|
|
|
// ------------------------------------------------------------------------------------------------ |
16
|
|
|
// ------------------------------------------------------------------------------------------------ |
17
|
|
|
// ------------------------------------------------------------------------------------------------ |
18
|
30 |
|
protected function isTranslatableModel() |
19
|
|
|
{ |
20
|
30 |
|
return method_exists($this->model, 'withoutTranslation'); |
21
|
|
|
} |
22
|
|
|
|
23
|
20 |
|
protected function findAutoDetectTranslation($id, $orfail = true) |
24
|
|
|
{ |
25
|
20 |
|
if ($orfail) { |
26
|
20 |
|
if ($this->isTranslatableModel()) { |
27
|
|
|
return $this->model->withoutTranslation()->findOrFail($id); |
28
|
|
|
} else { |
29
|
20 |
|
return $this->model->findOrFail($id); |
30
|
|
|
} |
31
|
|
|
} else { |
32
|
|
|
if ($this->isTranslatableModel()) { |
33
|
|
|
return $this->model->withoutTranslation()->find($id); |
34
|
|
|
} else { |
35
|
|
|
return $this->model->find($id); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return null; |
|
|
|
|
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// ------------------------------------------------------------------------------------------------ |
44
|
|
|
|
45
|
14 |
|
public function getEdit($id = '') |
46
|
|
|
{ |
47
|
14 |
|
$model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
48
|
14 |
|
$form = FormBuilder::create(get_class($this->form), [ |
49
|
14 |
|
'model' => $model |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
|
53
|
14 |
|
if ($this->isTranslatableModel()) { |
54
|
|
|
$local_overide = $this->model->getIso($model->getTable(), $model->id); |
55
|
|
|
if (!empty($local_overide)) { |
56
|
|
|
$form->add('local_override', 'hidden', ['default_value' => $local_overide]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
14 |
|
$form_content = view('form-builder::form.components.formgenerator.full', [ |
61
|
14 |
|
'form' => $form |
62
|
|
|
]); |
63
|
|
|
|
64
|
14 |
|
$this->layoutManager->add([ |
65
|
14 |
|
'content' => view('expendable::admin.form.state.form', [ |
66
|
14 |
|
'form' => $form_content |
67
|
|
|
]) |
68
|
|
|
]); |
69
|
|
|
|
70
|
14 |
|
return $this->layoutManager->render(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// ------------------------------------------------------------------------------------------------ |
74
|
|
|
|
75
|
|
|
public function getTranslation($iso, $id) |
76
|
|
|
{ |
77
|
|
|
|
78
|
|
|
$id_element = $this->model->hasBeenTranslated($this->model->getTable(), $id, $iso); |
79
|
|
|
if (!empty($id_element)) { |
80
|
|
|
return redirect()->to(action($this->getControllerNameForAction().'@getEdit', $id_element)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$model = (!empty($id) || $id === "0") ? $this->model->withoutTranslation()->findOrFail($id) : $this->model; |
84
|
|
|
$form = FormBuilder::create(get_class($this->form), [ |
85
|
|
|
'model' => $model |
86
|
|
|
]) |
87
|
|
|
->remove('id') |
88
|
|
|
->add('translation_iso', 'hidden', ['default_value' => $iso]) |
89
|
|
|
->add('translation_id_source', 'hidden', ['default_value' => $id]) |
90
|
|
|
->add('local_override', 'hidden', ['default_value' => $iso]); |
91
|
|
|
|
92
|
|
|
$form_content = view('form-builder::form.components.formgenerator.full', [ |
93
|
|
|
'form' => $form |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$this->layoutManager->add([ |
97
|
|
|
'content' => view('expendable::admin.form.state.form', [ |
98
|
|
|
'form' => $form_content |
99
|
|
|
]) |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
return $this->layoutManager->render(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// ------------------------------------------------------------------------------------------------ |
106
|
|
|
|
107
|
|
|
public function postTranslation(Request $request) |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
$form = FormBuilder::create(get_class($this->form), [ |
112
|
|
|
'model' => $this->model |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
if ($form->hasError()) { |
117
|
|
|
return $form->validateAndRedirectBack(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$result = $this->beforeSave(); |
|
|
|
|
121
|
|
|
|
122
|
|
|
if ($result != null) { |
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$result = $this->save($this->dataToSave($request), $request); |
|
|
|
|
127
|
|
|
|
128
|
|
|
if ($this->isTranslatableModel()) { |
129
|
|
|
$this->saveTranslation($request->get('translation_iso'), $request->get('translation_id_source')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($result != null) { |
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return redirect()->to(action($this->getControllerNameForAction().'@getIndex')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// ------------------------------------------------------------------------------------------------ |
140
|
|
|
|
141
|
18 |
|
public function postEdit(Request $request) |
142
|
|
|
{ |
143
|
|
|
|
144
|
18 |
|
$form = FormBuilder::create(get_class($this->form), [ |
145
|
18 |
|
'model' => $this->model |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
|
149
|
18 |
|
if ($form->hasError()) { |
150
|
10 |
|
return $form->validateAndRedirectBack(); |
151
|
|
|
} |
152
|
|
|
|
153
|
8 |
|
$result = $this->beforeSave(); |
|
|
|
|
154
|
|
|
|
155
|
8 |
|
if ($result != null) { |
|
|
|
|
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
8 |
|
$result = $this->save($this->dataToSave($request), $request); |
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
8 |
|
if ($this->isTranslatableModel() && !$this->model->hasTranslation($this->model->getTable(), $this->model->getKey())) { |
163
|
|
|
$this->saveTranslation(app()->getLocale()); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
8 |
|
if ($result != null) { |
|
|
|
|
167
|
|
|
return $result; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
return redirect()->to(action($this->getControllerNameForAction().'@getIndex')); |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// ------------------------------------------------------------------------------------------------ |
175
|
|
|
|
176
|
8 |
|
protected function dataToSave(Request $request) |
177
|
|
|
{ |
178
|
8 |
|
return $request->only($this->model->getFillable()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// ------------------------------------------------------------------------------------------------ |
182
|
|
|
|
183
|
8 |
|
protected function beforeSave() |
184
|
|
|
{ |
185
|
8 |
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// ------------------------------------------------------------------------------------------------ |
189
|
|
|
|
190
|
8 |
|
protected function afterSave() |
191
|
|
|
{ |
192
|
8 |
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
// ------------------------------------------------------------------------------------------------ |
197
|
|
|
|
198
|
8 |
|
protected function save($data, Request $request) |
199
|
|
|
{ |
200
|
|
|
|
201
|
8 |
|
$primary = $request->get($this->model->getKeyName()); |
202
|
8 |
|
if ($primary !== "0" && empty($primary)) { |
203
|
8 |
|
$this->model = $this->model->create($data); |
|
|
|
|
204
|
|
|
} else { |
205
|
|
|
$this->model = $this->findAutoDetectTranslation($primary, false); |
206
|
|
|
$this->model->update($data); |
207
|
|
|
} |
208
|
|
|
|
209
|
8 |
|
return $this->afterSave(); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// ------------------------------------------------------------------------------------------------ |
213
|
|
|
|
214
|
|
|
protected function saveTranslation($translation_iso, $translation_id_source = 0) |
215
|
|
|
{ |
216
|
|
|
$this->model->setTranslation($this->model->getKey(), $this->model->getTable(), $translation_id_source, $translation_iso); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
// ------------------------------------------------------------------------------------------------ |
221
|
|
|
|
222
|
8 |
|
public function getView($id) |
223
|
|
|
{ |
224
|
|
|
|
225
|
8 |
|
$model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
226
|
8 |
|
$form = FormBuilder::create(get_class($this->form), [ |
227
|
8 |
|
'model' => $model |
228
|
|
|
]); |
229
|
|
|
|
230
|
8 |
|
$form_content = view('form-builder::form.components.formgenerator.info', [ |
231
|
8 |
|
'form' => $form, |
232
|
8 |
|
'id' => $id, |
233
|
8 |
|
'route' => $this->getControllerNameForAction().'@', |
234
|
|
|
]); |
235
|
|
|
|
236
|
8 |
|
$this->layoutManager->add([ |
237
|
8 |
|
'content' => view('expendable::admin.form.state.form', [ |
238
|
8 |
|
'form' => $form_content |
239
|
|
|
]) |
240
|
|
|
]); |
241
|
|
|
|
242
|
8 |
|
return $this->layoutManager->render(); |
243
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// ------------------------------------------------------------------------------------------------ |
247
|
|
|
|
248
|
16 |
|
protected function getControllerNameForAction() |
249
|
|
|
{ |
250
|
|
|
|
251
|
16 |
|
$action = explode('@', \Route::currentRouteAction()); |
252
|
|
|
|
253
|
16 |
|
return '\\'.$action[0]; |
254
|
|
|
} |
255
|
|
|
} |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.