ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailOptions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Data\Factory as DataFactory;
26
28{
29 final public const int INCOMING_LOCAL = 0;
30 final public const int INCOMING_EMAIL = 1;
31 final public const int INCOMING_BOTH = 2;
32 final public const int FIRST_EMAIL = 3;
33 final public const int SECOND_EMAIL = 4;
34 final public const int BOTH_EMAIL = 5;
35 final public const bool ABSENCE_STATUS_PRESENT = false;
36 final public const bool ABSENCE_STATUS_ABSENT = true;
37
38 protected ILIAS $ilias;
39 protected ilDBInterface $db;
42 protected string $table_mail_options = 'mail_options';
43 protected string $signature = '';
44 protected bool $is_cron_notification_enabled = false;
49 protected ?int $stored_email_address_mode = null;
51 protected string $first_mail_address = '';
52 protected string $second_mail_address = '';
54 protected int $absent_from = 0;
55 protected int $absent_until = 0;
56 protected string $absence_auto_responder_body = '';
57 protected string $absence_auto_responder_subject = '';
59
60 public function __construct(
61 protected int $usr_id,
64 ?ilSetting $settings = null,
65 ?ilDBInterface $db = null,
67 ) {
68 global $DIC;
69 $this->db = $db ?? $DIC->database();
70 $this->settings = $settings ?? $DIC->settings();
71 $this->mail_transport_settings = $mail_transport_settings ?? new ilMailTransportSettings($this);
72 $this->clock_service = $clock_service ?? (new DataFactory())->clock()->utc();
73 $this->user_settings = $user_settings ?? $DIC['user']->getSettings();
74
75 $this->incoming_type = self::INCOMING_LOCAL;
76 $default_incoming_type = $this->settings->get('mail_incoming_mail', '');
77 if ($default_incoming_type !== '') {
78 $this->default_incoming_type = (int) $default_incoming_type;
79 $this->incoming_type = $this->default_incoming_type;
80 }
81
82 $this->email_address_mode = self::FIRST_EMAIL;
83 $default_email_address_mode = $this->settings->get('mail_address_option', '');
84 if ($default_email_address_mode !== '') {
85 $this->default_email_address_mode = (int) $default_email_address_mode;
86 $this->email_address_mode = $this->default_email_address_mode;
87 }
88
89 $this->is_cron_notification_enabled = false;
90 $this->signature = '';
91
92 $this->read();
93 }
94
99 public function createMailOptionsEntry(): void
100 {
101 $this->stored_email_address_mode = $this->default_email_address_mode;
102
103 $this->db->replace(
104 $this->table_mail_options,
105 [
106 'user_id' => ['integer', $this->usr_id],
107 ],
108 [
109 'signature' => ['text', $this->signature],
110 'incoming_type' => ['integer', $this->default_incoming_type],
111 'mail_address_option' => ['integer', $this->default_email_address_mode],
112 'cronjob_notification' => ['integer', (int) $this->is_cron_notification_enabled]
113 ]
114 );
115 }
116
118 {
119 return $this->mayManageInvididualSettings()
120 && $this->user_settings->settingAvailableToUser(IncomingMail::class);
121 }
122
124 {
125 return $this->mayManageInvididualSettings()
126 && $this->user_settings->settingAvailableToUser(NewMailNotification::class);
127 }
128
129 public function mayManageInvididualSettings(): bool
130 {
131 return $this->settings->get('show_mail_settings') === '1';
132 }
133
134 protected function read(): void
135 {
136 $query = 'SELECT mail_options.cronjob_notification,
137 mail_options.signature,
138
139 mail_options.incoming_type,
140 mail_options.mail_address_option,
141 mail_options.absence_status,
142 mail_options.absent_from,
143 mail_options.absent_until,
144 mail_options.absence_ar_subject,
145 mail_options.absence_ar_body,
146 usr_data.email,
147 usr_data.second_email
148 FROM mail_options
149 INNER JOIN usr_data ON mail_options.user_id = usr_data.usr_id
150 WHERE mail_options.user_id = %s';
151 $res = $this->db->queryF(
152 $query,
153 ['integer'],
154 [$this->usr_id]
155 );
156 $row = $this->db->fetchObject($res);
157 if ($row === null) {
158 $this->mail_transport_settings->adjust($this->first_mail_address, $this->second_mail_address, false);
159 return;
160 }
161
162 $this->first_mail_address = (string) $row->email;
163 $this->second_mail_address = (string) $row->second_email;
164 $this->stored_email_address_mode = (int) $row->mail_address_option;
165
167 $this->is_cron_notification_enabled = (bool) $row->cronjob_notification;
168 $this->signature = (string) $row->signature;
169 $this->setAbsenceStatus((bool) $row->absence_status);
170 $this->setAbsentFrom((int) $row->absent_from);
171 $this->setAbsentUntil((int) $row->absent_until);
172 $this->setAbsenceAutoresponderSubject($row->absence_ar_subject ?? '');
173 $this->setAbsenceAutoresponderBody($row->absence_ar_body ?? '');
174 }
175
177 $this->incoming_type = (int) $row->incoming_type;
178 $this->email_address_mode = (int) $row->mail_address_option;
179
180 if (filter_var(
181 $this->incoming_type,
182 FILTER_VALIDATE_INT,
183 ['options' => ['min_range' => self::INCOMING_LOCAL, 'max_range' => self::INCOMING_BOTH]]
184 ) === false) {
185 $this->incoming_type = self::INCOMING_LOCAL;
186 }
187
188 if (filter_var(
189 $this->email_address_mode,
190 FILTER_VALIDATE_INT,
191 ['options' => ['min_range' => self::FIRST_EMAIL, 'max_range' => self::BOTH_EMAIL]]
192 ) === false) {
193 $this->email_address_mode = self::FIRST_EMAIL;
194 }
195 }
196
197 $this->mail_transport_settings->adjust($this->first_mail_address, $this->second_mail_address);
198 }
199
200 public function updateOptions(): int
201 {
202 $data = [
203 'signature' => ['text', $this->getSignature()],
204 'incoming_type' => ['integer', $this->getIncomingType()],
205 'mail_address_option' => ['integer', $this->getEmailAddressMode()],
206 ];
207
208 if ($this->settings->get('mail_notification', '0')) {
209 $data['cronjob_notification'] = ['integer', (int) $this->isCronJobNotificationEnabled()];
210 } else {
211 $data['cronjob_notification'] = ['integer', self::lookupNotificationSetting($this->usr_id)];
212 }
213
214 $data['absence_status'] = ['integer', (int) $this->getAbsenceStatus()];
215 $data['absent_from'] = ['integer', $this->getAbsentFrom()];
216 $data['absent_until'] = ['integer', $this->getAbsentUntil()];
217 $data['absence_ar_subject'] = ['text', $this->getAbsenceAutoresponderSubject()];
218 $data['absence_ar_body'] = ['clob', $this->getAbsenceAutoresponderBody()];
219
220 $this->stored_email_address_mode = $this->getEmailAddressMode();
221
222 return $this->db->replace(
223 $this->table_mail_options,
224 [
225 'user_id' => ['integer', $this->usr_id],
226 ],
227 $data
228 );
229 }
230
231 public function getSignature(): string
232 {
233 return $this->signature;
234 }
235
236 public function getIncomingType(): int
237 {
239 }
240
241 public function setSignature(string $signature): void
242 {
243 $this->signature = $signature;
244 }
245
246 public function setIncomingType(int $incoming_type): void
247 {
248 $this->incoming_type = $incoming_type;
249 }
250
252 {
253 $this->is_cron_notification_enabled = $is_cron_notification_enabled;
254 }
255
256 public function isCronJobNotificationEnabled(): bool
257 {
259 }
260
261 public function getEmailAddressMode(): int
262 {
264 }
265
266 public function getStoredEmailAddressMode(): ?int
267 {
269 }
270
271 public function setEmailAddressmode(int $email_address_mode): void
272 {
273 $this->email_address_mode = $email_address_mode;
274 }
275
276 public function getUsrId(): int
277 {
278 return $this->usr_id;
279 }
280
281 private static function lookupNotificationSetting(int $usr_id): int
282 {
283 global $DIC;
284
285 $row = $DIC->database()->fetchAssoc($DIC->database()->queryF(
286 'SELECT cronjob_notification FROM mail_options WHERE user_id = %s',
287 ['integer'],
288 [$usr_id]
289 ));
290
291 return (int) $row['cronjob_notification'];
292 }
293
297 public function getExternalEmailAddresses(): array
298 {
299 $email_addresses = [];
300 switch ($this->getEmailAddressMode()) {
302 if ($this->second_mail_address !== '') {
303 $email_addresses[] = $this->second_mail_address;
304 } elseif ($this->first_mail_address !== '') {
305 // fallback, use first email address
306 $email_addresses[] = $this->first_mail_address;
307 }
308 break;
309
310 case self::BOTH_EMAIL:
311 if ($this->first_mail_address !== '') {
312 $email_addresses[] = $this->first_mail_address;
313 }
314 if ($this->second_mail_address !== '') {
315 $email_addresses[] = $this->second_mail_address;
316 }
317 break;
318
320 default:
321 if ($this->first_mail_address !== '') {
322 $email_addresses[] = $this->first_mail_address;
323 } elseif ($this->second_mail_address !== '') {
324 // fallback, use first email address
325 $email_addresses[] = $this->second_mail_address;
326 }
327 break;
328 }
329
330 return $email_addresses;
331 }
332
334 {
335 $this->absence_auto_responder_body = $absence_auto_responder_body;
336 }
337
338 public function getAbsenceAutoresponderBody(): string
339 {
341 }
342
343 public function setAbsenceStatus(bool $absence_status): void
344 {
345 $this->absence_status = $absence_status;
346 }
347
348 public function getAbsenceStatus(): bool
349 {
351 }
352
353 public function setAbsentFrom(int $absent_from): void
354 {
355 $this->absent_from = $absent_from;
356 }
357
358 public function getAbsentFrom(): int
359 {
360 return $this->absent_from;
361 }
362
363 public function setAbsentUntil(int $absent_until): void
364 {
365 $this->absent_until = $absent_until;
366 }
367
368 public function getAbsentUntil(): int
369 {
370 return $this->absent_until;
371 }
372
374 {
375 $this->absence_auto_responder_subject = $absence_auto_responder_subject;
376 }
377
378 public function getAbsenceAutoresponderSubject(): string
379 {
381 }
382
383 public function isAbsent(): bool
384 {
385 return
386 $this->getAbsenceStatus() &&
387 $this->getAbsentFrom() &&
388 $this->getAbsentUntil() &&
389 $this->getAbsentFrom() <= $this->clock_service->now()->getTimestamp() &&
390 $this->getAbsentUntil() >= $this->clock_service->now()->getTimestamp();
391 }
392}
Builds data types.
Definition: Factory.php:36
setSignature(string $signature)
bool $is_cron_notification_enabled
final const int INCOMING_BOTH
ilMailTransportSettings $mail_transport_settings
__construct(protected int $usr_id, ?ilMailTransportSettings $mail_transport_settings=null, ?ClockInterface $clock_service=null, ?ilSetting $settings=null, ?ilDBInterface $db=null, ?Settings $user_settings=null)
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()
final const int FIRST_EMAIL
setAbsenceStatus(bool $absence_status)
ilDBInterface $db
final const bool ABSENCE_STATUS_ABSENT
setAbsenceAutoresponderSubject(string $absence_auto_responder_subject)
setAbsentUntil(int $absent_until)
final const int INCOMING_LOCAL
setAbsenceAutoresponderBody(string $absence_auto_responder_body)
ClockInterface $clock_service
setEmailAddressmode(int $email_address_mode)
final const int BOTH_EMAIL
final const int INCOMING_EMAIL
string $absence_auto_responder_body
static lookupNotificationSetting(int $usr_id)
setIncomingType(int $incoming_type)
string $absence_auto_responder_subject
final const bool ABSENCE_STATUS_PRESENT
setAbsentFrom(int $absent_from)
setIsCronJobNotificationStatus(bool $is_cron_notification_enabled)
final const int SECOND_EMAIL
ILIAS Setting Class.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26