1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Frontend\HTML\Renderable; |
4
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
6
|
|
|
use Formularium\Field; |
7
|
|
|
use Formularium\Frontend\HTML\Framework; |
8
|
|
|
use Formularium\Frontend\HTML\Renderable; |
9
|
|
|
use Formularium\HTMLNode; |
10
|
|
|
use Modelarium\Datatype\Datatype_relationship; |
11
|
|
|
use Modelarium\Datatype\RelationshipFactory; |
12
|
|
|
|
13
|
|
|
class Renderable_relationship extends Renderable |
14
|
|
|
{ |
15
|
|
|
public function viewable($value, Field $field, HTMLNode $previous): HTMLNode |
16
|
|
|
{ |
17
|
|
|
return $previous; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function editable($value, Field $field, HTMLNode $previous): HTMLNode |
21
|
|
|
{ |
22
|
|
|
// TODO: autocomplete |
23
|
|
|
return $this->editableSelect($value, $field, $previous); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $value |
28
|
|
|
* @param Field $field |
29
|
|
|
* @param HTMLNode $previous |
30
|
|
|
* @return HTMLNode |
31
|
|
|
*/ |
32
|
|
|
public function editableSelect($value, Field $field, HTMLNode $previous): HTMLNode |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Datatype_relationship $datatype |
36
|
|
|
*/ |
37
|
|
|
$datatype = $field->getDatatype(); |
38
|
|
|
$relationship = $datatype->getRelationship(); |
39
|
|
|
|
40
|
|
|
if ($relationship === RelationshipFactory::RELATIONSHIP_MANY_TO_MANY || |
41
|
|
|
$relationship === RelationshipFactory::MORPH_MANY_TO_MANY |
42
|
|
|
// TODO: inverses 1:n? |
43
|
|
|
) { |
44
|
|
|
$input = new HTMLNode('input'); |
45
|
|
|
} elseif ($field->getRenderable('relationshipSelect', false)) { // TODO: document |
46
|
|
|
$input = new HTMLNode('select'); |
47
|
|
|
} else { |
48
|
|
|
$input = new HTMLNode('input'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$renderable = $field->getRenderables(); |
52
|
|
|
$input->setAttributes([ |
53
|
|
|
'id' => $field->getName() . Framework::counter(), |
54
|
|
|
'name' => $field->getName(), |
55
|
|
|
'class' => '', |
56
|
|
|
'data-attribute' => $field->getName(), |
57
|
|
|
'data-datatype' => $field->getDatatype()->getName(), |
58
|
|
|
'data-basetype' => $field->getDatatype()->getBasetype(), |
59
|
|
|
'title' => $field->getRenderable(static::LABEL, ''), |
60
|
|
|
'autocomplete' => 'off' |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
if (isset($renderable[static::PLACEHOLDER])) { |
64
|
|
|
$input->setAttribute('placeholder', $renderable[static::PLACEHOLDER]); |
65
|
|
|
} |
66
|
|
|
if ($field->getValidatorOption(Datatype::REQUIRED)) { |
67
|
|
|
$input->setAttribute('required', 'required'); |
68
|
|
|
} |
69
|
|
|
/* TODO if ($validators[Datatype_association::MULTIPLE] ?? false) { |
70
|
|
|
$input->setAttribute('multiple', 'multiple'); |
71
|
|
|
} */ |
72
|
|
|
foreach ([static::DISABLED, static::READONLY] as $v) { |
73
|
|
|
if ($field->getRenderable($v, false)) { |
74
|
|
|
$input->setAttribute($v, $v); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->container($input, $field); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $value |
83
|
|
|
* @param Field $field |
84
|
|
|
* @param HTMLNode $previous |
85
|
|
|
* @return HTMLNode |
86
|
|
|
*/ |
87
|
|
|
public function editableAutocomplete($value, Field $field, HTMLNode $previous): HTMLNode |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$input = new HTMLNode('input'); |
90
|
|
|
|
91
|
|
|
$renderable = $field->getRenderables(); |
92
|
|
|
$validators = $field->getValidators(); |
93
|
|
|
$input->setAttributes([ |
94
|
|
|
'id' => $field->getName() . Framework::counter(), |
95
|
|
|
'name' => $field->getName(), |
96
|
|
|
'class' => '', |
97
|
|
|
'data-attribute' => $field->getName(), |
98
|
|
|
'data-datatype' => $field->getDatatype()->getName(), |
99
|
|
|
'data-basetype' => $field->getDatatype()->getBasetype(), |
100
|
|
|
'title' => $field->getRenderable(static::LABEL, ''), |
101
|
|
|
'autocomplete' => 'off' |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
if (isset($renderable[static::PLACEHOLDER])) { |
105
|
|
|
$input->setAttribute('placeholder', $renderable[static::PLACEHOLDER]); |
106
|
|
|
} |
107
|
|
|
if ($validators[Datatype::REQUIRED] ?? false) { |
108
|
|
|
$input->setAttribute('required', 'required'); |
109
|
|
|
} |
110
|
|
|
foreach ([static::DISABLED, static::READONLY] as $v) { |
111
|
|
|
if ($field->getRenderable($v, false)) { |
112
|
|
|
$input->setAttribute($v, $v); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->container($input, $field); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.