1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
namespace Studip\Forms;
abstract class Part
{
protected $parent = null;
protected $contextobject = null;
protected $parts = [];
public $if = null;
/**
* Constructor of this Part. Can take one or more Part objects or Input objects or arrays representing an Input object.
* @param ...$parts
*/
public function __construct(...$parts)
{
foreach ($parts as $part) {
if (is_subclass_of($part, Part::class)) {
$this->addPart($part);
} else {
if (!is_array($part)) {
$part->setParent($this);
}
$this->parts[] = $part;
}
}
}
/**
* Sets the context-object which is most likely a SimpleORMap object
* @param $object
* @return $this
*/
public function setContextObject($object)
{
$this->contextobject = $object;
return $this;
}
/**
* Returns the context object of this Part if there is any. If there is none it tries to return the context-object
* of a parent object.
* @return void|null
*/
public function getContextObject()
{
if ($this->contextobject) {
return $this->contextobject;
} elseif ($this->parent) {
return $this->parent->getContextObject();
}
}
/**
* Adds a Part object on the next layer.
* @param Part $part
* @return $this
*/
public function addPart(Part $part)
{
$part->setParent($this);
$this->parts[] = $part;
return $this;
}
/**
* Adds an Input to this Part.
* @param Input $input
* @return Input
*/
public function addInput(Input $input)
{
$input->setParent($this);
$this->parts[] = $input;
return $input;
}
/**
* Renders this Part object. This could be a section or any other HTML element with child-elements.
* @return string
*/
public function render()
{
return '';
}
/**
* Renders the Part element with a condition.
* @return string
*/
public function renderWithCondition()
{
$html = $this->render();
if (!trim($html)) {
return '';
}
if ($this->if !== null) {
$html = '<template v-if="' . htmlReady($this->if) . '">' . $html . '</template>';
}
return $html;
}
/**
* Recursively returns all Input elements attached to this Part object or any child Parts.
* @return array
*/
public function getAllInputs()
{
$inputs = [];
foreach ($this->parts as $part) {
if (is_subclass_of($part, Input::class) && $part->permission) {
$inputs[] = $part;
} elseif(is_subclass_of($part, Part::class)) {
$inputs = array_merge($inputs, $part->getAllInputs());
}
}
return $inputs;
}
/**
* Sets the parent object of this Part. Usually this is done automatically.
* @param Part $parent
* @return $this
* @throws \Exception
*/
public function setParent(Part $parent)
{
$this->parent = $parent;
//Inputs aktualisieren?
foreach ($this->parts as $i => $part) {
if (is_array($part)) {
$input = $this->getInputFromArray($part);
$input->setParent($this);
$this->parts[$i] = $input;
}
}
return $this;
}
/**
* Sets a condition to display this Part. The condition is a javascript condition which is used by vue to
* hide the input if the condition is not satisfies.
* @param string $if
* @return $this
*/
public function setIfCondition($if)
{
$this->if = $if;
return $this;
}
/**
* Returns an Input element from an array.
* @param array $data
* @return array|mixed
* @throws \Exception
*/
public function getInputFromArray(array $data)
{
$context = $this->getContextObject();
if ($context && method_exists($context, 'getTableMetadata')) {
$metadata = $context->getTableMetadata();
$meta = $metadata['fields'][$data['name']];
if (!isset($data['type'])) {
if ($meta) {
$data = array_merge(Input::getFielddataFromMeta($meta, $context), $data);
} else {
$data['type'] = 'text';
}
}
}
if (!isset($data['label'])) {
$data['label'] = $data['name'];
}
if (!isset($data['value']) && $context && method_exists($context, 'isField')) {
if ($context->isField($data['name'])) {
$data['value'] = $context[$data['name']];
}
}
if (!$data['type']) {
return $data;
}
$classname = "\\Studip\\Forms\\".ucfirst($data['type'])."Input";
$attributes = $data;
unset($attributes['name']);
unset($attributes['label']);
unset($attributes['value']);
unset($attributes['type']);
unset($attributes['mapper']);
unset($attributes['store']);
unset($attributes['if']);
unset($attributes['permission']);
unset($attributes['required']);
unset($attributes['attributes']);
$attributes = array_merge($attributes, (array) $data['attributes']);
if (class_exists($classname)) {
$input = new $classname($data['name'], $data['label'], $data['value'], $attributes);
} elseif (class_exists($data['type'])) {
$classname = $data['type'];
$input = new $classname($data['name'], $data['label'], $data['value'], $attributes);
} else {
//this should not happen:
throw new \Exception(sprintf(_("Klasse %s oder %s existiert nicht."), $classname, $data['type']));
}
if ($data['mapper'] && is_callable($data['mapper'])) {
$input->mapper = $data['mapper'];
}
if ($data['store'] && is_callable($data['store'])) {
$input->store = $data['store'];
}
if ($data['if']) {
$input->if = $data['if'];
}
if (isset($data['permission'])) {
$input->permission = $data['permission'];
}
if ($data['required']) {
$input->required = true;
}
return $input;
}
}
|