ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Select.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use ILIAS\Refinery\Factory as Refinery;
28
29class Select implements Type
30{
31 public function getLabel(Language $lng): string
32 {
33 return $lng->txt('udf_type_select');
34 }
35
38 FieldFactory $ff,
40 ?string $data
41 ): ?FormInput {
42 $parsed_data = $this->parseData($data);
43 return $ff->group([
44 'allow_multiple' => $ff->checkbox($lng->txt('multiple_selection'))
45 ->withValue($parsed_data['allow_multiple']),
46 'options' => $ff->tag($lng->txt('udf_select_options'), [])
47 ->withValue($parsed_data['options'])
48 ])->withAdditionalTransformation(
49 $refinery->custom()->transformation(
50 static fn(array $vs): string => json_encode([
51 'allow_multiple' => $vs['allow_multiple'],
52 'options' => $vs['options']
53 ])
54 )
55 );
56 }
57
58 public function getLegacyInput(
61 array $user_value,
62 string $label,
63 ?string $data
65 $parsed_data = $this->parseData($data);
66 if ($parsed_data['allow_multiple']) {
67 $input = new \ilMultiSelectInputGUI($label);
68 $input->setOptions($this->parseData($data)['options']);
69 $input->setValue($user_value);
70 return $input;
71 }
72
73 $input = new \ilSelectInputGUI($label);
74 $input->setOptions(['' => $lng->txt('please_select')] + $this->parseData($data)['options']);
75 $input->setValue($user_value[0] ?? '');
76 return $input;
77 }
78
79 public function prepareUserInputForStorage(mixed $input): array
80 {
81 if (is_array($input)) {
82 return $input;
83 }
84
85 return [$input];
86 }
87
89 array $input,
90 ?string $data
91 ): string {
92 if ($data === null || $input === []) {
93 return '';
94 }
95
96 $options = $this->parseData($data)['options'];
97
98 return implode(
99 ', ',
100 array_reduce(
101 $input,
102 static function (array $c, string|int $v) use ($options): array {
103 if (array_key_exists($v, $options)) {
104 $c[] = $options[$v];
105 return $c;
106 }
107
108 $value = array_search($v, $options);
109 if ($value === false) {
110 return $c;
111 }
112
113 $c[] = $v;
114 return $c;
115 },
116 []
117 )
118 );
119 }
120
121 private function parseData(?string $data): array
122 {
123 if ($data === null) {
124 return [
125 'allow_multiple' => false,
126 'options' => []
127 ];
128 }
129
130 $values = json_decode($data, true) ?? unserialize($data, ['allowed_classes' => false]) ?? [];
131
132 return [
133 'allow_multiple' => $values['allow_multiple'] ?? false,
134 'options' => $values['options'] ?? $values
135 ];
136 }
137}
Builds data types.
Definition: Factory.php:36
getAdditionalEditFormInputs(Language $lng, FieldFactory $ff, Refinery $refinery, ?string $data)
MAY return a valid FormInput that will be added to the form to edit/create the custom field to specif...
Definition: Select.php:36
getLegacyInput(Language $lng, Context $context, array $user_value, string $label, ?string $data)
MUST return a ilFormPropertyGUI to be shown to the user when entering the information.
Definition: Select.php:58
buildPresentationValueFromUserValue(array $input, ?string $data)
If you have a value that has a $key => $value structure, e.g.
Definition: Select.php:88
This class represents a property in a property form.
$c
Definition: deliver.php:25
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is what a factory for input fields looks like.
Definition: Factory.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
$context
Definition: webdav.php:31