ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
StringFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31use ILIAS\Refinery\Factory as Refinery;
32
34{
35 protected VocabularyAdapter $vocabulary_adapter;
37
38 public function __construct(
39 UIFactory $ui_factory,
41 ConstraintDictionary $constraint_dictionary,
42 VocabularyAdapter $vocabulary_adapter,
44 ) {
46 $this->vocabulary_adapter = $vocabulary_adapter;
47 $this->refinery = $refinery;
48 }
49
50 protected function rawInput(
51 ElementInterface $element,
52 ElementInterface $context_element,
53 SlotIdentifier $conditional_slot = SlotIdentifier::NULL
54 ): FormInput {
55 $slot = $this->vocabulary_adapter->slotForElement($element);
56
57 if (!$this->vocabulary_adapter->doesSlotHaveVocabularies($slot)) {
58 return $this->buildTextInput(
59 $this->getInputLabelFromElement($this->presenter, $element, $context_element),
60 $element,
61 true
62 );
63 }
64 if (!$this->vocabulary_adapter->doesSlotAllowCustomInput($slot)) {
65 return $this->buildSelectInput(
66 $this->getInputLabelFromElement($this->presenter, $element, $context_element),
67 $slot,
68 $element,
69 true
70 );
71 }
72 return $this->buildRadioInput(
73 $this->getInputLabelFromElement($this->presenter, $element, $context_element),
74 $slot,
75 $element
76 );
77 }
78
79 protected function buildTextInput(
80 string $label,
81 ElementInterface $element,
82 bool $with_value
83 ): FormInput {
84 $super_name = $element->getSuperElement()
85 ->getDefinition()
86 ->name();
87 if ($super_name === 'description') {
88 $input = $this->ui_factory->textarea($label);
89 } else {
90 $input = $this->ui_factory->text($label);
91 }
92
93 if ($with_value) {
94 $input = $this->addValueFromElement($element, $input);
95 }
96 return $this->addConstraintsFromElement($this->constraint_dictionary, $element, $input, !$with_value);
97 }
98
102 protected function buildSelectInput(
103 string $label,
104 SlotIdentifier $slot,
105 ElementInterface $element,
106 bool $with_value
107 ): FormInput {
108 $values = [];
109 $data = $this->getValueFromElementOrConstraint($element);
110 $raw_values = $this->vocabulary_adapter->valuesInVocabulariesForSlot($slot, $data);
111 foreach ($this->presenter->data()->vocabularyValues($slot, ...$raw_values) as $labelled_value) {
112 $values[$labelled_value->value()] = $labelled_value->label();
113 }
114 $input = $this->ui_factory->select($label, $values);
115
116 if ($with_value) {
117 $input = $this->addValueFromElement($element, $input);
118 }
119 return $this->addConstraintsFromElement($this->constraint_dictionary, $element, $input, !$with_value);
120 }
121
122 protected function buildRadioInput(
123 string $label,
124 SlotIdentifier $slot,
125 ElementInterface $element
126 ): FormInput {
127 $data = $this->getValueFromElementOrConstraint($element);
128
129 $value_label = $this->presenter->utilities()->txt('md_editor_value');
130 $select_input = $this->buildSelectInput($value_label, $slot, $element, false);
131 $text_input = $this->buildTextInput($value_label, $element, false);
132
133 if (isset($data)) {
134 if ($this->vocabulary_adapter->isValueInVocabulariesForSlot($slot, $data)) {
135 $select_input = $select_input->withValue($data);
136 $radio_value = 'from_vocab';
137 } else {
138 $text_input = $text_input->withValue($data);
139 $radio_value = 'custom';
140 }
141 }
142
143 $input = $this->ui_factory->switchableGroup(
144 [
145 'from_vocab' => $this->ui_factory->group(
146 ['value' => $select_input],
147 $this->presenter->utilities()->txt('md_editor_from_vocab_input')
148 ),
149 'custom' => $this->ui_factory->group(
150 ['value' => $text_input],
151 $this->presenter->utilities()->txt('md_editor_custom_input')
152 )
153 ],
154 $label
155 );
156 if (isset($radio_value)) {
157 $input = $input->withValue($radio_value);
158 }
159 return $this->addConstraintsFromElement($this->constraint_dictionary, $element, $input, true)
160 ->withAdditionalTransformation(
161 $this->refinery->custom()->transformation(function ($vs) {
162 return $vs[1]['value'] ?? null;
163 })
164 );
165 }
166
171 protected function getValueFromElementOrConstraint(ElementInterface $element): ?string
172 {
173 $data = null;
174 if ($element->getData()->type() !== Type::NULL) {
175 $data = $element->getData()->value();
176 }
177 return $this->getPresetValueFromConstraints($this->constraint_dictionary, $element) ?? $data;
178 }
179
180 public function getInput(
181 ElementInterface $element,
182 ElementInterface $context_element
183 ): FormInput {
184 return $this->rawInput($element, $context_element);
185 }
186
187 public function getInputInCondition(
188 ElementInterface $element,
189 ElementInterface $context_element,
190 SlotIdentifier $conditional_slot
191 ): FormInput {
192 return $this->rawInput($element, $context_element);
193 }
194}
Builds data types.
Definition: Factory.php:36
buildSelectInput(string $label, SlotIdentifier $slot, ElementInterface $element, bool $with_value)
getInput(ElementInterface $element, ElementInterface $context_element)
rawInput(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot=SlotIdentifier::NULL)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, VocabularyAdapter $vocabulary_adapter, Refinery $refinery)
getValueFromElementOrConstraint(ElementInterface $element)
Not enough to use addConstraintsFromElement here, since the value might need to be added to the list ...
buildTextInput(string $label, ElementInterface $element, bool $with_value)
buildRadioInput(string $label, SlotIdentifier $slot, ElementInterface $element)
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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