ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 $this->checkArg("value", $this->isClientSideValueOk($values), "Values given do not match given inputs in group.");
67
68 $clone = clone $this;
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 (!sizeof($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 = parent::withInput($post_input);
119 if ($clone->getError()) {
120 return $clone;
121 }
122
123 return $clone->withGroupInput($post_input);
124 }
125
126
132 protected function withGroupInput(InputData $post_input)
133 {
134 $clone = $this;
135
136 if (sizeof($this->getInputs()) === 0) {
137 return $clone;
138 }
139
140 $inputs = [];
141 $values = [];
142 $error = false;
143 foreach ($this->getInputs() as $key => $input) {
144 $filled = $input->withInput($post_input);
148 //Todo: Is this correct here or should it be getValue? Design decision...
149 $content = $filled->getContent();
150 if ($content->isOk()) {
151 $values[$key] = $content->value();
152 } else {
153 $error = true;
154 }
155 $inputs[$key] = $filled;
156 }
157 $clone->inputs = $inputs;
158 if ($error) {
159 //Todo: Improve this error message on the group
160 $clone->content = $clone->data_factory->error("error in grouped input");
161
162 return $clone;
163 }
164
165 if ($clone->content->value()) {
166 $group_content = $clone->applyOperationsTo($values);
167 $f = $this->data_factory;
168 $clone->content = $clone->content->then(function ($value) use ($f, $group_content) {
169 if ($group_content->isError()) {
170 return $f->error($group_content->error());
171 }
172
173 return $f->ok(["value" => $value, "group_values" => $group_content->value()]);
174 });
175 } else {
176 $clone->content = $clone->applyOperationsTo($values);
177 }
178
179 if ($clone->content->isError()) {
180 return $clone->withError($clone->content->error());
181 }
182
183 return $clone;
184 }
185
186
190 public function withNameFrom(NameSource $source)
191 {
192 $clone = parent::withNameFrom($source);
196 $named_inputs = [];
197 foreach ($this->getInputs() as $key => $input) {
198 $named_inputs[$key] = $input->withNameFrom($source);
199 }
200
201 $clone->inputs = $named_inputs;
202
203 return $clone;
204 }
205
206
210 public function getInputs()
211 {
212 return $this->inputs;
213 }
214
218 protected function getConstraintForRequirement()
219 {
220 return null;
221 }
222}
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
trait GroupHelper
The code of Group is used in Checkbox, e.g., but a checkbox is not a group.
Definition: GroupHelper.php:21
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
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.