ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
InputFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
36 
38 {
40  protected Refinery $refinery;
47 
56 
57  public function __construct(
58  UIFactory $ui_factory,
59  Refinery $refinery,
60  PresenterInterface $presenter,
61  PathFactory $path_factory,
62  DataFinder $data_finder,
63  DatabaseDictionary $db_dictionary,
65  ElementHelperInterface $element_vocab_helper
66  ) {
67  $this->ui_factory = $ui_factory;
68  $this->refinery = $refinery;
69  $this->presenter = $presenter;
70  $this->path_factory = $path_factory;
71  $this->data_finder = $data_finder;
72  $this->db_dictionary = $db_dictionary;
73  $this->types = $types;
74  $this->element_vocab_helper = $element_vocab_helper;
75  }
76 
77  public function getInputFields(
78  ElementInterface $element,
79  ElementInterface $context_element,
80  bool $with_title
81  ): Section|Group {
86  $data_carriers = iterator_to_array($this->data_finder->getDataCarryingElements($element, true));
87  $conditional_elements = [];
88  $input_elements = [];
89  foreach ($data_carriers as $data_carrier) {
90  $conditional_element = null;
91  foreach ($this->element_vocab_helper->slotsForElementWithoutCondition($data_carrier) as $slot) {
96  if ($el = $this->element_vocab_helper->findElementOfCondition($slot, $data_carrier, ...$data_carriers)) {
97  $conditional_element = $data_carrier;
98  $data_carrier = $el;
99  break;
100  }
101  }
102  $path_string = $this->path_factory->toElement($data_carrier, true)
103  ->toString();
104  $input_elements[$path_string] = $data_carrier;
105  if (isset($conditional_element)) {
106  $conditional_elements[$path_string] = $conditional_element;
107  }
108  }
109 
110  $inputs = [];
111  $exclude_required = [];
112  foreach ($input_elements as $path_string => $input_element) {
113  $data_type = $input_element->getDefinition()->dataType();
114  if (isset($conditional_elements[$path_string])) {
115  $input = $this->types->conditionFactory($data_type)->getConditionInput(
116  $input_element,
117  $context_element,
118  $conditional_elements[$path_string]
119  );
120  } else {
121  $input = $this->types->factory($data_type)->getInput(
122  $input_element,
123  $context_element
124  );
125  }
126  $inputs[$path_string] = $input;
127 
132  if (is_null($this->db_dictionary->tagForElement($input_element))) {
133  $exclude_required[] = $path_string;
134  }
135  }
136 
137  if ($with_title) {
138  $fields = $this->ui_factory->section(
139  $inputs,
140  $this->presenter->elements()->nameWithParents($context_element)
141  );
142  } else {
143  $fields = $this->ui_factory->group($inputs);
144  }
145 
146  // needs flattening twice because of vocab sources in conditional inputs
147  return $this->addNotEmptyConstraintIfNeeded(
148  $context_element,
149  $this->flattenOutput($this->flattenOutput($fields)),
150  ...$exclude_required
151  );
152  }
153 
154  protected function flattenOutput(
155  Section|Group $fields
156  ): Section|Group {
157  return $fields->withAdditionalTransformation(
158  $this->refinery->custom()->transformation(function ($vs) {
159  foreach ($vs as $key => $value) {
160  if (!is_array($value)) {
161  continue;
162  }
163  $vs[$key] = $value[0];
164  foreach ($value[1] as $k => $v) {
165  $vs[$k] = $v;
166  }
167  }
168  return $vs;
169  })
170  );
171  }
172 
178  protected function addNotEmptyConstraintIfNeeded(
179  ElementInterface $context_element,
180  Section|Group $fields,
181  string ...$excluded_input_keys
182  ): Section|Group {
183  $db_tag = $this->db_dictionary->tagForElement($context_element);
184  if (!is_null($db_tag) && !$db_tag->hasData()) {
185  return $fields;
186  }
187  return $fields->withAdditionalTransformation(
188  $this->refinery->custom()->constraint(
189  function ($vs) use ($excluded_input_keys) {
190  foreach ($vs as $p => $v) {
191  if (in_array($p, $excluded_input_keys)) {
192  continue;
193  }
194  if ($v !== '' && $v !== null) {
195  return true;
196  }
197  }
198  return false;
199  },
200  $this->presenter->utilities()->txt('meta_error_empty_input')
201  )
202  );
203  }
204 }
__construct(UIFactory $ui_factory, Refinery $refinery, PresenterInterface $presenter, PathFactory $path_factory, DataFinder $data_finder, DatabaseDictionary $db_dictionary, FactoryWithConditionTypesService $types, ElementHelperInterface $element_vocab_helper)
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...
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)