|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\Marker; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class Form |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
public static function __callStatic($element_type, $arguments) |
|
12
|
|
|
{ |
|
13
|
|
|
// arguments [name, value, [attributes], [errors]] |
|
14
|
|
|
$i = 0; |
|
15
|
|
|
$field_name = $arguments[$i++] ?? null; |
|
16
|
|
|
$field_value = $arguments[$i++] ?? null; |
|
17
|
|
|
$attributes = $arguments[$i++] ?? []; |
|
18
|
|
|
$errors = $arguments[$i++] ?? []; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$attributes['type'] = $element_type; |
|
22
|
|
|
$attributes['name'] = $attributes['name'] ?? $field_name; |
|
23
|
|
|
$attributes['value'] = $attributes['value'] ?? $field_value; |
|
24
|
|
|
|
|
25
|
|
|
switch ($attributes['type']) { |
|
26
|
|
|
case 'datetime': |
|
27
|
|
|
$attributes['type'] = 'datetime-local'; |
|
28
|
|
|
break; |
|
29
|
|
|
|
|
30
|
|
|
case 'password': |
|
31
|
|
|
$attributes['value'] = ''; |
|
32
|
|
|
break; |
|
33
|
|
|
} |
|
34
|
|
|
return self::input($field_name, $field_value, $attributes, $errors); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function input($field_name, $field_value = null, $attributes = [], $errors = []) |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
if (isset($attributes['disabled']) || !isset($attributes['type']) || in_array('disabled', $attributes, true)) { |
|
41
|
|
|
$attributes['type'] = 'text'; |
|
42
|
|
|
} else { |
|
43
|
|
|
$attributes['type'] = $attributes['type'] ?? 'text'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$attributes['name'] = $attributes['name'] ?? $field_name; |
|
47
|
|
|
$attributes['value'] = $attributes['value'] ?? $field_value; |
|
48
|
|
|
|
|
49
|
|
|
return self::element_with_errors('input', null, $attributes, isset($errors[$field_name])); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function textarea($field_name, $field_value = null, $attributes = [], $errors = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$attributes['name'] = $attributes['name'] ?? $field_name; |
|
55
|
|
|
return self::element_with_errors('textarea', $field_value, $attributes, isset($errors[$field_name])); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function select($field_name, $option_list, $selected = null, $attributes = [], $errors = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$attributes['name'] = $attributes['name'] ?? $field_name; |
|
61
|
|
|
|
|
62
|
|
|
$options = ''; |
|
63
|
|
|
foreach ($option_list as $value => $label) { |
|
64
|
|
|
$option_attributes = ['value' => $value]; |
|
65
|
|
|
if ($selected == $value) { |
|
66
|
|
|
$option_attributes['selected'] = 'selected'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$options .= new Element('option', $label, $option_attributes); |
|
70
|
|
|
} |
|
71
|
|
|
return self::element_with_errors('select', $options, $attributes, isset($errors[$field_name])); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function legend($label, $attributes = []) |
|
75
|
|
|
{ |
|
76
|
|
|
return new Element('legend', $label, $attributes); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function label($field_for, $field_label, $attributes = [], $errors = []) |
|
80
|
|
|
{ |
|
81
|
|
|
$attributes['for'] = $field_for; |
|
82
|
|
|
return self::element_with_errors('label', $field_label, $attributes, isset($errors[$field_for])); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function submit($field_id, $field_label, $attributes = []) |
|
86
|
|
|
{ |
|
87
|
|
|
$attributes['id'] = $attributes['id'] ?? $field_id; |
|
88
|
|
|
unset($attributes['name']); |
|
89
|
|
|
$attributes['type'] = 'submit'; |
|
90
|
|
|
if (isset($attributes['tag']) && $attributes['tag'] === 'input') { |
|
91
|
|
|
return new Element('input', '', $attributes); |
|
92
|
|
|
} else { |
|
93
|
|
|
unset($attributes['tag']); |
|
94
|
|
|
unset($attributes['value']); |
|
95
|
|
|
return new Element('button', $field_label, $attributes); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private static function element_with_errors($tag, $content, $attributes = [], $errors = false) |
|
100
|
|
|
{ |
|
101
|
|
|
|
|
102
|
|
|
$attributes['id'] = $attributes['id'] ?? $attributes['name'] ?? ''; |
|
103
|
|
|
|
|
104
|
|
|
if ($errors === true) { |
|
105
|
|
|
$attributes['class'] = $attributes['class'] ?? ''; |
|
106
|
|
|
$attributes['class'] .= ' error'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$label = ''; |
|
110
|
|
|
if (isset($attributes['label'])) { |
|
111
|
|
|
$label = self::label($attributes['id'], $attributes['label'], [], $errors); |
|
112
|
|
|
unset($attributes['label']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// vd($attributes, $tag); |
|
116
|
|
|
return $label . (new Element($tag, $content, $attributes)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|