ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
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("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("options"), "opts");
117  $field->setRequired(true);
118  $field->setMulti(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 
298  //
299  // definition CRUD
300  //
301 
302  public function update()
303  {
304  parent::update();
305 
306  if(sizeof($this->confirmed_objects))
307  {
309  foreach($this->confirmed_objects as $old_option => $item_ids)
310  {
311  foreach($item_ids as $item => $new_option)
312  {
313  $item = explode("_", $item);
314  $obj_id = $item[0];
315  $sub_type = $item[1];
316  $sub_id = $item[2];
317 
318  if(!$new_option)
319  {
320  // remove existing value
321  $primary = array(
322  "obj_id" => array("integer", $obj_id),
323  "sub_type" => array("text", $sub_type),
324  "sub_id" => array("integer", $sub_id),
325  "field_id" => array("integer", $this->getFieldId())
326  );
327  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "Enum");
328  }
329  else
330  {
331  // update existing value
332  $primary = array(
333  "obj_id" => array("integer", $obj_id),
334  "sub_type" => array("text", $sub_type),
335  "sub_id" => array("integer", $sub_id),
336  "field_id" => array("integer", $this->getFieldId())
337  );
338  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "Enum", $new_option);
339  }
340 
341  if($sub_type == "wpg")
342  {
343  // #15763 - adapt advmd page lists
344  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
345  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option);
346  }
347  }
348  }
349  }
350  }
351 
352 
353  //
354  // export/import
355  //
356 
357  protected function addPropertiesToXML(ilXmlWriter $a_writer)
358  {
359  foreach($this->getOptions() as $value)
360  {
361  $a_writer->xmlElement('FieldValue',null,$value);
362  }
363  }
364 
365  public function importXMLProperty($a_key, $a_value)
366  {
367  $this->options[] = $a_value;
368  }
369 
370 
371  //
372  // import/export
373  //
374 
375  public function getValueForXML(ilADT $element)
376  {
377  return $element->getSelection();
378  }
379 
380  public function importValueFromXML($a_cdata)
381  {
382  $this->getADT()->setSelection($a_cdata);
383  }
384 }
385 
386 ?>