Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
forms | |
0.00% |
0 / 69 |
|
0.00% |
0 / 9 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFunctions | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
build_template | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
dimension | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
input | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
radio_buttons | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
select | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
textarea | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\template\twig\extension; |
15 | |
16 | use phpbb\template\twig\environment; |
17 | use phpbb\user; |
18 | use Twig\Extension\AbstractExtension; |
19 | use Twig\TwigFunction; |
20 | |
21 | class forms extends AbstractExtension |
22 | { |
23 | /** @var user */ |
24 | protected $user; |
25 | |
26 | /** |
27 | * Constructor. |
28 | * |
29 | * @param user $user User object |
30 | */ |
31 | public function __construct(user $user) |
32 | { |
33 | $this->user = $user; |
34 | } |
35 | |
36 | /** |
37 | * Returns the name of this extension. |
38 | * |
39 | * @return string The extension name |
40 | */ |
41 | public function getName() |
42 | { |
43 | return 'forms'; |
44 | } |
45 | |
46 | /** |
47 | * Returns a list of functions to add to the existing list. |
48 | * |
49 | * @return TwigFunction[] Array of twig functions |
50 | */ |
51 | public function getFunctions(): array |
52 | { |
53 | return [ |
54 | new TwigFunction('FormsBuildTemplate', [$this, 'build_template'], ['needs_environment' => true]), |
55 | new TwigFunction('FormsDimension', [$this, 'dimension'], ['needs_environment' => true]), |
56 | new TwigFunction('FormsInput', [$this, 'input'], ['needs_environment' => true]), |
57 | new TwigFunction('FormsRadioButtons', [$this, 'radio_buttons'], ['needs_environment' => true]), |
58 | new TwigFunction('FormsSelect', [$this, 'select'], ['needs_environment' => true]), |
59 | new TwigFunction('FormsTextarea', [$this, 'textarea'], ['needs_environment' => true]), |
60 | ]; |
61 | } |
62 | |
63 | /** |
64 | * Renders a form template |
65 | * |
66 | * @param environment $environment |
67 | * @param array $form_data |
68 | * |
69 | * @return string Rendered form template |
70 | */ |
71 | public function build_template(environment $environment, array $form_data): string |
72 | { |
73 | try |
74 | { |
75 | return $environment->render('macros/forms/build_template.twig', [ |
76 | 'form_data' => $form_data ?? [], |
77 | ]); |
78 | } |
79 | catch (\Twig\Error\Error $e) |
80 | { |
81 | return $e->getMessage(); |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * Renders form dimension fields |
87 | * |
88 | * @param environment $environment The twig environment |
89 | * @param array $form_data The form data |
90 | * |
91 | * @return string Form dimension fields |
92 | */ |
93 | public function dimension(environment $environment, array $form_data): string |
94 | { |
95 | try |
96 | { |
97 | return $environment->render('macros/forms/dimension.twig', [ |
98 | 'WIDTH' => $form_data['width'], |
99 | 'HEIGHT' => $form_data['height'], |
100 | ]); |
101 | } |
102 | catch (\Twig\Error\Error $e) |
103 | { |
104 | return $e->getMessage(); |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * Renders a form input field |
110 | * |
111 | * @param environment $environment The twig environment |
112 | * @param array $form_data The form data |
113 | * |
114 | * @return string Form input field |
115 | */ |
116 | public function input(environment $environment, array $form_data): string |
117 | { |
118 | try |
119 | { |
120 | return $environment->render('macros/forms/input.twig', [ |
121 | 'CLASS' => (string) ($form_data['class'] ?? ''), |
122 | 'ID' => (string) ($form_data['id'] ?? ''), |
123 | 'DATA' => $form_data['data'] ?? [], |
124 | 'TYPE' => (string) $form_data['type'], |
125 | 'NAME' => (string) $form_data['name'], |
126 | 'SIZE' => (int) ($form_data['size'] ?? 0), |
127 | 'MAXLENGTH' => (int) ($form_data['maxlength'] ?? 0), |
128 | 'MIN' => (int) ($form_data['min'] ?? 0), |
129 | 'MAX' => (int) ($form_data['max'] ?? 0), |
130 | 'STEP' => (int) ($form_data['step'] ?? 0), |
131 | 'CHECKED' => (bool) ($form_data['checked'] ?? false), |
132 | 'DISABLED' => (bool) ($form_data['disabled'] ?? false), |
133 | 'VALUE' => (string) ($form_data['value']), |
134 | ]); |
135 | } |
136 | catch (\Twig\Error\Error $e) |
137 | { |
138 | return $e->getMessage(); |
139 | } |
140 | } |
141 | |
142 | /** |
143 | * Renders form radio buttons |
144 | * |
145 | * @param environment $environment The twig environment |
146 | * @param array $form_data The form data |
147 | * |
148 | * @return string Form radio buttons |
149 | */ |
150 | public function radio_buttons(environment $environment, array $form_data): string |
151 | { |
152 | try |
153 | { |
154 | return $environment->render('macros/forms/radio_buttons.twig', [ |
155 | 'BUTTONS' => $form_data['buttons'], |
156 | ]); |
157 | } |
158 | catch (\Twig\Error\Error $e) |
159 | { |
160 | return $e->getMessage(); |
161 | } |
162 | } |
163 | |
164 | /** |
165 | * Renders a form select field |
166 | * |
167 | * @param environment $environment The twig environment |
168 | * @param array $form_data The form data |
169 | * |
170 | * @return string Form select field |
171 | */ |
172 | public function select(environment $environment, array $form_data): string |
173 | { |
174 | try |
175 | { |
176 | return $environment->render('macros/forms/select.twig', [ |
177 | 'CLASS' => (string) ($form_data['class'] ?? ''), |
178 | 'ID' => (string) ($form_data['id'] ?? ''), |
179 | 'DATA' => $form_data['data'] ?? [], |
180 | 'NAME' => (string) ($form_data['name'] ?? ''), |
181 | 'TOGGLEABLE' => (bool) ($form_data['toggleable'] ?? false), |
182 | 'OPTIONS' => $form_data['options'] ?? [], |
183 | 'GROUP_ONLY' => (bool) ($form_data['group_only'] ?? false), |
184 | 'SIZE' => (int) ($form_data['size'] ?? 0), |
185 | 'MULTIPLE' => (bool) ($form_data['multiple'] ?? false), |
186 | 'ONCHANGE' => (string) ($form_data['onchange'] ?? ''), |
187 | ]); |
188 | } |
189 | catch (\Twig\Error\Error $e) |
190 | { |
191 | return $e->getMessage(); |
192 | } |
193 | } |
194 | |
195 | /** |
196 | * Renders a form textarea field |
197 | * |
198 | * @param environment $environment |
199 | * @param array $form_data |
200 | * |
201 | * @return string Form textarea field |
202 | */ |
203 | public function textarea(environment $environment, array $form_data): string |
204 | { |
205 | try |
206 | { |
207 | return $environment->render('macros/forms/textarea.twig', [ |
208 | 'CLASS' => (string) ($form_data['class'] ?? ''), |
209 | 'ID' => (string) $form_data['id'], |
210 | 'DATA' => $form_data['data'] ?? [], |
211 | 'NAME' => (string) $form_data['name'], |
212 | 'ROWS' => (int) ($form_data['rows'] ?? ''), |
213 | 'COLS' => (int) ($form_data['cols'] ?? ''), |
214 | 'CONTENT' => (string) ($form_data['content'] ?? ''), |
215 | 'PLACEHOLDER' => (string) ($form_data['placeholder'] ?? ''), |
216 | ]); |
217 | } |
218 | catch (\Twig\Error\Error $e) |
219 | { |
220 | return $e->getMessage(); |
221 | } |
222 | } |
223 | } |