ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HasDynamicInputs.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ILIAS\Refinery\Factory as Refinery;
29use ILIAS\Data\Factory as DataFactory;
30use InvalidArgumentException;
31use LogicException;
33
40abstract class HasDynamicInputs extends FormInput
41{
42 // ==========================================
43 // BEGIN IMPLEMENTATION OF HasDynamicInputs
44 // ==========================================
45
49 protected array $generated_dynamic_inputs = [];
50
51 public function __construct(
52 protected Language $language,
53 DataFactory $data_factory,
55 protected FormInputInterface $dynamic_input_template,
56 string $label,
57 ?string $byline
58 ) {
59 parent::__construct($data_factory, $refinery, $label, $byline);
60 }
61
66 public function getTemplateForDynamicInputs(): FormInputInterface
67 {
68 return $this->dynamic_input_template;
69 }
70
77 public function getGeneratedDynamicInputs(): array
78 {
80 }
81
82 // ==========================================
83 // END IMPLEMENTATION OF HasDynamicInputs
84 // ==========================================
85
86 // ==========================================
87 // BEGIN OVERWRITTEN METHODS OF Input
88 // ==========================================
89
93 public function withValue($value): self
94 {
95 $this->checkArg('value', $this->isClientSideValueOk($value), "Display value does not match input(-template) type.");
96 $clone = clone $this;
97
98 if (!is_array($value)) {
99 $clone->generated_dynamic_inputs[] = $clone->getTemplateForDynamicInputs()->withValue($value);
100 return $clone;
101 }
102
103 foreach ($value as $input_name => $input_value) {
104 $clone->generated_dynamic_inputs[$input_name] = $clone->getTemplateForDynamicInputs()->withValue($input_value);
105 }
106
107 return $clone;
108 }
109
110 public function withDisabled(bool $is_disabled): self
111 {
112 $clone = parent::withDisabled($is_disabled);
113 $clone->dynamic_input_template = $clone->getTemplateForDynamicInputs()->withDisabled($is_disabled);
114
115 foreach ($clone->generated_dynamic_inputs as $key => $input) {
116 $clone->generated_dynamic_inputs[$key] = $input->withDisabled($is_disabled);
117 }
118
119 return $clone;
120 }
121
122 public function withNameFrom(NameSource $source, ?string $parent_name = null): self
123 {
124 $clone = parent::withNameFrom($source, $parent_name);
125
126 $clone->dynamic_input_template = $clone->getTemplateForDynamicInputs()->withNameFrom(
127 new DynamicInputsNameSource($clone->getName())
128 );
129
130 foreach ($clone->generated_dynamic_inputs as $key => $input) {
131 $clone->generated_dynamic_inputs[$key] = $input->withNameFrom(
132 new DynamicInputsNameSource($clone->getName())
133 );
134 }
135
136 return $clone;
137 }
138
139 public function withInput(InputData $post_data): self
140 {
141 if (null === $this->getName()) {
142 throw new LogicException(static::class . '::withNameFrom must be called first.');
143 }
144
145 $clone = clone $this;
146 $contains_error = false;
147 $contents = [];
148
149 foreach ((new DynamicInputDataIterator($post_data, $clone->getName())) as $index => $input_data) {
150 $clone->generated_dynamic_inputs[$index] = $clone->getTemplateForDynamicInputs()->withInput($input_data);
151 if ($clone->generated_dynamic_inputs[$index]->getContent()->isOk()) {
152 $contents[] = $clone->generated_dynamic_inputs[$index]->getContent()->value();
153 } else {
154 $contains_error = true;
155 }
156 }
157
158 if ($contains_error) {
159 $clone->content = $clone->data_factory->error($this->language->txt("ui_error_in_group"));
160 } else {
161 $clone->content = $clone->applyOperationsTo($contents);
162 }
163
164 if ($clone->content->isError()) {
165 $clone = $clone->withError((string) $clone->content->error());
166 }
167
168 return $clone;
169 }
170
171 public function getValue(): array
172 {
173 $values = [];
174 foreach ($this->getGeneratedDynamicInputs() as $key => $input) {
175 $values[$key] = $input->getValue();
176 }
177
178 return $values;
179 }
180
181 // ==========================================
182 // END OVERWRITTEN METHODS OF Input
183 // ==========================================
184
188 protected function isClientSideValueOk($value): bool
189 {
190 if (!is_array($value)) {
191 return $this->getTemplateForDynamicInputs()->isClientSideValueOk($value);
192 }
193
194 foreach ($value as $input_value) {
195 if (!$this->getTemplateForDynamicInputs()->isClientSideValueOk($input_value)) {
196 return false;
197 }
198 }
199
200 return true;
201 }
202}
Builds data types.
Definition: Factory.php:36
Other than the FormInputNameSource this name source is for inputs that can be dynamically added multi...
Describes an Input Field which dynamically generates inputs according to a template.
__construct(protected Language $language, DataFactory $data_factory, Refinery $refinery, protected FormInputInterface $dynamic_input_template, string $label, ?string $byline)
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
getValue()
Get the value that is displayed in the input client side.
getTemplateForDynamicInputs()
Returns an Input Field which will be used to generate similar inputs on both server and client.
getGeneratedDynamicInputs()
Returns serverside generated dynamic Inputs, which happens when providing values with.
withNameFrom(NameSource $source, ?string $parent_name=null)
This describes inputs that can be used in forms.
Definition: FormInput.php:33
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
Describes a source for input names.
Definition: NameSource.php:27
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withNameFrom(NameSource $source, ?string $parent_name=null)
@inheritDoc