ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
Group.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
14use ILIAS\Data\Factory as DataFactory;
15
19class Group extends Input implements C\Input\Field\Group
20{
22
28 protected $inputs = [];
29
33 protected $lng;
34
45 public function __construct(
46 DataFactory $data_factory,
47 \ILIAS\Refinery\Factory $refinery,
49 array $inputs,
50 string $label,
51 string $byline = null
52 ) {
53 parent::__construct($data_factory, $refinery, $label, $byline);
54 $this->checkArgListElements("inputs", $inputs, InputInternal::class);
55 $this->inputs = $inputs;
56 $this->lng = $lng;
57 }
58
59 public function withDisabled($is_disabled)
60 {
61 $clone = parent::withDisabled($is_disabled);
62 $clone->inputs = array_map(function ($i) use ($is_disabled) {
63 return $i->withDisabled($is_disabled);
64 }, $this->inputs);
65 return $clone;
66 }
67
68 public function withRequired($is_required)
69 {
70 $clone = parent::withRequired($is_required);
71 $inputs = [];
72 $clone->inputs = array_map(function ($i) use ($is_required) {
73 return $i->withRequired($is_required);
74 }, $this->inputs);
75 return $clone;
76 }
77
78 public function withOnUpdate(Signal $signal)
79 {
80 $clone = parent::withOnUpdate($signal);
81 $clone->inputs = array_map(function ($i) use ($signal) {
82 return $i->withOnUpdate($signal);
83 }, $this->inputs);
84 return $clone;
85 }
86
90 protected function isClientSideValueOk($value) : bool
91 {
92 if (!is_array($value)) {
93 return false;
94 }
95 if (count($this->getInputs()) !== count($value)) {
96 return false;
97 }
98 foreach ($this->getInputs() as $key => $input) {
99 if (!array_key_exists($key, $value)) {
100 return false;
101 }
102 if (!$input->isClientSideValueOk($value[$key])) {
103 return false;
104 }
105 }
106 return true;
107 }
108
114 public function getValue()
115 {
116 return array_map(function ($i) {
117 return $i->getValue();
118 }, $this->inputs);
119 }
120
121
131 public function withValue($value)
132 {
133 $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
134 $clone = clone $this;
135 foreach ($this->inputs as $k => $i) {
136 $clone->inputs[$k] = $i->withValue($value[$k]);
137 }
138 return $clone;
139 }
140
147 public function withInput(InputData $post_input)
148 {
149 if (sizeof($this->getInputs()) === 0) {
150 return $this;
151 }
152
156 $clone = clone $this;
157
158 $inputs = [];
159 $contents = [];
160 $error = false;
161
162 foreach ($this->getInputs() as $key => $input) {
163 $inputs[$key] = $input->withInput($post_input);
164 $content = $inputs[$key]->getContent();
165 if ($content->isError()) {
166 $error = true;
167 } else {
168 $contents[$key] = $content->value();
169 }
170 }
171
172 $clone->inputs = $inputs;
173 if ($error) {
174 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
175 } else {
176 $clone->content = $clone->applyOperationsTo($contents);
177 }
178
179 if ($clone->content->isError()) {
180 $clone = $clone->withError("" . $clone->content->error());
181 }
182
183 return $clone;
184 }
185
189 public function withNameFrom(NameSource $source)
190 {
191 $clone = parent::withNameFrom($source);
195 $named_inputs = [];
196 foreach ($this->getInputs() as $key => $input) {
197 $named_inputs[$key] = $input->withNameFrom($source);
198 }
199
200 $clone->inputs = $named_inputs;
201
202 return $clone;
203 }
204
208 public function getInputs()
209 {
210 return $this->inputs;
211 }
212
216 protected function getConstraintForRequirement()
217 {
218 return null;
219 }
220
224 public function getUpdateOnLoadCode() : \Closure
225 {
226 return function () {
227 /*
228 * Currently, there is no use case for Group here. The single Inputs
229 * within the Group are responsible for handling getUpdateOnLoadCode().
230 */
231 };
232 }
233
237 public function getContent()
238 {
239 if (0 === count($this->getInputs())) {
240 return new \ILIAS\Data\Result\Ok([]);
241 }
242 return parent::getContent();
243 }
244}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:131
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
Definition: Group.php:68
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Group.php:224
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:114
withDisabled($is_disabled)
Get an input like this, but set it to a disabled state.
Definition: Group.php:59
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, \ilLanguage $lng, array $inputs, string $label, string $byline=null)
Group constructor.
Definition: Group.php:45
withOnUpdate(Signal $signal)
Trigger a signal of another component on update.
Definition: Group.php:78
language handling
This describes a group of inputs.
Definition: Group.php:12
This describes commonalities between all inputs.
Definition: Input.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11
$i
Definition: metadata.php:24
$source
Definition: metadata.php:76
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes.
trait ComponentHelper
Provides common functionality for component implementations.
Class ChatMainBarProvider \MainMenu\Provider.
$lng