ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 \ilSelectInputGUI($label);
68 $input->setOptions(
69 ['' => $lng->txt('please_select')]
70 + array_combine($parsed_data['options'], $parsed_data['options'])
71 );
72 $input->setValue($user_value[0] ?? '');
73 return $input;
74 }
75
76 $input = new \ilMultiSelectInputGUI($label);
77 $input->setOptions(
78 array_combine($parsed_data['options'], $parsed_data['options'])
79 );
80 $input->setValue($user_value);
81 return $input;
82
83
84 }
85
86 public function prepareUserInputForStorage(mixed $input, ?string $data): array
87 {
88 $options = $this->parseData($data)['options'];
89 if (!is_array($input)) {
90 $input = [$input];
91 }
92
93 return array_filter(
94 $input,
95 fn(string $v): bool => in_array($v, $options)
96 );
97 }
98
99 private function parseData(?string $data): array
100 {
101 if ($data === null) {
102 return [
103 'allow_multiple' => false,
104 'options' => []
105 ];
106 }
107
108 $values = json_decode($data, true) ?? unserialize($data, ['allowed_classes' => false]) ?? [];
109
110 return [
111 'allow_multiple' => $values['allow_multiple'] ?? false,
112 'options' => $values['options'] ?? $values
113 ];
114 }
115}
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
prepareUserInputForStorage(mixed $input, ?string $data)
Definition: Select.php:86
This class represents a property in a property form.
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
$context
Definition: webdav.php:31