ILIAS  release_8 Revision v8.24
class.ilAdvancedMDFieldDefinitionSelect.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const REMOVE_ACTION_ID = "-iladvmdrm-";
29
30 protected ?array $options = null;
31 protected array $confirm_objects = [];
32 protected array $confirm_objects_values = [];
33 protected ?array $confirmed_objects = null;
34 protected ?array $old_options = null;
35
36 protected array $option_translations = [];
37 public function __construct(?int $a_field_id = null, string $language = '')
38 {
39 parent::__construct($a_field_id, $language);
40 }
41
42 public function getType(): int
43 {
44 return self::TYPE_SELECT;
45 }
46
47 public function getSearchQueryParserValue(ilADTSearchBridge $a_adt_search): string
48 {
49 return (string) $a_adt_search->getADT()->getSelection();
50 }
51
52 protected function initADTDefinition(): ilADTDefinition
53 {
54 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
55 $def->setNumeric(false);
56
57 $options = $this->getOptions();
58 $translated_options = [];
59 if (isset($this->getOptionTranslations()[$this->language])) {
60 $translated_options = $this->getOptionTranslations()[$this->language];
61 }
62 $def->setOptions(array_replace($options, $translated_options));
63 return $def;
64 }
65
66 public function setOptions(array $a_values = null): void
67 {
68 if ($a_values !== null) {
69 foreach ($a_values as $idx => $value) {
70 $a_values[$idx] = trim($value);
71 if (!$a_values[$idx]) {
72 unset($a_values[$idx]);
73 }
74 }
75 $a_values = array_unique($a_values);
76 }
77 $this->options = $a_values;
78 }
79
80 public function getOptions(): ?array
81 {
82 return $this->options;
83 }
84
85 public function getOptionTranslations(): array
86 {
88 }
89
90 public function getOptionTranslation(string $language): array
91 {
92 if (isset($this->getOptionTranslations()[$language])) {
93 return $this->getOptionTranslations()[$language];
94 }
95 return [];
96 }
97
98 public function setOptionTranslations(array $translations): void
99 {
100 $this->option_translations = $translations;
101 }
102
103 public function setOptionTranslationsForLanguage(array $translations, string $language): void
104 {
105 $this->option_translations[$language] = $translations;
106 }
107
108 protected function importFieldDefinition(array $a_def): void
109 {
110 // options (field_values from adv_mdf_field_definitions are not used)
111 $this->setOptions([]);
112 }
113
114 protected function getFieldDefinition(): array
115 {
116 return [];
117 }
118
119 public function getFieldDefinitionForTableGUI(string $content_language): array
120 {
121 global $DIC;
122
123 $lng = $DIC['lng'];
124
125 if (strlen($content_language)) {
126 $options = $this->getOptionTranslation($content_language);
127 } else {
128 $options = $this->getOptions();
129 }
130 return [
131 $lng->txt("meta_advmd_select_options") => implode(",", $options)
132 ];
133 }
134
136 ilPropertyFormGUI $a_form,
137 bool $a_disabled = false,
138 string $language = ''
139 ): void {
142 return;
143 }
144
145 $options = $this->getOptions();
146
147 if (is_null($options)) {
148 $this->addCreateOptionsFieldsToDefinitionForm($a_form, $a_disabled);
149 return;
150 }
151
152 $this->addEditOptionsFieldsToDefinitionForm($options, $a_form, $a_disabled);
153 }
154
156 ilPropertyFormGUI $a_form,
157 bool $a_disabled
158 ): void {
159 $field = new ilTextInputGUI($this->lng->txt('meta_advmd_select_options'), 'opts_new');
160 $field->setRequired(true);
161 $field->setMulti(true);
162 $field->setMaxLength(255);
163 $field->setDisabled($a_disabled);
164
165 $a_form->addItem($field);
166 }
167
169 array $options,
170 ilPropertyFormGUI $a_form,
171 bool $a_disabled
172 ): void {
173 $entries = new ilRadioGroupInputGUI(
174 $this->lng->txt('meta_advmd_select_options_edit'),
175 'opts_edit'
176 );
177
178 $position_identifiers = ['0' => $this->lng->txt('meta_advmd_select_first_position_identifier')];
179 $last_idx = 0;
180 foreach ($options as $idx => $option) {
181 $position_identifiers[$idx + 1] = sprintf(
182 $this->lng->txt('meta_advmd_select_position_identifier'),
183 $option
184 );
185 $last_idx = $idx + 1;
186 }
187
188 $options[$last_idx] = $this->lng->txt('meta_advmd_select_new_option');
189
190 $disabled_checkbox_overwrites = [];
191
192 foreach ($options as $idx => $option) {
193 $radio = new ilRadioOption($option, (string) $idx);
194
195 $value = new ilTextInputGUI(
196 $this->lng->txt('meta_advmd_select_option_value'),
197 'value_' . $idx
198 );
199 $value->setRequired(true);
200 $value->setMaxLength(255);
201 $value->setValue($idx !== $last_idx ? $option : '');
202 $value->setDisabled($a_disabled);
203 $radio->addSubItem($value);
204
205 $position = new ilSelectInputGUI(
206 $this->lng->txt('meta_advmd_select_option_position'),
207 'position_' . $idx
208 );
209 $relevant_position_identifiers = $position_identifiers;
210 unset($relevant_position_identifiers[$idx + 1]);
211 $position->setOptions($relevant_position_identifiers);
212 $position->setValue($idx);
213 $position->setDisabled($a_disabled);
214 $radio->addSubItem($position);
215
216 if ($idx !== $last_idx && $last_idx > 1) {
217 $delete = new ilCheckboxInputGUI(
218 $this->lng->txt('meta_advmd_select_delete_option'),
219 'delete_me_' . $idx
220 );
221 $delete->setDisabled($a_disabled);
222 $radio->addSubItem($delete);
223
224 /*
225 * If disabled, checkboxes don't come with a hidden input to write to post,
226 * this is a workaround.
227 */
228 if ($a_disabled) {
229 $hidden = new ilHiddenInputGUI('delete_me_' . $idx);
230 $hidden->setValue("1");
231 $disabled_checkbox_overwrites[] = $hidden;
232 }
233 }
234
235 $radio->setDisabled($a_disabled);
236 $entries->addOption($radio);
237 }
238
239 $entries->setDisabled($a_disabled);
240 $a_form->addItem($entries);
241
242 foreach ($disabled_checkbox_overwrites as $input) {
243 $a_form->addItem($input);
244 }
245 }
246
248 ilPropertyFormGUI $form,
249 bool $disabled,
250 string $language = ''
251 ): void {
252 $default_language = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id)->getDefaultLanguage();
253
254 $translation = $this->getOptionTranslation($language);
255
256 $first = true;
257 foreach ($this->getOptions() as $index => $option) {
258 $title = '';
259 if ($first) {
260 $title = $this->lng->txt("meta_advmd_select_options");
261 }
262 $text = new ilTextInputGUI(
263 $title,
264 'opts__' . $language . '__' . $index
265 );
266 if (isset($translation[$index])) {
267 $text->setValue($translation[$index]);
268 }
269 $text->setInfo($default_language . ': ' . $option);
270 $text->setMaxLength(255);
271 $text->setRequired(true);
272
273 $first = false;
274 $form->addItem($text);
275 }
276 }
277
281 protected function buildConfirmedObjects(ilPropertyFormGUI $a_form): ?array
282 {
283 // #15719
284 $recipes = $a_form->getInput("conf_det");
285 if (is_array($recipes[$this->getFieldId()] ?? null)) {
286 $recipes = $recipes[$this->getFieldId()];
287 $sum = $a_form->getInput("conf_det_act");
288 $sum = $sum[$this->getFieldId()];
289 $sgl = $a_form->getInput("conf");
290 $sgl = $sgl[$this->getFieldId()];
291
292 $res = array();
293 foreach ($recipes as $old_option => $recipe) {
294 $sum_act = $sum[$old_option];
295 $sgl_act = $sgl[$old_option];
296
297 if ($recipe == "sum") {
298 // #18885
299 if (!$sum_act) {
300 return null;
301 }
302
303 foreach (array_keys($sgl_act) as $obj_idx) {
304 if ($sum_act == self::REMOVE_ACTION_ID) {
305 $sum_act = "";
306 }
307 if (substr($sum_act, 0, 4) == 'idx_') {
308 $parts = explode('_', $sum_act);
309 $sum_act = $parts[1];
310 }
311 $res[$old_option][$obj_idx] = $sum_act;
312 }
313 } else {
314 // #18885
315 foreach ($sgl_act as $sgl_index => $sgl_item) {
316 if (!$sgl_item) {
317 return null;
318 } elseif ($sgl_item == self::REMOVE_ACTION_ID) {
319 $sgl_act[$sgl_index] = "";
320 }
321 if (substr($sgl_item, 0, 4) == 'idx_') {
322 $parts = explode('_', $sgl_item);
323 $sgl_act[$sgl_index] = $parts[1];
324 }
325 }
326
327 $res[$old_option] = $sgl_act;
328 }
329 }
330 return $res;
331 }
332 return null;
333 }
334
335 public function importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language = ''): void
336 {
337 $this->importNewSelectOptions(true, $a_form, $language);
338 }
339
340 protected function importNewSelectOptions(
341 bool $multi,
342 ilPropertyFormGUI $a_form,
343 string $language = ''
344 ): void {
345 if (!$this->useDefaultLanguageMode($language)) {
346 $this->importTranslatedFormPostValues($a_form, $language);
347 return;
348 }
349
350 if (!strlen($language)) {
351 $language = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordId())->getDefaultLanguage();
352 }
353
354 $old = $this->getOptionTranslation($language);
355 if ($new = $a_form->getInput('opts_new')) {
356 $this->setOptions($new);
357 $this->setOptionTranslationsForLanguage($new, $language);
358 return;
359 }
360
361 $edited_idx = $a_form->getInput('opts_edit');
362 if($edited_idx === '' || is_null($edited_idx)) {
363 return;
364 }
365 $edited_idx = (int) $edited_idx;
366
367 $new_value = (string) $a_form->getInput('value_' . $edited_idx);
368 $new_position = (int) $a_form->getInput('position_' . $edited_idx);
369 $delete = (bool) $a_form->getInput('delete_me_' . $edited_idx);
370
371 /*
372 * Build the new options, keeping track of how indices change in a map.
373 */
374 $new = [];
375 $index_map = [];
376 $new_idx = 0;
377
378 /*
379 * If an entry is is moved down, shift new position to reflect entry not
380 * being in its old position anymore.
381 */
382 if ($new_position > $edited_idx) {
383 $new_position -= 1;
384 }
385
386 foreach ($old as $old_idx => $old_value) {
387 if ($old_idx === $edited_idx) {
388 continue;
389 }
390
391 if (!$delete && $new_idx === $new_position) {
392 $new[$new_idx] = $new_value;
393 /*
394 * Newly added indices must not be used as 'old indices' in the index map,
395 * otherwise the search doesn't work.
396 */
397 if ($edited_idx !== $new_idx && array_key_exists($edited_idx, $old)) {
398 $index_map[$edited_idx] = $new_idx;
399 }
400 $new_idx++;
401 }
402
403 $new[$new_idx] = $old_value;
404 if ($old_idx !== $new_idx) {
405 $index_map[$old_idx] = $new_idx;
406 }
407 $new_idx++;
408 }
409
410 /*
411 * If an entry is moved to or added at the end, append it.
412 */
413 if (!$delete && $new_idx === $new_position) {
414 $new[$new_idx] = $new_value;
415 if ($edited_idx !== $new_idx) {
416 $index_map[$edited_idx] = $new_idx;
417 }
418 }
419
420 /*
421 * Prepare migration of existing values, prepare for confirmation if more
422 * user input is required.
423 */
424 if ($delete || count($index_map)) {
425 $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
426 $already_confirmed = is_array($this->confirmed_objects);
427
428 $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance(
429 $this->getADTDefinition(),
430 false,
431 $multi
432 );
433
434 /*
435 * If option is to be deleted, collect objects whith that value and
436 * and prepare confirmation of their migration.
437 */
438 if ($delete) {
439 $in_use = $this->findBySingleValue($search, $edited_idx);
440 foreach ($in_use as $item) {
441 if (!$already_confirmed) {
442 $this->confirm_objects[$edited_idx][] = $item;
443 $this->confirm_objects_values[$edited_idx] = $old[$edited_idx];
444 }
445 }
446 }
447
448 /*
449 * Prepare objects that can be automatically migrated.
450 */
451 foreach ($index_map as $old_idx => $new_idx) {
452 $in_use = $this->findBySingleValue($search, $old_idx);
453 foreach ($in_use as $item) {
454 $complete_id = $item[0] . "_" . $item[1] . "_" . $item[2];
455 $this->confirmed_objects[$old_idx][$complete_id] = $new_idx;
456 }
457 }
458 }
459 $this->old_options = $old;
460
461 /*
462 * Finally set the new options, and change the indices of translations
463 * according to the index map.
464 */
465 $this->setOptionTranslationsForLanguage($new, $language);
466 $this->setOptions($new);
467 foreach ($this->getOptionTranslations() as $current_lang => $options) {
468 $current_lang = (string) $current_lang;
469 if ($current_lang === $language) {
470 continue;
471 }
472 $updated_translations = [];
473 foreach ($options as $idx => $option) {
474 if (array_key_exists($idx, $index_map)) {
475 $updated_translations[$index_map[$idx]] = $option;
476 continue;
477 }
478 $updated_translations[$idx] = $option;
479 }
480 $this->setOptionTranslationsForLanguage($updated_translations, $current_lang);
481 }
482 }
483
490 protected function findBySingleValue(ilADTSearchBridge $a_search, $a_value): array
491 {
492 $res = array();
493 $a_search->getADT()->setSelections((array) $a_value);
494 $condition = $a_search->getSQLCondition('value_index');
495
497 "adv_md_values",
498 "Enum",
499 $this->getFieldId(),
500 $condition
501 );
502 if ($in_use) {
503 foreach ($in_use as $item) {
504 $res[] = array($item["obj_id"], $item["sub_type"], $item["sub_id"], $item["value_index"]);
505 }
506 }
507 return $res;
508 }
509
510 protected function importTranslatedFormPostValues(ilPropertyFormGUI $form, string $language): void
511 {
512 $translated_options = [];
513 foreach ($this->getOptions() as $idx => $value) {
514 $value = $form->getInput('opts__' . $language . '__' . $idx);
515 $translated_options[] = trim($value);
516 }
517 $translations = $this->getOptionTranslations();
518 $translations[$language] = $translated_options;
519 $this->setOptionTranslations($translations);
520 }
521
523 {
524 return is_array($this->confirm_objects) && count($this->confirm_objects) > 0;
525 }
527 {
528 global $DIC;
529
530 $lng = $DIC['lng'];
531 $objDefinition = $DIC['objDefinition'];
532
533 $post_conf_det = (array) ($this->http->request()->getParsedBody()['conf_det'] ?? []);
534 $post_conf = (array) ($this->http->request()->getParsedBody()['conf'] ?? []);
535
536 $custom_field = $a_form->getItemByPostVar('opts_edit');
537 $custom_field->setDisabled(true);
538 foreach ($custom_field->getSubInputItemsRecursive() as $sub_input) {
539 $sub_input->setDisabled(true);
540 /*
541 * If disabled, checkboxes don't come with a hidden input to write to post,
542 * this is a workaround.
543 */
544 if ($sub_input instanceof ilCheckboxInputGUI) {
545 $hidden = new ilHiddenInputGUI($sub_input->getPostVar());
546 $hidden->setValue($sub_input->getValue());
547 $a_form->addItem($hidden);
548 }
549 }
550
551 if (is_array($this->confirm_objects) && count($this->confirm_objects) > 0) {
552 $sec = new ilFormSectionHeaderGUI();
553 $sec->setTitle($lng->txt("md_adv_confirm_definition_select_section"));
554 $a_form->addItem($sec);
555
556 foreach ($this->confirm_objects as $old_option_index => $items) {
557 $old_option_value = $this->confirm_objects_values[$old_option_index];
559 $lng->txt("md_adv_confirm_definition_select_option") . ': "' . $old_option_value . '"',
560 "conf_det[" . $this->getFieldId() . "][" . $old_option_index . "]"
561 );
562 $details->setRequired(true);
563 $details->setValue("sum");
564 $a_form->addItem($details);
565
566 // automatic reload does not work
567 if (isset($post_conf_det[$this->getFieldId()][$old_option_index])) {
568 $details->setValue($post_conf_det[$this->getFieldId()][$old_option_index]);
569 }
570
571 $sum = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_all"), "sum");
572 $details->addOption($sum);
573
574 $sel = new ilSelectInputGUI(
575 $lng->txt("md_adv_confirm_definition_select_option_all_action"),
576 "conf_det_act[" . $this->getFieldId() . "][" . $old_option_index . "]"
577 );
578 $sel->setRequired(true);
579 $options = array(
580 "" => $lng->txt("please_select"),
581 self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
582 );
583 foreach ($this->getOptions() as $new_option_index => $new_option) {
584 $options['idx_' . $new_option_index] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_option . '"';
585 }
586 $sel->setOptions($options);
587 $sum->addSubItem($sel);
588
589 // automatic reload does not work
590 if (isset($post_conf_det[$this->getFieldId()][$old_option_index])) {
591 if ($post_conf_det[$this->getFieldId()][$old_option_index]) {
592 $sel->setValue($post_conf_det[$this->getFieldId()][$old_option_index]);
593 } elseif ($post_conf_det[$this->getFieldId()][$old_option_index] == "sum") {
594 $sel->setAlert($lng->txt("msg_input_is_required"));
595 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
596 }
597 }
598 $single = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_single"), "sgl");
599 $details->addOption($single);
600 foreach ($items as $item) {
601 $obj_id = (int) $item[0];
602 $sub_type = (string) $item[1];
603 $sub_id = (int) $item[2];
604
605 /*
606 * media objects are saved in adv_md_values with obj_id=0, and their actual obj_id
607 * as sub_id.
608 */
609 if ($sub_type === 'mob') {
610 $obj_id = $sub_id;
611 $sub_id = 0;
612 }
613
614 $item_id = $obj_id . "_" . $sub_type . "_" . $sub_id;
615
616 $type = ilObject::_lookupType($obj_id);
617 $type_title = $lng->txt("obj_" . $type);
618 $title = ' "' . ilObject::_lookupTitle($obj_id) . '"';
619
620 if ($sub_id) {
621 $class = "ilObj" . $objDefinition->getClassName($type);
622 $class_path = $objDefinition->getLocation($type);
623 $ints = class_implements($class);
624 if (isset($ints["ilAdvancedMetaDataSubItems"])) {
626 $sub_title = $class::getAdvMDSubItemTitle($obj_id, $sub_type, $sub_id);
627 if ($sub_title) {
628 $title .= ' (' . $sub_title . ')';
629 }
630 }
631 }
632
633 $sel = new ilSelectInputGUI(
634 $type_title . ' ' . $title,
635 "conf[" . $this->getFieldId() . "][" . $old_option_index . "][" . $item_id . "]"
636 );
637 $sel->setRequired(true);
638 $options = array(
639 "" => $lng->txt("please_select"),
640 self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
641 );
642 foreach ($this->getOptions() as $new_option_index => $new_option) {
643 $options['idx_' . $new_option_index] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_option . '"';
644 }
645 $sel->setOptions($options);
646
647 // automatic reload does not work
648 if (isset($post_conf[$this->getFieldId()][$old_option_index][$item_id])) {
649 if ($post_conf[$this->getFieldId()][$old_option_index][$item_id]) {
650 $sel->setValue($post_conf[$this->getFieldId()][$old_option_index][$item_id]);
651 } elseif ($post_conf_det[$this->getFieldId()][$old_option_index] == "sgl") {
652 $sel->setAlert($lng->txt("msg_input_is_required"));
653 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
654 }
655 }
656
657 $single->addSubItem($sel);
658 }
659 }
660 }
661 }
662
663 public function delete(): void
664 {
665 $this->deleteOptionTranslations();
666 parent::delete();
667 }
668
669 public function save(bool $a_keep_pos = false): void
670 {
671 parent::save($a_keep_pos);
672 $this->saveOptionTranslations();
673 }
674
675 protected function deleteOptionTranslations(): void
676 {
677 $query = 'delete from adv_mdf_enum ' .
678 'where field_id = ' . $this->db->quote($this->getFieldId(), ilDBConstants::T_INTEGER);
679 $this->db->manipulate($query);
680 }
681
682 protected function updateOptionTranslations(): void
683 {
684 $this->deleteOptionTranslations();
685 $this->saveOptionTranslations();
686 }
687
688 protected function saveOptionTranslations(): void
689 {
690 $options = $this->getOptionTranslations();
691 /*
692 * e.g. on import from <7 no translations are set, so one has to save
693 * the default options for the default language (32410).
694 */
695 if (empty($options)) {
696 $record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
697 $options = [$record->getDefaultLanguage() => $this->getOptions()];
698 }
699 foreach ($options as $lang_key => $options_in_lang) {
700 foreach ($options_in_lang as $idx => $option) {
701 $query = 'insert into adv_mdf_enum (field_id, lang_code, idx, value )' .
702 'values ( ' .
703 $this->db->quote($this->getFieldId(), ilDBConstants::T_INTEGER) . ', ' .
704 $this->db->quote($lang_key, ilDBConstants::T_TEXT) . ', ' .
705 $this->db->quote($idx, ilDBConstants::T_INTEGER) . ', ' .
706 $this->db->quote($option, ilDBConstants::T_TEXT) .
707 ')';
708 $this->db->manipulate($query);
709 }
710 }
711 }
712
713 public function update(): void
714 {
715 if (is_array($this->confirmed_objects) && count($this->confirmed_objects) > 0) {
716 // we need the "old" options for the search
717 $def = $this->getADTDefinition();
718 $def = clone($def);
719 $def->setOptions($this->old_options);
720 $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def, false, true);
722
723 $page_list_mappings = [];
724
725 foreach ($this->confirmed_objects as $old_option => $item_ids) {
726 // get complete old values
727 $old_values = array();
728 foreach ($this->findBySingleValue($search, $old_option) as $item) {
729 $old_values[$item[0] . "_" . $item[1] . "_" . $item[2]] = $item[3];
730 }
731
732 foreach ($item_ids as $item => $new_option) {
733 $parts = explode("_", $item);
734 $obj_id = $parts[0];
735 $sub_type = $parts[1];
736 $sub_id = $parts[2];
737
738 // update existing value (with changed option)
739 if (isset($old_values[$item])) {
740 $old_value = $old_values[$item];
741 // find changed option in old value
742 //$old_value = explode(ilADTMultiEnumDBBridge::SEPARATOR, $old_values[$item]);
743 // remove separators
744 //array_shift($old_value);
745 //array_pop($old_value);
746
747 //$old_idx = array_keys($old_value, $old_option);
748 $old_idx = $old_value;
749 if (isset($old_idx)) {
750 $primary = array(
751 "obj_id" => array("integer", $obj_id),
752 "sub_type" => array("text", $sub_type),
753 "sub_id" => array("integer", $sub_id),
754 "field_id" => array("integer", $this->getFieldId())
755 );
756
757 $index_old = array_merge(
758 $primary,
759 [
760 'value_index' => [ilDBConstants::T_INTEGER, $old_idx]
761 ]
762 );
763 $index_new = array_merge(
764 $primary,
765 [
766 'value_index' => [ilDBConstants::T_INTEGER, $new_option]
767 ]
768 );
769 ilADTActiveRecordByType::deleteByPrimary('adv_md_values', $index_old, 'MultiEnum');
770
771 if (is_numeric($new_option)) {
772 ilADTActiveRecordByType::deleteByPrimary('adv_md_values', $index_new, 'MultiEnum');
773 ilADTActiveRecordByType::create('adv_md_values', $index_new, 'MultiEnum');
774 } else {
775 }
776 }
777 }
778
779 if ($sub_type == "wpg") {
780 // #15763 - adapt advmd page lists
781 $page_list_mappings[(string) $old_option] = (string) $new_option;
782 }
783 }
784 }
785
786 if (!empty($page_list_mappings)) {
788 $this->getFieldId(),
789 $page_list_mappings
790 );
791 }
792
793 $this->confirmed_objects = array();
794 }
795
796 parent::update();
797 $this->updateOptionTranslations();
798 }
799
800 protected function addPropertiesToXML(ilXmlWriter $a_writer): void
801 {
802 foreach ($this->getOptions() as $value) {
803 $a_writer->xmlElement('FieldValue', null, $value);
804 }
805 foreach ($this->getOptionTranslations() as $lang_key => $translations) {
806 foreach ((array) $translations as $value) {
807 $a_writer->xmlElement('FieldValue', ['id' => $lang_key], $value);
808 }
809 }
810 }
811
812 public function importXMLProperty(string $a_key, string $a_value): void
813 {
814 if (!$a_key) {
815 $this->options[] = $a_value;
816 } else {
817 $this->option_translations[$a_key][] = $a_value;
818 }
819 }
820
821 public function getValueForXML(ilADT $element): string
822 {
823 return $element->getSelection();
824 }
825
826 public function importValueFromXML(string $a_cdata): void
827 {
828 $a_cdata = $this->translateLegacyImportValueFromXML($a_cdata);
829 $this->getADT()->setSelection($a_cdata);
830 }
831
841 protected function translateLegacyImportValueFromXML(string $value): string
842 {
843 if (
844 !in_array($value, array_keys($this->options)) &&
845 in_array($value, $this->options)
846 ) {
847 $value = (string) array_search($value, $this->options);
848 }
849 return $value;
850 }
851
852 public function prepareElementForEditor(ilADTFormBridge $a_bridge): void
853 {
854 assert($a_bridge instanceof ilADTEnumFormBridge);
855
856 $a_bridge->setAutoSort(false);
857 }
858
859 protected function import(array $a_data): void
860 {
861 parent::import($a_data);
862
863 $query = 'select * from adv_mdf_enum ' .
864 'where field_id = ' . $this->db->quote($this->getFieldId(), ilDBConstants::T_INTEGER) . ' ' .
865 'order by idx';
866 $res = $this->db->query($query);
867 $options = [];
868 $default = [];
870 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
871 if ($row->lang_code == $record->getDefaultLanguage()) {
872 $default[(int) $row->idx] = (string) $row->value;
873 }
874 $options[(string) $row->lang_code][(int) $row->idx] = (string) $row->value;
875 }
876 $this->setOptions($default);
877 $this->setOptionTranslations($options);
878 }
879
880 public function _clone(int $a_new_record_id): self
881 {
883 $obj = parent::_clone($a_new_record_id);
884 $query = 'select * from adv_mdf_enum ' .
885 'where field_id = ' . $this->db->quote($this->getFieldId(), ilDBConstants::T_INTEGER) . ' ' .
886 'order by idx';
887 $res = $this->db->query($query);
888 $options = [];
889 $default = [];
891 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
892 if ($row->lang_code == $record->getDefaultLanguage()) {
893 $default[(int) $row->idx] = (string) $row->value;
894 }
895 $options[(string) $row->lang_code][(int) $row->idx] = (string) $row->value;
896 }
897 $obj->setOptions($default);
898 $obj->setOptionTranslations($options);
899 $obj->update();
900 return $obj;
901 }
902}
static find(string $a_table, string $a_type, int $a_field_id, string $a_condition, ?string $a_additional_fields=null)
Find entries.
static create(string $table, array $fields, string $type)
static deleteByPrimary(string $a_table, array $a_primary, string $a_type=null)
ADT definition base class.
static initActiveRecordByType()
Init active record by type.
ADT form bridge base class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getSQLCondition(string $a_element_id, int $mode=self::SQL_LIKE, array $quotedWords=[])
Get SQL condition for current value(s)
ADT base class.
Definition: class.ilADT.php:12
importTranslatedFormPostValues(ilPropertyFormGUI $form, string $language)
addCustomFieldToDefinitionFormInTranslationMode(ilPropertyFormGUI $form, bool $disabled, string $language='')
getSearchQueryParserValue(ilADTSearchBridge $a_adt_search)
Get value for search query parser.
importValueFromXML(string $a_cdata)
Import value from xml.
addCustomFieldToDefinitionForm(ilPropertyFormGUI $a_form, bool $a_disabled=false, string $language='')
Add custom input elements to definition form.
importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language='')
Import custom post values from definition form.
addCreateOptionsFieldsToDefinitionForm(ilPropertyFormGUI $a_form, bool $a_disabled)
importXMLProperty(string $a_key, string $a_value)
Import property from XML.
importFieldDefinition(array $a_def)
Import (type-specific) field definition from DB.
importNewSelectOptions(bool $multi, ilPropertyFormGUI $a_form, string $language='')
addEditOptionsFieldsToDefinitionForm(array $options, ilPropertyFormGUI $a_form, bool $a_disabled)
prepareElementForEditor(ilADTFormBridge $a_bridge)
Prepare editor form elements.
getFieldDefinitionForTableGUI(string $content_language)
Parse properties for table gui.
getValueForXML(ilADT $element)
Parse ADT value for xml (export)
save(bool $a_keep_pos=false)
Create new field entry.
translateLegacyImportValueFromXML(string $value)
On import from <7 options are not given by index but by their label.
getFieldDefinition()
Get (type-specific) field definition.
__construct(?int $a_field_id=null, string $language='')
setOptionTranslationsForLanguage(array $translations, string $language)
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.
findBySingleValue(ilADTSearchBridge $a_search, $a_value)
addPropertiesToXML(ilXmlWriter $a_writer)
Add (type-specific) properties to xml export.
useDefaultLanguageMode(string $language)
Check if default language mode has to be used: no language given or language equals default language.
static _getInstanceByRecordId(int $a_record_id)
This class represents a checkbox property in a property form.
setRequired(bool $a_required)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
static migrateField(int $a_field_id, array $mapping)
Migrate search/filter values on advmd change.
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a selection list property in a property form.
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
$index
Definition: metadata.php:145
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
getRecordId()
Get the system record ID.
Definition: System.php:214
array $details
Details for error message relating to last request processed.
Definition: System.php:109
$query
$type
$lng