ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilAdvancedMDFieldDefinitionGroupBased.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use ILIAS\UI\Component\Table\Data as DataTable;
25
32{
33 protected $options = [];
34 protected $complex = [];
35
36 protected function initADTDefinition(): ilADTDefinition
37 {
38 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
39 $def->setNumeric(false);
40
41 $options = $this->getOptions();
42 $def->setOptions($options);
43 return $def;
44 }
45
46 public function setOptions(?array $a_values = null): void
47 {
48 if ($a_values !== null) {
49 foreach ($a_values as $idx => $value) {
50 $a_values[$idx] = trim($value);
51 if (!$a_values[$idx]) {
52 unset($a_values[$idx]);
53 }
54 }
55 $a_values = array_unique($a_values);
56 // sort($a_values);
57 }
58 $this->options = $a_values;
59 }
60
61 public function getOptions(): array
62 {
63 return $this->options;
64 }
65
66 protected function importFieldDefinition(array $a_def): void
67 {
68 $this->setOptions($a_def["options"] ?? []);
69 $this->complex = $a_def["complex"] ?? [];
70 }
71
72 protected function getFieldDefinition(): array
73 {
74 return array(
75 "options" => $this->options,
76 "complex" => $this->complex
77 );
78 }
79
80 public function getFieldDefinitionForTableGUI(string $content_language): array
81 {
82 global $lng;
83
84 return array($lng->txt("options") => implode(",", $this->getOptions()));
85 }
86
91 ilPropertyFormGUI $a_form,
92 bool $a_disabled = false,
93 string $language = ''
94 ): void {
95 global $lng;
96
97 $field = new ilTextInputGUI($lng->txt("options"), "opts");
98 $field->setRequired(true);
99 $field->setMulti(true);
100 $field->setMaxLength(255); // :TODO:
101 $a_form->addItem($field);
102
103 $options = $this->getOptions();
104 if ($options) {
105 $field->setMultiValues($options);
106 $field->setValue(array_shift($options));
107 }
108
109 if ($a_disabled) {
110 $field->setDisabled(true);
111 }
112 }
113
117 public function importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language = ''): void
118 {
119 $old = $this->getOptions();
120 $new = $a_form->getInput("opts");
121
122 if (is_array($old)) {
123 $missing = array_diff($old, $new);
124 if (sizeof($missing)) {
125 foreach ($missing as $item) {
126 unset($this->complex[$item]);
127 }
128 }
129 }
130
131 $this->setOptions($new);
132 }
133
134 protected function addPropertiesToXML(ilXmlWriter $a_writer): void
135 {
136 foreach ($this->getOptions() as $value) {
137 $a_writer->xmlElement('FieldValue', null, $value);
138 }
139 }
140
141 public function importXMLProperty(string $a_key, string $a_value): void
142 {
143 $this->options[] = $a_value;
144 }
145
146
147 //
148 // import/export
149 //
150
151 public function getValueForXML(ilADT $element): string
152 {
153 return $element->getSelection();
154 }
155
156 public function importValueFromXML(string $a_cdata): void
157 {
158 /*
159 * On import from <7 options are not given by index but by
160 * their label. There is nothing in the XML by which one could
161 * tell apart legacy and standard imports, so we have to
162 * make a best guess here (32410).
163 *
164 * Might fail for enums where the labels are integers.
165 * See also ilAdvancedMDFieldDefinitionSelect::translateLegacyImportValueFromXML.
166 */
167 if (
168 !in_array($a_cdata, array_keys($this->options)) &&
169 in_array($a_cdata, $this->options)
170 ) {
171 $a_cdata = (string) array_search($a_cdata, $this->options);
172 }
173 $this->getADT()->setSelection($a_cdata);
174 }
175
176
177 //
178 // complex options
179 //
180
181 abstract public function getADTGroup(): ilADTDefinition;
182
183 abstract public function getTitles(): array;
184
185 public function hasComplexOptions(): bool
186 {
187 return true;
188 }
189
190 protected function getADTForOption(string $a_option): ilADT
191 {
192 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
193 if (array_key_exists($a_option, $this->complex)) {
194 $adt->importStdClass($this->complex[$a_option]);
195 }
196 return $adt;
197 }
198
199
200 public function getComplexOptionsOverview(): ?DataTable
201 {
202 $data_retrieval = new ComplexOptionsDataRetrieval($this);
203 $table_builder = new ComplexOptionsTableBuilder(
204 $data_retrieval,
205 $this->lng,
206 $this->ui_factory,
207 $this->http,
208 $this->ctrl,
209 $this
210 );
211
212 return $table_builder->get();
213 }
214
215 public function exportOptionToTableGUI($a_option, array &$a_item): void
216 {
217 $adt = $this->getADTForOption($a_option);
218 foreach ($adt->getElements() as $title => $element) {
219 $pres = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
220 $a_item[$title] = $pres->getList();
221 }
222 }
223
224 public function initOptionForm(ilPropertyFormGUI $a_form, $a_option_id)
225 {
226 global $lng;
227
228 $option = $this->findOptionById($a_option_id);
229 if ($option) {
230 $title = new ilTextInputGUI($lng->txt("option"), "option");
231 $title->setValue($option);
232 $title->setDisabled(true);
233 $a_form->addItem($title);
234
235 $adt = $this->getADTForOption($option);
236 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
237 $adt_form->setForm($a_form);
238
239 $titles = $this->getTitles();
240 foreach ($adt_form->getElements() as $id => $element) {
241 $element->setTitle($titles[$id]);
242 }
243
244 $adt_form->addToForm();
245 }
246 }
247
248 public function updateComplexOption(ilPropertyFormGUI $a_form, $a_option_id)
249 {
250 $option = $this->findOptionById($a_option_id);
251 if ($option) {
252 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
253 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
254 $adt_form->setForm($a_form);
255 if ($adt_form->validate()) {
256 $adt_form->importFromPost();
257 $this->importComplexOptionFromForm($option, $adt);
258 return true;
259 }
260 }
261
262 return false;
263 }
264
265 protected function importComplexOptionFromForm(string $a_option, ilADT $a_adt)
266 {
267 $this->complex[$a_option] = $a_adt->exportStdClass();
268 }
269
270 protected function findOptionById(string $a_id): ?string
271 {
272 foreach ($this->getOptions() as $item) {
273 if (md5($item) == $a_id) {
274 return $item;
275 }
276 }
277 return null;
278 }
279}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
@ilCtrl_Calls ilAdvancedMDSettingsGUI: ilPropertyFormGUI
ADT definition base class.
ADT base class.
Definition: class.ilADT.php:26
exportStdClass()
Export value as stdClass.
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, bool $a_disabled=false, string $language='')
Add custom input elements to definition form.
importFieldDefinition(array $a_def)
Import (type-specific) field definition from DB.
getValueForXML(ilADT $element)
Parse ADT value for xml (export)
updateComplexOption(ilPropertyFormGUI $a_form, $a_option_id)
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language='')
Import custom post values from definition form.
getFieldDefinitionForTableGUI(string $content_language)
Parse properties for table gui.
addPropertiesToXML(ilXmlWriter $a_writer)
Add (type-specific) properties to xml export.
importXMLProperty(string $a_key, string $a_value)
Import property from XML.
txt(string|LanguageIdentifier $a_topic, ?int $n=null)
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes a Data Table.
Definition: Data.php:33
static http()
Fetches the global http state from ILIAS.
global $lng
Definition: privfeed.php:26