ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
HasDynamicInputsBase.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
33 use LogicException;
34 use ilLanguage;
35 
39 abstract class HasDynamicInputsBase extends FormInput implements HasDynamicInputs
40 {
41  // ==========================================
42  // BEGIN IMPLEMENTATION OF DynamicInputsAware
43  // ==========================================
44 
48  protected array $dynamic_inputs = [];
50  protected ilLanguage $language;
51 
52  public function __construct(
53  ilLanguage $language,
54  DataFactory $data_factory,
56  string $label,
57  FormInputInterface $template,
58  ?string $byline
59  ) {
60  parent::__construct($data_factory, $refinery, $label, $byline);
61  $this->dynamic_input_template = $template;
62  $this->language = $language;
63  }
64 
70  {
72  }
73 
79  public function getDynamicInputs(): array
80  {
81  return $this->dynamic_inputs;
82  }
83 
84  // ==========================================
85  // END IMPLEMENTATION OF DynamicInputsAware
86  // ==========================================
87 
88  // ==========================================
89  // BEGIN OVERWRITTEN METHODS OF Input
90  // ==========================================
91 
95  public function withValue($value): self
96  {
97  if (!$this->isDynamicInputsValueOk($value)) {
98  throw new InvalidArgumentException("Display value does not match input(-template) type.");
99  }
100 
101  $clone = clone $this;
102 
103  foreach ($value as $input_name => $input_value) {
104  $clone->dynamic_inputs[$input_name] = $clone->dynamic_input_template->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->dynamic_input_template->withDisabled($is_disabled);
114 
115  foreach ($clone->dynamic_inputs as $key => $input) {
116  $clone->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->dynamic_input_template->withNameFrom(
127  new DynamicInputsNameSource($clone->getName())
128  );
129 
130  foreach ($clone->dynamic_inputs as $key => $input) {
131  $clone->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->dynamic_inputs[$index] = $this->dynamic_input_template->withInput($input_data);
151  if ($clone->dynamic_inputs[$index]->getContent()->isOk()) {
152  $contents[] = $clone->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  if (null === $this->getTemplateForDynamicInputs()) {
174  return parent::getValue();
175  }
176 
177  $values = [];
178  foreach ($this->getDynamicInputs() as $key => $input) {
179  $values[$key] = $input->getValue();
180  }
181 
182  return $values;
183  }
184 
185  // ==========================================
186  // END OVERWRITTEN METHODS OF Input
187  // ==========================================
188 
192  protected function isDynamicInputsValueOk($value): bool
193  {
194  if (!is_array($value)) {
195  return $this->dynamic_input_template->isClientSideValueOk($value);
196  }
197 
198  if (empty($value)) {
199  return false;
200  }
201 
202  foreach ($value as $input_value) {
203  if (!$this->dynamic_input_template->isClientSideValueOk($input_value)) {
204  return false;
205  }
206  }
207 
208  return true;
209  }
210 }
getTemplateForDynamicInputs()
Returns the instance of Field which should be used to generate dynamic inputs on clientside.
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:47
getValue()
Get the value that is displayed in the input client side.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
$index
Definition: metadata.php:145
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Other than the FormInputNameSource this name source is for inputs that can be dynamically added multi...
getDynamicInputs()
Returns serverside generated dynamic Inputs, which happens when providing this InputInterface::withVa...
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(ilLanguage $language, DataFactory $data_factory, Refinery $refinery, string $label, FormInputInterface $template, ?string $byline)
withNameFrom(NameSource $source, ?string $parent_name=null)
__construct(Container $dic, ilPlugin $plugin)
This describes inputs that can be used in forms.
Definition: FormInput.php:31
Describes a source for input names.
Definition: NameSource.php:26
$source
Definition: metadata.php:93
Refinery Factory $refinery