|
1
|
|
|
<?php
|
|
2
|
|
|
namespace Suricate;
|
|
3
|
|
|
|
|
4
|
|
|
class FormItem
|
|
5
|
|
|
{
|
|
6
|
|
|
public $objectHtmlProperties = array(
|
|
7
|
|
|
'type',
|
|
8
|
|
|
'name',
|
|
9
|
|
|
'id',
|
|
10
|
|
|
'class',
|
|
11
|
|
|
'value',
|
|
12
|
|
|
'checked',
|
|
13
|
|
|
'rows',
|
|
14
|
|
|
'cols',
|
|
15
|
|
|
'placeholder',
|
|
16
|
|
|
'tabindex',
|
|
17
|
|
|
'accesskey',
|
|
18
|
|
|
'disabled',
|
|
19
|
|
|
'spellcheck',
|
|
20
|
|
|
'events',
|
|
21
|
|
|
'multiple',
|
|
22
|
|
|
'autocomplete',
|
|
23
|
|
|
'autofocus',
|
|
24
|
|
|
'required',
|
|
25
|
|
|
'pattern',
|
|
26
|
|
|
'min',
|
|
27
|
|
|
'step',
|
|
28
|
|
|
'max'
|
|
29
|
|
|
);
|
|
30
|
|
|
public $label;
|
|
31
|
|
|
public $objectHtmlValues = array();
|
|
32
|
|
|
public static $encoding = 'UTF-8';
|
|
33
|
|
|
|
|
34
|
|
|
public function __construct($itemData = array())
|
|
35
|
|
|
{
|
|
36
|
|
|
foreach ($itemData as $itemProperty => $itemValue) {
|
|
37
|
|
|
$this->$itemProperty = $itemValue;
|
|
38
|
|
|
}
|
|
39
|
|
|
}
|
|
40
|
|
|
|
|
41
|
|
|
public function __get($name)
|
|
42
|
|
|
{
|
|
43
|
|
|
if (isset($this->objectHtmlValues[$name])) {
|
|
44
|
|
|
return $this->objectHtmlValues[$name];
|
|
45
|
|
|
} else {
|
|
46
|
|
|
return null;
|
|
47
|
|
|
}
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
public function __set($name, $value)
|
|
51
|
|
|
{
|
|
52
|
|
|
if (in_array($name, $this->objectHtmlProperties)) {
|
|
53
|
|
|
$this->objectHtmlValues[$name] = $value;
|
|
54
|
|
|
}
|
|
55
|
|
|
}
|
|
56
|
|
|
|
|
57
|
|
|
public function __isset($name)
|
|
58
|
|
|
{
|
|
59
|
|
|
return in_array($name, $this->objectHtmlProperties);
|
|
60
|
|
|
}
|
|
61
|
|
|
|
|
62
|
|
|
public static function input($type, $name, $value = null, $label = null, $htmlAttributes = array())
|
|
63
|
|
|
{
|
|
64
|
|
|
$itemData = array();
|
|
65
|
|
|
$itemData['type'] = $type;
|
|
66
|
|
|
$itemData['name'] = $name;
|
|
67
|
|
|
$itemData['value'] = $value;
|
|
68
|
|
|
$itemData['label'] = $label;
|
|
69
|
|
View Code Duplication |
if ($label !== null && !isset($htmlAttributes['id'])) {
|
|
|
|
|
|
|
70
|
|
|
$itemData['id'] = $name;
|
|
71
|
|
|
}
|
|
72
|
|
|
$itemData = array_merge($itemData, $htmlAttributes);
|
|
73
|
|
|
|
|
74
|
|
|
$item = new FormItem($itemData);
|
|
75
|
|
|
|
|
76
|
|
|
$output = $item->renderLabel();
|
|
77
|
|
|
$output .= '<input';
|
|
78
|
|
|
$output .= $item->renderAttributes();
|
|
79
|
|
|
$output .= '/>';
|
|
80
|
|
|
|
|
81
|
|
|
return $output;
|
|
82
|
|
|
}
|
|
83
|
|
|
|
|
84
|
|
|
public static function text($name, $value = null, $label = null, $htmlAttributes = array())
|
|
85
|
|
|
{
|
|
86
|
|
|
return static::input('text', $name, $value, $label, $htmlAttributes);
|
|
87
|
|
|
}
|
|
88
|
|
|
|
|
89
|
|
|
public static function password($name, $value, $label = null, $htmlAttributes = array())
|
|
90
|
|
|
{
|
|
91
|
|
|
return static::input('password', $name, $value, $label, $htmlAttributes);
|
|
92
|
|
|
}
|
|
93
|
|
|
|
|
94
|
|
|
public static function number($name, $value, $label = null, $htmlAttributes = array())
|
|
95
|
|
|
{
|
|
96
|
|
|
return static::input('number', $name, $value, $label, $htmlAttributes);
|
|
97
|
|
|
}
|
|
98
|
|
|
|
|
99
|
|
|
public static function button($name, $value, $label = null, $htmlAttributes = array())
|
|
100
|
|
|
{
|
|
101
|
|
|
return static::input('button', $name, $value, $label, $htmlAttributes);
|
|
102
|
|
|
}
|
|
103
|
|
|
|
|
104
|
|
|
public static function checkbox($name, $value = 1, $checked = false, $label = null, $htmlAttributes = array())
|
|
105
|
|
|
{
|
|
106
|
|
|
if (!isset($htmlAttributes['checked']) && $checked) {
|
|
107
|
|
|
$htmlAttributes['checked'] = 'checked';
|
|
108
|
|
|
}
|
|
109
|
|
|
|
|
110
|
|
|
return static::input('checkbox', $name, $value, $label, $htmlAttributes);
|
|
111
|
|
|
}
|
|
112
|
|
|
|
|
113
|
|
|
public static function file($name, $label = null, $htmlAttributes = array())
|
|
114
|
|
|
{
|
|
115
|
|
|
return static::input('file', $name, null, $label, $htmlAttributes);
|
|
116
|
|
|
}
|
|
117
|
|
|
|
|
118
|
|
|
public static function hidden($name, $value, $label = null, $htmlAttributes = array())
|
|
119
|
|
|
{
|
|
120
|
|
|
return static::input('hidden', $name, $value, $label, $htmlAttributes);
|
|
121
|
|
|
}
|
|
122
|
|
|
|
|
123
|
|
|
public static function image($name, $url, $htmlAttributes = array())
|
|
124
|
|
|
{
|
|
125
|
|
|
$htmlAttributes['src'] = $url;
|
|
126
|
|
|
|
|
127
|
|
|
return static::input('email', $name, null, null, $htmlAttributes);
|
|
128
|
|
|
}
|
|
129
|
|
|
|
|
130
|
|
|
public static function radio($name, $availableValues = array(), $value = null, $label = null, $htmlAttributes = array(), $errors = array())
|
|
131
|
|
|
{
|
|
132
|
|
|
$itemData = array();
|
|
133
|
|
|
$itemData['name'] = $name;
|
|
134
|
|
|
$itemData['value'] = $value;
|
|
135
|
|
|
$itemData['label'] = $label;
|
|
136
|
|
|
$itemData['errors'] = $errors;
|
|
137
|
|
|
$itemData = array_merge($itemData, $htmlAttributes);
|
|
138
|
|
|
|
|
139
|
|
|
$item = new FormItem($itemData);
|
|
140
|
|
|
|
|
141
|
|
|
$output = $item->renderLabel();
|
|
142
|
|
|
$output .= '<div class="radio-list">'."\n";
|
|
143
|
|
|
foreach ($availableValues as $currentValue => $currentLabel) {
|
|
144
|
|
|
$htmlAttributes = array('id' => $name . '-' . $currentValue);
|
|
145
|
|
|
if ($currentValue == $value) {
|
|
146
|
|
|
$htmlAttributes['checked'] = 'checked';
|
|
147
|
|
|
}
|
|
148
|
|
|
|
|
149
|
|
|
$output .= '<div class="radio-item">' . FormItem::input('radio', $name, $currentValue, $currentLabel, $htmlAttributes) . '</div>'."\n";
|
|
150
|
|
|
}
|
|
151
|
|
|
$output .= '</div>'."\n";
|
|
152
|
|
|
|
|
153
|
|
|
return $output;
|
|
154
|
|
|
}
|
|
155
|
|
|
|
|
156
|
|
|
/*public static function radio($name, $value = null, $checked = false, $label = null, $htmlAttributes = array())
|
|
|
|
|
|
|
157
|
|
|
{
|
|
158
|
|
|
if (!isset($htmlAttributes['checked']) && $checked) {
|
|
159
|
|
|
$htmlAttributes['checked'] = 'checked';
|
|
160
|
|
|
}
|
|
161
|
|
|
return static::input('radio', $name, $value, $label, $htmlAttributes);
|
|
162
|
|
|
}
|
|
163
|
|
|
*/
|
|
164
|
|
|
public static function reset($value = null, $htmlAttributes)
|
|
165
|
|
|
{
|
|
166
|
|
|
return static::input('reset', null, $value, null, $htmlAttributes);
|
|
167
|
|
|
}
|
|
168
|
|
|
|
|
169
|
|
|
public static function select($name, $availableValues = array(), $value = null, $label = null, $htmlAttributes = array())
|
|
170
|
|
|
{
|
|
171
|
|
|
$itemData = array();
|
|
172
|
|
|
$itemData['name'] = $name;
|
|
173
|
|
|
$itemData['value'] = $value;
|
|
174
|
|
|
$itemData['label'] = $label;
|
|
175
|
|
|
$itemData = array_merge($itemData, $htmlAttributes);
|
|
176
|
|
|
|
|
177
|
|
|
$item = new FormItem($itemData);
|
|
178
|
|
|
|
|
179
|
|
|
$output = $item->renderLabel();
|
|
180
|
|
|
$output .= '<select';
|
|
181
|
|
|
$output .= $item->renderAttributes(true);
|
|
182
|
|
|
$output .= '>' . "\n";
|
|
183
|
|
|
foreach ($availableValues as $currentKey => $currentOption) {
|
|
184
|
|
|
if (is_array($currentOption)) {
|
|
185
|
|
|
$output .= '<optgroup label="' . $currentKey . '">'."\n";
|
|
186
|
|
|
foreach ($currentOption as $subKey => $subOption) {
|
|
187
|
|
View Code Duplication |
if (is_array($value)) {
|
|
|
|
|
|
|
188
|
|
|
$selected = in_array($subKey, $value) ? ' selected' : '';
|
|
|
|
|
|
|
189
|
|
|
} else {
|
|
190
|
|
|
$selected = ($subKey == $value) ? ' selected' : '';
|
|
|
|
|
|
|
191
|
|
|
}
|
|
192
|
|
|
$selected = $subKey == $value ? ' selected' : '';
|
|
193
|
|
|
$output .= '<option value="' . $subKey . '"' . $selected . '>' . $subOption . '</option>'."\n";
|
|
194
|
|
|
}
|
|
195
|
|
|
$output .= '</optgroup>'."\n";
|
|
196
|
|
|
} else {
|
|
197
|
|
View Code Duplication |
if (is_array($value)) {
|
|
|
|
|
|
|
198
|
|
|
$selected = in_array($currentKey, $value) ? ' selected' : '';
|
|
199
|
|
|
} else {
|
|
200
|
|
|
$selected = ($currentKey == $value) ? ' selected' : '';
|
|
201
|
|
|
}
|
|
202
|
|
|
$output .= '<option value="' . $currentKey . '"' . $selected . '>' . $currentOption . '</option>'."\n";
|
|
203
|
|
|
}
|
|
204
|
|
|
}
|
|
205
|
|
|
|
|
206
|
|
|
$output .= '</select>';
|
|
207
|
|
|
|
|
208
|
|
|
return $output;
|
|
209
|
|
|
}
|
|
210
|
|
|
|
|
211
|
|
|
public static function submit($name, $value, $label = '', $htmlAttributes = array())
|
|
212
|
|
|
{
|
|
213
|
|
|
return static::input('submit', $name, $value, $label, $htmlAttributes);
|
|
214
|
|
|
}
|
|
215
|
|
|
|
|
216
|
|
|
public static function textarea($name, $value, $label = '', $htmlAttributes = array())
|
|
217
|
|
|
{
|
|
218
|
|
|
$itemData = array();
|
|
219
|
|
|
$itemData['name'] = $name;
|
|
220
|
|
|
$itemData['value'] = $value;
|
|
221
|
|
|
$itemData['label'] = $label;
|
|
222
|
|
View Code Duplication |
if ($label !== null && !isset($htmlAttributes['id'])) {
|
|
|
|
|
|
|
223
|
|
|
$itemData['id'] = $name;
|
|
224
|
|
|
}
|
|
225
|
|
|
$itemData = array_merge($itemData, $htmlAttributes);
|
|
226
|
|
|
|
|
227
|
|
|
$item = new FormItem($itemData);
|
|
228
|
|
|
|
|
229
|
|
|
$output = $item->renderLabel();
|
|
230
|
|
|
$output .= '<textarea';
|
|
231
|
|
|
$output .= $item->renderAttributes(true);
|
|
232
|
|
|
$output .= '>';
|
|
233
|
|
|
$output .= $item->value;
|
|
|
|
|
|
|
234
|
|
|
$output .= '</textarea>'."\n";
|
|
235
|
|
|
|
|
236
|
|
|
return $output;
|
|
237
|
|
|
}
|
|
238
|
|
|
|
|
239
|
|
|
public static function tel($name, $value, $label = '', $htmlAttributes = array())
|
|
240
|
|
|
{
|
|
241
|
|
|
return static::input('tel', $name, $value, $label, $htmlAttributes);
|
|
242
|
|
|
}
|
|
243
|
|
|
|
|
244
|
|
|
public static function url($name, $value, $label = '', $htmlAttributes = array())
|
|
245
|
|
|
{
|
|
246
|
|
|
return static::input('url', $name, $value, $label, $htmlAttributes);
|
|
247
|
|
|
}
|
|
248
|
|
|
|
|
249
|
|
|
public static function email($name, $value, $label = '', $htmlAttributes = array())
|
|
250
|
|
|
{
|
|
251
|
|
|
return static::input('email', $name, $value, $label, $htmlAttributes);
|
|
252
|
|
|
}
|
|
253
|
|
|
|
|
254
|
|
|
public static function search($name, $value, $label = '', $htmlAttributes = array())
|
|
255
|
|
|
{
|
|
256
|
|
|
return static::input('search', $name, $value, $label, $htmlAttributes);
|
|
257
|
|
|
}
|
|
258
|
|
|
|
|
259
|
|
|
public static function date($name, $value, $label = '', $htmlAttributes = array())
|
|
260
|
|
|
{
|
|
261
|
|
|
return static::input('date', $name, $value, $label, $htmlAttributes);
|
|
262
|
|
|
}
|
|
263
|
|
|
|
|
264
|
|
|
public static function dateTime($name, $value, $label = '', $htmlAttributes = array())
|
|
265
|
|
|
{
|
|
266
|
|
|
return static::input('datetime', $name, $value, $label, $htmlAttributes);
|
|
267
|
|
|
}
|
|
268
|
|
|
|
|
269
|
|
|
public static function time($name, $value, $label = '', $htmlAttributes = array())
|
|
270
|
|
|
{
|
|
271
|
|
|
return static::input('time', $name, $value, $label, $htmlAttributes);
|
|
272
|
|
|
}
|
|
273
|
|
|
|
|
274
|
|
|
protected function renderLabel()
|
|
275
|
|
|
{
|
|
276
|
|
|
$output = '';
|
|
277
|
|
|
if ($this->label != '') {
|
|
278
|
|
|
$output .= '<label';
|
|
279
|
|
|
if ($this->id != '') {
|
|
|
|
|
|
|
280
|
|
|
$output .= ' for="' . htmlentities($this->id, ENT_COMPAT, static::$encoding) . '"';
|
|
|
|
|
|
|
281
|
|
|
}
|
|
282
|
|
|
$output .= '>';
|
|
283
|
|
|
$output .= $this->label;
|
|
284
|
|
|
$output .= '</label>'."\n";
|
|
285
|
|
|
}
|
|
286
|
|
|
|
|
287
|
|
|
return $output;
|
|
288
|
|
|
}
|
|
289
|
|
|
|
|
290
|
|
|
protected function renderAttributes($skipValue = false)
|
|
291
|
|
|
{
|
|
292
|
|
|
$output = '';
|
|
293
|
|
|
foreach ($this->objectHtmlProperties as $currentAttribute) {
|
|
294
|
|
|
if (!($currentAttribute == 'value' && $skipValue)) {
|
|
295
|
|
|
if ($this->$currentAttribute !== null) {
|
|
296
|
|
|
$output .= ' ' . $currentAttribute . '="' . htmlentities($this->$currentAttribute, ENT_COMPAT, static::$encoding) . '"';
|
|
297
|
|
|
}
|
|
298
|
|
|
}
|
|
299
|
|
|
}
|
|
300
|
|
|
|
|
301
|
|
|
return $output;
|
|
302
|
|
|
}
|
|
303
|
|
|
}
|
|
304
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.