ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_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  const REMOVE_ACTION_ID = "-iladvmdrm-";
21 
22 
23  //
24  // generic types
25  //
26 
27  public function getType()
28  {
29  return self::TYPE_SELECT;
30  }
31 
32 
33  //
34  // ADT
35  //
36 
37  protected function initADTDefinition()
38  {
39  $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
40  $def->setNumeric(false);
41 
42  $options = $this->getOptions();
43  $def->setOptions(array_combine($options, $options));
44 
45  return $def;
46  }
47 
48 
49  //
50  // properties
51  //
52 
58  public function setOptions(array $a_values = null)
59  {
60  if($a_values !== null)
61  {
62  foreach($a_values as $idx => $value)
63  {
64  $a_values[$idx] = trim($value);
65  if(!$a_values[$idx])
66  {
67  unset($a_values[$idx]);
68  }
69  }
70  $a_values = array_unique($a_values);
71  // sort($a_values);
72  }
73  $this->options = $a_values;
74  }
75 
81  public function getOptions()
82  {
83  return $this->options;
84  }
85 
86 
87  //
88  // definition (NOT ADT-based)
89  //
90 
91  protected function importFieldDefinition(array $a_def)
92  {
93  $this->setOptions($a_def);
94  }
95 
96  protected function getFieldDefinition()
97  {
98  return $this->options;
99  }
100 
102  {
103  global $lng;
104 
105  return array($lng->txt("meta_advmd_select_options") => implode(",", $this->getOptions()));
106  }
107 
114  public function addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled = false)
115  {
116  global $lng;
117 
118  $field = new ilTextInputGUI($lng->txt("meta_advmd_select_options"), "opts");
119  $field->setRequired(true);
120  $field->setMulti(true, true);
121  $field->setMaxLength(255); // :TODO:
122  $a_form->addItem($field);
123 
124  $options = $this->getOptions();
125  if($options)
126  {
127  $field->setMultiValues($options);
128  $field->setValue(array_shift($options));
129  }
130 
131  if($a_disabled)
132  {
133  $field->setDisabled(true);
134  }
135  }
136 
142  protected function buildConfirmedObjects(ilPropertyFormGUI $a_form)
143  {
144  // #15719
145  $recipes = $a_form->getInput("conf_det");
146  if(is_array($recipes[$this->getFieldId()]))
147  {
148  $recipes = $recipes[$this->getFieldId()];
149  $sum = $a_form->getInput("conf_det_act");
150  $sum = $sum[$this->getFieldId()];
151  $sgl = $a_form->getInput("conf");
152  $sgl = $sgl[$this->getFieldId()];
153 
154  $res = array();
155  foreach($recipes as $old_option => $recipe)
156  {
157  $sum_act = $sum[$old_option];
158  $sgl_act = $sgl[$old_option];
159 
160  if($recipe == "sum")
161  {
162  // #18885
163  if(!$sum_act)
164  {
165  return;
166  }
167 
168  foreach(array_keys($sgl_act) as $obj_idx)
169  {
170  if($sum_act == self::REMOVE_ACTION_ID)
171  {
172  $sum_act = "";
173  }
174  $res[$old_option][$obj_idx] = $sum_act;
175  }
176  }
177  else
178  {
179  // #18885
180  foreach($sgl_act as $sgl_index => $sgl_item)
181  {
182  if(!$sgl_item)
183  {
184  return;
185  }
186  else if($sgl_item == self::REMOVE_ACTION_ID)
187  {
188  $sgl_act[$sgl_index] = "";
189  }
190  }
191 
192  $res[$old_option] = $sgl_act;
193  }
194  }
195 
196  return $res;
197  }
198  }
199 
206  {
207  $old = $this->getOptions();
208  $new = $a_form->getInput("opts");
209 
210  $missing = array_diff($old, $new);
211  if(sizeof($missing))
212  {
213  $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
214  if(!is_array($this->confirmed_objects))
215  {
217  $primary = array(
218  "field_id" => array("integer", $this->getFieldId()),
220  );
221  $in_use = ilADTActiveRecordByType::readByPrimary("adv_md_values", $primary, "Enum");
222  if($in_use)
223  {
224  $this->confirm_objects = array();
225  foreach($in_use as $item)
226  {
227  $this->confirm_objects[$item[ilADTActiveRecordByType::SINGLE_COLUMN_NAME]][] = array($item["obj_id"], $item["sub_type"], $item["sub_id"]);
228  }
229  }
230  }
231  }
232 
233  $this->setOptions($new);
234  }
235 
237  {
238  return sizeof($this->confirm_objects);
239  }
240 
242  {
243  global $lng, $objDefinition;
244 
245  $a_form->getItemByPostVar("opts")->setDisabled(true);
246 
247  if(sizeof($this->confirm_objects))
248  {
249  $new_options = $a_form->getInput("opts");
250 
251  $sec = new ilFormSectionHeaderGUI();
252  $sec->setTitle($lng->txt("md_adv_confirm_definition_select_section"));
253  $a_form->addItem($sec);
254 
255  foreach($this->confirm_objects as $old_option => $items)
256  {
257  $details = new ilRadioGroupInputGUI($lng->txt("md_adv_confirm_definition_select_option").': "'.$old_option.'"', "conf_det[".$this->getFieldId()."][".$old_option."]");
258  $details->setRequired(true);
259  $details->setValue("sum");
260  $a_form->addItem($details);
261 
262  // automatic reload does not work
263  if(isset($_POST["conf_det"][$this->getFieldId()][$old_option]))
264  {
265  $details->setValue($_POST["conf_det"][$this->getFieldId()][$old_option]);
266  }
267 
268  $sum = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_all"), "sum");
269  $details->addOption($sum);
270 
271  $sel = new ilSelectInputGUI($lng->txt("md_adv_confirm_definition_select_option_all_action"),
272  "conf_det_act[".$this->getFieldId()."][".$old_option."]");
273  $sel->setRequired(true);
274  $options = array(
275  "" => $lng->txt("please_select"),
276  self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
277  );
278  foreach($new_options as $new_option)
279  {
280  $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite").': "'.$new_option.'"';
281  }
282  $sel->setOptions($options);
283  $sum->addSubItem($sel);
284 
285  // automatic reload does not work
286  if(isset($_POST["conf_det_act"][$this->getFieldId()][$old_option]))
287  {
288  if($_POST["conf_det_act"][$this->getFieldId()][$old_option])
289  {
290  $sel->setValue($_POST["conf_det_act"][$this->getFieldId()][$old_option]);
291  }
292  else if($_POST["conf_det"][$this->getFieldId()][$old_option] == "sum")
293  {
294  $sel->setAlert($lng->txt("msg_input_is_required"));
295  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
296  }
297  }
298 
299  $single = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_single"), "sgl");
300  $details->addOption($single);
301 
302  foreach($items as $item)
303  {
304  $obj_id = $item[0];
305  $sub_type = $item[1];
306  $sub_id = $item[2];
307 
308  $item_id = $obj_id."_".$sub_type."_".$sub_id;
309 
310  $type = ilObject::_lookupType($obj_id);
311  $type_title = $lng->txt("obj_".$type);
312  $title = ' "'.ilObject::_lookupTitle($obj_id).'"';
313 
314  if($sub_id)
315  {
316  $class = "ilObj".$objDefinition->getClassName($type);
317  $class_path = $objDefinition->getLocation($type);
318  include_once $class_path."/class.".$class.".php";
319  if(class_implements($class, ilAdvancedMetaDataSubItem))
320  {
321  $sub_title = $class::getAdvMDSubItemTitle($obj_id, $sub_type, $sub_id);
322  if($sub_title)
323  {
324  $title .= ' ('.$sub_title.')';
325  }
326  }
327  }
328 
329  $sel = new ilSelectInputGUI($type_title.' '.$title,
330  "conf[".$this->getFieldId()."][".$old_option."][".$item_id."]");
331  $sel->setRequired(true);
332  $options = array(
333  "" => $lng->txt("please_select"),
334  self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
335  );
336  foreach($new_options as $new_option)
337  {
338  $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite").': "'.$new_option.'"';
339  }
340  $sel->setOptions($options);
341 
342  // automatic reload does not work
343  if(isset($_POST["conf"][$this->getFieldId()][$old_option][$item_id]))
344  {
345  if($_POST["conf"][$this->getFieldId()][$old_option][$item_id])
346  {
347  $sel->setValue($_POST["conf"][$this->getFieldId()][$old_option][$item_id]);
348  }
349  else if($_POST["conf_det"][$this->getFieldId()][$old_option] == "sgl")
350  {
351  $sel->setAlert($lng->txt("msg_input_is_required"));
352  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
353  }
354  }
355 
356  $single->addSubItem($sel);
357  }
358  }
359  }
360  }
361 
362 
363  //
364  // definition CRUD
365  //
366 
367  public function update()
368  {
369  parent::update();
370 
371  if(sizeof($this->confirmed_objects))
372  {
374  foreach($this->confirmed_objects as $old_option => $item_ids)
375  {
376  foreach($item_ids as $item => $new_option)
377  {
378  $item = explode("_", $item);
379  $obj_id = $item[0];
380  $sub_type = $item[1];
381  $sub_id = $item[2];
382 
383  if(!$new_option)
384  {
385  // remove existing value
386  $primary = array(
387  "obj_id" => array("integer", $obj_id),
388  "sub_type" => array("text", $sub_type),
389  "sub_id" => array("integer", $sub_id),
390  "field_id" => array("integer", $this->getFieldId())
391  );
392  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "Enum");
393  }
394  else
395  {
396  // update existing value
397  $primary = array(
398  "obj_id" => array("integer", $obj_id),
399  "sub_type" => array("text", $sub_type),
400  "sub_id" => array("integer", $sub_id),
401  "field_id" => array("integer", $this->getFieldId())
402  );
403  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "Enum", $new_option);
404  }
405 
406  if($sub_type == "wpg")
407  {
408  // #15763 - adapt advmd page lists
409  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
410  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option);
411  }
412  }
413  }
414  }
415  }
416 
417 
418  //
419  // export/import
420  //
421 
422  protected function addPropertiesToXML(ilXmlWriter $a_writer)
423  {
424  foreach($this->getOptions() as $value)
425  {
426  $a_writer->xmlElement('FieldValue',null,$value);
427  }
428  }
429 
430  public function importXMLProperty($a_key, $a_value)
431  {
432  $this->options[] = $a_value;
433  }
434 
435 
436  //
437  // import/export
438  //
439 
440  public function getValueForXML(ilADT $element)
441  {
442  return $element->getSelection();
443  }
444 
445  public function importValueFromXML($a_cdata)
446  {
447  $this->getADT()->setSelection($a_cdata);
448  }
449 
450 
451  //
452  // presentation
453  //
454 
455  public function prepareElementForEditor(ilADTFormBridge $a_enum)
456  {
457  assert($a_enum instanceof ilADTEnumFormBridge);
458 
459  $a_enum->setAutoSort(false);
460  }
461 }
462 
463 ?>
This class represents an option in a radio group.
getItemByPostVar($a_post_var)
Get Item by POST variable.
This class represents a selection list property in a property form.
This class represents a property form user interface.
static initActiveRecordByType()
Init active record by type.
ADT form bridge base class.
This class represents a section header 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)
addItem($a_item)
Add Item (Property, SectionHeader).
static writeByPrimary($a_table, array $a_primary, $a_type, $a_value)
Write directly.
static getInstance()
Get singleton.
ADT base class.
Definition: class.ilADT.php:11
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled=false)
Add input elements to definition form.
This class represents a property in a property form.
static deleteByPrimary($a_table, array $a_primary, $a_type=null)
Delete values by (partial) primary key.
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form)
Import custom post values from definition form.
static migrateField($a_obj_id, $a_field_id, $old_option, $new_option, $a_is_multi=false)
Migrate search/filter values on advmd change.
static readByPrimary($a_table, array $a_primary, $a_type=null)
Read directly.
This class represents a text property in a property form.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
$old
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
global $lng
Definition: privfeed.php:17
$_POST["username"]
setRequired($a_required)
Set Required.
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.