ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  foreach ($a_values as $idx => $value) {
62  $a_values[$idx] = trim($value);
63  if (!$a_values[$idx]) {
64  unset($a_values[$idx]);
65  }
66  }
67  $a_values = array_unique($a_values);
68  // sort($a_values);
69  }
70  $this->options = $a_values;
71  }
72 
78  public function getOptions()
79  {
80  return $this->options;
81  }
82 
83 
84  //
85  // definition (NOT ADT-based)
86  //
87 
88  protected function importFieldDefinition(array $a_def)
89  {
90  $this->setOptions($a_def);
91  }
92 
93  protected function getFieldDefinition()
94  {
95  return $this->options;
96  }
97 
99  {
100  global $DIC;
101 
102  $lng = $DIC['lng'];
103 
104  return array($lng->txt("meta_advmd_select_options") => implode(",", $this->getOptions()));
105  }
106 
113  public function addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, $a_disabled = false)
114  {
115  global $DIC;
116 
117  $lng = $DIC['lng'];
118 
119  $field = new ilTextInputGUI($lng->txt("meta_advmd_select_options"), "opts");
120  $field->setRequired(true);
121  $field->setMulti(true, true);
122  $field->setMaxLength(255); // :TODO:
123  $a_form->addItem($field);
124 
125  $options = $this->getOptions();
126  if ($options) {
127  $field->setMultiValues($options);
128  $field->setValue(array_shift($options));
129  }
130 
131  if ($a_disabled) {
132  $field->setDisabled(true);
133  }
134  }
135 
141  protected function buildConfirmedObjects(ilPropertyFormGUI $a_form)
142  {
143  // #15719
144  $recipes = $a_form->getInput("conf_det");
145  if (is_array($recipes[$this->getFieldId()])) {
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  $sum_act = $sum[$old_option];
155  $sgl_act = $sgl[$old_option];
156 
157  if ($recipe == "sum") {
158  // #18885
159  if (!$sum_act) {
160  return;
161  }
162 
163  foreach (array_keys($sgl_act) as $obj_idx) {
164  if ($sum_act == self::REMOVE_ACTION_ID) {
165  $sum_act = "";
166  }
167  $res[$old_option][$obj_idx] = $sum_act;
168  }
169  } else {
170  // #18885
171  foreach ($sgl_act as $sgl_index => $sgl_item) {
172  if (!$sgl_item) {
173  return;
174  } elseif ($sgl_item == self::REMOVE_ACTION_ID) {
175  $sgl_act[$sgl_index] = "";
176  }
177  }
178 
179  $res[$old_option] = $sgl_act;
180  }
181  }
182 
183  return $res;
184  }
185  }
186 
193  {
194  $old = $this->getOptions();
195  $new = $a_form->getInput("opts");
196 
197  $missing = array_diff($old, $new);
198  if (sizeof($missing)) {
199  $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
200  if (!is_array($this->confirmed_objects)) {
202  $primary = array(
203  "field_id" => array("integer", $this->getFieldId()),
205  );
206  $in_use = ilADTActiveRecordByType::readByPrimary("adv_md_values", $primary, "Enum");
207  if ($in_use) {
208  $this->confirm_objects = array();
209  foreach ($in_use as $item) {
210  $this->confirm_objects[$item[ilADTActiveRecordByType::SINGLE_COLUMN_NAME]][] = array($item["obj_id"], $item["sub_type"], $item["sub_id"]);
211  }
212  }
213  }
214  }
215 
216  $this->setOptions($new);
217  }
218 
220  {
221  return sizeof($this->confirm_objects);
222  }
223 
225  {
226  global $DIC;
227 
228  $lng = $DIC['lng'];
229  $objDefinition = $DIC['objDefinition'];
230 
231  $a_form->getItemByPostVar("opts")->setDisabled(true);
232 
233  if (is_array($this->confirm_objects) && count($this->confirm_objects) > 0) {
234  $new_options = $a_form->getInput("opts");
235 
236  $sec = new ilFormSectionHeaderGUI();
237  $sec->setTitle($lng->txt("md_adv_confirm_definition_select_section"));
238  $a_form->addItem($sec);
239 
240  foreach ($this->confirm_objects as $old_option => $items) {
241  $details = new ilRadioGroupInputGUI($lng->txt("md_adv_confirm_definition_select_option") . ': "' . $old_option . '"', "conf_det[" . $this->getFieldId() . "][" . $old_option . "]");
242  $details->setRequired(true);
243  $details->setValue("sum");
244  $a_form->addItem($details);
245 
246  // automatic reload does not work
247  if (isset($_POST["conf_det"][$this->getFieldId()][$old_option])) {
248  $details->setValue($_POST["conf_det"][$this->getFieldId()][$old_option]);
249  }
250 
251  $sum = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_all"), "sum");
252  $details->addOption($sum);
253 
254  $sel = new ilSelectInputGUI(
255  $lng->txt("md_adv_confirm_definition_select_option_all_action"),
256  "conf_det_act[" . $this->getFieldId() . "][" . $old_option . "]"
257  );
258  $sel->setRequired(true);
259  $options = array(
260  "" => $lng->txt("please_select"),
261  self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
262  );
263  foreach ($new_options as $new_option) {
264  $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_option . '"';
265  }
266  $sel->setOptions($options);
267  $sum->addSubItem($sel);
268 
269  // automatic reload does not work
270  if (isset($_POST["conf_det_act"][$this->getFieldId()][$old_option])) {
271  if ($_POST["conf_det_act"][$this->getFieldId()][$old_option]) {
272  $sel->setValue($_POST["conf_det_act"][$this->getFieldId()][$old_option]);
273  } elseif ($_POST["conf_det"][$this->getFieldId()][$old_option] == "sum") {
274  $sel->setAlert($lng->txt("msg_input_is_required"));
275  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
276  }
277  }
278 
279  $single = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_single"), "sgl");
280  $details->addOption($single);
281 
282  foreach ($items as $item) {
283  $obj_id = $item[0];
284  $sub_type = $item[1];
285  $sub_id = $item[2];
286 
287  $item_id = $obj_id . "_" . $sub_type . "_" . $sub_id;
288 
289  $type = ilObject::_lookupType($obj_id);
290  $type_title = $lng->txt("obj_" . $type);
291  $title = ' "' . ilObject::_lookupTitle($obj_id) . '"';
292 
293  if ($sub_id) {
294  $class = "ilObj" . $objDefinition->getClassName($type);
295  $class_path = $objDefinition->getLocation($type);
296  include_once $class_path . "/class." . $class . ".php";
297  if (class_implements($class, ilAdvancedMetaDataSubItem)) {
298  $sub_title = $class::getAdvMDSubItemTitle($obj_id, $sub_type, $sub_id);
299  if ($sub_title) {
300  $title .= ' (' . $sub_title . ')';
301  }
302  }
303  }
304 
305  $sel = new ilSelectInputGUI(
306  $type_title . ' ' . $title,
307  "conf[" . $this->getFieldId() . "][" . $old_option . "][" . $item_id . "]"
308  );
309  $sel->setRequired(true);
310  $options = array(
311  "" => $lng->txt("please_select"),
312  self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
313  );
314  foreach ($new_options as $new_option) {
315  $options[$new_option] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_option . '"';
316  }
317  $sel->setOptions($options);
318 
319  // automatic reload does not work
320  if (isset($_POST["conf"][$this->getFieldId()][$old_option][$item_id])) {
321  if ($_POST["conf"][$this->getFieldId()][$old_option][$item_id]) {
322  $sel->setValue($_POST["conf"][$this->getFieldId()][$old_option][$item_id]);
323  } elseif ($_POST["conf_det"][$this->getFieldId()][$old_option] == "sgl") {
324  $sel->setAlert($lng->txt("msg_input_is_required"));
325  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
326  }
327  }
328 
329  $single->addSubItem($sel);
330  }
331  }
332  }
333  }
334 
335 
336  //
337  // definition CRUD
338  //
339 
340  public function update()
341  {
342  parent::update();
343 
344  if (is_array($this->confirmed_objects) && count($this->confirmed_objects) > 0) {
346  foreach ($this->confirmed_objects as $old_option => $item_ids) {
347  foreach ($item_ids as $item => $new_option) {
348  $item = explode("_", $item);
349  $obj_id = $item[0];
350  $sub_type = $item[1];
351  $sub_id = $item[2];
352 
353  if (!$new_option) {
354  // remove existing value
355  $primary = array(
356  "obj_id" => array("integer", $obj_id),
357  "sub_type" => array("text", $sub_type),
358  "sub_id" => array("integer", $sub_id),
359  "field_id" => array("integer", $this->getFieldId())
360  );
361  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "Enum");
362  } else {
363  // update existing value
364  $primary = array(
365  "obj_id" => array("integer", $obj_id),
366  "sub_type" => array("text", $sub_type),
367  "sub_id" => array("integer", $sub_id),
368  "field_id" => array("integer", $this->getFieldId())
369  );
370  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "Enum", $new_option);
371  }
372 
373  if ($sub_type == "wpg") {
374  // #15763 - adapt advmd page lists
375  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
376  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option);
377  }
378  }
379  }
380  }
381  }
382 
383 
384  //
385  // export/import
386  //
387 
388  protected function addPropertiesToXML(ilXmlWriter $a_writer)
389  {
390  foreach ($this->getOptions() as $value) {
391  $a_writer->xmlElement('FieldValue', null, $value);
392  }
393  }
394 
395  public function importXMLProperty($a_key, $a_value)
396  {
397  $this->options[] = $a_value;
398  }
399 
400 
401  //
402  // import/export
403  //
404 
405  public function getValueForXML(ilADT $element)
406  {
407  return $element->getSelection();
408  }
409 
410  public function importValueFromXML($a_cdata)
411  {
412  $this->getADT()->setSelection($a_cdata);
413  }
414 
415 
416  //
417  // presentation
418  //
419 
420  public function prepareElementForEditor(ilADTFormBridge $a_enum)
421  {
422  assert($a_enum instanceof ilADTEnumFormBridge);
423 
424  $a_enum->setAutoSort(false);
425  }
426 }
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.
$type
global $DIC
Definition: saml.php:7
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.
static _lookupTitle($a_id)
lookup object title
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.
foreach($_POST as $key=> $value) $res
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.
update($pash, $contents, Config $config)
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
global $lng
Definition: privfeed.php:17
$def
Definition: croninfo.php:21
$_POST["username"]
setRequired($a_required)
Set Required.
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.