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