1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Form helpers. |
18
|
|
|
*/ |
19
|
|
|
class FormHelper extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Hidden CSRF token field. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function csrf() |
27
|
|
|
{ |
28
|
|
|
return '<input type="hidden" name="csrf_token" value="'.$this->token->getCSRFToken().'"/>'; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Display a hidden form field. |
33
|
|
|
* |
34
|
|
|
* @param string $name Field name |
35
|
|
|
* @param array $values Form values |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function hidden($name, array $values = []) |
40
|
|
|
{ |
41
|
|
|
return '<input type="hidden" name="'.$name.'" id="form-'.$name.'" '.$this->formValue($values, $name).'/>'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display a select field. |
46
|
|
|
* |
47
|
|
|
* @param string $name Field name |
48
|
|
|
* @param array $options Options |
49
|
|
|
* @param array $values Form values |
50
|
|
|
* @param array $errors Form errors |
51
|
|
|
* @param array $attributes |
52
|
|
|
* @param string $class CSS class |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function select($name, array $options, array $values = [], array $errors = [], array $attributes = [], $class = '') |
57
|
|
|
{ |
58
|
|
|
$html = '<select name="'.$name.'" id="form-'.$name.'" class="'.$class.'" '.implode(' ', $attributes).'>'; |
59
|
|
|
|
60
|
|
|
foreach ($options as $id => $value) { |
61
|
|
|
$html .= '<option value="'.$this->helper->text->e($id).'"'; |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (isset($values->$name) && $id == $values->$name) { |
64
|
|
|
$html .= ' selected="selected"'; |
65
|
|
|
} |
66
|
|
|
if (isset($values[$name]) && $id == $values[$name]) { |
67
|
|
|
$html .= ' selected="selected"'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$html .= '>'.$this->helper->text->e($value).'</option>'; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$html .= '</select>'; |
74
|
|
|
$html .= $this->errorList($errors, $name); |
75
|
|
|
|
76
|
|
|
return $html; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Display a radio field group. |
81
|
|
|
* |
82
|
|
|
* @param string $name Field name |
83
|
|
|
* @param array $options Options |
84
|
|
|
* @param array $values Form values |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function radios($name, array $options, array $values = []) |
89
|
|
|
{ |
90
|
|
|
$html = ''; |
91
|
|
|
|
92
|
|
|
foreach ($options as $value => $label) { |
93
|
|
|
$html .= $this->radio($name, $label, $value, isset($values[$name]) && $values[$name] == $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $html; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Display a radio field. |
101
|
|
|
* |
102
|
|
|
* @param string $name Field name |
103
|
|
|
* @param string $label Form label |
104
|
|
|
* @param string $value Form value |
105
|
|
|
* @param bool $selected Field selected or not |
106
|
|
|
* @param string $class CSS class |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function radio($name, $label, $value, $selected = false, $class = '') |
111
|
|
|
{ |
112
|
|
|
return '<label><input type="radio" name="'.$name.'" class="'.$class.'" value="'.$this->helper->text->e($value).'" '.($selected ? 'checked="checked"' : '').'> '.$this->helper->text->e($label).'</label>'; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Display a checkboxes group. |
117
|
|
|
* |
118
|
|
|
* @param string $name Field name |
119
|
|
|
* @param array $options Options |
120
|
|
|
* @param array $values Form values |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function checkboxes($name, array $options, array $values = []) |
125
|
|
|
{ |
126
|
|
|
$html = ''; |
127
|
|
|
|
128
|
|
|
foreach ($options as $value => $label) { |
129
|
|
|
$html .= $this->checkbox($name.'['.$value.']', $label, $value, isset($values[$name]) && in_array($value, $values[$name])); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $html; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Display a checkbox field. |
137
|
|
|
* |
138
|
|
|
* @param string $name Field name |
139
|
|
|
* @param string $label Form label |
140
|
|
|
* @param string $value Form value |
141
|
|
|
* @param bool $checked Field selected or not |
142
|
|
|
* @param string $class CSS class |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function checkbox($name, $label, $value, $checked = false, $class = '') |
147
|
|
|
{ |
148
|
|
|
return '<label><input type="checkbox" name="'.$name.'" class="'.$class.'" value="'.$this->helper->text->e($value).'" '.($checked ? 'checked="checked"' : '').'> '.$this->helper->text->e($label).'</label>'; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Display a form label. |
153
|
|
|
* |
154
|
|
|
* @param string $name Field name |
155
|
|
|
* @param string $label Form label |
156
|
|
|
* @param array $attributes HTML attributes |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function label($label, $name, array $attributes = []) |
161
|
|
|
{ |
162
|
|
|
return '<label for="form-'.$name.'" '.implode(' ', $attributes).'>'.$this->helper->text->e($label).'</label>'; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Display a textarea. |
167
|
|
|
* |
168
|
|
|
* @param string $name Field name |
169
|
|
|
* @param array $values Form values |
170
|
|
|
* @param array $errors Form errors |
171
|
|
|
* @param array $attributes HTML attributes |
172
|
|
|
* @param string $class CSS class |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function textarea($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
177
|
|
|
{ |
178
|
|
|
$class .= $this->errorClass($errors, $name); |
179
|
|
|
|
180
|
|
|
$html = '<textarea name="'.$name.'" id="form-'.$name.'" class="'.$class.'" '; |
181
|
|
|
$html .= implode(' ', $attributes).'>'; |
182
|
|
|
$html .= isset($values[$name]) ? $this->helper->text->e($values[$name]) : ''; |
|
|
|
|
183
|
|
|
$html .= '</textarea>'; |
184
|
|
|
$html .= $this->errorList($errors, $name); |
185
|
|
|
|
186
|
|
|
return $html; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Display a markdown editor. |
191
|
|
|
* |
192
|
|
|
* @param string $name Field name |
193
|
|
|
* @param array $values Form values |
194
|
|
|
* @param array $errors Form errors |
195
|
|
|
* @param array $attributes |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function textEditor($name, $values = [], array $errors = [], array $attributes = []) |
200
|
|
|
{ |
201
|
|
|
$params = [ |
202
|
|
|
'name' => $name, |
203
|
|
|
'text' => isset($values[$name]) ? $values[$name] : '', |
204
|
|
|
'css' => $this->errorClass($errors, $name), |
205
|
|
|
'required' => isset($attributes['required']) && $attributes['required'], |
206
|
|
|
'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1', |
207
|
|
|
'labelPreview' => t('Preview'), |
208
|
|
|
'labelWrite' => t('Write'), |
209
|
|
|
'placeholder' => isset($attributes['placeholder']) ? $attributes['placeholder'] : t('Write your text in Markdown'), |
210
|
|
|
]; |
211
|
|
|
$html = '<div class="js-text-editor" data-params=\''.json_encode($params, JSON_HEX_APOS).'\'></div>'; |
212
|
|
|
$html .= $this->errorList($errors, $name); |
213
|
|
|
|
214
|
|
|
return $html; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Display file field. |
219
|
|
|
* |
220
|
|
|
* @param string $name |
221
|
|
|
* @param array $errors |
222
|
|
|
* @param bool $multiple |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function file($name, array $errors = [], $multiple = false) |
227
|
|
|
{ |
228
|
|
|
$html = '<input type="file" name="'.$name.'" id="form-'.$name.'" '.($multiple ? 'multiple' : '').'>'; |
229
|
|
|
$html .= $this->errorList($errors, $name); |
230
|
|
|
|
231
|
|
|
return $html; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Display a input field. |
236
|
|
|
* |
237
|
|
|
* @param string $type HMTL input tag type |
238
|
|
|
* @param string $name Field name |
239
|
|
|
* @param array $values Form values |
240
|
|
|
* @param array $errors Form errors |
241
|
|
|
* @param array $attributes HTML attributes |
242
|
|
|
* @param string $class CSS class |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function input($type, $name, $values = [], array $errors = [], array $attributes = [], $class = '') |
247
|
|
|
{ |
248
|
|
|
$class .= $this->errorClass($errors, $name); |
249
|
|
|
|
250
|
|
|
$html = '<input type="'.$type.'" name="'.$name.'" id="form-'.$name.'" '.$this->formValue($values, $name).' class="'.$class.'" '; |
251
|
|
|
$html .= implode(' ', $attributes).'>'; |
252
|
|
|
|
253
|
|
|
if (in_array('required', $attributes)) { |
254
|
|
|
$html .= '<span class="form-required">*</span>'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$html .= $this->errorList($errors, $name); |
258
|
|
|
|
259
|
|
|
return $html; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Display a text field. |
264
|
|
|
* |
265
|
|
|
* @param string $name Field name |
266
|
|
|
* @param array $values Form values |
267
|
|
|
* @param array $errors Form errors |
268
|
|
|
* @param array $attributes HTML attributes |
269
|
|
|
* @param string $class CSS class |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function text($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
274
|
|
|
{ |
275
|
|
|
return $this->input('text', $name, $values, $errors, $attributes, $class); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Display a password field. |
280
|
|
|
* |
281
|
|
|
* @param string $name Field name |
282
|
|
|
* @param array $values Form values |
283
|
|
|
* @param array $errors Form errors |
284
|
|
|
* @param array $attributes HTML attributes |
285
|
|
|
* @param string $class CSS class |
286
|
|
|
* |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
public function password($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
290
|
|
|
{ |
291
|
|
|
return $this->input('password', $name, $values, $errors, $attributes, $class); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Display an email field. |
296
|
|
|
* |
297
|
|
|
* @param string $name Field name |
298
|
|
|
* @param array $values Form values |
299
|
|
|
* @param array $errors Form errors |
300
|
|
|
* @param array $attributes HTML attributes |
301
|
|
|
* @param string $class CSS class |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function email($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
306
|
|
|
{ |
307
|
|
|
return $this->input('email', $name, $values, $errors, $attributes, $class); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Display a number field. |
312
|
|
|
* |
313
|
|
|
* @param string $name Field name |
314
|
|
|
* @param array $values Form values |
315
|
|
|
* @param array $errors Form errors |
316
|
|
|
* @param array $attributes HTML attributes |
317
|
|
|
* @param string $class CSS class |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function number($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
322
|
|
|
{ |
323
|
|
|
return $this->input('number', $name, $values, $errors, $attributes, $class); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Display a numeric field (allow decimal number). |
328
|
|
|
* |
329
|
|
|
* @param string $name Field name |
330
|
|
|
* @param array $values Form values |
331
|
|
|
* @param array $errors Form errors |
332
|
|
|
* @param array $attributes HTML attributes |
333
|
|
|
* @param string $class CSS class |
334
|
|
|
* |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
|
|
public function numeric($name, $values = [], array $errors = [], array $attributes = [], $class = '') |
338
|
|
|
{ |
339
|
|
|
return $this->input('text', $name, $values, $errors, $attributes, $class.' form-numeric'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Date field. |
344
|
|
|
* |
345
|
|
|
* @param string $label |
346
|
|
|
* @param string $name |
347
|
|
|
* @param array $values |
348
|
|
|
* @param array $errors |
349
|
|
|
* @param array $attributes |
350
|
|
|
* |
351
|
|
|
* @return string |
352
|
|
|
*/ |
353
|
|
View Code Duplication |
public function date($label, $name, array $values, array $errors = [], array $attributes = []) |
|
|
|
|
354
|
|
|
{ |
355
|
|
|
$userFormat = $this->dateParser->getUserDateFormat(); |
|
|
|
|
356
|
|
|
$values = $this->dateParser->format($values, [$name], $userFormat); |
|
|
|
|
357
|
|
|
$attributes = array_merge(['placeholder="'.date($userFormat).'"'], $attributes); |
358
|
|
|
|
359
|
|
|
return $this->helper->form->label($label, $name). |
|
|
|
|
360
|
|
|
$this->helper->form->text($name, $values, $errors, $attributes, 'form-date'); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Datetime field. |
365
|
|
|
* |
366
|
|
|
* @param string $label |
367
|
|
|
* @param string $name |
368
|
|
|
* @param array $values |
369
|
|
|
* @param array $errors |
370
|
|
|
* @param array $attributes |
371
|
|
|
* |
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
|
View Code Duplication |
public function datetime($label, $name, array $values, array $errors = [], array $attributes = []) |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
$userFormat = $this->dateParser->getUserDateTimeFormat(); |
|
|
|
|
377
|
|
|
$values = $this->dateParser->format($values, [$name], $userFormat); |
|
|
|
|
378
|
|
|
$attributes = array_merge(['placeholder="'.date($userFormat).'"'], $attributes); |
379
|
|
|
|
380
|
|
|
return $this->helper->form->label($label, $name). |
|
|
|
|
381
|
|
|
$this->helper->form->text($name, $values, $errors, $attributes, 'form-datetime'); |
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Display the form error class. |
386
|
|
|
* |
387
|
|
|
* @param array $errors Error list |
388
|
|
|
* @param string $name Field name |
389
|
|
|
* |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
|
|
private function errorClass(array $errors, $name) |
393
|
|
|
{ |
394
|
|
|
return !isset($errors[$name]) ? '' : ' form-error'; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Display a list of form errors. |
399
|
|
|
* |
400
|
|
|
* @param array $errors List of errors |
401
|
|
|
* @param string $name Field name |
402
|
|
|
* |
403
|
|
|
* @return string |
404
|
|
|
*/ |
405
|
|
|
private function errorList(array $errors, $name) |
406
|
|
|
{ |
407
|
|
|
$html = ''; |
408
|
|
|
|
409
|
|
|
if (isset($errors[$name])) { |
410
|
|
|
$html .= '<ul class="form-errors">'; |
411
|
|
|
|
412
|
|
|
foreach ($errors[$name] as $error) { |
413
|
|
|
$html .= '<li>'.$this->helper->text->e($error).'</li>'; |
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
$html .= '</ul>'; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
return $html; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Get an escaped form value. |
424
|
|
|
* |
425
|
|
|
* @param mixed $values Values |
426
|
|
|
* @param string $name Field name |
427
|
|
|
* |
428
|
|
|
* @return string |
429
|
|
|
*/ |
430
|
|
|
private function formValue($values, $name) |
431
|
|
|
{ |
432
|
|
|
if (isset($values->$name)) { |
433
|
|
|
return 'value="'.$this->helper->text->e($values->$name).'"'; |
|
|
|
|
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
return isset($values[$name]) ? 'value="'.$this->helper->text->e($values[$name]).'"' : ''; |
|
|
|
|
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.