ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilAdvancedMDFieldDefinitionGroupBased.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once "Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php";
5
15{
16 protected $options = array();
17 protected $complex = array();
18
19
20 //
21 // ADT
22 //
23
24 protected function initADTDefinition()
25 {
26 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
27 $def->setNumeric(false);
28
29 $options = $this->getOptions();
30 $def->setOptions(array_combine($options, $options));
31
32 return $def;
33 }
34
35
36 //
37 // properties
38 //
39
45 public function setOptions(array $a_values = null)
46 {
47 if ($a_values !== null) {
48 foreach ($a_values as $idx => $value) {
49 $a_values[$idx] = trim($value);
50 if (!$a_values[$idx]) {
51 unset($a_values[$idx]);
52 }
53 }
54 $a_values = array_unique($a_values);
55 // sort($a_values);
56 }
57 $this->options = $a_values;
58 }
59
65 public function getOptions()
66 {
67 return $this->options;
68 }
69
70
71 //
72 // definition (NOT ADT-based)
73 //
74
75 protected function importFieldDefinition(array $a_def)
76 {
77 $this->setOptions($a_def["options"]);
78 $this->complex = $a_def["complex"];
79 }
80
81 protected function getFieldDefinition()
82 {
83 return array(
84 "options" => $this->options,
85 "complex" => $this->complex
86 );
87 }
88
90 {
91 global $lng;
92
93 return array($lng->txt("options") => implode(",", $this->getOptions()));
94 }
95
102 protected function addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled = false)
103 {
104 global $lng;
105
106 $field = new ilTextInputGUI($lng->txt("options"), "opts");
107 $field->setRequired(true);
108 $field->setMulti(true);
109 $field->setMaxLength(255); // :TODO:
110 $a_form->addItem($field);
111
112 $options = $this->getOptions();
113 if ($options) {
114 $field->setMultiValues($options);
115 $field->setValue(array_shift($options));
116 }
117
118 if ($a_disabled) {
119 $field->setDisabled(true);
120 }
121 }
122
129 {
130 $old = $this->getOptions();
131 $new = $a_form->getInput("opts");
132
133 if (is_array($old)) {
134 $missing = array_diff($old, $new);
135 if (sizeof($missing)) {
136 foreach ($missing as $item) {
137 unset($this->complex[$item]);
138 }
139 }
140 }
141
142 $this->setOptions($new);
143 }
144
145
146 //
147 // export/import
148 //
149
150 protected function addPropertiesToXML(ilXmlWriter $a_writer)
151 {
152 foreach ($this->getOptions() as $value) {
153 $a_writer->xmlElement('FieldValue', null, $value);
154 }
155 }
156
157 public function importXMLProperty($a_key, $a_value)
158 {
159 $this->options[] = $a_value;
160 }
161
162
163 //
164 // import/export
165 //
166
167 public function getValueForXML(ilADT $element)
168 {
169 return $element->getSelection();
170 }
171
172 public function importValueFromXML($a_cdata)
173 {
174 $this->getADT()->setSelection($a_cdata);
175 }
176
177
178 //
179 // complex options
180 //
181
182 abstract public function getADTGroup();
183
184 abstract public function getTitles();
185
186 public function hasComplexOptions()
187 {
188 return true;
189 }
190
191 protected function getADTForOption($a_option)
192 {
193 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
194 if (array_key_exists($a_option, $this->complex)) {
195 $adt->importStdClass($this->complex[$a_option]);
196 }
197 return $adt;
198 }
199
200 // table
201
205 public function getComplexOptionsOverview($a_parent_gui, string $a_parent_cmd) : ?string
206 {
207 include_once "Services/AdvancedMetaData/classes/Types/class.ilAdvancedMDFieldDefinitionGroupTableGUI.php";
208 $tbl = new ilAdvancedMDFieldDefinitionGroupTableGUI($a_parent_gui, $a_parent_cmd, $this);
209 return $tbl->getHTML();
210 }
211
212 public function exportOptionToTableGUI($a_option, array &$a_item)
213 {
214 $adt = $this->getADTForOption($a_option);
215 foreach ($adt->getElements() as $title => $element) {
216 $pres = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
217 $a_item[$title] = $pres->getList();
218 }
219 }
220
221 // form
222
223 public function initOptionForm(ilPropertyFormGUI $a_form, $a_option_id)
224 {
225 global $lng;
226
227 $option = $this->findOptionById($a_option_id);
228 if ($option) {
229 $title = new ilTextInputGUI($lng->txt("option"), "option");
230 $title->setValue($option);
231 $title->setDisabled(true);
232 $a_form->addItem($title);
233
234 $adt = $this->getADTForOption($option);
235 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
236 $adt_form->setForm($a_form);
237
238 $titles = $this->getTitles();
239 foreach ($adt_form->getElements() as $id => $element) {
240 $element->setTitle($titles[$id]);
241 }
242
243 $adt_form->addToForm();
244 }
245 }
246
247 public function updateComplexOption(ilPropertyFormGUI $a_form, $a_option_id)
248 {
249 $option = $this->findOptionById($a_option_id);
250 if ($option) {
251 $adt = ilADTFactory::getInstance()->getInstanceByDefinition($this->getADTGroup());
252 $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($adt);
253 $adt_form->setForm($a_form);
254 if ($adt_form->validate()) {
255 $adt_form->importFromPost();
256 $this->importComplexOptionFromForm($option, $adt);
257 return true;
258 }
259 }
260
261 return false;
262 }
263
264 protected function importComplexOptionFromForm($a_option, ilADT $a_adt)
265 {
266 $this->complex[$a_option] = $a_adt->exportStdClass();
267 }
268
269 protected function findOptionById($a_id)
270 {
271 foreach ($this->getOptions() as $item) {
272 if (md5($item) == $a_id) {
273 return $item;
274 }
275 }
276 }
277}
An exception for terminatinating execution or to throw for unit testing.
static getInstance()
Get singleton.
ADT base class.
Definition: class.ilADT.php:12
exportStdClass()
Export value as stdClass.
importFieldDefinition(array $a_def)
Import (type-specific) field definition from DB.
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled=false)
Add input elements to definition form.
getValueForXML(ilADT $element)
Parse ADT value for xml (export)
updateComplexOption(ilPropertyFormGUI $a_form, $a_option_id)
getComplexOptionsOverview($a_parent_gui, string $a_parent_cmd)
null
importXMLProperty($a_key, $a_value)
Import property from XML.
addPropertiesToXML(ilXmlWriter $a_writer)
Add (type-specific) properties to xml export.
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form)
Import custom post values from definition form.
This class represents a property form user interface.
addItem($a_item)
Add Item (Property, SectionHeader).
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
This class represents a text property in a property form.
XML writer class.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
$lng