1 | <?php |
||
2 | /** |
||
3 | * ExpandedForm.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Support; |
||
24 | |||
25 | use Amount as Amt; |
||
26 | use Carbon\Carbon; |
||
27 | use Eloquent; |
||
28 | use FireflyIII\Models\Account; |
||
29 | use FireflyIII\Models\AccountType; |
||
30 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
31 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||
32 | use Illuminate\Support\Collection; |
||
33 | use Illuminate\Support\MessageBag; |
||
34 | use RuntimeException; |
||
35 | use Session; |
||
36 | |||
37 | /** |
||
38 | * Class ExpandedForm. |
||
39 | */ |
||
40 | class ExpandedForm |
||
41 | { |
||
42 | /** |
||
43 | * @param string $name |
||
44 | * @param null $value |
||
45 | * @param array $options |
||
46 | * |
||
47 | * @return string |
||
48 | * @throws \FireflyIII\Exceptions\FireflyException |
||
49 | * @throws \Throwable |
||
50 | */ |
||
51 | public function amount(string $name, $value = null, array $options = []): string |
||
52 | { |
||
53 | return $this->currencyField($name, 'amount', $value, $options); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | * @param null $value |
||
59 | * @param array $options |
||
60 | * |
||
61 | * @return string |
||
62 | * @throws \FireflyIII\Exceptions\FireflyException |
||
63 | * @throws \Throwable |
||
64 | */ |
||
65 | public function amountSmall(string $name, $value = null, array $options = []): string |
||
66 | { |
||
67 | return $this->currencyField($name, 'amount-small', $value, $options); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $name |
||
72 | * @param null $value |
||
73 | * @param array $options |
||
74 | * |
||
75 | * @return string |
||
76 | * @throws \Throwable |
||
77 | */ |
||
78 | public function assetAccountList(string $name, $value = null, array $options = []): string |
||
79 | { |
||
80 | // properties for cache |
||
81 | $cache = new CacheProperties; |
||
82 | $cache->addProperty('exp-form-asset-list'); |
||
83 | $cache->addProperty($name); |
||
84 | $cache->addProperty($value); |
||
85 | $cache->addProperty($options); |
||
86 | |||
87 | if ($cache->has()) { |
||
88 | return $cache->get(); |
||
89 | } |
||
90 | // make repositories |
||
91 | /** @var AccountRepositoryInterface $repository */ |
||
92 | $repository = app(AccountRepositoryInterface::class); |
||
93 | /** @var CurrencyRepositoryInterface $currencyRepos */ |
||
94 | $currencyRepos = app(CurrencyRepositoryInterface::class); |
||
95 | |||
96 | $assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); |
||
97 | $defaultCurrency = app('amount')->getDefaultCurrency(); |
||
98 | $grouped = []; |
||
99 | // group accounts: |
||
100 | /** @var Account $account */ |
||
101 | foreach ($assetAccounts as $account) { |
||
102 | $balance = app('steam')->balance($account, new Carbon); |
||
103 | $currencyId = intval($account->getMeta('currency_id')); |
||
104 | $currency = $currencyRepos->findNull($currencyId); |
||
105 | $role = $account->getMeta('accountRole'); |
||
106 | if (0 === strlen($role)) { |
||
107 | $role = 'no_account_type'; // @codeCoverageIgnore |
||
108 | } |
||
109 | if (is_null($currency)) { |
||
110 | $currency = $defaultCurrency; |
||
111 | } |
||
112 | |||
113 | $key = strval(trans('firefly.opt_group_' . $role)); |
||
114 | $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; |
||
115 | } |
||
116 | $res = $this->select($name, $grouped, $value, $options); |
||
117 | $cache->store($res); |
||
118 | |||
119 | return $res; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param null $value |
||
125 | * @param array $options |
||
126 | * |
||
127 | * @return string |
||
128 | * @throws \FireflyIII\Exceptions\FireflyException |
||
129 | * @throws \Throwable |
||
130 | */ |
||
131 | public function balance(string $name, $value = null, array $options = []): string |
||
132 | { |
||
133 | return $this->currencyField($name, 'balance', $value, $options); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param $name |
||
138 | * @param int $value |
||
139 | * @param null $checked |
||
140 | * @param array $options |
||
141 | * |
||
142 | * @return string |
||
143 | * |
||
144 | * @throws \Throwable |
||
145 | */ |
||
146 | public function checkbox(string $name, $value = 1, $checked = null, $options = []): string |
||
147 | { |
||
148 | $options['checked'] = true === $checked ? true : null; |
||
149 | $label = $this->label($name, $options); |
||
150 | $options = $this->expandOptionArray($name, $label, $options); |
||
151 | $classes = $this->getHolderClasses($name); |
||
152 | $value = $this->fillFieldValue($name, $value); |
||
153 | |||
154 | unset($options['placeholder'], $options['autocomplete'], $options['class']); |
||
155 | |||
156 | $html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
157 | |||
158 | return $html; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param $name |
||
163 | * @param null $value |
||
164 | * @param array $options |
||
165 | * |
||
166 | * @return string |
||
167 | * |
||
168 | * @throws \Throwable |
||
169 | */ |
||
170 | public function date(string $name, $value = null, array $options = []): string |
||
171 | { |
||
172 | $label = $this->label($name, $options); |
||
173 | $options = $this->expandOptionArray($name, $label, $options); |
||
174 | $classes = $this->getHolderClasses($name); |
||
175 | $value = $this->fillFieldValue($name, $value); |
||
176 | unset($options['placeholder']); |
||
177 | $html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
178 | |||
179 | return $html; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param $name |
||
184 | * @param array $options |
||
185 | * |
||
186 | * @return string |
||
187 | * |
||
188 | * @throws \Throwable |
||
189 | */ |
||
190 | public function file(string $name, array $options = []): string |
||
191 | { |
||
192 | $label = $this->label($name, $options); |
||
193 | $options = $this->expandOptionArray($name, $label, $options); |
||
194 | $classes = $this->getHolderClasses($name); |
||
195 | $html = view('form.file', compact('classes', 'name', 'label', 'options'))->render(); |
||
196 | |||
197 | return $html; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param $name |
||
202 | * @param null $value |
||
203 | * @param array $options |
||
204 | * |
||
205 | * @return string |
||
206 | * |
||
207 | * @throws \Throwable |
||
208 | */ |
||
209 | public function integer(string $name, $value = null, array $options = []): string |
||
210 | { |
||
211 | $label = $this->label($name, $options); |
||
212 | $options = $this->expandOptionArray($name, $label, $options); |
||
213 | $classes = $this->getHolderClasses($name); |
||
214 | $value = $this->fillFieldValue($name, $value); |
||
215 | $options['step'] = '1'; |
||
216 | $html = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
217 | |||
218 | return $html; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param $name |
||
223 | * @param null $value |
||
224 | * @param array $options |
||
225 | * |
||
226 | * @return string |
||
227 | * |
||
228 | * @throws \Throwable |
||
229 | */ |
||
230 | public function location(string $name, $value = null, array $options = []): string |
||
231 | { |
||
232 | $label = $this->label($name, $options); |
||
233 | $options = $this->expandOptionArray($name, $label, $options); |
||
234 | $classes = $this->getHolderClasses($name); |
||
235 | $value = $this->fillFieldValue($name, $value); |
||
236 | $html = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
237 | |||
238 | return $html; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Takes any collection and tries to make a sensible select list compatible array of it. |
||
243 | * |
||
244 | * @param \Illuminate\Support\Collection $set |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public function makeSelectList(Collection $set): array |
||
249 | { |
||
250 | $selectList = []; |
||
251 | $fields = ['title', 'name', 'description']; |
||
252 | /** @var Eloquent $entry */ |
||
253 | foreach ($set as $entry) { |
||
254 | $entryId = intval($entry->id); |
||
255 | $title = null; |
||
256 | |||
257 | foreach ($fields as $field) { |
||
258 | if (isset($entry->$field) && null === $title) { |
||
259 | $title = $entry->$field; |
||
260 | } |
||
261 | } |
||
262 | $selectList[$entryId] = $title; |
||
263 | } |
||
264 | |||
265 | return $selectList; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param \Illuminate\Support\Collection $set |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | public function makeSelectListWithEmpty(Collection $set): array |
||
274 | { |
||
275 | $selectList = []; |
||
276 | $selectList[0] = '(none)'; |
||
277 | $fields = ['title', 'name', 'description']; |
||
278 | /** @var Eloquent $entry */ |
||
279 | foreach ($set as $entry) { |
||
280 | $entryId = intval($entry->id); |
||
281 | $title = null; |
||
282 | |||
283 | foreach ($fields as $field) { |
||
284 | if (isset($entry->$field) && null === $title) { |
||
285 | $title = $entry->$field; |
||
286 | } |
||
287 | } |
||
288 | $selectList[$entryId] = $title; |
||
289 | } |
||
290 | |||
291 | return $selectList; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param $name |
||
296 | * @param array $list |
||
297 | * @param null $selected |
||
298 | * @param array $options |
||
299 | * |
||
300 | * @return string |
||
301 | * |
||
302 | * @throws \Throwable |
||
303 | */ |
||
304 | public function multiCheckbox(string $name, array $list = [], $selected = null, array $options = []): string |
||
305 | { |
||
306 | $label = $this->label($name, $options); |
||
307 | $options = $this->expandOptionArray($name, $label, $options); |
||
308 | $classes = $this->getHolderClasses($name); |
||
309 | $selected = $this->fillFieldValue($name, $selected); |
||
310 | |||
311 | unset($options['class']); |
||
312 | $html = view('form.multiCheckbox', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); |
||
313 | |||
314 | return $html; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param $name |
||
319 | * @param array $list |
||
320 | * @param null $selected |
||
321 | * @param array $options |
||
322 | * |
||
323 | * @return string |
||
324 | * |
||
325 | * @throws \Throwable |
||
326 | */ |
||
327 | public function multiRadio(string $name, array $list = [], $selected = null, array $options = []): string |
||
328 | { |
||
329 | $label = $this->label($name, $options); |
||
330 | $options = $this->expandOptionArray($name, $label, $options); |
||
331 | $classes = $this->getHolderClasses($name); |
||
332 | $selected = $this->fillFieldValue($name, $selected); |
||
333 | |||
334 | unset($options['class']); |
||
335 | $html = view('form.multiRadio', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); |
||
336 | |||
337 | return $html; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param string $name |
||
342 | * @param null $value |
||
343 | * @param array $options |
||
344 | * |
||
345 | * @return string |
||
346 | * |
||
347 | * @throws \Throwable |
||
348 | */ |
||
349 | public function nonSelectableAmount(string $name, $value = null, array $options = []): string |
||
350 | { |
||
351 | $label = $this->label($name, $options); |
||
352 | $options = $this->expandOptionArray($name, $label, $options); |
||
353 | $classes = $this->getHolderClasses($name); |
||
354 | $value = $this->fillFieldValue($name, $value); |
||
355 | $options['step'] = 'any'; |
||
356 | $selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
357 | unset($options['currency'], $options['placeholder']); |
||
358 | |||
359 | // make sure value is formatted nicely: |
||
360 | if (null !== $value && '' !== $value) { |
||
361 | $value = round($value, $selectedCurrency->decimal_places); |
||
362 | } |
||
363 | |||
364 | $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render(); |
||
365 | |||
366 | return $html; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @param string $name |
||
371 | * @param null $value |
||
372 | * @param array $options |
||
373 | * |
||
374 | * @return string |
||
375 | * |
||
376 | * @throws \Throwable |
||
377 | */ |
||
378 | public function nonSelectableBalance(string $name, $value = null, array $options = []): string |
||
379 | { |
||
380 | $label = $this->label($name, $options); |
||
381 | $options = $this->expandOptionArray($name, $label, $options); |
||
382 | $classes = $this->getHolderClasses($name); |
||
383 | $value = $this->fillFieldValue($name, $value); |
||
384 | $options['step'] = 'any'; |
||
385 | $selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency(); |
||
386 | unset($options['currency'], $options['placeholder']); |
||
387 | |||
388 | // make sure value is formatted nicely: |
||
389 | if (null !== $value && '' !== $value) { |
||
390 | $decimals = $selectedCurrency->decimal_places ?? 2; |
||
391 | $value = round($value, $decimals); |
||
392 | } |
||
393 | |||
394 | $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render(); |
||
395 | |||
396 | return $html; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @param string $name |
||
401 | * @param null $value |
||
402 | * @param array $options |
||
403 | * |
||
404 | * @return string |
||
405 | * |
||
406 | * @throws \Throwable |
||
407 | */ |
||
408 | public function number(string $name, $value = null, array $options = []): string |
||
409 | { |
||
410 | $label = $this->label($name, $options); |
||
411 | $options = $this->expandOptionArray($name, $label, $options); |
||
412 | $classes = $this->getHolderClasses($name); |
||
413 | $value = $this->fillFieldValue($name, $value); |
||
414 | $options['step'] = 'any'; |
||
415 | unset($options['placeholder']); |
||
416 | |||
417 | $html = view('form.number', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
418 | |||
419 | return $html; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param $type |
||
424 | * @param $name |
||
425 | * |
||
426 | * @return string |
||
427 | * |
||
428 | * @throws \Throwable |
||
429 | */ |
||
430 | public function optionsList(string $type, string $name): string |
||
431 | { |
||
432 | $previousValue = null; |
||
433 | |||
434 | try { |
||
435 | $previousValue = request()->old('post_submit_action'); |
||
436 | } catch (RuntimeException $e) { |
||
437 | // don't care |
||
438 | } |
||
439 | |||
440 | $previousValue = null === $previousValue ? 'store' : $previousValue; |
||
441 | $html = view('form.options', compact('type', 'name', 'previousValue'))->render(); |
||
442 | |||
443 | return $html; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @param $name |
||
448 | * @param array $options |
||
449 | * |
||
450 | * @return string |
||
451 | * |
||
452 | * @throws \Throwable |
||
453 | */ |
||
454 | public function password(string $name, array $options = []): string |
||
455 | { |
||
456 | $label = $this->label($name, $options); |
||
457 | $options = $this->expandOptionArray($name, $label, $options); |
||
458 | $classes = $this->getHolderClasses($name); |
||
459 | $html = view('form.password', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
460 | |||
461 | return $html; |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * @param $name |
||
466 | * @param array $list |
||
467 | * @param null $selected |
||
468 | * @param array $options |
||
469 | * |
||
470 | * @return string |
||
471 | * |
||
472 | * @throws \Throwable |
||
473 | */ |
||
474 | public function select(string $name, array $list = [], $selected = null, array $options = []): string |
||
475 | { |
||
476 | $label = $this->label($name, $options); |
||
477 | $options = $this->expandOptionArray($name, $label, $options); |
||
478 | $classes = $this->getHolderClasses($name); |
||
479 | $selected = $this->fillFieldValue($name, $selected); |
||
480 | unset($options['autocomplete'], $options['placeholder']); |
||
481 | $html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); |
||
482 | |||
483 | return $html; |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * @param $name |
||
488 | * @param null $value |
||
489 | * @param array $options |
||
490 | * |
||
491 | * @return string |
||
492 | * |
||
493 | * @throws \Throwable |
||
494 | */ |
||
495 | public function staticText(string $name, $value, array $options = []): string |
||
496 | { |
||
497 | $label = $this->label($name, $options); |
||
498 | $options = $this->expandOptionArray($name, $label, $options); |
||
499 | $classes = $this->getHolderClasses($name); |
||
500 | $html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
501 | |||
502 | return $html; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @param $name |
||
507 | * @param null $value |
||
508 | * @param array $options |
||
509 | * |
||
510 | * @return string |
||
511 | * |
||
512 | * @throws \Throwable |
||
513 | */ |
||
514 | public function tags(string $name, $value = null, array $options = []): string |
||
515 | { |
||
516 | $label = $this->label($name, $options); |
||
517 | $options = $this->expandOptionArray($name, $label, $options); |
||
518 | $classes = $this->getHolderClasses($name); |
||
519 | $value = $this->fillFieldValue($name, $value); |
||
520 | $options['data-role'] = 'tagsinput'; |
||
521 | $html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
522 | |||
523 | return $html; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param $name |
||
528 | * @param null $value |
||
529 | * @param array $options |
||
530 | * |
||
531 | * @return string |
||
532 | * |
||
533 | * @throws \Throwable |
||
534 | */ |
||
535 | public function text(string $name, $value = null, array $options = []): string |
||
536 | { |
||
537 | $label = $this->label($name, $options); |
||
538 | $options = $this->expandOptionArray($name, $label, $options); |
||
539 | $classes = $this->getHolderClasses($name); |
||
540 | $value = $this->fillFieldValue($name, $value); |
||
541 | $html = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
542 | |||
543 | return $html; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * @param $name |
||
548 | * @param null $value |
||
549 | * @param array $options |
||
550 | * |
||
551 | * @return string |
||
552 | * |
||
553 | * @throws \Throwable |
||
554 | */ |
||
555 | public function textarea(string $name, $value = null, array $options = []): string |
||
556 | { |
||
557 | $label = $this->label($name, $options); |
||
558 | $options = $this->expandOptionArray($name, $label, $options); |
||
559 | $classes = $this->getHolderClasses($name); |
||
560 | $value = $this->fillFieldValue($name, $value); |
||
561 | $options['rows'] = 4; |
||
562 | $html = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render(); |
||
563 | |||
564 | return $html; |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * @param $name |
||
569 | * @param $label |
||
570 | * @param array $options |
||
571 | * |
||
572 | * @return array |
||
573 | */ |
||
574 | protected function expandOptionArray(string $name, $label, array $options): array |
||
575 | { |
||
576 | $name = str_replace('[]', '', $name); |
||
577 | $options['class'] = 'form-control'; |
||
578 | $options['id'] = 'ffInput_' . $name; |
||
579 | $options['autocomplete'] = 'off'; |
||
580 | $options['placeholder'] = ucfirst($label); |
||
581 | |||
582 | return $options; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * @param $name |
||
587 | * @param $value |
||
588 | * |
||
589 | * @return mixed |
||
590 | */ |
||
591 | protected function fillFieldValue(string $name, $value) |
||
592 | { |
||
593 | if (Session::has('preFilled')) { |
||
594 | $preFilled = session('preFilled'); |
||
595 | $value = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value; |
||
596 | } |
||
597 | try { |
||
598 | if (null !== request()->old($name)) { |
||
599 | $value = request()->old($name); |
||
600 | } |
||
601 | } catch (RuntimeException $e) { |
||
602 | // don't care about session errors. |
||
603 | } |
||
604 | if ($value instanceof Carbon) { |
||
605 | $value = $value->format('Y-m-d'); |
||
606 | } |
||
607 | |||
608 | return $value; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * @param $name |
||
613 | * |
||
614 | * @return string |
||
615 | */ |
||
616 | protected function getHolderClasses(string $name): string |
||
617 | { |
||
618 | // Get errors from session: |
||
619 | /** @var MessageBag $errors */ |
||
620 | $errors = session('errors'); |
||
621 | $classes = 'form-group'; |
||
622 | |||
623 | if (null !== $errors && $errors->has($name)) { |
||
624 | $classes = 'form-group has-error has-feedback'; |
||
625 | } |
||
626 | |||
627 | return $classes; |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * @param $name |
||
632 | * @param $options |
||
633 | * |
||
634 | * @return mixed |
||
635 | */ |
||
636 | protected function label(string $name, array $options): string |
||
637 | { |
||
638 | if (isset($options['label'])) { |
||
639 | return $options['label']; |
||
640 | } |
||
641 | $name = str_replace('[]', '', $name); |
||
642 | |||
643 | return strval(trans('form.' . $name)); |
||
644 | } |
||
645 | |||
646 | /** |
||
647 | * @param string $name |
||
648 | * @param string $view |
||
649 | * @param null $value |
||
650 | * @param array $options |
||
651 | * |
||
652 | * @return string |
||
653 | * |
||
654 | * @throws \FireflyIII\Exceptions\FireflyException |
||
655 | * @throws \Throwable |
||
656 | */ |
||
657 | private function currencyField(string $name, string $view, $value = null, array $options = []): string |
||
658 | { |
||
659 | $label = $this->label($name, $options); |
||
660 | $options = $this->expandOptionArray($name, $label, $options); |
||
661 | $classes = $this->getHolderClasses($name); |
||
662 | $value = $this->fillFieldValue($name, $value); |
||
663 | $options['step'] = 'any'; |
||
664 | $defaultCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency(); |
||
665 | $currencies = app('amount')->getAllCurrencies(); |
||
666 | unset($options['currency'], $options['placeholder']); |
||
667 | |||
668 | // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) |
||
669 | $preFilled = session('preFilled'); |
||
670 | $key = 'amount_currency_id_' . $name; |
||
671 | $sentCurrencyId = isset($preFilled[$key]) ? intval($preFilled[$key]) : $defaultCurrency->id; |
||
672 | |||
673 | // find this currency in set of currencies: |
||
674 | foreach ($currencies as $currency) { |
||
675 | if ($currency->id === $sentCurrencyId) { |
||
676 | $defaultCurrency = $currency; |
||
677 | break; |
||
678 | } |
||
679 | } |
||
680 | |||
681 | // make sure value is formatted nicely: |
||
682 | if (null !== $value && '' !== $value) { |
||
683 | $value = round($value, $defaultCurrency->decimal_places); |
||
684 | } |
||
685 | |||
686 | $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); |
||
687 | |||
688 | return $html; |
||
689 | } |
||
690 | } |
||
691 |