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