ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
InputFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
34 
36 {
38  protected Refinery $refinery;
41  protected VocabularyAdapter $vocabulary_adapter;
44 
53 
54  public function __construct(
55  UIFactory $ui_factory,
56  Refinery $refinery,
57  PresenterInterface $presenter,
58  PathFactory $path_factory,
59  DataFinder $data_finder,
60  DatabaseDictionary $db_dictionary,
62  VocabularyAdapter $vocabulary_adapter
63  ) {
64  $this->ui_factory = $ui_factory;
65  $this->refinery = $refinery;
66  $this->presenter = $presenter;
67  $this->path_factory = $path_factory;
68  $this->data_finder = $data_finder;
69  $this->db_dictionary = $db_dictionary;
70  $this->types = $types;
71  $this->vocabulary_adapter = $vocabulary_adapter;
72  }
73 
74  public function getInputFields(
75  ElementInterface $element,
76  ElementInterface $context_element,
77  bool $with_title
78  ): Section|Group {
83  $data_carriers = iterator_to_array($this->data_finder->getDataCarryingElements($element, true));
84  $conditional_elements = [];
85  $input_elements = [];
86  foreach ($data_carriers as $data_carrier) {
87  $conditional_element = null;
88  foreach ($this->vocabulary_adapter->slotsForElementWithoutCondition($data_carrier) as $slot) {
93  if ($el = $this->vocabulary_adapter->findElementOfCondition($slot, $data_carrier, ...$data_carriers)) {
94  $conditional_element = $data_carrier;
95  $data_carrier = $el;
96  break;
97  }
98  }
99  $path_string = $this->path_factory->toElement($data_carrier, true)
100  ->toString();
101  $input_elements[$path_string] = $data_carrier;
102  if (isset($conditional_element)) {
103  $conditional_elements[$path_string] = $conditional_element;
104  }
105  }
106 
107  $inputs = [];
108  $exclude_required = [];
109  foreach ($input_elements as $path_string => $input_element) {
110  $data_type = $input_element->getDefinition()->dataType();
111  if (isset($conditional_elements[$path_string])) {
112  $input = $this->types->conditionFactory($data_type)->getConditionInput(
113  $input_element,
114  $context_element,
115  $conditional_elements[$path_string]
116  );
117  } else {
118  $input = $this->types->factory($data_type)->getInput(
119  $input_element,
120  $context_element
121  );
122  }
123  $inputs[$path_string] = $input;
124 
129  if (is_null($this->db_dictionary->tagForElement($input_element))) {
130  $exclude_required[] = $path_string;
131  }
132  }
133 
134  if ($with_title) {
135  $fields = $this->ui_factory->section(
136  $inputs,
137  $this->presenter->elements()->nameWithParents($context_element)
138  );
139  } else {
140  $fields = $this->ui_factory->group($inputs);
141  }
142 
143  // needs flattening twice because of vocab sources in conditional inputs
144  return $this->addNotEmptyConstraintIfNeeded(
145  $context_element,
146  $this->flattenOutput($this->flattenOutput($fields)),
147  ...$exclude_required
148  );
149  }
150 
151  protected function flattenOutput(
152  Section|Group $fields
153  ): Section|Group {
154  return $fields->withAdditionalTransformation(
155  $this->refinery->custom()->transformation(function ($vs) {
156  foreach ($vs as $key => $value) {
157  if (!is_array($value)) {
158  continue;
159  }
160  $vs[$key] = $value[0];
161  foreach ($value[1] as $k => $v) {
162  $vs[$k] = $v;
163  }
164  }
165  return $vs;
166  })
167  );
168  }
169 
175  protected function addNotEmptyConstraintIfNeeded(
176  ElementInterface $context_element,
177  Section|Group $fields,
178  string ...$excluded_input_keys
179  ): Section|Group {
180  $db_tag = $this->db_dictionary->tagForElement($context_element);
181  if (!is_null($db_tag) && !$db_tag->hasData()) {
182  return $fields;
183  }
184  return $fields->withAdditionalTransformation(
185  $this->refinery->custom()->constraint(
186  function ($vs) use ($excluded_input_keys) {
187  foreach ($vs as $p => $v) {
188  if (in_array($p, $excluded_input_keys)) {
189  continue;
190  }
191  if ($v !== '' && $v !== null) {
192  return true;
193  }
194  }
195  return false;
196  },
197  $this->presenter->utilities()->txt('meta_error_empty_input')
198  )
199  );
200  }
201 }
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...
__construct(UIFactory $ui_factory, Refinery $refinery, PresenterInterface $presenter, PathFactory $path_factory, DataFinder $data_finder, DatabaseDictionary $db_dictionary, FactoryWithConditionTypesService $types, VocabularyAdapter $vocabulary_adapter)
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the content of the input.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
DatabaseDictionary $db_dictionary
This is only here because the editor needs to know which elements can be created (meaning have a non-...
getInputFields(ElementInterface $element, ElementInterface $context_element, bool $with_title)