ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
VocabValueFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
35 
37 {
39  protected Refinery $refinery;
41 
42  public function __construct(
46  ElementHelperInterface $element_vocab_helper,
47  Refinery $refinery,
48  PathFactory $path_factory
49  ) {
50  parent::__construct($ui_factory, $presenter, $constraint_dictionary);
51  $this->element_vocab_helper = $element_vocab_helper;
52  $this->refinery = $refinery;
53  $this->path_factory = $path_factory;
54  }
55 
56  protected function rawInput(
57  ElementInterface $element,
58  ElementInterface $context_element,
59  SlotIdentifier $slot,
60  bool $add_value_from_data
61  ): FormInput {
62  $data = null;
63  if ($element->getData()->type() !== Type::NULL) {
64  $data = $element->getData()->value();
65  }
66 
67  $raw_values = [];
68  $sources_by_value = [];
69  foreach ($this->element_vocab_helper->vocabulariesForSlot($slot) as $vocab) {
70  $values_from_vocab = iterator_to_array($vocab->values());
71 
72  $raw_values = array_merge($raw_values, $values_from_vocab);
73  $sources_by_value = array_merge(
74  $sources_by_value,
75  array_fill_keys($values_from_vocab, $vocab->source())
76  );
77  }
78  if ($add_value_from_data && isset($data) && !in_array($data, $raw_values)) {
79  array_unshift($raw_values, $data);
80  }
81 
82  $values = [];
83  foreach ($this->presenter->data()->vocabularyValues($slot, ...$raw_values) as $labelled_value) {
84  $values[$labelled_value->value()] = $labelled_value->label();
85  }
86 
87  $input = $this->ui_factory->select(
88  $this->getInputLabelFromElement($this->presenter, $element, $context_element),
89  $values
90  );
91  if ($add_value_from_data && isset($data)) {
92  $input = $input->withValue($data);
93  }
94 
95  $source_path = $this->getPathToSourceElement($element);
96  return $this->addConstraintsFromElement($this->constraint_dictionary, $element, $input)
97  ->withAdditionalTransformation(
98  $this->refinery->custom()->transformation(function ($vs) use ($sources_by_value, $source_path) {
99  $source = $sources_by_value[$vs] ?? null;
100  return [
101  $vs,
102  [$source_path->toString() => $source]
103  ];
104  })
105  );
106  }
107 
108  public function getInput(
109  ElementInterface $element,
110  ElementInterface $context_element
111  ): FormInput {
112  return $this->rawInput(
113  $element,
114  $context_element,
115  $slot = $this->element_vocab_helper->slotForElement($element),
116  true
117  );
118  }
119 
120  public function getInputInCondition(
121  ElementInterface $element,
122  ElementInterface $context_element,
123  SlotIdentifier $conditional_slot
124  ): FormInput {
125  $slot = $this->element_vocab_helper->slotForElement($element);
126  return $this->rawInput(
127  $element,
128  $context_element,
129  $conditional_slot,
130  $slot === $conditional_slot
131  );
132  }
133 
135  {
136  foreach ($element->getSuperElement()->getSubElements() as $el) {
137  if ($el->getDefinition()->dataType() === Type::VOCAB_SOURCE) {
138  return $this->path_factory->toElement($el, true);
139  }
140  }
141  throw new \ilMDEditorException('Vocab values must not be separated from their source.');
142  }
143 }
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, ElementHelperInterface $element_vocab_helper, Refinery $refinery, PathFactory $path_factory)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
rawInput(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $slot, bool $add_value_from_data)
getInput(ElementInterface $element, ElementInterface $context_element)
$vocab
__construct(Container $dic, ilPlugin $plugin)
This describes inputs that can be used in forms.
Definition: FormInput.php:32