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 | 60 | protected function isTranslatableModel() |
|
22 | |||
23 | 40 | protected function findAutoDetectTranslation($id, $orfail = true) |
|
24 | { |
||
25 | 40 | if ($orfail) { |
|
26 | 40 | if ($this->isTranslatableModel()) { |
|
27 | 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 | 28 | public function getEdit($id = '') |
|
46 | { |
||
47 | 28 | $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
|
48 | 28 | $form = FormBuilder::create(get_class($this->form), [ |
|
49 | 28 | 'model' => $model |
|
50 | ]); |
||
51 | |||
52 | |||
53 | 28 | 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 | 28 | $form_content = view('form-builder::form.components.formgenerator.full', [ |
|
61 | 28 | 'form' => $form |
|
62 | ]); |
||
63 | |||
64 | 28 | $this->layoutManager->add([ |
|
65 | 28 | 'content' => view('expendable::admin.form.state.form', [ |
|
66 | 28 | 'form' => $form_content |
|
67 | ]) |
||
68 | ]); |
||
69 | |||
70 | 28 | 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 | 36 | public function postEdit(Request $request) |
|
142 | { |
||
143 | |||
144 | 36 | $form = FormBuilder::create(get_class($this->form), [ |
|
145 | 36 | 'model' => $this->model |
|
146 | ]); |
||
147 | |||
148 | |||
149 | 36 | if ($form->hasError()) { |
|
150 | 20 | return $form->validateAndRedirectBack(); |
|
151 | } |
||
152 | |||
153 | 16 | $result = $this->beforeSave(); |
|
154 | |||
155 | 16 | if ($result != null) { |
|
156 | return $result; |
||
157 | } |
||
158 | |||
159 | 16 | $result = $this->save($this->dataToSave($request), $request); |
|
160 | |||
161 | |||
162 | 16 | if ($this->isTranslatableModel() && !$this->model->hasTranslation($this->model->getTable(), $this->model->getKey())) { |
|
163 | $this->saveTranslation(app()->getLocale()); |
||
164 | } |
||
165 | |||
166 | 16 | if ($result != null) { |
|
167 | return $result; |
||
168 | } |
||
169 | |||
170 | 16 | return redirect()->to(action($this->getControllerNameForAction() . '@getIndex')); |
|
171 | |||
172 | } |
||
173 | |||
174 | // ------------------------------------------------------------------------------------------------ |
||
175 | |||
176 | 16 | protected function dataToSave(Request $request) |
|
180 | |||
181 | // ------------------------------------------------------------------------------------------------ |
||
182 | |||
183 | 16 | protected function beforeSave() |
|
187 | |||
188 | // ------------------------------------------------------------------------------------------------ |
||
189 | |||
190 | 16 | protected function afterSave() |
|
194 | |||
195 | |||
196 | // ------------------------------------------------------------------------------------------------ |
||
197 | |||
198 | 16 | protected function save($data, Request $request) |
|
199 | { |
||
200 | |||
201 | 16 | $primary = $request->get($this->model->getKeyName()); |
|
202 | 16 | if ($primary !== "0" && empty($primary)) { |
|
203 | 16 | $this->model = $this->model->create($data); |
|
204 | } else { |
||
205 | $this->model = $this->findAutoDetectTranslation($primary, false); |
||
206 | $this->model->update($data); |
||
207 | } |
||
208 | |||
209 | 16 | 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 | 16 | public function getView($id) |
|
223 | { |
||
224 | |||
225 | 16 | $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model; |
|
226 | 16 | $form = FormBuilder::create(get_class($this->form), [ |
|
227 | 16 | 'model' => $model |
|
228 | ]); |
||
229 | |||
230 | 16 | $form_content = view('form-builder::form.components.formgenerator.info', [ |
|
231 | 16 | 'form' => $form, |
|
232 | 16 | 'id' => $id, |
|
233 | 16 | 'route' => $this->getControllerNameForAction() . '@', |
|
234 | ]); |
||
235 | |||
236 | 16 | $this->layoutManager->add([ |
|
237 | 16 | 'content' => view('expendable::admin.form.state.form', [ |
|
238 | 16 | 'form' => $form_content |
|
239 | ]) |
||
240 | ]); |
||
241 | |||
242 | 16 | return $this->layoutManager->render(); |
|
243 | |||
244 | } |
||
245 | |||
246 | // ------------------------------------------------------------------------------------------------ |
||
247 | |||
248 | 32 | 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: