ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilAdvancedMDFieldDefinitionSelect.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 $confirm_objects; // [array]
18 protected $confirmed_objects; // [array]
19
20
21 //
22 // generic types
23 //
24
25 public function getType()
26 {
27 return self::TYPE_SELECT;
28 }
29
30
31 //
32 // ADT
33 //
34
35 protected function initADTDefinition()
36 {
37 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
38 $def->setNumeric(false);
39
40 $options = $this->getOptions();
41 $def->setOptions(array_combine($options, $options));
42
43 return $def;
44 }
45
46
47 //
48 // properties
49 //
50
56 public function setOptions(array $a_values = null)
57 {
58 if($a_values !== null)
59 {
60 foreach($a_values as $idx => $value)
61 {
62 $a_values[$idx] = trim($value);
63 if(!$a_values[$idx])
64 {
65 unset($a_values[$idx]);
66 }
67 }
68 $a_values = array_unique($a_values);
69 // sort($a_values);
70 }
71 $this->options = $a_values;
72 }
73
79 public function getOptions()
80 {
81 return $this->options;
82 }
83
84
85 //
86 // definition (NOT ADT-based)
87 //
88
89 protected function importFieldDefinition(array $a_def)
90 {
91 $this->setOptions($a_def);
92 }
93
94 protected function getFieldDefinition()
95 {
96 return $this->options;
97 }
98
100 {
101 global $lng;
102
103 return array($lng->txt("meta_advmd_select_options") => implode(",", $this->getOptions()));
104 }
105
112 public function addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled = false)
113 {
114 global $lng;
115
116 $field = new ilTextInputGUI($lng->txt("meta_advmd_select_options"), "opts");
117 $field->setRequired(true);
118 $field->setMulti(true, true);
119 $field->setMaxLength(255); // :TODO:
120 $a_form->addItem($field);
121
122 $options = $this->getOptions();
123 if($options)
124 {
125 $field->setMultiValues($options);
126 $field->setValue(array_shift($options));
127 }
128
129 if($a_disabled)
130 {
131 $field->setDisabled(true);
132 }
133 }
134
140 protected function buildConfirmedObjects(ilPropertyFormGUI $a_form)
141 {
142 // #15719
143 $recipes = $a_form->getInput("conf_det");
144 if(is_array($recipes[$this->getFieldId()]))
145 {
146 $recipes = $recipes[$this->getFieldId()];
147 $sum = $a_form->getInput("conf_det_act");
148 $sum = $sum[$this->getFieldId()];
149 $sgl = $a_form->getInput("conf");
150 $sgl = $sgl[$this->getFieldId()];
151
152 $res = array();
153 foreach($recipes as $old_option => $recipe)
154 {
155 $sum_act = $sum[$old_option];
156 $sgl_act = $sgl[$old_option];
157
158 if($recipe == "sum")
159 {
160 foreach(array_keys($sgl_act) as $obj_idx)
161 {
162 $res[$old_option][$obj_idx] = $sum_act;
163 }
164 }
165 else
166 {
167 $res[$old_option] = $sgl_act;
168 }
169 }
170
171 return $res;
172 }
173 }
174
181 {
182 $old = $this->getOptions();
183 $new = $a_form->getInput("opts");
184
185 $missing = array_diff($old, $new);
186 if(sizeof($missing))
187 {
188 $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
189 if(!is_array($this->confirmed_objects))
190 {
192 $primary = array(
193 "field_id" => array("integer", $this->getFieldId()),
194 ilADTActiveRecordByType::SINGLE_COLUMN_NAME => array("text", $missing)
195 );
196 $in_use = ilADTActiveRecordByType::readByPrimary("adv_md_values", $primary, "Enum");
197 if($in_use)
198 {
199 $this->confirm_objects = array();
200 foreach($in_use as $item)
201 {
202 $this->confirm_objects[$item[ilADTActiveRecordByType::SINGLE_COLUMN_NAME]][] = array($item["obj_id"], $item["sub_type"], $item["sub_id"]);
203 }
204 }
205 }
206 }
207
208 $this->setOptions($new);
209 }
210
212 {
213 return sizeof($this->confirm_objects);
214 }
215
217 {
218 global $lng, $objDefinition;
219
220 $a_form->getItemByPostVar("opts")->setDisabled(true);
221
222 if(sizeof($this->confirm_objects))
223 {
224 $new_options = $a_form->getInput("opts");
225
226 $sec = new ilFormSectionHeaderGUI();
227 $sec->setTitle($lng->txt("md_adv_confirm_definition_select_section"));
228 $a_form->addItem($sec);
229
230 foreach($this->confirm_objects as $old_option => $items)
231 {
232 $details = new ilRadioGroupInputGUI($lng->txt("md_adv_confirm_definition_select_option").': "'.$old_option.'"', "conf_det[".$this->getFieldId()."][".$old_option."]");
233 $details->setRequired(true);
234 $details->setValue("sum");
235 $a_form->addItem($details);
236
237 $sum = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_all"), "sum");
238 $details->addOption($sum);
239
240 $sel = new ilSelectInputGUI($lng->txt("md_adv_confirm_definition_select_option_all_action"),
241 "conf_det_act[".$this->getFieldId()."][".$old_option."]");
242 $options = array(""=>$lng->txt("md_adv_confirm_definition_select_option_remove"));
243 foreach($new_options as $new_option)
244 {
245 $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite").': "'.$new_option.'"';
246 }
247 $sel->setOptions($options);
248 $sum->addSubItem($sel);
249
250 $single = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_single"), "sgl");
251 $details->addOption($single);
252
253 foreach($items as $item)
254 {
255 $obj_id = $item[0];
256 $sub_type = $item[1];
257 $sub_id = $item[2];
258
259 $item_id = $obj_id."_".$sub_type."_".$sub_id;
260
261 $type = ilObject::_lookupType($obj_id);
262 $type_title = $lng->txt("obj_".$type);
263 $title = ' "'.ilObject::_lookupTitle($obj_id).'"';
264
265 if($sub_id)
266 {
267 $class = "ilObj".$objDefinition->getClassName($type);
268 $class_path = $objDefinition->getLocation($type);
269 include_once $class_path."/class.".$class.".php";
270 if(class_implements($class, ilAdvancedMetaDataSubItem))
271 {
272 $sub_title = $class::getAdvMDSubItemTitle($obj_id, $sub_type, $sub_id);
273 if($sub_title)
274 {
275 $title .= ' ('.$sub_title.')';
276 }
277 }
278 }
279
280 $sel = new ilSelectInputGUI($type_title.' '.$title,
281 "conf[".$this->getFieldId()."][".$old_option."][".$item_id."]");
282 $options = array(""=>$lng->txt("md_adv_confirm_definition_select_option_remove"));
283 foreach($new_options as $new_option)
284 {
285 $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite").': "'.$new_option.'"';
286 }
287 $sel->setOptions($options);
288
289 $single->addSubItem($sel);
290 }
291 }
292 }
293 }
294
295
296 //
297 // definition CRUD
298 //
299
300 public function update()
301 {
302 parent::update();
303
304 if(sizeof($this->confirmed_objects))
305 {
307 foreach($this->confirmed_objects as $old_option => $item_ids)
308 {
309 foreach($item_ids as $item => $new_option)
310 {
311 $item = explode("_", $item);
312 $obj_id = $item[0];
313 $sub_type = $item[1];
314 $sub_id = $item[2];
315
316 if(!$new_option)
317 {
318 // remove existing value
319 $primary = array(
320 "obj_id" => array("integer", $obj_id),
321 "sub_type" => array("text", $sub_type),
322 "sub_id" => array("integer", $sub_id),
323 "field_id" => array("integer", $this->getFieldId())
324 );
325 ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "Enum");
326 }
327 else
328 {
329 // update existing value
330 $primary = array(
331 "obj_id" => array("integer", $obj_id),
332 "sub_type" => array("text", $sub_type),
333 "sub_id" => array("integer", $sub_id),
334 "field_id" => array("integer", $this->getFieldId())
335 );
336 ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "Enum", $new_option);
337 }
338
339 if($sub_type == "wpg")
340 {
341 // #15763 - adapt advmd page lists
342 include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
343 ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option);
344 }
345 }
346 }
347 }
348 }
349
350
351 //
352 // export/import
353 //
354
355 protected function addPropertiesToXML(ilXmlWriter $a_writer)
356 {
357 foreach($this->getOptions() as $value)
358 {
359 $a_writer->xmlElement('FieldValue',null,$value);
360 }
361 }
362
363 public function importXMLProperty($a_key, $a_value)
364 {
365 $this->options[] = $a_value;
366 }
367
368
369 //
370 // import/export
371 //
372
373 public function getValueForXML(ilADT $element)
374 {
375 return $element->getSelection();
376 }
377
378 public function importValueFromXML($a_cdata)
379 {
380 $this->getADT()->setSelection($a_cdata);
381 }
382
383
384 //
385 // presentation
386 //
387
389 {
390 $a_enum->setAutoSort(false);
391 }
392}
393
394?>
static writeByPrimary($a_table, array $a_primary, $a_type, $a_value)
Write directly.
static deleteByPrimary($a_table, array $a_primary, $a_type=null)
Delete values by (partial) primary key.
static readByPrimary($a_table, array $a_primary, $a_type=null)
Read directly.
static getInstance()
Get singleton.
static initActiveRecordByType()
Init active record by type.
ADT base class.
Definition: class.ilADT.php:12
importXMLProperty($a_key, $a_value)
Import property from XML.
getFieldDefinitionForTableGUI()
Parse properties for table gui.
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form)
Import custom post values from definition form.
importFieldDefinition(array $a_def)
Import (type-specific) field definition from DB.
getValueForXML(ilADT $element)
Parse ADT value for xml (export)
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled=false)
Add input elements to definition form.
getFieldDefinition()
Get (type-specific) field definition.
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.
addPropertiesToXML(ilXmlWriter $a_writer)
Add (type-specific) properties to xml export.
This class represents a section header in a property form.
static _lookupType($a_id, $a_reference=false)
lookup object type
static migrateField($a_obj_id, $a_field_id, $old_option, $new_option, $a_is_multi=false)
Migrate search/filter values on advmd change.
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.
getItemByPostVar($a_post_var)
Get Item by POST variable.
This class represents a property in a property form.
This class represents an option in a radio group.
This class represents a selection list property in a property form.
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)
global $lng
Definition: privfeed.php:40