1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FormSupport.php |
4
|
|
|
* Copyright (c) 2019 [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
|
|
|
|
22
|
|
|
namespace FireflyIII\Support\Form; |
23
|
|
|
|
24
|
|
|
use Carbon\Carbon; |
25
|
|
|
use Exception; |
26
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
27
|
|
|
use Illuminate\Support\MessageBag; |
28
|
|
|
use Log; |
29
|
|
|
use RuntimeException; |
30
|
|
|
use Throwable; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Trait FormSupport |
34
|
|
|
*/ |
35
|
|
|
trait FormSupport |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return AccountRepositoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected function getAccountRepository(): AccountRepositoryInterface |
43
|
|
|
{ |
44
|
|
|
return app(AccountRepositoryInterface::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Carbon |
49
|
|
|
*/ |
50
|
|
|
protected function getDate(): Carbon |
51
|
|
|
{ |
52
|
|
|
/** @var Carbon $date */ |
53
|
|
|
$date = null; |
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$date = new Carbon; |
56
|
|
|
} catch (Exception $e) { |
57
|
|
|
$e->getMessage(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $date; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @param array $list |
66
|
|
|
* @param mixed $selected |
67
|
|
|
* @param array $options |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function select(string $name, array $list = null, $selected = null, array $options = null): string |
72
|
|
|
{ |
73
|
|
|
$list = $list ?? []; |
74
|
|
|
$label = $this->label($name, $options); |
75
|
|
|
$options = $this->expandOptionArray($name, $label, $options); |
76
|
|
|
$classes = $this->getHolderClasses($name); |
77
|
|
|
$selected = $this->fillFieldValue($name, $selected); |
78
|
|
|
unset($options['autocomplete'], $options['placeholder']); |
79
|
|
|
try { |
80
|
|
|
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); |
81
|
|
|
} catch (Throwable $e) { |
82
|
|
|
Log::debug(sprintf('Could not render select(): %s', $e->getMessage())); |
83
|
|
|
$html = 'Could not render select.'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $html; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $name |
91
|
|
|
* @param $label |
92
|
|
|
* @param array $options |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function expandOptionArray(string $name, $label, array $options = null): array |
97
|
|
|
{ |
98
|
|
|
$options = $options ?? []; |
99
|
|
|
$name = str_replace('[]', '', $name); |
100
|
|
|
$options['class'] = 'form-control'; |
101
|
|
|
$options['id'] = 'ffInput_' . $name; |
102
|
|
|
$options['autocomplete'] = 'off'; |
103
|
|
|
$options['placeholder'] = ucfirst($label); |
104
|
|
|
|
105
|
|
|
return $options; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* @param $value |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
protected function fillFieldValue(string $name, $value = null) |
115
|
|
|
{ |
116
|
|
|
if (app('session')->has('preFilled')) { |
117
|
|
|
$preFilled = session('preFilled'); |
118
|
|
|
$value = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
if (null !== request()->old($name)) { |
123
|
|
|
$value = request()->old($name); |
124
|
|
|
} |
125
|
|
|
} catch (RuntimeException $e) { |
126
|
|
|
// don't care about session errors. |
127
|
|
|
Log::debug(sprintf('Run time: %s', $e->getMessage())); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($value instanceof Carbon) { |
131
|
|
|
$value = $value->format('Y-m-d'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $name |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function getHolderClasses(string $name): string |
143
|
|
|
{ |
144
|
|
|
// Get errors from session: |
145
|
|
|
/** @var MessageBag $errors */ |
146
|
|
|
$errors = session('errors'); |
147
|
|
|
$classes = 'form-group'; |
148
|
|
|
|
149
|
|
|
if (null !== $errors && $errors->has($name)) { |
150
|
|
|
$classes = 'form-group has-error has-feedback'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $classes; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $name |
158
|
|
|
* @param $options |
159
|
|
|
* |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
protected function label(string $name, array $options = null): string |
163
|
|
|
{ |
164
|
|
|
$options = $options ?? []; |
165
|
|
|
if (isset($options['label'])) { |
166
|
|
|
return $options['label']; |
167
|
|
|
} |
168
|
|
|
$name = str_replace('[]', '', $name); |
169
|
|
|
|
170
|
|
|
return (string)trans('form.' . $name); |
171
|
|
|
} |
172
|
|
|
} |