ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
InputFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
37 
39 {
41  protected Refinery $refinery;
48 
57 
58  public function __construct(
59  UIFactory $ui_factory,
60  Refinery $refinery,
61  PresenterInterface $presenter,
62  PathFactory $path_factory,
63  NavigatorFactoryInterface $navigator_factory,
64  DataFinder $data_finder,
65  VocabulariesInterface $vocabularies,
66  DatabaseDictionary $db_dictionary,
68  ) {
69  $this->ui_factory = $ui_factory;
70  $this->refinery = $refinery;
71  $this->presenter = $presenter;
72  $this->path_factory = $path_factory;
73  $this->navigator_factory = $navigator_factory;
74  $this->data_finder = $data_finder;
75  $this->vocabularies = $vocabularies;
76  $this->db_dictionary = $db_dictionary;
77  $this->types = $types;
78  }
79 
80  public function getInputFields(
81  ElementInterface $element,
82  ElementInterface $context_element,
83  bool $with_title
84  ): Section|Group {
85  $conditional_elements = [];
86  $input_elements = [];
87  foreach ($this->data_finder->getDataCarryingElements($element) as $data_carrier) {
88  $conditional_element = null;
93  if (
94  $data_carrier->getDefinition()->dataType() !== Type::VOCAB_SOURCE &&
95  $el = $this->getConditionElement($data_carrier)
96  ) {
97  $conditional_element = $data_carrier;
98  $data_carrier = $el;
99  }
100  $path_string = $this->path_factory->toElement($data_carrier, true)
101  ->toString();
102  $input_elements[$path_string] = $data_carrier;
103  if (isset($conditional_element)) {
104  $conditional_elements[$path_string][] = $conditional_element;
105  }
106  }
107 
108  $inputs = [];
109  $exclude_required = [];
110  foreach ($input_elements as $path_string => $input_element) {
111  $data_type = $input_element->getDefinition()->dataType();
112  if (isset($conditional_elements[$path_string])) {
113  $input = $this->types->conditionFactory($data_type)->getConditionInput(
114  $input_element,
115  $context_element,
116  ...$conditional_elements[$path_string]
117  );
118  } else {
119  $input = $this->types->factory($data_type)->getInput(
120  $input_element,
121  $context_element
122  );
123  }
124  $inputs[$path_string] = $input;
125 
130  if (is_null($this->db_dictionary->tagForElement($input_element))) {
131  $exclude_required[] = $path_string;
132  }
133  }
134 
135  if ($with_title) {
136  $fields = $this->ui_factory->section(
137  $inputs,
138  $this->presenter->elements()->nameWithParents($context_element)
139  );
140  } else {
141  $fields = $this->ui_factory->group($inputs);
142  }
143 
144  return $this->addNotEmptyConstraintIfNeeded(
145  $context_element,
146  $this->flattenOutput($fields),
147  ...$exclude_required
148  );
149  }
150 
151  protected function getConditionElement(
152  ElementInterface $element
153  ): ?ElementInterface {
154  foreach ($this->vocabularies->vocabulariesForElement($element) as $vocab) {
155  if ($path = $vocab->condition()?->path()) {
156  return $this->navigator_factory->navigator($path, $element)
157  ->lastElementAtFinalStep();
158  }
159  }
160  return null;
161  }
162 
163  protected function flattenOutput(
164  Section|Group $fields
165  ): Section|Group {
166  return $fields->withAdditionalTransformation(
167  $this->refinery->custom()->transformation(function ($vs) {
168  foreach ($vs as $key => $value) {
169  if (is_array($value)) {
170  $vs[$key] = $value[0];
171  foreach ($value[1] as $k => $v) {
172  $vs[$k] = $v[0];
173  }
174  }
175  }
176  return $vs;
177  })
178  );
179  }
180 
186  protected function addNotEmptyConstraintIfNeeded(
187  ElementInterface $context_element,
188  Section|Group $fields,
189  string ...$excluded_input_keys
190  ): Section|Group {
191  $db_tag = $this->db_dictionary->tagForElement($context_element);
192  if (!is_null($db_tag) && !$db_tag->hasData()) {
193  return $fields;
194  }
195  return $fields->withAdditionalTransformation(
196  $this->refinery->custom()->constraint(
197  function ($vs) use ($excluded_input_keys) {
198  foreach ($vs as $p => $v) {
199  if (in_array($p, $excluded_input_keys)) {
200  continue;
201  }
202  if ($v !== '' && $v !== null) {
203  return true;
204  }
205  }
206  return false;
207  },
208  $this->presenter->utilities()->txt('meta_error_empty_input')
209  )
210  );
211  }
212 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
This describes section inputs.
Definition: Section.php:28
addNotEmptyConstraintIfNeeded(ElementInterface $context_element, Section|Group $fields, string ... $excluded_input_keys)
If the current element can&#39;t be created on its own due to the db structure, the editor has to require...
$path
Definition: ltiservices.php:32
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the content of the input.
string $key
Consumer key/client ID value.
Definition: System.php:193
DatabaseDictionary $db_dictionary
This is only here because the editor needs to know which elements can be created (meaning have a non-...
$vocab
getInputFields(ElementInterface $element, ElementInterface $context_element, bool $with_title)
__construct(UIFactory $ui_factory, Refinery $refinery, PresenterInterface $presenter, PathFactory $path_factory, NavigatorFactoryInterface $navigator_factory, DataFinder $data_finder, VocabulariesInterface $vocabularies, DatabaseDictionary $db_dictionary, FactoryWithConditionTypesService $types)
vocabularies(ElementInterface $element, bool $ignore_marker)