ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
IncomingMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
33{
35 private array $mail_options_by_user = [];
36
37 public function getIdentifier(): string
38 {
39 return 'incoming_mail';
40 }
41
42 public function isAvailable(): bool
43 {
44 return (new \ilSetting())->get('show_mail_settings') === '1';
45 }
46
47 public function getLabel(Language $lng): string
48 {
49 return $lng->txt('mail_incoming');
50 }
51
53 {
54 return AvailablePages::MainSettings;
55 }
56
57 public function getSection(): AvailableSections
58 {
59 return AvailableSections::Communication;
60 }
61
62 public function getInput(
63 FieldFactory $field_factory,
66 \ilSetting $settings,
67 ?\ilObjUser $user = null
68 ): Input {
69 $lng->loadLanguageModule('mail');
70
71 $inputs = $this->buildEmailInputs($lng, $field_factory, $user);
72
73 return $field_factory->switchableGroup(
74 [
75 \ilMailOptions::INCOMING_LOCAL => $field_factory->group([], $lng->txt('mail_incoming_local')),
76 \ilMailOptions::INCOMING_EMAIL => $field_factory->group(
77 $inputs,
78 $lng->txt('mail_incoming_smtp')
79 )->withDisabled(
80 $user === null || (
81 $user->getEmail() === '' &&
82 $user->getSecondEmail() === ''
83 )
84 ),
85 \ilMailOptions::INCOMING_BOTH => $field_factory->group(
86 $inputs,
87 $lng->txt('mail_incoming_both')
88 )->withDisabled(
89 $user === null || (
90 $user->getEmail() === '' &&
91 $user->getSecondEmail() === ''
92 )
93 )
94 ],
95 $lng->txt('mail_incoming')
96 )->withAdditionalTransformation(
97 $this->buildTransformation($refinery, $user)
98 )->withValue(
99 $this->buildValueSetterArray($settings, $user)
100 );
101 }
102
103 public function getLegacyInput(
105 \ilSetting $settings,
106 ?\ilObjUser $user = null
108 $lng->loadLanguageModule('mail');
109
110 $input = new \ilIncomingMailInputGUI($lng->txt('mail_incoming'), 'incoming_mail');
111 $input->setFreeOptionChoice(false);
112 $input->setValueByArray(
113 $user !== null
114 ? $this->retrieveValueFromUser($user)
115 : [
116 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
117 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
118 ]
119 );
120
121 return $input;
122 }
123
126 \ilSetting $settings
127 ): string {
128 $setting = (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL);
129 $value = match ($setting) {
130 \ilMailOptions::INCOMING_LOCAL => $lng->txt('mail_incoming_local'),
131 \ilMailOptions::INCOMING_EMAIL => $lng->txt('mail_incoming_smtp'),
132 \ilMailOptions::INCOMING_BOTH => $lng->txt('mail_incoming_both')
133 };
134 if ($setting === \ilMailOptions::INCOMING_LOCAL) {
135 return $value;
136 }
137
138 return match ((int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)) {
139 \ilMailOptions::FIRST_EMAIL => "{$value}: {$lng->txt('mail_first_email')}",
140 \ilMailOptions::SECOND_EMAIL => "{$value}: {$lng->txt('mail_second_email')}",
141 \ilMailOptions::BOTH_EMAIL => "{$value}: {$lng->txt('mail_both_email')}",
142 };
143 }
144
146 \ilSetting $settings,
147 \ilObjUser $user
148 ): bool {
149 $current = $this->retrieveValueFromUser($user);
150 $default = [
151 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
152 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
153 ];
154
155 return $current !== $default;
156 }
157
158 public function persistUserInput(
159 \ilObjUser $user,
160 mixed $input,
161 ?\ilPropertyFormGUI $form = null
162 ): \ilObjUser {
163 $mail_options = $this->mailOptionsFor($user);
164 if ($input === null) {
165 $settings = new \ilSetting();
166 $mail_options->setIncomingType(
167 (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL)
168 );
169 $mail_options->setEmailAddressmode(
170 (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
171 );
172 $mail_options->updateOptions();
173
174 return $user;
175 }
176
177 $incoming_type = \is_array($input) ? $input['incoming_mail'] : (int) $input;
178 $mail_options->setIncomingType($incoming_type);
179 if ((int) $input === \ilMailOptions::INCOMING_LOCAL) {
180 $mail_options->updateOptions();
181
182 return $user;
183 }
184
185 if (\is_array($input)) {
186 $address_mode = $input['mail_address_option'];
187 } elseif ($form !== null) {
188 $address_mode = (int) $form->getInput('mail_address_option');
189 } else {
190 $address_mode = $mail_options->getEmailAddressMode();
191 }
192 if (\is_int($address_mode)) {
193 $mail_options->setEmailAddressmode($address_mode);
194 }
195
196 $mail_options->updateOptions();
197
198 return $user;
199 }
200
201 public function retrieveValueFromUser(\ilObjUser $user): ?array
202 {
203 $mail_options = $this->mailOptionsFor($user);
204
205 return [
206 'incoming_mail' => $mail_options->getIncomingType(),
207 'mail_address_option' => $mail_options->getEmailAddressMode()
208 ];
209 }
210
211 private function buildTransformation(
213 ?\ilObjUser $user = null
214 ): Transformation {
215 return $refinery->custom()->transformation(
216 function (array $v) use ($refinery, $user): array {
217 $email_address_option = $v[1]['mail_address_option'] ?? null;
218 if ($user !== null) {
219 if ($user->getEmail() !== '' && $user->getSecondEmail() === '') {
220 $email_address_option = \ilMailOptions::FIRST_EMAIL;
221 } elseif ($user->getSecondEmail() !== '' && $user->getEmail() === '') {
222 $email_address_option = \ilMailOptions::SECOND_EMAIL;
223 }
224 }
225
226 $incoming_type = $refinery->kindlyTo()->int()->transform($v[0]);
227 if ($email_address_option === null) {
228 $incoming_type = \ilMailOptions::INCOMING_LOCAL;
229 }
230
231 return [
232 'incoming_mail' => $incoming_type,
233 'mail_address_option' => $email_address_option === null && $user !== null
234 ? $this->mailOptionsFor($user)->getStoredEmailAddressMode()
235 : $refinery->byTrying([
236 $refinery->kindlyTo()->int(),
237 $refinery->always(null)
238 ])->transform($email_address_option)
239 ];
240 }
241 );
242 }
243
244 private function buildValueSetterArray(
245 \ilSetting $settings,
246 ?\ilObjUser $user = null
247 ): int|array {
248 $value = $user !== null
249 ? $this->retrieveValueFromUser($user)
250 : [
251 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
252 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
253 ];
254
255 if ($value['incoming_mail'] === \ilMailOptions::INCOMING_LOCAL ||
256 ($user->getEmail() === '' && $user->getSecondEmail() === '')) {
257 return $value['incoming_mail'];
258 }
259
260 return [
261 0 => $value['incoming_mail'],
262 1 => [
263 'mail_address_option' => $value['mail_address_option']
264 ]
265 ];
266 }
267
268 private function buildEmailInputs(
270 FieldFactory $field_factory,
271 ?\ilObjUser $user
272 ): array {
273 if ($user === null || ($user->getEmail() === '' && $user->getSecondEmail() === '')) {
274 return [];
275 }
276
277 return [
278 'mail_address_option' => $field_factory
279 ->radio(
280 $lng->txt('email')
281 )
282 ->withOption(
284 $lng->txt('mail_first_email'),
285 $user->getEmail() === ''
286 ? $lng->txt('first_email_missing_info')
287 : $user->getEmail()
288 )
289 ->withOption(
291 $lng->txt('mail_second_email'),
292 $user->getSecondEmail() === ''
293 ? $lng->txt('second_email_missing_info')
294 : $user->getSecondEmail()
295 )
296 ->withOption((string) \ilMailOptions::BOTH_EMAIL, $lng->txt('mail_both_email'))
297 ];
298 }
299
300 private function mailOptionsFor(\ilObjUser $user): \ilMailOptions
301 {
302 if (!\array_key_exists($user->getId(), $this->mail_options_by_user)) {
303 $this->mail_options_by_user[$user->getId()] = new \ilMailOptions($user->getId());
304 }
305
306 return $this->mail_options_by_user[$user->getId()];
307 }
308}
Builds data types.
Definition: Factory.php:36
getLegacyInput(Language $lng, \ilSetting $settings, ?\ilObjUser $user=null)
You don't need to add a post_var to the input as the User will handle this for you,...
getDefaultValueForDisplay(Language $lng, \ilSetting $settings)
persistUserInput(\ilObjUser $user, mixed $input, ?\ilPropertyFormGUI $form=null)
getInput(FieldFactory $field_factory, Language $lng, Refinery $refinery, \ilSetting $settings, ?\ilObjUser $user=null)
buildValueSetterArray(\ilSetting $settings, ?\ilObjUser $user=null)
buildTransformation(Refinery $refinery, ?\ilObjUser $user=null)
hasUserPersonalizedSetting(\ilSetting $settings, \ilObjUser $user)
isAvailable()
If this function returns false the setting will not be shown, even if it's PropertyAttributes would a...
buildEmailInputs(Language $lng, FieldFactory $field_factory, ?\ilObjUser $user)
This class represents a property in a property form.
final const int INCOMING_BOTH
final const int FIRST_EMAIL
final const int INCOMING_LOCAL
final const int BOTH_EMAIL
final const int INCOMING_EMAIL
final const int SECOND_EMAIL
User class.
This class represents a property form user interface.
ILIAS Setting Class.
get(string $a_keyword, ?string $a_default_value=null)
get setting
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A transformation is a function from one datatype to another.
This is what a factory for input fields looks like.
Definition: Factory.php:31
This describes commonalities between all inputs.
Definition: Input.php:47
get(string $class_name)
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))