ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDFieldDefinitionGroupBased.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 protected $options = [];
29 protected $complex = [];
30
31 protected function initADTDefinition(): ilADTDefinition
32 {
33 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
34 $def->setNumeric(false);
35
36 $options = $this->getOptions();
37 $def->setOptions($options);
38 return $def;
39 }
40
41 public function setOptions(?array $a_values = null): void
42 {
43 if ($a_values !== null) {
44 foreach ($a_values as $idx => $value) {
45 $a_values[$idx] = trim($value);
46 if (!$a_values[$idx]) {
47 unset($a_values[$idx]);
48 }
49 }
50 $a_values = array_unique($a_values);
51 // sort($a_values);
52 }
53 $this->options = $a_values;
54 }
55
56 public function getOptions(): array
57 {
58 return $this->options;
59 }
60
61 protected function importFieldDefinition(array $a_def): void
62 {
63 $this->setOptions($a_def["options"] ?? []);
64 $this->complex = $a_def["complex"] ?? [];
65 }
66
67 protected function getFieldDefinition(): array
68 {
69 return array(
70 "options" => $this->options,
71 "complex" => $this->complex
72 );
73 }
74
75 public function getFieldDefinitionForTableGUI(string $content_language): array
76 {
77 global $lng;
78
79 return array($lng->txt("options") => implode(",", $this->getOptions()));
80 }
81
86 ilPropertyFormGUI $a_form,
87 bool $a_disabled = false,
88 string $language = ''
89 ): void {
90 global $lng;
91
92 $field = new ilTextInputGUI($lng->txt("options"), "opts");
93 $field->setRequired(true);
94 $field->setMulti(true);
95 $field->setMaxLength(255); // :TODO:
96 $a_form->addItem($field);
97
98 $options = $this->getOptions();
99 if ($options) {
100 $field->setMultiValues($options);
101 $field->setValue(array_shift($options));
102 }
103
104 if ($a_disabled) {
105 $field->setDisabled(true);
106 }
107 }
108
112 public function importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language = ''): void
113 {
114 $old = $this->getOptions();
115 $new = $a_form->getInput("opts");
116
117 if (is_array($old)) {
118 $missing = array_diff($old, $new);
119 if (sizeof($missing)) {
120 foreach ($missing as $item) {
121 unset($this->complex[$item]);
122 }
123 }
124 }
125
126 $this->setOptions($new);
127 }
128
129 protected function addPropertiesToXML(ilXmlWriter $a_writer): void
130 {
131 foreach ($this->getOptions() as $value) {
132 $a_writer->xmlElement('FieldValue', null, $value);
133 }
134 }
135
136 public function importXMLProperty(string $a_key, string $a_value): void
137 {
138 $this->options[] = $a_value;
139 }
140
141
142 //
143 // import/export
144 //
145
146 public function getValueForXML(ilADT $element): string
147 {
148 return $element->getSelection();
149 }
150
151 public function importValueFromXML(string $a_cdata): void
152 {
153 /*
154 * On import from <7 options are not given by index but by
155 * their label. There is nothing in the XML by which one could
156 * tell apart legacy and standard imports, so we have to
157 * make a best guess here (32410).
158 *
159 * Might fail for enums where the labels are integers.
160 * See also ilAdvancedMDFieldDefinitionSelect::translateLegacyImportValueFromXML.
161 */
162 if (
163 !in_array($a_cdata, array_keys($this->options)) &&
164 in_array($a_cdata, $this->options)
165 ) {
166 $a_cdata = (string) array_search($a_cdata, $this->options);
167 }
168 $this->getADT()->setSelection($a_cdata);
169 }
170
171
172 //
173 // complex options
174 //
175
176 abstract public function getADTGroup(): ilADTDefinition;
177
178 abstract public function getTitles(): array;
179
180 public function hasComplexOptions(): bool
181 {
182 return true;
183 }
184
185 protected function getADTForOption(string $a_option): ilADT
186 {
187 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
188 if (array_key_exists($a_option, $this->complex)) {
189 $adt->importStdClass($this->complex[$a_option]);
190 }
191 return $adt;
192 }
193
197 public function getComplexOptionsOverview(object $a_parent_gui, string $a_parent_cmd): ?string
198 {
199 $tbl = new ilAdvancedMDFieldDefinitionGroupTableGUI($a_parent_gui, $a_parent_cmd, $this);
200 return $tbl->getHTML();
201 }
202
203 public function exportOptionToTableGUI($a_option, array &$a_item): void
204 {
205 $adt = $this->getADTForOption($a_option);
206 foreach ($adt->getElements() as $title => $element) {
207 $pres = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
208 $a_item[$title] = $pres->getList();
209 }
210 }
211
212 public function initOptionForm(ilPropertyFormGUI $a_form, $a_option_id)
213 {
214 global $lng;
215
216 $option = $this->findOptionById($a_option_id);
217 if ($option) {
218 $title = new ilTextInputGUI($lng->txt("option"), "option");
219 $title->setValue($option);
220 $title->setDisabled(true);
221 $a_form->addItem($title);
222
223 $adt = $this->getADTForOption($option);
224 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
225 $adt_form->setForm($a_form);
226
227 $titles = $this->getTitles();
228 foreach ($adt_form->getElements() as $id => $element) {
229 $element->setTitle($titles[$id]);
230 }
231
232 $adt_form->addToForm();
233 }
234 }
235
236 public function updateComplexOption(ilPropertyFormGUI $a_form, $a_option_id)
237 {
238 $option = $this->findOptionById($a_option_id);
239 if ($option) {
240 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
241 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
242 $adt_form->setForm($a_form);
243 if ($adt_form->validate()) {
244 $adt_form->importFromPost();
245 $this->importComplexOptionFromForm($option, $adt);
246 return true;
247 }
248 }
249
250 return false;
251 }
252
253 protected function importComplexOptionFromForm(string $a_option, ilADT $a_adt)
254 {
255 $this->complex[$a_option] = $a_adt->exportStdClass();
256 }
257
258 protected function findOptionById(string $a_id): ?string
259 {
260 foreach ($this->getOptions() as $item) {
261 if (md5($item) == $a_id) {
262 return $item;
263 }
264 }
265 return null;
266 }
267}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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.
getComplexOptionsOverview(object $a_parent_gui, string $a_parent_cmd)
null
importXMLProperty(string $a_key, string $a_value)
Import property from XML.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
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)
global $lng
Definition: privfeed.php:31