/
Form.php
695 lines (647 loc) · 28.3 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<?php
namespace Concrete\Core\Form\Service;
use Concrete\Core\Application\Application;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Localization\Service\CountryList;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Concrete\Core\Utility\Service\Arrays as ArraysService;
use Concrete\Core\Utility\Service\Text as TextService;
/**
* Helpful functions for working with forms. Includes HTML input tags and the like.
*
* \@package Helpers
*
* @category Concrete
*
* @author Andrew Embler <andrew@concrete5.org>
* @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
class Form
{
/**
* Internal counter used to generate unique IDs for radio inputs with the same name.
*
* @var int
*/
protected $radioIndex = 1;
/**
* Internal counter used to generate unique IDs for select inputs with the same name.
*
* @var int
*/
protected $selectIndex = 1;
/**
* The Application instance.
*
* @var Application
*/
protected $app;
/**
* The text service instance.
*
* @var TextService
*/
protected $th;
/**
* Arrays helper instance.
*
* @var ArraysService
*/
protected $ah;
/**
* Initialize the instance.
*
* @param Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
$this->th = $this->app->make(TextService::class);
$this->ah = $this->app->make(ArraysService::class);
}
/**
* Returns an action suitable for including in a form action property.
*
* @param string $action
* @param string $task
*/
public function action($action, $task = null)
{
return $this->app->make(ResolverManagerInterface::class)->resolve(func_get_args());
}
/**
* Creates a submit button.
*
* @param string $key the name/id of the element
* @param string $value The value of the element
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param string $additionalClasses list of additional space-separated CSS class names
*
* @return string
*/
public function submit($key, $value, $miscFields = [], $additionalClasses = '')
{
return '<input type="submit"' . $this->parseMiscFields('btn ccm-input-submit ' . $additionalClasses, $miscFields) . ' id="' . $key . '" name="' . $key . '" value="' . $value . '" />';
}
/**
* Creates a button.
*
* @param string $key the name/id of the element
* @param string $value The value of the element
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param string $additionalClasses list of additional space-separated CSS class names
*
* @return string
*/
public function button($key, $value, $miscFields = [], $additionalClasses = '')
{
return '<input type="button"' . $this->parseMiscFields('btn ccm-input-button ' . $additionalClasses, $miscFields) . ' id="' . $key . '" name="' . $key . '" value="' . $value . '" />';
}
/**
* Creates a label tag.
*
* @param string $forFieldID the id of the associated element
* @param string $innerHTML the inner html of the label
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function label($forFieldID, $innerHTML, $miscFields = [])
{
return '<label for="' . $forFieldID . '"' . $this->parseMiscFields('control-label ', $miscFields) . '>' . $innerHTML . '</label>';
}
/**
* Creates a file input element.
*
* @param string $key the name/id of the element
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function file($key, $miscFields = [])
{
return '<input type="file" id="' . $key . '" name="' . $key . '" value=""' . $this->parseMiscFields('form-control', $miscFields) . ' />';
}
/**
* Creates a hidden form field.
*
* @param string $key the name/id of the element
* @param string $value the value of the element (overriden if we received some data in POST or GET)
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function hidden($key, $value = null, $miscFields = [])
{
$requestValue = $this->getRequestValue($key);
if ($requestValue !== false && (!is_array($requestValue))) {
$value = $requestValue;
}
return '<input type="hidden" id="' . $key . '" name="' . $key . '"' . $this->parseMiscFields('', $miscFields) . ' value="' . $value . '" />';
}
/**
* Generates a checkbox.
*
* @param string $key The name/id of the element. It should end with '[]' if it's to return an array on submit.
* @param string $value String value sent to server, if checkbox is checked, on submit
* @param string $isChecked "Checked" value (subject to be overridden by $_REQUEST). Checkbox is checked if value is true (string). Note that 'false' (string) evaluates to true (boolean)!
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function checkbox($key, $value, $isChecked = false, $miscFields = [])
{
if (substr($key, -2) == '[]') {
$_field = substr($key, 0, -2);
$id = $_field . '_' . $value;
} else {
$_field = $key;
$id = $key;
}
$checked = false;
if ($isChecked && \Request::request($_field) === null && !\Request::isPost()) {
$checked = true;
} else {
$requestValue = $this->getRequestValue($key);
if ($requestValue !== false) {
if (is_array($requestValue)) {
if (in_array($value, $requestValue)) {
$checked = true;
}
} elseif ($requestValue == $value) {
$checked = true;
}
}
}
$checked = $checked ? ' checked="checked"' : '';
return '<input type="checkbox" id="' . $id . '" name="' . $key . '"' . $this->parseMiscFields('ccm-input-checkbox', $miscFields) . ' value="' . $value . '"' . $checked . ' />';
}
/**
* Creates a textarea field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function textarea($key, $valueOrMiscFields = '', $miscFields = [])
{
if (is_array($valueOrMiscFields)) {
$value = '';
$miscFields = $valueOrMiscFields;
} else {
$value = $valueOrMiscFields;
}
$requestValue = $this->getRequestValue($key);
if (is_string($requestValue)) {
$value = $requestValue;
}
return '<textarea id="' . $key . '" name="' . $key . '"' . $this->parseMiscFields('form-control', $miscFields) . '>' . $value . '</textarea>';
}
/**
* Generates a radio button.
*
* @param string $key the name of the element (its id will start with $key but will have a progressive unique number added)
* @param string $value the value of the radio button
* @param string|array $checkedValueOrMiscFields the value of the element (if it should be initially checked) or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $checkedValueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function radio($key, $value, $checkedValueOrMiscFields = '', $miscFields = [])
{
if (is_array($checkedValueOrMiscFields)) {
$checkedValue = '';
$miscFields = $checkedValueOrMiscFields;
} else {
$checkedValue = $checkedValueOrMiscFields;
}
$checked = false;
$requestValue = $this->getRequestValue($key);
if ($requestValue !== false) {
if ($requestValue == $value) {
$checked = true;
}
} else {
if ($checkedValue == $value) {
$checked = true;
}
}
$id = null;
if (isset($miscFields['id'])) {
$id = $miscFields['id'];
unset($miscFields['id']);
}
$id = $id ?: $key . $this->radioIndex;
$str = '<input type="radio" id="' . $id . '" name="' . $key . '" value="' . $value . '"';
$str .= $this->parseMiscFields('ccm-input-radio', $miscFields);
if ($checked) {
$str .= ' checked="checked"';
}
$str .= ' />';
++$this->radioIndex;
return $str;
}
/**
* Checks the request (first POST then GET) based on the key passed.
* If $key denotes an array (eg akID[34]['value']) we'll turn the key into arrays if the key has text versions of [ and ] in it
* If the result is a string, it'll be escaped (with htmlspecialchars).
*
* @param string $key the name of the field to be checked
* @param string $type 'post' to check in POST data, other values to check in GET data
*
* @return false|array|string returns an array if $key denotes an array and we received that data, a string if $key is the name of a received data, false if $key is not found in the received data
*/
public function getRequestValue($key)
{
$result = $this->processRequestValue($key, 'post');
if ($result === false) {
$result = $this->processRequestValue($key, 'get');
}
return $result;
}
/**
* Renders a text input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function text($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'text', $valueOrMiscFields, $miscFields);
}
/**
* Renders a number input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function number($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'number', $valueOrMiscFields, $miscFields);
}
/**
* Renders an email input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function email($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'email', $valueOrMiscFields, $miscFields);
}
/**
* Renders a telephone input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function telephone($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'tel', $valueOrMiscFields, $miscFields);
}
/**
* Renders a URL input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function url($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'url', $valueOrMiscFields, $miscFields);
}
/**
* Renders a search input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function search($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'search', $valueOrMiscFields, $miscFields);
}
/**
* Renders a select field.
*
* @param string $key The name of the element. If $key denotes an array, the ID will start with $key but will have a progressive unique number added; if $key does not denotes an array, the ID attribute will be $key.
* @param array $optionValues an associative array of key => display
* @param string|array $valueOrMiscFields the value of the field to be selected or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return $html
*/
public function select($key, $optionValues, $valueOrMiscFields = '', $miscFields = [])
{
if (!is_array($optionValues)) {
$optionValues = [];
}
if (is_array($valueOrMiscFields)) {
$selectedValue = '';
$miscFields = $valueOrMiscFields;
} else {
$selectedValue = (string) $valueOrMiscFields;
}
if ($selectedValue !== '') {
$miscFields['ccm-passed-value'] = $selectedValue;
}
$requestValue = $this->getRequestValue($key);
if (is_array($requestValue) && isset($requestValue[0]) && is_string($requestValue[0])) {
$selectedValue = (string) $requestValue[0];
} elseif ($requestValue !== false) {
if (!is_array($requestValue)) {
$selectedValue = (string) $requestValue;
} else {
$selectedValue = '';
}
}
if (substr($key, -2) == '[]') {
$_key = substr($key, 0, -2);
$id = $_key . $this->selectIndex;
++$this->selectIndex;
} else {
$id = $key;
}
$str = '<select id="' . $id . '" name="' . $key . '"' . $this->parseMiscFields('form-control', $miscFields) . '>';
foreach ($optionValues as $k => $text) {
if (is_array($text)) {
$str .= '<optgroup label="' . h($k) . '">';
foreach ($text as $optionValue => $optionText) {
$str .= '<option value="' . h($optionValue) . '"';
if ((string) $optionValue === (string) $selectedValue) {
$str .= ' selected="selected"';
}
$str .= '>' . h($optionText) . '</option>';
}
$str .= '</optgroup>';
} else {
$str .= '<option value="' . $k . '"';
if ((string) $k === (string) $selectedValue) {
$str .= ' selected="selected"';
}
$str .= '>' . $text . '</option>';
}
}
$str .= '</select>';
return $str;
}
/**
* Renders a select menu to choose a Country.
*
* @param string $key The name of the element. If $key denotes an array, the ID will start with $key but will have a progressive unique number added; if $key does not denotes an array, the ID attribute will be $key.
* @param string $selectedCountryCode the code of the Country to be initially selected
* @param array $configuration Configuration options. Supported keys are:
* - 'required': do users must choose a Country?
* - 'allowedCountries': an array containing a list of acceptable Country codes. If not set, all the countries will be selectable.
* - 'linkStateProvinceField': set to true to look for text fields that have a "data-countryfield" attribute with the same value as this Country field name (updating the Country select will automatically update the State/Province list).
* @param array $miscFields Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*/
public function selectCountry($key, $selectedCountryCode = '', array $configuration = [], array $miscFields = [])
{
$configuration += [
'required' => false,
'allowedCountries' => null,
'linkStateProvinceField' => false,
];
$allCountries = $this->app->make(CountryList::class)->getCountries();
if (is_array($configuration['allowedCountries'])) {
$allCountries = array_intersect_key($allCountries, array_flip($configuration['allowedCountries']));
}
// Fix the selected Country code specified in the code
if ($configuration['required'] && count($allCountries) === 1) {
$selectedCountryCode = key($allCountries);
} else {
$selectedCountryCode = (string) $selectedCountryCode;
if ($selectedCountryCode !== '' && !isset($allCountries[$selectedCountryCode])) {
$selectedCountryCode = '';
}
}
// Fix the Country code received via the current request
$requestValue = $this->getRequestValue($key);
if (is_array($requestValue)) {
$requestValue = (string) $requestValue[0];
} elseif ($requestValue !== false && !is_string($requestValue)) {
$requestValue = '';
}
if ($requestValue !== false && !isset($allCountries[$requestValue])) {
$requestValue = '';
}
if ($requestValue !== false) {
$selectedOption = $requestValue;
} else {
$selectedOption = $selectedCountryCode;
}
if (substr($key, -2) === '[]') {
$id = substr($key, 0, -2) . $this->selectIndex;
++$this->selectIndex;
} else {
$id = $key;
}
if ($selectedCountryCode === '' || !$configuration['required']) {
$optionValues = ['' => ''];
} else {
$optionValues = [];
}
$optionValues += $allCountries;
if ($selectedCountryCode !== '') {
$miscFields['ccm-passed-value'] = $selectedCountryCode;
}
$str = '<select id="' . $id . '" name="' . $key . '"' . $this->parseMiscFields('form-control', $miscFields) . '>';
foreach ($optionValues as $k => $text) {
$str .= '<option value="' . h($k) . '"';
if ((string) $k === (string) $selectedOption) {
$str .= ' selected="selected"';
}
$str .= '>' . h($text) . '</option>';
}
$str .= '</select>';
if ($configuration['linkStateProvinceField']) {
$escapedID = preg_replace('/[!"#$%&\'()*+,.\\/:;<=>?@\\[\\]^`{|}~\\\\]/', '\\\\$0', $id);
$r = ResponseAssetGroup::get();
$r->requireAsset('core/country-stateprovince-link');
$str .= '<script>';
$str .= '$(document).ready(function() { ccmCountryStateprovinceLink.withCountryField($(' . json_encode('#' . $escapedID) . ')); });';
$str .= '</script>';
}
return $str;
}
/**
* Renders a multiple select box.
*
* @param string $key The ID of the element. The name attribute will be $key followed by '[].
* @param array $optionValues Hash array with name/value as the select's option value/text
* @param array|string $defaultValues Default value(s) which match with the option values; overridden by $_REQUEST
* @param array $miscFields additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return $html
*/
public function selectMultiple($key, $optionValues, $defaultValues = false, $miscFields = [])
{
$requestValue = $this->getRequestValue($key . '[]');
if ($requestValue !== false) {
$selectedValues = $requestValue;
} else {
$selectedValues = $defaultValues;
}
if (!is_array($selectedValues)) {
if (isset($selectedValues) && ($selectedValues !== false)) {
$selectedValues = (array) $selectedValues;
} else {
$selectedValues = [];
}
}
if (!is_array($optionValues)) {
$optionValues = [];
}
$str = "<select id=\"$key\" name=\"{$key}[]\" multiple=\"multiple\"" . $this->parseMiscFields('form-control', $miscFields) . '>';
foreach ($optionValues as $k => $text) {
$str .= '<option value="' . $k . '"';
if (in_array($k, $selectedValues)) {
$str .= ' selected="selected"';
}
$str .= '>' . $text . '</option>';
}
$str .= '</select>';
return $str;
}
/**
* Renders a password input field.
*
* @param string $key the name/id of the element
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
public function password($key, $valueOrMiscFields = '', $miscFields = [])
{
return $this->inputType($key, 'password', $valueOrMiscFields, $miscFields);
}
/**
* Generates HTML code that can be added at the beginning of a form to disable username/password autocompletion.
*
* @return string
*/
public function getAutocompletionDisabler()
{
$id = str_replace('.', '_', uniqid('ccm_form_autocompletiondisabler_', true));
$result = <<<EOT
<div id="{$id}" style="position: absolute; top: -1000px; opacity: 0">
<input type="text" id="{$id}_username" tabindex="-1" />
<input type="password" id="{$id}_password" tabindex="-1" />
<script>
(function() {
function removeFake() {
setTimeout(
function() {
var div = document.getElementById('{$id}');
div.parentNode.removeChild(div);
},
500
);
}
if (window.addEventListener) {
window.addEventListener('load', removeFake, false);
} else if (window.attachEvent) {
window.attachEvent('onload', removeFake);
}
})();
</script>
</div>
EOT;
return $result;
}
/**
* Checks the request based on the key passed.
* If $key denotes an array (eg akID[34]['value']) we'll turn the key into arrays if the key has text versions of [ and ] in it
* If the result is a string, it'll be escaped (with htmlspecialchars).
*
* @param string $key the name of the field to be checked
* @param string $type 'post' to check in POST data, other values to check in GET data
*
* @return false|array|string returns an array if $key denotes an array and we received that data, a string if $key is the name of a received data, false if $key is not found in the received data
*/
protected function processRequestValue($key, $type = 'post')
{
$arr = ($type == 'post') ? $_POST : $_GET;
if (strpos($key, '[') !== false) {
$key = str_replace(']', '', $key);
$key = explode('[', trim($key, '['));
$v2 = $this->ah->get($arr, $key);
if (isset($v2)) {
if (is_string($v2)) {
return $this->th->specialchars($v2);
} else {
return $v2;
}
}
} elseif (isset($arr[$key]) && is_string($arr[$key])) {
return $this->th->specialchars($arr[$key]);
}
return false;
}
/**
* Internal function that creates an <input> element of type $type. Handles the messiness of evaluating $valueOrMiscFields. Assigns a default class of ccm-input-$type.
*
* @param string $key the name/id of the element
* @param string $type Accepted value for HTML attribute "type"
* @param string|array $valueOrMiscFields the value of the element or an array with additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
* @param array $miscFields (used if $valueOrMiscFields is not an array) Additional fields appended to the element (a hash array of attributes name => value), possibly including 'class'
*
* @return string
*/
protected function inputType($key, $type, $valueOrMiscFields, $miscFields)
{
if (is_array($valueOrMiscFields)) {
$value = '';
$miscFields = $valueOrMiscFields;
} else {
$value = $valueOrMiscFields;
}
$requestValue = $this->getRequestValue($key);
if (is_string($requestValue)) {
$value = $requestValue;
}
$value = h($value);
return "<input type=\"$type\" id=\"$key\" name=\"$key\" value=\"$value\"" . $this->parseMiscFields("form-control ccm-input-$type", $miscFields) . ' />';
}
/**
* Create an HTML fragment of attribute values, merging any CSS class names as necessary.
*
* @param string $defaultClass Default CSS class name
* @param array $attributes a hash array of attributes (name => value), possibly including 'class'
*
* @return string A fragment of attributes suitable to put inside of an HTML tag
*/
protected function parseMiscFields($defaultClass, $attributes)
{
$attributes = (array) $attributes;
if ($defaultClass) {
$attributes['class'] = trim((isset($attributes['class']) ? $attributes['class'] : '') . ' ' . $defaultClass);
}
$attr = '';
foreach ($attributes as $k => $v) {
$attr .= " $k=\"$v\"";
}
return $attr;
}
}