ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
VocabValueConditionFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
32use ILIAS\Refinery\Factory as Refinery;
35
37{
38 protected PathFactory $path_factory;
40 protected VocabularyAdapter $vocabulary_adapter;
41
42 public function __construct(
43 UIFactory $ui_factory,
45 ConstraintDictionary $constraint_dictionary,
47 PathFactory $path_factory,
49 VocabularyAdapter $vocabulary_adapter
50 ) {
52 $this->path_factory = $path_factory;
53 $this->refinery = $refinery;
54 $this->vocabulary_adapter = $vocabulary_adapter;
55 }
56
57 public function getConditionInput(
58 ElementInterface $element,
59 ElementInterface $context_element,
60 ElementInterface $conditional_element
61 ): FormInput {
62 $slot = $this->vocabulary_adapter->slotForElement($element);
63 $unique_path_to_conditional_element = $this->path_factory->toElement($conditional_element, true);
64
65 $data = $this->getDataFromElementOrConstraint($element);
66 $conditional_data = $this->getDataFromElementOrConstraint($conditional_element);
67
68 $groups = [];
69 $values = iterator_to_array($this->vocabulary_adapter->valuesInVocabulariesForSlot($slot));
70 $labels_by_value = $this->getLabelsByValueForVocabulary($slot, ...$values);
71 foreach ($values as $value) {
72 $conditional_slot = $this->vocabulary_adapter->potentialSlotForElementByCondition(
73 $conditional_element,
74 $element,
75 $value
76 );
77
78 $input = $this->getInputInCondition(
79 $conditional_element,
80 $context_element,
81 $conditional_slot
82 );
83
84 if ($data === $value && isset($conditional_data)) {
85 $input = $input->withValue($conditional_data);
86 }
87
88 $groups[$value] = $this->ui_factory->group(
89 [$unique_path_to_conditional_element->toString() => $input],
90 $labels_by_value[$value] ?? ''
91 );
92 }
93
94 $radios = $this->ui_factory->switchableGroup(
95 $groups,
96 $this->getInputLabelFromElement($this->presenter, $element, $context_element)
97 );
98 if (isset($data)) {
99 $radios = $radios->withValue($data);
100 }
101
102 $source_map = $this->vocabulary_adapter->sourceMapForSlot($slot);
103 $source_path = $this->getPathToSourceElement($element);
104 return $this->addConstraintsFromElement(
105 $this->constraint_dictionary,
106 $element,
107 $radios
108 )->withAdditionalTransformation(
109 $this->refinery->custom()->transformation(function ($vs) use ($source_map, $source_path) {
110 $source = $source_map((string) $vs[0]);
111 $vs[1][$source_path->toString()] = $source;
112 return $vs;
113 })
114 );
115 }
116
117 protected function getLabelsByValueForVocabulary(SlotIdentifier $identifier, string ...$values): array
118 {
119 $presentable_labels = $this->presenter->data()->vocabularyValues(
120 $identifier,
121 ...$values
122 );
123 $labels_by_value = [];
124 foreach ($presentable_labels as $labelled_value) {
125 $labels_by_value[$labelled_value->value()] = $labelled_value->label();
126 }
127 return $labels_by_value;
128 }
129
130 protected function getDataFromElementOrConstraint(ElementInterface $element): ?string
131 {
132 $data = null;
133 if ($element->getData()->type() !== Type::NULL) {
134 $data = $element->getData()->value();
135 }
136 return $this->getPresetValueFromConstraints($this->constraint_dictionary, $element) ?? $data;
137 }
138
140 {
141 foreach ($element->getSuperElement()->getSubElements() as $el) {
142 if ($el->getDefinition()->dataType() === Type::VOCAB_SOURCE) {
143 return $this->path_factory->toElement($el, true);
144 }
145 }
146 throw new \ilMDEditorException('Vocab values must not be separated from their source.');
147 }
148}
Builds data types.
Definition: Factory.php:36
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
getConditionInput(ElementInterface $element, ElementInterface $context_element, ElementInterface $conditional_element)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, FactoryWithoutConditionTypesService $types, PathFactory $path_factory, Refinery $refinery, VocabularyAdapter $vocabulary_adapter)
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is what a factory for input fields looks like.
Definition: Factory.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc