|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\HTML\Renderable; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Field; |
|
6
|
|
|
use Formularium\HTMLElement; |
|
7
|
|
|
|
|
8
|
|
|
class Renderable_bool extends \Formularium\Renderable |
|
9
|
|
|
{ |
|
10
|
|
|
public const FORMAT_CHOOSER = 'format_chooser'; |
|
11
|
|
|
public const FORMAT_CHOOSER_SELECT = 'format_chooser_select'; |
|
12
|
|
|
public const FORMAT_CHOOSER_RADIO = 'format_chooser_radio'; |
|
13
|
|
|
|
|
14
|
|
|
use \Formularium\Frontend\HTML\RenderableViewableTrait { |
|
15
|
|
|
viewable as _viewable; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function viewable($value, Field $field, HTMLElement $previous): HTMLElement |
|
19
|
|
|
{ |
|
20
|
|
|
$formatted = $field->getDatatype()->format($value, $field); |
|
21
|
|
|
|
|
22
|
|
|
return $this->_viewable($formatted, $field, $previous); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function editable($value, Field $field, HTMLElement $previous): HTMLElement |
|
26
|
|
|
{ |
|
27
|
|
|
if (is_string($value)) { |
|
28
|
|
|
if ($value === 'true') { |
|
29
|
|
|
$value = true; |
|
30
|
|
|
} elseif ($value === 'false') { |
|
31
|
|
|
$value = false; |
|
32
|
|
|
} else { |
|
33
|
|
|
$value = null; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$format = $field->getExtension(static::FORMAT_CHOOSER, static::FORMAT_CHOOSER_SELECT); |
|
38
|
|
|
|
|
39
|
|
|
if ($field->getExtension('required', false)) { |
|
40
|
|
|
if ($format == static::FORMAT_CHOOSER_SELECT) { |
|
41
|
|
|
$element = $this->editableSelect($value, $field, $previous); |
|
42
|
|
|
} else { |
|
43
|
|
|
$element = $this->editableRadio($value, $field, $previous); |
|
44
|
|
|
} |
|
45
|
|
|
} else { |
|
46
|
|
|
$element = $this->editableSelect($value, $field, $previous); |
|
47
|
|
|
} |
|
48
|
|
|
$container = new HTMLElement('div', [], $element); |
|
49
|
|
|
$extensions = $field->getExtensions(); |
|
50
|
|
|
if (array_key_exists('label', $extensions)) { |
|
51
|
|
|
$container->prependContent(new HTMLElement('label', ['for' => $element->getAttribute('id')], $extensions['label'])); |
|
52
|
|
|
} |
|
53
|
|
|
return $container; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $value |
|
58
|
|
|
* @param Field $field |
|
59
|
|
|
* @param HTMLElement $previous |
|
60
|
|
|
* @return HTMLElement |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function editableRadio($value, Field $field, HTMLElement $previous): HTMLElement |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if (empty($value) && array_key_exists(static::DEFAULTVALUE, $field->getExtensions())) { |
|
65
|
|
|
$value = $field->getExtensions()[static::DEFAULTVALUE]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$element = new HTMLElement('ul'); |
|
69
|
|
|
|
|
70
|
|
|
$idcounter = 1; |
|
71
|
|
|
foreach ([true => 'True', false => 'False'] as $v => $label) { |
|
72
|
|
|
$input = new HTMLElement('input'); |
|
73
|
|
|
|
|
74
|
|
|
// send ids to delete/edit data later correctly. |
|
75
|
|
|
if ($value !== null && $v == $value) { |
|
76
|
|
|
$input->addAttribute('checked', 'checked'); |
|
77
|
|
|
} |
|
78
|
|
|
$elementname = $field->getName(); // 'loh:' . $this->name . '[' . $attrid . '][value]' . '[' . $attrid . ']'; |
|
79
|
|
|
$id = $elementname . $idcounter++; |
|
80
|
|
|
$input->addAttributes([ |
|
81
|
|
|
'name' => $elementname, |
|
82
|
|
|
'data-attribute' => $field->getName(), |
|
83
|
|
|
'data-datatype' => $field->getDatatype()->getName(), |
|
84
|
|
|
'data-basetype' => $field->getDatatype()->getBasetype(), |
|
85
|
|
|
'value' => $value ? 'true' : 'false', |
|
86
|
|
|
'type' => 'radio', |
|
87
|
|
|
'title' => $field->getExtension(static::LABEL, '') |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
foreach ([static::DISABLED, static::READONLY, static::REQUIRED] as $v) { |
|
|
|
|
|
|
91
|
|
|
if ($field->getExtension($v, false)) { |
|
92
|
|
|
$input->setAttribute($v, $v); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$li = new HTMLElement( |
|
97
|
|
|
'li', |
|
98
|
|
|
[], |
|
99
|
|
|
[$input, new HTMLElement('label', ['for' => $id], [$label])] |
|
100
|
|
|
); |
|
101
|
|
|
$element->addContent($li); |
|
102
|
|
|
} |
|
103
|
|
|
return $element; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param mixed $value |
|
108
|
|
|
* @param Field $field |
|
109
|
|
|
* @param HTMLElement $previous |
|
110
|
|
|
* @return HTMLElement |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function editableSelect($value, Field $field, HTMLElement $previous): HTMLElement |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$element = new HTMLElement('select'); |
|
115
|
|
|
$extensions = $field->getExtensions(); |
|
116
|
|
|
$element->setAttributes([ |
|
117
|
|
|
'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'text'), |
|
118
|
|
|
'name' => $field->getName(), |
|
119
|
|
|
'class' => '', |
|
120
|
|
|
'data-attribute' => $field->getName(), |
|
121
|
|
|
'data-datatype' => $field->getDatatype()->getName(), |
|
122
|
|
|
'data-basetype' => $field->getDatatype()->getBasetype(), |
|
123
|
|
|
'title' => $field->getExtension(static::LABEL, '') |
|
124
|
|
|
]); |
|
125
|
|
|
|
|
126
|
|
|
$optionEmpty = new HTMLElement('option', ['value' => ''], '', true); |
|
127
|
|
|
$optionTrue = new HTMLElement('option', ['value' => 'true'], 'True', true); |
|
128
|
|
|
$optionFalse = new HTMLElement('option', ['value' => 'false'], 'False', true); |
|
129
|
|
|
|
|
130
|
|
|
if ($value) { |
|
131
|
|
|
$optionTrue->setAttribute('selected', 'selected'); |
|
132
|
|
|
} elseif (!($value === null)) { |
|
133
|
|
|
$optionFalse->setAttribute('selected', 'selected'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($field->getExtension('required', false)) { |
|
137
|
|
|
$element->addContent($optionEmpty); |
|
138
|
|
|
} |
|
139
|
|
|
$element->addContent($optionFalse); |
|
140
|
|
|
$element->addContent($optionTrue); |
|
141
|
|
|
|
|
142
|
|
|
foreach ([static::DISABLED, static::READONLY, static::REQUIRED] as $v) { |
|
143
|
|
|
if ($field->getExtension($v, false)) { |
|
144
|
|
|
$element->setAttribute($v, $v); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $element; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.