ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDFieldDefinitionSelect.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
33{
34 private const REMOVE_ACTION_ID = "-iladvmdrm-";
35 private const ADD_NEW_ENTRY_ID = "-advmd_add_new_entry-";
36
37 protected array $confirm_objects = [];
38 protected array $confirm_objects_values = [];
39 protected ?array $confirmed_objects = null;
40 protected ?array $old_options_array = null;
42
43 protected string $default_language;
44
45 private \ilGlobalTemplateInterface $main_tpl;
46
48
49 public function __construct(GenericData $generic_data, string $language = '')
50 {
51 global $DIC;
52
54
55 $this->main_tpl = $DIC->ui()->mainTemplate();
56 $this->db_gateway = new DatabaseGatewayImplementation($DIC->database());
57
58 $this->readOptions();
59 }
60
61 public function getType(): int
62 {
63 return self::TYPE_SELECT;
64 }
65
66 public function getSearchQueryParserValue(ilADTSearchBridge $a_adt_search): string
67 {
68 return (string) $a_adt_search->getADT()->getSelection();
69 }
70
71 protected function initADTDefinition(): ilADTDefinition
72 {
73 $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("Enum");
74 $def->setNumeric(false);
75
76 $def->setOptions($this->getOptionsInLanguageAsArray($this->language));
77 return $def;
78 }
79
80 protected function options(): SelectSpecificData
81 {
82 return $this->options;
83 }
84
85 public function getOptionsInDefaultLanguageAsArray(): ?array
86 {
87 $default_language_values = [];
88 foreach ($this->options()->getOptions() as $option) {
89 if ($translation = $option->getTranslationInLanguage($this->default_language)) {
90 $default_language_values[$option->optionID()] = $translation->getValue();
91 }
92 }
93 return $default_language_values;
94 }
95
96 protected function getOptionsInLanguageAsArray(
97 string $language,
98 bool $default_as_fallback = true
99 ): ?array {
100 $current_language_values = [];
101 foreach ($this->options()->getOptions() as $option) {
102 if ($translation = $option->getTranslationInLanguage($language)) {
103 $current_language_values[$option->optionID()] = $translation->getValue();
104 } elseif (
105 $default_as_fallback &&
106 $translation = $option->getTranslationInLanguage($this->default_language)
107 ) {
108 $current_language_values[$option->optionID()] = $translation->getValue();
109 }
110 }
111 return $current_language_values;
112 }
113
114 protected function importFieldDefinition(array $a_def): void
115 {
116 }
117
118 protected function getFieldDefinition(): array
119 {
120 return [];
121 }
122
123 public function getFieldDefinitionForTableGUI(string $content_language): array
124 {
125 if (strlen($content_language)) {
126 $options = $this->getOptionsInLanguageAsArray($content_language);
127 } else {
128 $options = $this->getOptionsInDefaultLanguageAsArray();
129 }
130 return [
131 $this->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 {
140 if (!$this->useDefaultLanguageMode($language)) {
141 $this->addCustomFieldToDefinitionFormInTranslationMode($a_form, $a_disabled, $language);
142 return;
143 }
144
145 if (!$this->options()->hasOptions()) {
146 $this->addCreateOptionsFieldsToDefinitionForm($a_form, $a_disabled);
147 return;
148 }
149
150 $this->addEditOptionsFieldsToDefinitionForm($this->options(), $a_form, $a_disabled);
151 }
152
154 ilPropertyFormGUI $a_form,
155 bool $a_disabled
156 ): void {
157 $field = new ilTextInputGUI($this->lng->txt('meta_advmd_select_options'), 'opts_new');
158 $field->setRequired(true);
159 $field->setMulti(true);
160 $field->setMaxLength(255);
161 $field->setDisabled($a_disabled);
162
163 $a_form->addItem($field);
164 }
165
167 SelectSpecificData $options,
168 ilPropertyFormGUI $a_form,
169 bool $a_disabled
170 ): void {
171 $entries = new ilRadioGroupInputGUI(
172 $this->lng->txt('meta_advmd_select_options_edit'),
173 'opts_edit'
174 );
175
176 $position_identifiers = [
177 '0' => $this->lng->txt('meta_advmd_select_first_position_identifier')
178 ];
179 foreach ($options->getOptions() as $option) {
180 $position_identifiers[(string) ($option->getPosition() + 1)] = sprintf(
181 $this->lng->txt('meta_advmd_select_position_identifier'),
182 $option->getTranslationInLanguage($this->default_language)?->getValue() ?? ''
183 );
184 }
185
186 $disabled_checkbox_overwrites = [];
187
188 foreach ($options->getOptions() as $option) {
189 $option_value = $option->getTranslationInLanguage($this->default_language)?->getValue() ?? '';
190 $hidden = $this->addRadioToEntriesGroup(
191 $entries,
192 $position_identifiers,
193 $option->getPosition(),
194 $option_value,
195 $option_value,
196 (string) $option->optionID(),
197 count(iterator_to_array($options->getOptions())) > 1,
198 $a_disabled
199 );
200 if (!is_null($hidden)) {
201 $disabled_checkbox_overwrites[] = $hidden;
202 }
203 }
204
205 /*
206 * Add radio to add new entry
207 */
208 $this->addRadioToEntriesGroup(
209 $entries,
210 $position_identifiers,
211 null,
212 $this->lng->txt('meta_advmd_select_new_option'),
213 '',
214 self::ADD_NEW_ENTRY_ID,
215 false,
216 $a_disabled
217 );
218
219 $entries->setDisabled($a_disabled);
220 $a_form->addItem($entries);
221
222 foreach ($disabled_checkbox_overwrites as $input) {
223 $a_form->addItem($input);
224 }
225 }
226
232 protected function addRadioToEntriesGroup(
233 ilRadioGroupInputGUI $entries,
234 array $position_select_options,
235 ?int $position_value,
236 string $label,
237 string $value,
238 string $id,
239 bool $with_delete_checkbox,
240 bool $disabled
241 ): ?ilHiddenInputGUI {
242 $disabled_checkbox_overwrite = null;
243 $radio = new ilRadioOption($label, $id);
244
245 $value_input = new ilTextInputGUI(
246 $this->lng->txt('meta_advmd_select_option_value'),
247 'value_' . $id
248 );
249 $value_input->setRequired(true);
250 $value_input->setMaxLength(255);
251 $value_input->setValue($value);
252 $value_input->setDisabled($disabled);
253 $radio->addSubItem($value_input);
254
255 $position = new ilSelectInputGUI(
256 $this->lng->txt('meta_advmd_select_option_position'),
257 'position_' . $id
258 );
259 $relevant_position_select_options = $position_select_options;
260 if (!is_null($position_value) && isset($relevant_position_select_options[$position_value + 1])) {
261 unset($relevant_position_select_options[$position_value + 1]);
262 }
263 $position->setOptions($relevant_position_select_options);
264 $position->setValue($position_value);
265 $position->setDisabled($disabled);
266 $radio->addSubItem($position);
267
268 if ($with_delete_checkbox) {
269 $delete = new ilCheckboxInputGUI(
270 $this->lng->txt('meta_advmd_select_delete_option'),
271 'delete_me_' . $id
272 );
273 $delete->setDisabled($disabled);
274 $radio->addSubItem($delete);
275
276 /*
277 * If disabled, checkboxes don't come with a hidden input to write to post,
278 * this is a workaround.
279 */
280 if ($disabled) {
281 $hidden = new ilHiddenInputGUI('delete_me_' . $id);
282 $hidden->setValue("1");
283 $disabled_checkbox_overwrite = $hidden;
284 }
285 }
286
287 $radio->setDisabled($disabled);
288 $entries->addOption($radio);
289 return $disabled_checkbox_overwrite;
290 }
291
293 ilPropertyFormGUI $form,
294 bool $disabled,
295 string $language = ''
296 ): void {
297 $default_language = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordId())->getDefaultLanguage();
298
299 $first = true;
300 foreach ($this->options()->getOptions() as $option) {
301 $title = '';
302 if ($first) {
303 $title = $this->lng->txt("meta_advmd_select_options");
304 }
305 $text = new ilTextInputGUI(
306 $title,
307 'opts__' . $language . '__' . $option->optionID()
308 );
309
310 if ($option->hasTranslationInLanguage($language)) {
311 $text->setValue($option->getTranslationInLanguage($language)->getValue());
312 }
313
314 $default_value = '';
315 if ($option->hasTranslationInLanguage($default_language)) {
316 $default_value = $option->getTranslationInLanguage($default_language)->getValue();
317 }
318
319 $text->setInfo($default_language . ': ' . $default_value);
320 $text->setMaxLength(255);
321 $text->setRequired(true);
322
323 $first = false;
324 $form->addItem($text);
325 }
326 }
327
331 protected function buildConfirmedObjects(ilPropertyFormGUI $a_form): ?array
332 {
333 // #15719
334 $recipes = $a_form->getInput("conf_det");
335 if (is_array($recipes[$this->getFieldId()] ?? null)) {
336 $recipes = $recipes[$this->getFieldId()];
337 $sum = $a_form->getInput("conf_det_act");
338 $sum = $sum[$this->getFieldId()];
339 $sgl = $a_form->getInput("conf");
340 $sgl = $sgl[$this->getFieldId()];
341
342 $res = array();
343 foreach ($recipes as $old_option => $recipe) {
344 $sum_act = $sum[$old_option];
345 $sgl_act = $sgl[$old_option];
346
347 if ($recipe == "sum") {
348 // #18885
349 if (!$sum_act) {
350 return null;
351 }
352
353 foreach (array_keys($sgl_act) as $obj_idx) {
354 if ($sum_act == self::REMOVE_ACTION_ID) {
355 $sum_act = "";
356 }
357 if (substr($sum_act, 0, 4) == 'idx_') {
358 $parts = explode('_', $sum_act);
359 $sum_act = $parts[1];
360 }
361 $res[$old_option][$obj_idx] = $sum_act;
362 }
363 } else {
364 // #18885
365 foreach ($sgl_act as $sgl_index => $sgl_item) {
366 if (!$sgl_item) {
367 return null;
368 } elseif ($sgl_item == self::REMOVE_ACTION_ID) {
369 $sgl_act[$sgl_index] = "";
370 }
371 if (substr($sgl_item, 0, 4) == 'idx_') {
372 $parts = explode('_', $sgl_item);
373 $sgl_act[$sgl_index] = $parts[1];
374 }
375 }
376
377 $res[$old_option] = $sgl_act;
378 }
379 }
380 return $res;
381 }
382 return null;
383 }
384
385 public function importCustomDefinitionFormPostValues(ilPropertyFormGUI $a_form, string $language = ''): void
386 {
387 $this->importNewSelectOptions(true, $a_form, $language);
388 }
389
390 protected function importNewSelectOptions(
391 bool $multi,
392 ilPropertyFormGUI $a_form,
393 string $language = ''
394 ): void {
395 if (!$this->useDefaultLanguageMode($language)) {
396 $this->importTranslatedFormPostValues($a_form, $language);
397 return;
398 }
399
400 $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance(
401 $this->getADTDefinition(),
402 false,
403 $multi
404 );
405
406 if (!strlen($language)) {
407 $language = $this->default_language;
408 }
409
410 if ($new = $a_form->getInput('opts_new')) {
411 foreach ($new as $position => $value) {
412 $new_option = $this->options()->addOption();
413 $new_option->setPosition((int) $position);
414 $new_translation = $new_option->addTranslation($language);
415 $new_translation->setValue(trim($value));
416 }
417 return;
418 }
419
420 $edited_id = $a_form->getInput('opts_edit');
421 if ($edited_id === '' || is_null($edited_id)) {
422 return;
423 }
424 if ($edited_id !== self::ADD_NEW_ENTRY_ID) {
425 $edited_id = (int) $edited_id;
426 }
427
428 $this->old_options_array = $this->getOptionsInLanguageAsArray($language);
429
430 /*
431 * Delete option if needed
432 */
433 if ($a_form->getInput('delete_me_' . $edited_id) && $edited_id !== self::ADD_NEW_ENTRY_ID) {
434 $old_option_value = $this->options()
435 ->getOption($edited_id)
436 ?->getTranslationInLanguage($language)?->getValue() ?? '';
437 $this->options()->removeOption($edited_id);
438
439 /*
440 * If option is to be deleted, collect objects whith that value and
441 * and prepare confirmation of their migration.
442 */
443 $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
444 $already_confirmed = is_array($this->confirmed_objects);
445
446 $in_use = $this->findBySingleValue($search, $edited_id);
447 if (is_array($in_use)) {
448 foreach ($in_use as $item) {
449 if (!$already_confirmed) {
450 $this->confirm_objects[$edited_id][] = $item;
451 $this->confirm_objects_values[$edited_id] = $old_option_value;
452 }
453 }
454 }
455
456 return;
457 }
458
459 $new_value = (string) $a_form->getInput('value_' . $edited_id);
460 $new_position = (int) $a_form->getInput('position_' . $edited_id);
461
462 /*
463 * Create new option if needed, else assign the new value
464 */
465 if ($edited_id === self::ADD_NEW_ENTRY_ID) {
466 $edited_option = $this->options()->addOption();
467 } else {
468 $edited_option = $this->options()->getOption($edited_id);
469 }
470
471 if ($edited_option->hasTranslationInLanguage($this->default_language)) {
472 $edited_option->getTranslationInLanguage($this->default_language)->setValue($new_value);
473 } else {
474 $edited_option->addTranslation($this->default_language)->setValue($new_value);
475 }
476
477 /*
478 * Update positions and value
479 * If an entry is is moved down, shift new position to reflect entry not
480 * being in its old position anymore.
481 */
482 if ($new_position > $edited_option->getPosition()) {
483 $new_position -= 1;
484 }
485
486 $position = 0;
487 if ($position === $new_position) {
488 $edited_option->setPosition($position);
489 $position++;
490 }
491 foreach ($this->options()->getOptions() as $option) {
492 if ($option === $edited_option) {
493 continue;
494 }
495 $option->setPosition($position);
496 $position++;
497
498 if ($position === $new_position) {
499 $edited_option->setPosition($position);
500 $position++;
501 }
502 }
503 }
504
511 protected function findBySingleValue(ilADTSearchBridge $a_search, $a_value): array
512 {
513 $res = array();
514 $a_search->getADT()->setSelections((array) $a_value);
515 $condition = $a_search->getSQLCondition('value_index');
516
518 "adv_md_values",
519 "Enum",
520 $this->getFieldId(),
521 $condition
522 );
523 if ($in_use) {
524 foreach ($in_use as $item) {
525 $res[] = array($item["obj_id"], $item["sub_type"], $item["sub_id"], $item["value_index"]);
526 }
527 }
528 return $res;
529 }
530
531 protected function importTranslatedFormPostValues(ilPropertyFormGUI $form, string $language): void
532 {
533 foreach ($this->options()->getOptions() as $option) {
534 $value = $form->getInput('opts__' . $language . '__' . $option->optionID());
535 $value = trim($value);
536
537 if ($option->hasTranslationInLanguage($language)) {
538 $option->getTranslationInLanguage($language)->setValue($value);
539 continue;
540 }
541 $new_translation = $option->addTranslation($language);
542 $new_translation->setValue($value);
543 }
544 }
545
547 {
548 return is_array($this->confirm_objects) && count($this->confirm_objects) > 0;
549 }
551 {
552 global $DIC;
553
554 $lng = $DIC['lng'];
555 $objDefinition = $DIC['objDefinition'];
556
557 $post_conf_det = (array) ($this->http->request()->getParsedBody()['conf_det'] ?? []);
558 $post_conf = (array) ($this->http->request()->getParsedBody()['conf'] ?? []);
559
560 $custom_field = $a_form->getItemByPostVar('opts_edit');
561 $custom_field->setDisabled(true);
562 foreach ($custom_field->getSubInputItemsRecursive() as $sub_input) {
563 $sub_input->setDisabled(true);
564 /*
565 * If disabled, checkboxes don't come with a hidden input to write to post,
566 * this is a workaround.
567 */
568 if ($sub_input instanceof ilCheckboxInputGUI) {
569 $hidden = new ilHiddenInputGUI($sub_input->getPostVar());
570 $hidden->setValue($sub_input->getValue());
571 $a_form->addItem($hidden);
572 }
573 }
574
575 if (is_array($this->confirm_objects) && count($this->confirm_objects) > 0) {
576 $sec = new ilFormSectionHeaderGUI();
577 $sec->setTitle($lng->txt("md_adv_confirm_definition_select_section"));
578 $a_form->addItem($sec);
579
580 foreach ($this->confirm_objects as $old_option_index => $items) {
581 $old_option_value = $this->confirm_objects_values[$old_option_index];
582 $details = new ilRadioGroupInputGUI(
583 $lng->txt("md_adv_confirm_definition_select_option") . ': "' . $old_option_value . '"',
584 "conf_det[" . $this->getFieldId() . "][" . $old_option_index . "]"
585 );
586 $details->setRequired(true);
587 $details->setValue("sum");
588 $a_form->addItem($details);
589
590 // automatic reload does not work
591 if (isset($post_conf_det[$this->getFieldId()][$old_option_index])) {
592 $details->setValue($post_conf_det[$this->getFieldId()][$old_option_index]);
593 }
594
595 $sum = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_all"), "sum");
596 $details->addOption($sum);
597
598 $sel = new ilSelectInputGUI(
599 $lng->txt("md_adv_confirm_definition_select_option_all_action"),
600 "conf_det_act[" . $this->getFieldId() . "][" . $old_option_index . "]"
601 );
602 $sel->setRequired(true);
603 $options = array(
604 "" => $lng->txt("please_select"),
605 self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
606 );
607 foreach ($this->options()->getOptions() as $new_option) {
608 $new_id = $new_option->optionID();
609 $new_value = $new_option->getTranslationInLanguage($this->default_language)->getValue();
610 $options['idx_' . $new_id] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_value . '"';
611 }
612 $sel->setOptions($options);
613 $sum->addSubItem($sel);
614
615 // automatic reload does not work
616 if (isset($post_conf_det[$this->getFieldId()][$old_option_index])) {
617 if ($post_conf_det[$this->getFieldId()][$old_option_index]) {
618 $sel->setValue($post_conf_det[$this->getFieldId()][$old_option_index]);
619 } elseif ($post_conf_det[$this->getFieldId()][$old_option_index] == "sum") {
620 $sel->setAlert($lng->txt("msg_input_is_required"));
621 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
622 }
623 }
624 $single = new ilRadioOption($lng->txt("md_adv_confirm_definition_select_option_single"), "sgl");
625 $details->addOption($single);
626 foreach ($items as $item) {
627 $obj_id = (int) $item[0];
628 $sub_type = (string) $item[1];
629 $sub_id = (int) $item[2];
630
631 /*
632 * media objects are saved in adv_md_values with obj_id=0, and their actual obj_id
633 * as sub_id.
634 */
635 if ($sub_type === 'mob') {
636 $obj_id = $sub_id;
637 $sub_id = 0;
638 }
639
640 $item_id = $obj_id . "_" . $sub_type . "_" . $sub_id;
641
642 $type = ilObject::_lookupType($obj_id);
643 $type_title = $lng->txt("obj_" . $type);
644 $title = ' "' . ilObject::_lookupTitle($obj_id) . '"';
645
646 if ($sub_id) {
647 $class = "ilObj" . $objDefinition->getClassName($type);
648 $class_path = $objDefinition->getLocation($type);
649 $ints = class_implements($class);
650 if (isset($ints["ilAdvancedMetaDataSubItems"])) {
652 $sub_title = $class::getAdvMDSubItemTitle($obj_id, $sub_type, $sub_id);
653 if ($sub_title) {
654 $title .= ' (' . $sub_title . ')';
655 }
656 }
657 }
658
659 $sel = new ilSelectInputGUI(
660 $type_title . ' ' . $title,
661 "conf[" . $this->getFieldId() . "][" . $old_option_index . "][" . $item_id . "]"
662 );
663 $sel->setRequired(true);
664 $options = array(
665 "" => $lng->txt("please_select"),
666 self::REMOVE_ACTION_ID => $lng->txt("md_adv_confirm_definition_select_option_remove")
667 );
668 foreach ($this->options()->getOptions() as $new_option) {
669 $new_id = $new_option->optionID();
670 $new_value = $new_option->getTranslationInLanguage($this->default_language)->getValue();
671 $options['idx_' . $new_id] = $lng->txt("md_adv_confirm_definition_select_option_overwrite") . ': "' . $new_value . '"';
672 }
673 $sel->setOptions($options);
674
675 // automatic reload does not work
676 if (isset($post_conf[$this->getFieldId()][$old_option_index][$item_id])) {
677 if ($post_conf[$this->getFieldId()][$old_option_index][$item_id]) {
678 $sel->setValue($post_conf[$this->getFieldId()][$old_option_index][$item_id]);
679 } elseif ($post_conf_det[$this->getFieldId()][$old_option_index] == "sgl") {
680 $sel->setAlert($lng->txt("msg_input_is_required"));
681 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
682 }
683 }
684
685 $single->addSubItem($sel);
686 }
687 }
688 }
689 }
690
691 public function delete(): void
692 {
693 $this->deleteOptions();
694 parent::delete();
695 }
696
697 public function save(bool $keep_pos_and_import_id = false, bool $keep_import_id = false): void
698 {
699 parent::save($keep_pos_and_import_id, $keep_import_id);
700 $this->saveOptions();
701 }
702
703 protected function deleteOptions(): void
704 {
705 $this->db_gateway->delete($this->getFieldId());
706 }
707
708 protected function updateOptions(): void
709 {
710 $this->db_gateway->update($this->options());
711 $this->options = $this->db_gateway->readByID($this->getFieldId());
712 }
713
714 protected function saveOptions(): void
715 {
716 $this->db_gateway->create($this->getFieldId(), $this->options());
717 $options = $this->db_gateway->readByID($this->getFieldId());
718 if ($options) {
719 $this->options = $this->db_gateway->readByID($this->getFieldId());
720 }
721 }
722
723 public function update(): void
724 {
725 if (is_array($this->confirmed_objects) && count($this->confirmed_objects) > 0) {
726 // we need the "old" options for the search
727 $def = $this->getADTDefinition();
728 $def = clone($def);
729 $def->setOptions($this->old_options_array);
730 $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def, false, true);
732
733 $page_list_mappings = [];
734
735 foreach ($this->confirmed_objects as $old_option => $item_ids) {
736 // get complete old values
737 $old_values = [];
738 foreach ($this->findBySingleValue($search, $old_option) as $item) {
739 $old_values[$item[0] . "_" . $item[1] . "_" . $item[2]] = $item[3];
740 }
741
742 foreach ($item_ids as $item => $new_option) {
743 $parts = explode("_", $item);
744 $obj_id = (int) $parts[0];
745 $sub_type = $parts[1];
746 $sub_id = (int) $parts[2];
747
748 // update existing value (with changed option)
749 if (isset($old_values[$item])) {
750 $old_id = $old_values[$item];
751
752 $primary = array(
753 "obj_id" => array("integer", $obj_id),
754 "sub_type" => array("text", $sub_type),
755 "sub_id" => array("integer", $sub_id),
756 "field_id" => array("integer", $this->getFieldId())
757 );
758
759 $id_old = array_merge(
760 $primary,
761 [
762 'value_index' => [ilDBConstants::T_INTEGER, $old_id]
763 ]
764 );
765 $id_new = array_merge(
766 $primary,
767 [
768 'value_index' => [ilDBConstants::T_INTEGER, $new_option]
769 ]
770 );
771 ilADTActiveRecordByType::deleteByPrimary('adv_md_values', $id_old, 'MultiEnum');
772
773 if (is_numeric($new_option)) {
774 ilADTActiveRecordByType::deleteByPrimary('adv_md_values', $id_new, 'MultiEnum');
775 ilADTActiveRecordByType::create('adv_md_values', $id_new, 'MultiEnum');
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->updateOptions();
798 }
799
800 protected function addPropertiesToXML(ilXmlWriter $a_writer): void
801 {
802 foreach ($this->options()->getOptions() as $option) {
803 foreach ($option->getTranslations() as $translation) {
804 $a_writer->xmlElement(
805 'FieldValue',
806 ['id' => $translation->language()],
807 $translation->getValue()
808 );
809 }
810 }
811 }
812
817 public function importXMLProperty(string $a_key, string $a_value): void
818 {
819 $language = $a_key;
820 if ($language === '') {
821 $language = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordId())->getDefaultLanguage();
822 }
823
824 $associated_option = null;
825 $max_position = -1;
826 foreach ($this->options()->getOptions() as $option) {
827 if (
828 !$option->hasTranslationInLanguage($language) &&
829 !isset($associated_option)
830 ) {
831 $associated_option = $option;
832 }
833 $max_position = max($max_position, $option->getPosition());
834 }
835 if (!isset($associated_option)) {
836 $associated_option = $this->options()->addOption();
837 $associated_option->setPosition($max_position + 1);
838 }
839
840 $new_translation = $associated_option->addTranslation($language);
841 $new_translation->setValue($a_value);
842 }
843
844 public function getValueForXML(ilADT $element): string
845 {
846 $record = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordID());
847 if (!$record->getParentObject()) {
848 return (string) $element->getSelection();
849 }
855 $index = 1;
856 foreach ($this->options()->getOptions() as $option) {
857 if ($option->optionID() === (int) $element->getSelection()) {
858 return (string) $index;
859 }
860 $index++;
861 }
862 return '';
863 }
864
865 public function importValueFromXML(string $a_cdata): void
866 {
867 $a_cdata = $this->translateLegacyImportValueFromXML($a_cdata);
868 $this->getADT()->setSelection($a_cdata);
869 }
870
880 protected function translateLegacyImportValueFromXML(string $value): string
881 {
882 if (is_numeric($value) && !is_null($this->options()->getOption((int) $value))) {
883 return $value;
884 }
885
886 $default_language = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordId())->getDefaultLanguage();
887 foreach ($this->options()->getOptions() as $option) {
888 if ($value === $option->getTranslationInLanguage($default_language)) {
889 return (string) $option->optionID();
890 }
891 }
892
893 return $value;
894 }
895
896 public function prepareElementForEditor(ilADTFormBridge $a_bridge): void
897 {
898 assert($a_bridge instanceof ilADTEnumFormBridge);
899
900 $a_bridge->setAutoSort(false);
901 }
902
903 protected function readOptions(): void
904 {
905 if ($this->getFieldId()) {
906 $this->options = $this->db_gateway->readByID($this->getFieldId());
907 }
908 if (!isset($this->options)) {
909 $this->options = new SelectSpecificDataImplementation();
910 }
911
912 $record = ilAdvancedMDRecord::_getInstanceByRecordId($this->getRecordId());
913 $this->default_language = $record->getDefaultLanguage();
914 }
915
916 public function _clone(int $a_new_record_id): self
917 {
919 $obj = parent::_clone($a_new_record_id);
920 $this->db_gateway->delete($obj->getFieldId());
921 $this->db_gateway->create($obj->getFieldId(), $this->options());
922 $obj->update();
923 return $obj;
924 }
925}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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.
ADT search bridge base class.
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:26
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)
getOptionsInLanguageAsArray(string $language, bool $default_as_fallback=true)
addEditOptionsFieldsToDefinitionForm(SelectSpecificData $options, ilPropertyFormGUI $a_form, bool $a_disabled)
importXMLProperty(string $a_key, string $a_value)
Since the XML import only allows for a key-value pair, we also rely on the order of properties to sor...
addRadioToEntriesGroup(ilRadioGroupInputGUI $entries, array $position_select_options, ?int $position_value, string $label, string $value, string $id, bool $with_delete_checkbox, bool $disabled)
Returns hidden inputs if a disabled checkbox is in the radio, such that we can use the workaround for...
__construct(GenericData $generic_data, string $language='')
importFieldDefinition(array $a_def)
Import (type-specific) field definition from DB.
importNewSelectOptions(bool $multi, ilPropertyFormGUI $a_form, string $language='')
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 $keep_pos_and_import_id=false, bool $keep_import_id=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.
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.
const TYPE_SELECT
TODO: put this in when minimum php version is set to 8.2.
static _getInstanceByRecordId(int $a_record_id)
This class represents a checkbox property in a property form.
setRequired(bool $a_required)
This class represents a section header in a property form.
This class represents a hidden form property in a property form.
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.
addOption(ilRadioOption $a_option)
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.
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)
$res
Definition: ltiservices.php:69
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26