1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\kadro; |
4
|
|
|
|
5
|
|
|
use HexMakina\BlackBox\ORM\ModelInterface; |
6
|
|
|
use HexMakina\Marker\{Form,Element}; |
7
|
|
|
|
8
|
|
|
class TableToForm |
9
|
|
|
{ |
10
|
|
|
private static function compute_field_value($model, $field_name) |
11
|
|
|
{ |
12
|
|
|
$ret = ''; |
13
|
|
|
if (method_exists($model, $field_name)) { |
14
|
|
|
$ret = $model->$field_name(); |
15
|
|
|
}elseif (property_exists($model, $field_name)) { |
16
|
|
|
$ret = $model->$field_name; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return $ret; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function label($model, $field_name, $attributes = [], $errors = []) |
23
|
|
|
{ |
24
|
|
|
$label_content = $field_name; |
25
|
|
|
|
26
|
|
|
if (isset($attributes['label'])) { |
27
|
|
|
$label_content = $attributes['label']; |
28
|
|
|
unset($attributes['label']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$ret = Form::label($field_name, $label_content, $attributes, $errors); |
32
|
|
|
return $ret; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function field(ModelInterface $model, $field_name, $attributes = [], $errors = []): string |
36
|
|
|
{ |
37
|
|
|
$field_value = $attributes['value'] ?? self::computeFieldValue($model, $field_name); |
38
|
|
|
$attributes['name'] = $attributes['name'] ?? $field_name; |
39
|
|
|
|
40
|
|
|
$table = get_class($model)::table(); |
41
|
|
|
|
42
|
|
|
if (is_null($table->column($field_name))) { |
43
|
|
|
return Form::input($field_name, $field_value, $attributes, $errors); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$field = $table->column($field_name); |
47
|
|
|
|
48
|
|
|
if (isset($attributes['required']) && $attributes['required'] === false) { |
49
|
|
|
unset($attributes['required']); |
50
|
|
|
} elseif (!$field->isNullable()) { |
51
|
|
|
$attributes[] = 'required'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return self::fieldByType($field, $field_value, $attributes, $errors); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function fieldWithLabel($model, $field_name, $attributes = [], $errors = []): string |
58
|
|
|
{ |
59
|
|
|
$field_attributes = $attributes; |
60
|
|
|
if (isset($attributes['label'])) { |
61
|
|
|
unset($field_attributes['label']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return sprintf('%s %s', self::label($model, $field_name, $attributes, $errors), self::field($model, $field_name, $field_attributes, $errors)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function fields(ModelInterface $model, $group_class = null): string |
68
|
|
|
{ |
69
|
|
|
$table = get_class($model)::table(); |
70
|
|
|
$ret = ''; |
71
|
|
|
foreach ($table->columns() as $column_name => $column) { |
72
|
|
|
// vd($column_name); |
73
|
|
|
|
74
|
|
|
$form_field = ''; |
75
|
|
|
if ($column->isAutoIncremented()) { |
76
|
|
|
if (!$model->isNew()) { |
77
|
|
|
$form_field = self::field($model, $column_name); |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$form_field = self::fieldWithLabel($model, $column_name); |
81
|
|
|
if (!is_null($group_class)) { |
82
|
|
|
$form_field = new Element('div', $form_field, ['class' => $group_class]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
$ret .= PHP_EOL . $form_field; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $ret; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private static function fieldByType($field, $field_value, $attributes = [], $errors = []): string |
92
|
|
|
{ |
93
|
|
|
if ($field->isAutoIncremented()) { |
94
|
|
|
return Form::hidden($field->name(), $field_value); |
95
|
|
|
} |
96
|
|
|
if ($field->type()->isBoolean()) { |
97
|
|
|
$option_list = $attributes['values'] ?? [0 => 0, 1 => 1]; |
98
|
|
|
return Form::select($field->name(), $option_list, $field_value, $attributes); // |
99
|
|
|
} |
100
|
|
|
if ($field->type()->isInteger()) { |
101
|
|
|
return Form::input($field->name(), $field_value, $attributes, $errors); |
102
|
|
|
} |
103
|
|
|
if ($field->type()->isYear()) { |
104
|
|
|
$attributes['size'] = $attributes['maxlength'] = 4; |
105
|
|
|
return Form::input($field->name(), $field_value, $attributes, $errors); |
106
|
|
|
} |
107
|
|
|
if ($field->type()->isDate()) { |
108
|
|
|
return Form::date($field->name(), $field_value, $attributes, $errors); |
109
|
|
|
} |
110
|
|
|
if ($field->type()->isTime()) { |
111
|
|
|
return Form::time($field->name(), $field_value, $attributes, $errors); |
112
|
|
|
} |
113
|
|
|
if ($field->type()->isDatetime()) { |
114
|
|
|
return Form::datetime($field->name(), $field_value, $attributes, $errors); |
115
|
|
|
} |
116
|
|
|
if ($field->type()->isText()) { |
117
|
|
|
return Form::textarea($field->name(), $field_value, $attributes, $errors); |
118
|
|
|
} |
119
|
|
|
if ($field->type()->isEnum()) { |
120
|
|
|
$enum_values = []; |
121
|
|
|
foreach ($field->type()->getEnumValues() as $e_val) { |
122
|
|
|
$enum_values[$e_val] = $e_val; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$selected = $attributes['value'] ?? $field_value ?? ''; |
126
|
|
|
// foreach($field->) |
127
|
|
|
return Form::select($field->name(), $enum_values, $selected, $attributes); // |
128
|
|
|
|
129
|
|
|
// throw new \Exception('ENUM IS NOT HANDLED BY TableToFom'); |
130
|
|
|
} |
131
|
|
|
if ($field->type()->isString()) { |
132
|
|
|
$max_length = $field->type()->getLength(); |
133
|
|
|
$attributes['size'] = $attributes['maxlength'] = $max_length; |
134
|
|
|
return Form::input($field->name(), $field_value, $attributes, $errors); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return Form::input($field->name(), $field_value, $attributes, $errors); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private static function computeFieldValue($model, $field_name) |
141
|
|
|
{ |
142
|
|
|
if (method_exists($model, $field_name)) { |
143
|
|
|
return $model->$field_name(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (property_exists($model, $field_name)) { |
147
|
|
|
return $model->$field_name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return ''; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|