1 | <?php namespace Distilleries\Expendable\States; |
||
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 | 72 | protected function isTranslatableModel() |
|
22 | |||
23 | 48 | protected function findAutoDetectTranslation($id, $orfail = true) |
|
24 | { |
||
25 | 48 | if ($orfail) { |
|
26 | 48 | if ($this->isTranslatableModel()) { |
|
27 | 8 | return $this->model->withoutTranslation()->findOrFail($id); |
|
28 | } else { |
||
29 | 40 | 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 | 32 | public function getEdit($id = '') |
|
46 | { |
||
47 | 32 | $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
|
48 | 32 | $form = FormBuilder::create(get_class($this->form), [ |
|
49 | 8 | 'model' => $model |
|
50 | 24 | ]); |
|
51 | |||
52 | |||
53 | 32 | if ($this->isTranslatableModel()) { |
|
54 | 4 | $local_overide = $this->model->getIso($model->getTable(), $model->id); |
|
55 | 4 | if (!empty($local_overide)) { |
|
56 | 4 | $form->add('local_override', 'hidden', ['default_value' => $local_overide]); |
|
57 | 3 | } |
|
58 | 3 | } |
|
59 | |||
60 | 32 | $form_content = view('form-builder::form.components.formgenerator.full', [ |
|
61 | 8 | 'form' => $form |
|
62 | 24 | ]); |
|
63 | |||
64 | 32 | $this->layoutManager->add([ |
|
65 | 32 | 'content' => view('expendable::admin.form.state.form', [ |
|
66 | 8 | 'form' => $form_content |
|
67 | 24 | ]) |
|
68 | 24 | ]); |
|
69 | |||
70 | 32 | 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 | 3 | public function postTranslation(Request $request) |
|
138 | |||
139 | // ------------------------------------------------------------------------------------------------ |
||
140 | |||
141 | 44 | public function postEdit(Request $request) |
|
173 | |||
174 | // ------------------------------------------------------------------------------------------------ |
||
175 | |||
176 | 20 | protected function dataToSave(Request $request) |
|
180 | |||
181 | // ------------------------------------------------------------------------------------------------ |
||
182 | |||
183 | 20 | protected function beforeSave() |
|
187 | |||
188 | // ------------------------------------------------------------------------------------------------ |
||
189 | |||
190 | 20 | protected function afterSave() |
|
194 | |||
195 | |||
196 | // ------------------------------------------------------------------------------------------------ |
||
197 | |||
198 | 20 | protected function save($data, Request $request) |
|
199 | { |
||
200 | |||
201 | 20 | $primary = $request->get($this->model->getKeyName()); |
|
202 | 20 | if ($primary !== "0" && empty($primary)) { |
|
203 | 20 | $this->model = $this->model->create($data); |
|
204 | 15 | } else { |
|
205 | $this->model = $this->findAutoDetectTranslation($primary, false); |
||
206 | $this->model->update($data); |
||
207 | } |
||
208 | |||
209 | 20 | return $this->afterSave(); |
|
210 | } |
||
211 | |||
212 | // ------------------------------------------------------------------------------------------------ |
||
213 | |||
214 | 4 | protected function saveTranslation($translation_iso, $translation_id_source = 0) |
|
218 | |||
219 | |||
220 | // ------------------------------------------------------------------------------------------------ |
||
221 | |||
222 | 20 | public function getView($id) |
|
223 | { |
||
224 | |||
225 | 20 | $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
|
226 | 20 | $form = FormBuilder::create(get_class($this->form), [ |
|
227 | 5 | 'model' => $model |
|
228 | 15 | ]); |
|
229 | |||
230 | 20 | $form_content = view('form-builder::form.components.formgenerator.info', [ |
|
231 | 20 | 'form' => $form, |
|
232 | 20 | 'id' => $id, |
|
233 | 20 | 'route' => $this->getControllerNameForAction() . '@', |
|
234 | 15 | ]); |
|
235 | |||
236 | 20 | $this->layoutManager->add([ |
|
237 | 20 | 'content' => view('expendable::admin.form.state.form', [ |
|
238 | 5 | 'form' => $form_content |
|
239 | 15 | ]) |
|
240 | 15 | ]); |
|
241 | |||
242 | 20 | return $this->layoutManager->render(); |
|
243 | |||
244 | } |
||
245 | |||
246 | // ------------------------------------------------------------------------------------------------ |
||
247 | |||
248 | 40 | protected function getControllerNameForAction() |
|
255 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: