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