1
|
|
|
<?php /** MicroFieldTagTrait */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web\Html; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* FieldTagTrait trait file. |
8
|
|
|
* |
9
|
|
|
* @author Oleg Lunegov <[email protected]> |
10
|
|
|
* @link https://github.com/linpax/microphp-framework |
11
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
12
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
13
|
|
|
* @package Micro |
14
|
|
|
* @subpackage Web\Html |
15
|
|
|
* @version 1.0 |
16
|
|
|
* @since 1.0 |
17
|
|
|
*/ |
18
|
|
|
trait FieldTagTrait |
19
|
|
|
{ |
20
|
|
|
use TagTrait; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Render input button tag |
25
|
|
|
* |
26
|
|
|
* @access public |
27
|
|
|
* |
28
|
|
|
* @param string $name button name |
29
|
|
|
* @param string $value button value |
30
|
|
|
* @param array $attributes attributes tag |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
* @static |
34
|
|
|
*/ |
35
|
|
|
public static function buttonField($name, $value = null, array $attributes = []) |
36
|
|
|
{ |
37
|
|
|
return static::field('button', $name, $value, $attributes); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Base field tag |
42
|
|
|
* |
43
|
|
|
* @access private |
44
|
|
|
* |
45
|
|
|
* @param string $type type of element |
46
|
|
|
* @param string $name name of element |
47
|
|
|
* @param string $value value of element |
48
|
|
|
* @param array $attributes attributes tag |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
* @static |
52
|
|
|
*/ |
53
|
|
|
private static function field($type, $name, $value = null, array $attributes = []) |
54
|
|
|
{ |
55
|
|
|
$attributes['id'] = !empty($attributes['id']) ? $attributes['id'] : $name; |
56
|
|
|
$attributes['type'] = $type; |
57
|
|
|
$attributes['name'] = $name; |
58
|
|
|
$attributes['value'] = $value; |
59
|
|
|
|
60
|
|
|
return static::tag('input', $attributes); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Render input file tag |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
* |
68
|
|
|
* @param string $name file name |
69
|
|
|
* @param string $value file value |
70
|
|
|
* @param array $attributes attributes tag |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
* @static |
74
|
|
|
*/ |
75
|
|
|
public static function fileField($name, $value = null, array $attributes = []) |
76
|
|
|
{ |
77
|
|
|
return static::field('file', $name, $value, $attributes); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Render input hidden tag |
82
|
|
|
* |
83
|
|
|
* @access public |
84
|
|
|
* |
85
|
|
|
* @param string $name hidden name |
86
|
|
|
* @param string $value hidden value |
87
|
|
|
* @param array $attributes attributes tag |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
* @static |
91
|
|
|
*/ |
92
|
|
|
public static function hiddenField($name, $value = null, array $attributes = []) |
93
|
|
|
{ |
94
|
|
|
return static::field('hidden', $name, $value, $attributes); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Render input image tag |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* |
102
|
|
|
* @param string $name image name |
103
|
|
|
* @param string $value image value |
104
|
|
|
* @param string $srcFile path to image |
105
|
|
|
* @param array $attributes attributes tag |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
* @static |
109
|
|
|
*/ |
110
|
|
|
public static function imageField($name, $value = null, $srcFile, array $attributes = []) |
111
|
|
|
{ |
112
|
|
|
$attributes['src'] = $srcFile; |
113
|
|
|
|
114
|
|
|
return static::field('image', $name, $value, $attributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Render input password tag |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* |
122
|
|
|
* @param string $name password name |
123
|
|
|
* @param string $value password value |
124
|
|
|
* @param array $attributes attributes tag |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @static |
128
|
|
|
*/ |
129
|
|
|
public static function passwordField($name, $value = null, array $attributes = []) |
130
|
|
|
{ |
131
|
|
|
return static::field('password', $name, $value, $attributes); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Render input text tag |
136
|
|
|
* |
137
|
|
|
* @access public |
138
|
|
|
* |
139
|
|
|
* @param string $name text name |
140
|
|
|
* @param string $value text value |
141
|
|
|
* @param array $attributes attributes tag |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
* @static |
145
|
|
|
*/ |
146
|
|
|
public static function textField($name, $value = null, array $attributes = []) |
147
|
|
|
{ |
148
|
|
|
return static::field('text', $name, $value, $attributes); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Render input color tag |
153
|
|
|
* |
154
|
|
|
* @access public |
155
|
|
|
* |
156
|
|
|
* @param string $name color name |
157
|
|
|
* @param string $value color value |
158
|
|
|
* @param array $attributes attributes tag |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
* @static |
162
|
|
|
*/ |
163
|
|
|
public static function colorField($name, $value = null, array $attributes = []) |
164
|
|
|
{ |
165
|
|
|
return static::field('color', $name, $value, $attributes); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Render input date tag |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* |
173
|
|
|
* @param string $name date name |
174
|
|
|
* @param string $value date value |
175
|
|
|
* @param array $attributes attributes tag |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
* @static |
179
|
|
|
*/ |
180
|
|
|
public static function dateField($name, $value = null, array $attributes = []) |
181
|
|
|
{ |
182
|
|
|
return static::field('date', $name, $value, $attributes); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Render input datetime tag |
187
|
|
|
* |
188
|
|
|
* @access public |
189
|
|
|
* |
190
|
|
|
* @param string $name datetime name |
191
|
|
|
* @param string $value datetime value |
192
|
|
|
* @param array $attributes attributes tag |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
* @static |
196
|
|
|
*/ |
197
|
|
|
public static function datetimeField($name, $value = null, array $attributes = []) |
198
|
|
|
{ |
199
|
|
|
return static::field('datetime', $name, $value, $attributes); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Render input datetime-local tag |
204
|
|
|
* |
205
|
|
|
* @access public |
206
|
|
|
* |
207
|
|
|
* @param string $name datetime-local name |
208
|
|
|
* @param string $value datetime-local value |
209
|
|
|
* @param array $attributes attributes tag |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
* @static |
213
|
|
|
*/ |
214
|
|
|
public static function datetimeLocalField($name, $value = null, array $attributes = []) |
215
|
|
|
{ |
216
|
|
|
return static::field('datetime-local', $name, $value, $attributes); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Render input email tag |
221
|
|
|
* |
222
|
|
|
* @access public |
223
|
|
|
* |
224
|
|
|
* @param string $name email name |
225
|
|
|
* @param string $value email value |
226
|
|
|
* @param array $attributes attributes tag |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
* @static |
230
|
|
|
*/ |
231
|
|
|
public static function emailField($name, $value = null, array $attributes = []) |
232
|
|
|
{ |
233
|
|
|
return static::field('email', $name, $value, $attributes); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Render input number tag |
238
|
|
|
* |
239
|
|
|
* @access public |
240
|
|
|
* |
241
|
|
|
* @param string $name number name |
242
|
|
|
* @param string $value number value |
243
|
|
|
* @param array $attributes attributes tag |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
* @static |
247
|
|
|
*/ |
248
|
|
|
public static function numberField($name, $value = null, array $attributes = []) |
249
|
|
|
{ |
250
|
|
|
return static::field('number', $name, $value, $attributes); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Render input range tag |
255
|
|
|
* |
256
|
|
|
* @access public |
257
|
|
|
* |
258
|
|
|
* @param string $name range name |
259
|
|
|
* @param string $value range value |
260
|
|
|
* @param array $attributes attributes tag |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
* @static |
264
|
|
|
*/ |
265
|
|
|
public static function rangeField($name, $value = null, array $attributes = []) |
266
|
|
|
{ |
267
|
|
|
return static::field('range', $name, $value, $attributes); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Render input search tag |
272
|
|
|
* |
273
|
|
|
* @access public |
274
|
|
|
* |
275
|
|
|
* @param string $name search name |
276
|
|
|
* @param string $value search value |
277
|
|
|
* @param array $attributes attributes tag |
278
|
|
|
* |
279
|
|
|
* @return string |
280
|
|
|
* @static |
281
|
|
|
*/ |
282
|
|
|
public static function searchField($name, $value = null, array $attributes = []) |
283
|
|
|
{ |
284
|
|
|
return static::field('search', $name, $value, $attributes); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Render input tel tag |
289
|
|
|
* |
290
|
|
|
* @access public |
291
|
|
|
* |
292
|
|
|
* @param string $name telephone name |
293
|
|
|
* @param string $value telephone value |
294
|
|
|
* @param array $attributes attributes tag |
295
|
|
|
* |
296
|
|
|
* @return string |
297
|
|
|
* @static |
298
|
|
|
*/ |
299
|
|
|
public static function telField($name, $value = null, array $attributes = []) |
300
|
|
|
{ |
301
|
|
|
return static::field('tel', $name, $value, $attributes); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Render input time tag |
306
|
|
|
* |
307
|
|
|
* @access public |
308
|
|
|
* |
309
|
|
|
* @param string $name time name |
310
|
|
|
* @param string $value time value |
311
|
|
|
* @param array $attributes attributes tag |
312
|
|
|
* |
313
|
|
|
* @return string |
314
|
|
|
* @static |
315
|
|
|
*/ |
316
|
|
|
public static function timeField($name, $value = null, array $attributes = []) |
317
|
|
|
{ |
318
|
|
|
return static::field('time', $name, $value, $attributes); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Render input url tag |
323
|
|
|
* |
324
|
|
|
* @access public |
325
|
|
|
* |
326
|
|
|
* @param string $name url name |
327
|
|
|
* @param string $value url path |
328
|
|
|
* @param array $attributes attributes tag |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
* @static |
332
|
|
|
*/ |
333
|
|
|
public static function urlField($name, $value = null, array $attributes = []) |
334
|
|
|
{ |
335
|
|
|
return static::field('url', $name, $value, $attributes); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Render input month tag |
340
|
|
|
* |
341
|
|
|
* @access public |
342
|
|
|
* |
343
|
|
|
* @param string $name month name |
344
|
|
|
* @param string $value month value |
345
|
|
|
* @param array $attributes attributes tag |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
* @static |
349
|
|
|
*/ |
350
|
|
|
public static function monthField($name, $value = null, array $attributes = []) |
351
|
|
|
{ |
352
|
|
|
return static::field('month', $name, $value, $attributes); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Render input week tag |
357
|
|
|
* |
358
|
|
|
* @access public |
359
|
|
|
* |
360
|
|
|
* @param string $name week name |
361
|
|
|
* @param string $value week value |
362
|
|
|
* @param array $attributes attributes tag |
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
* @static |
366
|
|
|
*/ |
367
|
|
|
public static function weekField($name, $value = null, array $attributes = []) |
368
|
|
|
{ |
369
|
|
|
return static::field('week', $name, $value, $attributes); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Render radio button list tag |
374
|
|
|
* |
375
|
|
|
* @access public |
376
|
|
|
* |
377
|
|
|
* @param string $name radio name |
378
|
|
|
* @param array $radios format array(text, value, attributes) |
379
|
|
|
* @param string $format %radio% - radio , %text% - text |
380
|
|
|
* @param string $selected name selected element |
381
|
|
|
* |
382
|
|
|
* @return string |
383
|
|
|
* @static |
384
|
|
|
*/ |
385
|
|
|
public static function radioButtonList($name, array $radios = [], $format = '<p>%radio% %text%</p>', $selected = '') |
386
|
|
|
{ |
387
|
|
|
$rads = ''; |
388
|
|
|
foreach ($radios AS $radio) { |
389
|
|
|
if (strcmp($radio['value'], $selected) === 0) { |
390
|
|
|
$radio['attributes']['checked'] = 'checked'; |
391
|
|
|
} |
392
|
|
|
$rad = static::radioField($name, $radio['value'], |
393
|
|
|
!empty($radio['attributes']) ? $radio['attributes'] : []); |
394
|
|
|
$rads .= str_replace(['%radio%', '%text%'], [$rad, $radio['text']], $format); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return $rads; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Render input radio tag |
402
|
|
|
* |
403
|
|
|
* @access public |
404
|
|
|
* |
405
|
|
|
* @param string $name radio name |
406
|
|
|
* @param string $value radio value |
407
|
|
|
* @param array $attributes attributes tag |
408
|
|
|
* |
409
|
|
|
* @return string |
410
|
|
|
* @static |
411
|
|
|
*/ |
412
|
|
|
public static function radioField($name, $value = null, array $attributes = []) |
413
|
|
|
{ |
414
|
|
|
return static::field('radio', $name, $value, $attributes); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Render checkBoxList (input checkbox tags) |
419
|
|
|
* |
420
|
|
|
* @access public |
421
|
|
|
* |
422
|
|
|
* @param string $name name for checkBox'es in list |
423
|
|
|
* @param array $checkboxes format array(text, value, attributes) |
424
|
|
|
* @param string $format %check% - checkbox , %text% - text |
425
|
|
|
* @param string $selected name selected element |
426
|
|
|
* |
427
|
|
|
* @return string |
428
|
|
|
* @static |
429
|
|
|
*/ |
430
|
|
|
public static function checkBoxList( |
431
|
|
|
$name, |
432
|
|
|
array $checkboxes = [], |
433
|
|
|
$format = '<p>%check% %text%</p>', |
434
|
|
|
$selected = '' |
435
|
|
|
) { |
436
|
|
|
$checks = ''; |
437
|
|
|
foreach ($checkboxes AS $checkbox) { |
438
|
|
|
if ($checkbox['value'] === $selected) { |
439
|
|
|
$checkbox['attributes']['selected'] = 'selected'; |
440
|
|
|
} |
441
|
|
|
$check = static::checkBoxField($name, $checkbox['value'], $checkbox['attributes']); |
442
|
|
|
$checks .= str_replace('%text%', $checkbox['text'], str_replace('%check%', $check, $format)); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
return $checks; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Render input checkbox tag |
450
|
|
|
* |
451
|
|
|
* @access public |
452
|
|
|
* |
453
|
|
|
* @param string $name checkBox name |
454
|
|
|
* @param string $value checkBox value |
455
|
|
|
* @param array $attributes attributes tag |
456
|
|
|
* |
457
|
|
|
* @return string |
458
|
|
|
* @static |
459
|
|
|
*/ |
460
|
|
|
public static function checkBoxField($name, $value = null, array $attributes = []) |
461
|
|
|
{ |
462
|
|
|
return static::field('checkbox', $name, $value, $attributes); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|