ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMailOptions.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
30 {
31  final public const INCOMING_LOCAL = 0;
32  final public const INCOMING_EMAIL = 1;
33  final public const INCOMING_BOTH = 2;
34  final public const FIRST_EMAIL = 3;
35  final public const SECOND_EMAIL = 4;
36  final public const BOTH_EMAIL = 5;
37  final public const ABSENCE_STATUS_PRESENT = false;
38  final public const ABSENCE_STATUS_ABSENT = true;
39 
40  protected ILIAS $ilias;
41  protected ilDBInterface $db;
42  protected ilSetting $settings;
43  protected string $table_mail_options = 'mail_options';
44  protected string $signature = '';
45  protected bool $isCronJobNotificationEnabled = false;
46  protected int $incomingType = self::INCOMING_LOCAL;
47  protected int $default_incoming_type = self::INCOMING_LOCAL;
48  protected int $emailAddressMode = self::FIRST_EMAIL;
49  protected int $default_email_address_mode = self::FIRST_EMAIL;
51  protected string $firstEmailAddress = '';
52  protected string $secondEmailAddress = '';
53  protected bool $absence_status = self::ABSENCE_STATUS_PRESENT;
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 $usrId,
62  ?ilMailTransportSettings $mailTransportSettings = null,
63  ?ClockInterface $clockService = null,
64  ?ilSetting $settings = null,
65  ?ilDBInterface $db = null
66  ) {
67  global $DIC;
68  $this->db = $db ?? $DIC->database();
69  $this->settings = $settings ?? $DIC->settings();
70  $this->mailTransportSettings = $mailTransportSettings ?? new ilMailTransportSettings($this);
71  $this->clockService = $clockService ?? (new DataFactory())->clock()->utc();
72 
73  $this->incomingType = self::INCOMING_LOCAL;
74  $default_incoming_type = $this->settings->get('mail_incoming_mail', '');
75  if ($default_incoming_type !== '') {
76  $this->default_incoming_type = (int) $default_incoming_type;
77  $this->incomingType = $this->default_incoming_type;
78  }
79 
80  $this->emailAddressMode = self::FIRST_EMAIL;
81  $default_email_address_mode = $this->settings->get('mail_address_option', '');
82  if ($default_email_address_mode !== '') {
83  $this->default_email_address_mode = (int) $default_email_address_mode;
84  $this->emailAddressMode = $this->default_email_address_mode;
85  }
86 
87  $this->isCronJobNotificationEnabled = false;
88  $this->signature = '';
89 
90  $this->read();
91  }
92 
97  public function createMailOptionsEntry(): void
98  {
99  $this->db->replace(
100  $this->table_mail_options,
101  [
102  'user_id' => ['integer', $this->usrId],
103  ],
104  [
105  'signature' => ['text', $this->signature],
106  'incoming_type' => ['integer', $this->default_incoming_type],
107  'mail_address_option' => ['integer', $this->default_email_address_mode],
108  'cronjob_notification' => ['integer', (int) $this->isCronJobNotificationEnabled]
109  ]
110  );
111  }
112 
113  public function mayModifyIndividualTransportSettings(): bool
114  {
115  return (
116  $this->mayManageInvididualSettings() &&
118  $this->settings->get('usr_settings_disable_mail_incoming_mail') !== '1'
119  );
120  }
121 
122  public function maySeeIndividualTransportSettings(): bool
123  {
124  return $this->settings->get('usr_settings_hide_mail_incoming_mail') !== '1';
125  }
126 
127  public function mayManageInvididualSettings(): bool
128  {
129  return $this->settings->get('show_mail_settings') === '1';
130  }
131 
132  protected function read(): void
133  {
134  $query = 'SELECT mail_options.cronjob_notification,
135  mail_options.signature,
136 
137  mail_options.incoming_type,
138  mail_options.mail_address_option,
139  mail_options.absence_status,
140  mail_options.absent_from,
141  mail_options.absent_until,
142  mail_options.absence_ar_subject,
143  mail_options.absence_ar_body,
144  usr_data.email,
145  usr_data.second_email
146  FROM mail_options
147  INNER JOIN usr_data ON mail_options.user_id = usr_data.usr_id
148  WHERE mail_options.user_id = %s';
149  $res = $this->db->queryF(
150  $query,
151  ['integer'],
152  [$this->usrId]
153  );
154  $row = $this->db->fetchObject($res);
155  if ($row === null) {
156  $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress, false);
157  return;
158  }
159 
160  $this->firstEmailAddress = (string) $row->email;
161  $this->secondEmailAddress = (string) $row->second_email;
162  if ($this->mayManageInvididualSettings()) {
163  $this->isCronJobNotificationEnabled = (bool) $row->cronjob_notification;
164  $this->signature = (string) $row->signature;
165  $this->setAbsenceStatus((bool) $row->absence_status);
166  $this->setAbsentFrom((int) $row->absent_from);
167  $this->setAbsentUntil((int) $row->absent_until);
168  $this->setAbsenceAutoresponderSubject($row->absence_ar_subject ?? '');
169  $this->setAbsenceAutoresponderBody($row->absence_ar_body ?? '');
170  }
171 
173  $this->incomingType = (int) $row->incoming_type;
174  $this->emailAddressMode = (int) $row->mail_address_option;
175 
176  if (false === filter_var(
177  $this->incomingType,
178  FILTER_VALIDATE_INT,
179  ['options' => ['min_range' => self::INCOMING_LOCAL, 'max_range' => self::INCOMING_BOTH]]
180  )) {
181  $this->incomingType = self::INCOMING_LOCAL;
182  }
183 
184  if (false === filter_var(
185  $this->emailAddressMode,
186  FILTER_VALIDATE_INT,
187  ['options' => ['min_range' => self::FIRST_EMAIL, 'max_range' => self::BOTH_EMAIL]]
188  )) {
189  $this->emailAddressMode = self::FIRST_EMAIL;
190  }
191  }
192 
193  $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress);
194  }
195 
196  public function updateOptions(): int
197  {
198  $data = [
199  'signature' => ['text', $this->getSignature()],
200  'incoming_type' => ['integer', $this->getIncomingType()],
201  'mail_address_option' => ['integer', $this->getEmailAddressMode()],
202  ];
203 
204  if ($this->settings->get('mail_notification', '0')) {
205  $data['cronjob_notification'] = ['integer', (int) $this->isCronJobNotificationEnabled()];
206  } else {
207  $data['cronjob_notification'] = ['integer', self::lookupNotificationSetting($this->usrId)];
208  }
209 
210  $data['absence_status'] = ['integer', (int) $this->getAbsenceStatus()];
211  $data['absent_from'] = ['integer', $this->getAbsentFrom()];
212  $data['absent_until'] = ['integer', $this->getAbsentUntil()];
213  $data['absence_ar_subject'] = ['text', $this->getAbsenceAutoresponderSubject()];
214  $data['absence_ar_body'] = ['clob', $this->getAbsenceAutoresponderBody()];
215 
216  return $this->db->replace(
217  $this->table_mail_options,
218  [
219  'user_id' => ['integer', $this->usrId],
220  ],
221  $data
222  );
223  }
224 
225  public function getSignature(): string
226  {
227  return $this->signature;
228  }
229 
230  public function getIncomingType(): int
231  {
232  return $this->incomingType;
233  }
234 
235  public function setSignature(string $signature): void
236  {
237  $this->signature = $signature;
238  }
239 
240  public function setIncomingType(int $incomingType): void
241  {
242  $this->incomingType = $incomingType;
243  }
244 
245  public function setIsCronJobNotificationStatus(bool $isCronJobNotificationEnabled): void
246  {
248  }
249 
250  public function isCronJobNotificationEnabled(): bool
251  {
253  }
254 
255  public function getEmailAddressMode(): int
256  {
258  }
259 
260  public function setEmailAddressMode(int $emailAddressMode): void
261  {
262  $this->emailAddressMode = $emailAddressMode;
263  }
264 
265  public function getUsrId(): int
266  {
267  return $this->usrId;
268  }
269 
270  private static function lookupNotificationSetting(int $usrId): int
271  {
272  global $DIC;
273 
274  $row = $DIC->database()->fetchAssoc($DIC->database()->queryF(
275  'SELECT cronjob_notification FROM mail_options WHERE user_id = %s',
276  ['integer'],
277  [$usrId]
278  ));
279 
280  return (int) $row['cronjob_notification'];
281  }
282 
286  public function getExternalEmailAddresses(): array
287  {
288  $emailAddresses = [];
289 
290  switch ($this->getEmailAddressMode()) {
291  case self::SECOND_EMAIL:
292  if ($this->secondEmailAddress !== '') {
293  $emailAddresses[] = $this->secondEmailAddress;
294  } elseif ($this->firstEmailAddress !== '') {
295  // fallback, use first email address
296  $emailAddresses[] = $this->firstEmailAddress;
297  }
298  break;
299 
300  case self::BOTH_EMAIL:
301  if ($this->firstEmailAddress !== '') {
302  $emailAddresses[] = $this->firstEmailAddress;
303  }
304  if ($this->secondEmailAddress !== '') {
305  $emailAddresses[] = $this->secondEmailAddress;
306  }
307  break;
308 
309  case self::FIRST_EMAIL:
310  default:
311  if ($this->firstEmailAddress !== '') {
312  $emailAddresses[] = $this->firstEmailAddress;
313  } elseif ($this->secondEmailAddress !== '') {
314  // fallback, use first email address
315  $emailAddresses[] = $this->secondEmailAddress;
316  }
317  break;
318  }
319 
320  return $emailAddresses;
321  }
322 
323  public function setAbsenceAutoresponderBody(string $absence_auto_responder_body): void
324  {
325  $this->absence_auto_responder_body = $absence_auto_responder_body;
326  }
327 
328  public function getAbsenceAutoresponderBody(): string
329  {
331  }
332 
333  public function setAbsenceStatus(bool $absence_status): void
334  {
335  $this->absence_status = $absence_status;
336  }
337 
338  public function getAbsenceStatus(): bool
339  {
340  return $this->absence_status;
341  }
342 
343  public function setAbsentFrom(int $absent_from): void
344  {
345  $this->absent_from = $absent_from;
346  }
347 
348  public function getAbsentFrom(): int
349  {
350  return $this->absent_from;
351  }
352 
353  public function setAbsentUntil(int $absent_until): void
354  {
355  $this->absent_until = $absent_until;
356  }
357 
358  public function getAbsentUntil(): int
359  {
360  return $this->absent_until;
361  }
362 
363  public function setAbsenceAutoresponderSubject(string $absence_auto_responder_subject): void
364  {
365  $this->absence_auto_responder_subject = $absence_auto_responder_subject;
366  }
367 
368  public function getAbsenceAutoresponderSubject(): string
369  {
371  }
372 
373  public function isAbsent(): bool
374  {
375  return
376  $this->getAbsenceStatus() &&
377  $this->getAbsentFrom() &&
378  $this->getAbsentUntil() &&
379  $this->getAbsentFrom() <= $this->clockService->now()->getTimestamp() &&
380  $this->getAbsentUntil() >= $this->clockService->now()->getTimestamp();
381  }
382 }
$res
Definition: ltiservices.php:66
ilDBInterface $db
string $absence_auto_responder_subject
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()...
Interface Observer Contains several chained tasks and infos about them.
setAbsentFrom(int $absent_from)
__construct(protected int $usrId, ?ilMailTransportSettings $mailTransportSettings=null, ?ClockInterface $clockService=null, ?ilSetting $settings=null, ?ilDBInterface $db=null)
setSignature(string $signature)
final const SECOND_EMAIL
bool $isCronJobNotificationEnabled
ClockInterface $clockService
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
final const INCOMING_LOCAL
final const INCOMING_EMAIL
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
final const ABSENCE_STATUS_ABSENT
setAbsenceStatus(bool $absence_status)
setIsCronJobNotificationStatus(bool $isCronJobNotificationEnabled)
string $absence_auto_responder_body
setAbsenceAutoresponderBody(string $absence_auto_responder_body)
setAbsenceAutoresponderSubject(string $absence_auto_responder_subject)
ilMailTransportSettings $mailTransportSettings
setEmailAddressMode(int $emailAddressMode)
final const FIRST_EMAIL
setIncomingType(int $incomingType)
setAbsentUntil(int $absent_until)
final const BOTH_EMAIL
final const INCOMING_BOTH
final const ABSENCE_STATUS_PRESENT
static lookupNotificationSetting(int $usrId)