ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
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', '0') === '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,
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 empty($user->getEmail()) &&
82 empty($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 empty($user->getEmail()) &&
91 empty($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(
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->setUser($user);
113 $input->setValueByArray(
114 $user !== null
115 ? $this->retrieveValueFromUser($user)
116 : [
117 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
118 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
119 ]
120 );
121
122 return $input;
123 }
124
128 ): string {
129 $setting = (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL);
130 $value = match ($setting) {
131 \ilMailOptions::INCOMING_LOCAL => $lng->txt('mail_incoming_local'),
132 \ilMailOptions::INCOMING_EMAIL => $lng->txt('mail_incoming_smtp'),
133 \ilMailOptions::INCOMING_BOTH => $lng->txt('mail_incoming_both')
134 };
135 if ($setting === \ilMailOptions::INCOMING_LOCAL) {
136 return $value;
137 }
138
139 return match ((int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)) {
140 \ilMailOptions::FIRST_EMAIL => "{$value}: {$lng->txt('mail_first_email')}",
141 \ilMailOptions::SECOND_EMAIL => "{$value}: {$lng->txt('mail_second_email')}",
142 \ilMailOptions::BOTH_EMAIL => "{$value}: {$lng->txt('mail_both_email')}",
143 };
144 }
145
148 \ilObjUser $user
149 ): bool {
150 $current = $this->retrieveValueFromUser($user);
151 $default = [
152 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
153 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
154 ];
155
156 return $current !== $default;
157 }
158
159 public function persistUserInput(
160 \ilObjUser $user,
161 mixed $input,
162 ?\ilPropertyFormGUI $form = null
163 ): \ilObjUser {
164 $mail_options = $this->mailOptionsFor($user);
165 if ($input === null) {
166 $settings = new \ilSetting();
167 $mail_options->setIncomingType(
168 (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL)
169 );
170 $mail_options->setEmailAddressmode(
171 (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
172 );
173 $mail_options->updateOptions();
174
175 return $user;
176 }
177
178 $incoming_type = \is_array($input) ? $input['incoming_mail'] : (int) $input;
179 $mail_options->setIncomingType($incoming_type);
180 if ((int) $input === \ilMailOptions::INCOMING_LOCAL) {
181 $mail_options->updateOptions();
182
183 return $user;
184 }
185
186 if (\is_array($input)) {
187 $address_mode = $input['mail_address_option'];
188 } elseif ($form !== null) {
189 $address_mode = (int) $form->getInput('mail_address_option');
190 } else {
191 $address_mode = $mail_options->getEmailAddressMode();
192 }
193 if (\is_int($address_mode)) {
194 $mail_options->setEmailAddressmode($address_mode);
195 }
196
197 $mail_options->updateOptions();
198
199 return $user;
200 }
201
202 public function retrieveValueFromUser(\ilObjUser $user): ?array
203 {
204 $mail_options = $this->mailOptionsFor($user);
205
206 return [
207 'incoming_mail' => $mail_options->getIncomingType(),
208 'mail_address_option' => $mail_options->getEmailAddressMode()
209 ];
210 }
211
212 private function buildTransformation(
214 ?\ilObjUser $user = null
215 ): Transformation {
216 return $refinery->custom()->transformation(
217 function (array $v) use ($refinery, $user): array {
218 $email_address_option = $v[1]['mail_address_option'] ?? null;
219 if ($user !== null) {
220 if (!empty($user->getEmail()) && empty($user->getSecondEmail())) {
221 $email_address_option = \ilMailOptions::FIRST_EMAIL;
222 } elseif (!empty($user->getSecondEmail()) && empty($user->getEmail())) {
223 $email_address_option = \ilMailOptions::SECOND_EMAIL;
224 }
225 }
226
227 $incoming_type = $refinery->kindlyTo()->int()->transform($v[0]);
228 if ($email_address_option === null) {
229 $incoming_type = \ilMailOptions::INCOMING_LOCAL;
230 }
231
232 return [
233 'incoming_mail' => $incoming_type,
234 'mail_address_option' => $email_address_option === null && $user !== null
235 ? $this->mailOptionsFor($user)->getStoredEmailAddressMode()
236 : $refinery->byTrying([
237 $refinery->kindlyTo()->int(),
238 $refinery->always(null)
239 ])->transform($email_address_option)
240 ];
241 }
242 );
243 }
244
245 private function buildValueSetterArray(
247 ?\ilObjUser $user = null
248 ): int|array {
249 $value = $user !== null
250 ? $this->retrieveValueFromUser($user)
251 : [
252 'incoming_mail' => (int) $settings->get('mail_incoming_mail', (string) \ilMailOptions::INCOMING_LOCAL),
253 'mail_address_option' => (int) $settings->get('mail_address_option', (string) \ilMailOptions::FIRST_EMAIL)
254 ];
255
256 if ($value['incoming_mail'] === \ilMailOptions::INCOMING_LOCAL ||
257 (empty($user->getEmail()) && empty($user->getSecondEmail()))) {
258 return $value['incoming_mail'];
259 }
260
261 return [
262 0 => $value['incoming_mail'],
263 1 => [
264 'mail_address_option' => $value['mail_address_option']
265 ]
266 ];
267 }
268
269 private function buildEmailInputs(
271 FieldFactory $field_factory,
272 ?\ilObjUser $user
273 ): array {
274 if ($user === null || (empty($user->getEmail()) && empty($user->getSecondEmail()))) {
275 return [];
276 }
277
278 return [
279 'mail_address_option' => $field_factory
280 ->radio(
281 $lng->txt('email')
282 )
283 ->withOption(
285 $lng->txt('mail_first_email'),
286 $user->getEmail() === ''
287 ? $lng->txt('first_email_missing_info')
288 : $user->getEmail()
289 )
290 ->withOption(
292 $lng->txt('mail_second_email'),
293 empty($user->getSecondEmail())
294 ? $lng->txt('second_email_missing_info')
295 : $user->getSecondEmail()
296 )
297 ->withOption((string) \ilMailOptions::BOTH_EMAIL, $lng->txt('mail_both_email'))
298 ];
299 }
300
301 private function mailOptionsFor(\ilObjUser $user): \ilMailOptions
302 {
303 if (!\array_key_exists($user->getId(), $this->mail_options_by_user)) {
304 $this->mail_options_by_user[$user->getId()] = new \ilMailOptions($user->getId());
305 }
306
307 return $this->mail_options_by_user[$user->getId()];
308 }
309}
$settings
Definition: index.php:44
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.
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
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:26
if(!file_exists('../ilias.ini.php'))