ILIAS  release_7 Revision v7.30-3-g800a261c036
GroupHelper.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see
4docs/LICENSE */
5
7
8use ILIAS\UI\Component as Component;
11use ILIAS\Data\Factory as DataFactory;
12
21{
27 protected $inputs = [];
28
29
33 protected function isClientSideValueOk($value)
34 {
35 return true;
36 }
37
38
44 public function getGroupValues()
45 {
46 $values = [];
47 foreach ($this->getInputs() as $key => $input) {
48 $values[$key] = $input->getValue();
49 }
50
51 return $values;
52 }
53
54
64 public function withGroupValues($values)
65 {
66 $clone = clone $this;
67 $clone->checkArg("value", $clone->isClientSideValueOk($values), "Values given do not match given inputs in group.");
68
69 $inputs = [];
70
71 foreach ($this->getInputs() as $key => $input) {
72 $inputs[$key] = $input->withValue($values[$key]);
73 }
74
75 $clone->inputs = $inputs;
76
77 return $clone;
78 }
79
80
88 protected function isClientGroupSideValueOk($value)
89 {
90 if (!is_array($value)) {
91 return false;
92 }
93 if (!($this->getInputs() == sizeof($value))) {
94 return false;
95 }
96
97 foreach ($this->getInputs() as $key => $input) {
98 if (!array_key_exists($key, $value)) {
99 return false;
100 }
101 }
102
103 return true;
104 }
105
106
113 public function withInput(InputData $post_input)
114 {
115 $clone = clone $this;
116 $clone = $clone::withInput($post_input);
120 if ($clone->getError()) {
121 return $clone;
122 }
123
124 return $clone->withGroupInput($post_input);
125 }
126
127
133 protected function withGroupInput(InputData $post_input)
134 {
138 $clone = clone $this;
139
140 if (sizeof($this->getInputs()) === 0) {
141 return $clone;
142 }
143
144 $inputs = [];
145 $values = [];
146 $error = false;
147 foreach ($this->getInputs() as $key => $input) {
148 $filled = $input->withInput($post_input);
152 //Todo: Is this correct here or should it be getValue? Design decision...
153 $content = $filled->getContent();
154 if ($content->isOK()) {
155 $values[$key] = $content->value();
156 } else {
157 $error = true;
158 }
159 $inputs[$key] = $filled;
160 }
161 $clone->inputs = $inputs;
162 if ($error) {
163 //Todo: Improve this error message on the group
164 $clone->content = $clone->data_factory->error("error in grouped input");
165
166 return $clone;
167 }
168
169 if ($clone->content->value()) {
170 $group_content = $clone->applyOperationsTo($values);
171 $f = $clone->data_factory;
172 $clone->content = $clone->content->then(function ($value) use ($f, $group_content) {
173 if ($group_content->isError()) {
174 return $f->error($group_content->error());
175 }
176
177 return $f->ok(["value" => $value, "group_values" => $group_content->value()]);
178 });
179 } else {
180 $clone->content = $clone->applyOperationsTo($values);
181 }
182
183 if ($clone->content->isError()) {
184 return $clone->withError($clone->content->error());
185 }
186
187 return $clone;
188 }
189
190
194 public function withNameFrom(NameSource $source)
195 {
199 $clone = clone $this;
200 $clone = $clone::withNameFrom($source);
201
202 $named_inputs = [];
203 foreach ($this->getInputs() as $key => $input) {
204 $named_inputs[$key] = $input->withNameFrom($source);
205 }
206
207 $clone->inputs = $named_inputs;
208
209 return $clone;
210 }
211
212
216 public function getInputs()
217 {
218 return $this->inputs;
219 }
220
224 protected function getConstraintForRequirement()
225 {
226 return null;
227 }
228}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11
$source
Definition: metadata.php:76
withGroupValues($values)
Get an input like this with children with other values displayed on the client side.
Definition: GroupHelper.php:64
getGroupValues()
Get the value that is displayed in the groups input as Generator instance.
Definition: GroupHelper.php:44
isClientGroupSideValueOk($value)
Default implementation for groups.
Definition: GroupHelper.php:88
trait GroupHelper
The code of Group is used in Checkbox, e.g., but a checkbox is not a group.
Definition: GroupHelper.php:21