ILIAS  release_8 Revision v8.24
class.ilMailOptions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const INCOMING_LOCAL = 0;
29 public const INCOMING_EMAIL = 1;
30 public const INCOMING_BOTH = 2;
31 public const FIRST_EMAIL = 3;
32 public const SECOND_EMAIL = 4;
33 public const BOTH_EMAIL = 5;
34 public const DEFAULT_LINE_BREAK = 60;
35 protected ILIAS $ilias;
36 protected ilDBInterface $db;
37 protected int $usrId = 0;
39 protected string $table_mail_options = 'mail_options';
40 protected int $linebreak = 0;
41 protected string $signature = '';
42 protected bool $isCronJobNotificationEnabled = false;
48 protected string $firstEmailAddress = '';
49 protected string $secondEmailAddress = '';
50
51 public function __construct(
52 int $usrId,
54 ilSetting $settings = null,
55 ilDBInterface $db = null
56 ) {
57 global $DIC;
58 $this->usrId = $usrId;
59 $this->db = $db ?? $DIC->database();
60 $this->settings = $settings ?? $DIC->settings();
61 $this->mailTransportSettings = $mailTransportSettings ?? new ilMailTransportSettings($this);
62
63 $this->incomingType = self::INCOMING_LOCAL;
64 $default_incoming_type = $this->settings->get('mail_incoming_mail', '');
65 if ($default_incoming_type !== '') {
66 $this->default_incoming_type = (int) $default_incoming_type;
67 $this->incomingType = $this->default_incoming_type;
68 }
69
70 $this->emailAddressMode = self::FIRST_EMAIL;
71 $default_email_address_mode = $this->settings->get('mail_address_option', '');
72 if ($default_email_address_mode !== '') {
73 $this->default_email_address_mode = (int) $default_email_address_mode;
74 $this->emailAddressMode = $this->default_email_address_mode;
75 }
76
77 $this->linebreak = self::DEFAULT_LINE_BREAK;
78 $this->isCronJobNotificationEnabled = false;
79 $this->signature = '';
80
81 $this->read();
82 }
83
88 public function createMailOptionsEntry(): void
89 {
90 $this->db->replace(
91 $this->table_mail_options,
92 [
93 'user_id' => ['integer', $this->usrId],
94 ],
95 [
96 'linebreak' => ['integer', $this->linebreak],
97 'signature' => ['text', $this->signature],
98 'incoming_type' => ['integer', $this->default_incoming_type],
99 'mail_address_option' => ['integer', $this->default_email_address_mode],
100 'cronjob_notification' => ['integer', (int) $this->isCronJobNotificationEnabled]
101 ]
102 );
103 }
104
106 {
107 return (
110 $this->settings->get('usr_settings_disable_mail_incoming_mail') !== '1'
111 );
112 }
113
114 public function maySeeIndividualTransportSettings(): bool
115 {
116 return $this->settings->get('usr_settings_hide_mail_incoming_mail') !== '1';
117 }
118
119 public function mayManageInvididualSettings(): bool
120 {
121 return $this->settings->get('show_mail_settings') === '1';
122 }
123
124 protected function read(): void
125 {
126 $query = implode(' ', [
127 'SELECT mail_options.cronjob_notification,',
128 'mail_options.signature, mail_options.linebreak, mail_options.incoming_type,',
129 'mail_options.mail_address_option, usr_data.email, usr_data.second_email',
130 'FROM mail_options',
131 'INNER JOIN usr_data ON mail_options.user_id = usr_data.usr_id',
132 'WHERE mail_options.user_id = %s',
133 ]);
134 $res = $this->db->queryF(
135 $query,
136 ['integer'],
137 [$this->usrId]
138 );
139 $row = $this->db->fetchObject($res);
140 if ($row === null) {
141 $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress, false);
142 return;
143 }
144
145 $this->firstEmailAddress = (string) $row->email;
146 $this->secondEmailAddress = (string) $row->second_email;
147 if ($this->mayManageInvididualSettings()) {
148 $this->signature = (string) $row->signature;
149 $this->linebreak = (int) $row->linebreak;
150 $this->isCronJobNotificationEnabled = (bool) $row->cronjob_notification;
151 }
152
154 $this->incomingType = (int) $row->incoming_type;
155 $this->emailAddressMode = (int) $row->mail_address_option;
156
157 if (false === filter_var(
158 $this->incomingType,
159 FILTER_VALIDATE_INT,
160 ['options' => ['min_range' => self::INCOMING_LOCAL, 'max_range' => self::INCOMING_BOTH]]
161 )) {
162 $this->incomingType = self::INCOMING_LOCAL;
163 }
164
165 if (false === filter_var(
166 $this->emailAddressMode,
167 FILTER_VALIDATE_INT,
168 ['options' => ['min_range' => self::FIRST_EMAIL, 'max_range' => self::BOTH_EMAIL]]
169 )) {
170 $this->emailAddressMode = self::FIRST_EMAIL;
171 }
172 }
173
174 $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress);
175 }
176
177 public function updateOptions(): int
178 {
179 $data = [
180 'signature' => ['text', $this->getSignature()],
181 'linebreak' => ['integer', $this->getLinebreak()],
182 'incoming_type' => ['integer', $this->getIncomingType()],
183 'mail_address_option' => ['integer', $this->getEmailAddressMode()],
184 ];
185
186 if ($this->settings->get('mail_notification', '0')) {
187 $data['cronjob_notification'] = ['integer', (int) $this->isCronJobNotificationEnabled()];
188 } else {
189 $data['cronjob_notification'] = ['integer', $this->lookupNotificationSetting($this->usrId)];
190 }
191
192 return $this->db->replace(
193 $this->table_mail_options,
194 [
195 'user_id' => ['integer', $this->usrId],
196 ],
197 $data
198 );
199 }
200
201 public function getLinebreak(): int
202 {
203 return $this->linebreak;
204 }
205
206 public function getSignature(): string
207 {
208 return $this->signature;
209 }
210
211 public function getIncomingType(): int
212 {
213 return $this->incomingType;
214 }
215
216 public function setLinebreak(int $linebreak): void
217 {
218 $this->linebreak = $linebreak;
219 }
220
221 public function setSignature(string $signature): void
222 {
223 $this->signature = $signature;
224 }
225
226 public function setIncomingType(int $incomingType): void
227 {
228 $this->incomingType = $incomingType;
229 }
230
232 {
234 }
235
236 public function isCronJobNotificationEnabled(): bool
237 {
239 }
240
241 public function getEmailAddressMode(): int
242 {
244 }
245
246 public function setEmailAddressMode(int $emailAddressMode): void
247 {
248 $this->emailAddressMode = $emailAddressMode;
249 }
250
251 private static function lookupNotificationSetting(int $usrId): int
252 {
253 global $DIC;
254
255 $row = $DIC->database()->fetchAssoc($DIC->database()->queryF(
256 'SELECT cronjob_notification FROM mail_options WHERE user_id = %s',
257 ['integer'],
258 [$usrId]
259 ));
260
261 return (int) $row['cronjob_notification'];
262 }
263
267 public function getExternalEmailAddresses(): array
268 {
269 $emailAddresses = [];
270
271 switch ($this->getEmailAddressMode()) {
273 if ($this->secondEmailAddress !== '') {
274 $emailAddresses[] = $this->secondEmailAddress;
275 } elseif ($this->firstEmailAddress !== '') {
276 // fallback, use first email address
277 $emailAddresses[] = $this->firstEmailAddress;
278 }
279 break;
280
281 case self::BOTH_EMAIL:
282 if ($this->firstEmailAddress !== '') {
283 $emailAddresses[] = $this->firstEmailAddress;
284 }
285 if ($this->secondEmailAddress !== '') {
286 $emailAddresses[] = $this->secondEmailAddress;
287 }
288 break;
289
291 default:
292 if ($this->firstEmailAddress !== '') {
293 $emailAddresses[] = $this->firstEmailAddress;
294 } elseif ($this->secondEmailAddress !== '') {
295 // fallback, use first email address
296 $emailAddresses[] = $this->secondEmailAddress;
297 }
298 break;
299 }
300
301 return $emailAddresses;
302 }
303}
Class ilMailOptions this class handles user mails.
ilMailTransportSettings $mailTransportSettings
setSignature(string $signature)
bool $isCronJobNotificationEnabled
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()
setEmailAddressMode(int $emailAddressMode)
ilDBInterface $db
static lookupNotificationSetting(int $usrId)
setLinebreak(int $linebreak)
setIsCronJobNotificationStatus(bool $isCronJobNotificationEnabled)
__construct(int $usrId, ilMailTransportSettings $mailTransportSettings=null, ilSetting $settings=null, ilDBInterface $db=null)
setIncomingType(int $incomingType)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
Class ChatMainBarProvider \MainMenu\Provider.
$query