ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailOptions.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11{
12 const INCOMING_LOCAL = 0;
13 const INCOMING_EMAIL = 1;
14 const INCOMING_BOTH = 2;
15
16 const FIRST_EMAIL = 3;
17 const SECOND_EMAIL = 4;
18 const BOTH_EMAIL = 5;
19
21
23 protected $ilias;
25 protected $db;
27 protected $user_id;
29 protected $settings;
31 protected $table_mail_options = 'mail_options';
35 protected $signature;
37 protected $cronjob_notification = 0;
44
50 {
51 global $DIC;
52
53 $this->user_id = $a_user_id;
54
55 $this->db = $DIC->database();
56 $this->settings = $DIC->settings();
57
58 if ($mailTransportSettings === null) {
60 }
61 $this->mailTransportSettings = $mailTransportSettings;
62
63 $this->read();
64 }
65
70 public function createMailOptionsEntry()
71 {
72 $this->incoming_type = self::INCOMING_LOCAL;
73 if (strlen($this->settings->get('mail_incoming_mail', '')) > 0) {
74 $this->incoming_type = (int) $this->settings->get('mail_incoming_mail');
75 }
76
77 $this->mail_address_option = self::FIRST_EMAIL;
78 if (strlen($this->settings->get('mail_address_option', '')) > 0) {
79 $this->mail_address_option = (int) $this->settings->get('mail_address_option');
80 }
81
82 $this->linebreak = self::DEFAULT_LINE_BREAK;
83 $this->cronjob_notification = 0;
84 $this->signature = null;
85
86 $this->db->replace(
87 $this->table_mail_options,
88 [
89 'user_id' => ['integer', $this->user_id],
90 ],
91 [
92 'linebreak' => ['integer', $this->linebreak],
93 'signature' => ['text', $this->signature],
94 'incoming_type' => ['integer', $this->incoming_type],
95 'mail_address_option' => ['integer', $this->mail_address_option],
96 'cronjob_notification' => ['integer', $this->cronjob_notification]
97 ]
98 );
99 }
100
101 protected function read()
102 {
103 $res = $this->db->queryF(
104 'SELECT mail_options.cronjob_notification,
105 mail_options.signature,
106 mail_options.linebreak,
107 mail_options.incoming_type,
108 mail_options.mail_address_option,
109 usr_data.email,
110 usr_data.second_email
111 FROM mail_options
112 LEFT JOIN usr_data ON mail_options.user_id = usr_data.usr_id
113 WHERE mail_options.user_id = %s',
114 array('integer'),
115 array($this->user_id)
116 );
117 $row = $this->db->fetchObject($res);
118
119 if ($row !== null) {
120 $this->cronjob_notification = (int) $row->cronjob_notification;
121 $this->signature = $row->signature;
122 $this->linebreak = (int) $row->linebreak;
123 $this->incoming_type = (int) $row->incoming_type;
124 $this->mail_address_option = (int) $row->mail_address_option;
125
126 if (
127 filter_var(
128 $this->incoming_type,
129 FILTER_VALIDATE_INT,
130 ['options' => ['min_range' => self::INCOMING_LOCAL, 'max_range' => self::INCOMING_BOTH]]
131 ) === false
132 ) {
133 $this->incoming_type = self::INCOMING_LOCAL;
134 }
135
136 if (
137 filter_var(
138 $this->mail_address_option,
139 FILTER_VALIDATE_INT,
140 ['options' => ['min_range' => self::FIRST_EMAIL, 'max_range' => self::BOTH_EMAIL]]
141 ) === false
142 ) {
143 $this->mail_address_option = self::FIRST_EMAIL;
144 }
145
146 $firstMailAddress = (string) $row->email;
147 $secondMailAddress = (string) $row->second_email;
148
149 $this->mailTransportSettings->adjust($firstMailAddress, $secondMailAddress);
150 }
151 }
152
153 public function updateOptions()
154 {
155 $data = [
156 'signature' => ['text', $this->getSignature()],
157 'linebreak' => ['integer', (int) $this->getLinebreak()],
158 'incoming_type' => ['integer', $this->getIncomingType()],
159 'mail_address_option' => ['integer', $this->getMailAddressOption()]
160 ];
161
162 if ($this->settings->get('mail_notification')) {
163 $data['cronjob_notification'] = ['integer', (int) $this->getCronjobNotification()];
164 } else {
165 $data['cronjob_notification'] = ['integer', (int) self::lookupNotificationSetting($this->user_id)];
166 }
167
168 return $this->db->replace(
169 $this->table_mail_options,
170 [
171 'user_id' => ['integer', $this->user_id]
172 ],
173 $data
174 );
175 }
176
180 public function getLinebreak()
181 {
182 return $this->linebreak;
183 }
184
188 public function getSignature()
189 {
190 return $this->signature;
191 }
192
196 public function getIncomingType()
197 {
199 }
200
204 public function setLinebreak($linebreak)
205 {
206 $this->linebreak = $linebreak;
207 }
208
212 public function setSignature($signature)
213 {
214 $this->signature = $signature;
215 }
216
221 {
222 $this->incoming_type = $incoming_type;
223 }
224
229 {
230 $this->cronjob_notification = $cronjob_notification;
231 }
232
236 public function getCronjobNotification()
237 {
239 }
240
244 public function getMailAddressOption()
245 {
247 }
248
253 {
254 $this->mail_address_option = $mail_address_option;
255 }
256
261 protected static function lookupNotificationSetting($usr_id)
262 {
263 global $DIC;
264
265 $query = "SELECT cronjob_notification FROM mail_options WHERE user_id = " . $DIC->database()->quote($usr_id, 'integer');
266 $row = $DIC->database()->fetchAssoc($DIC->database()->query($query));
267 return (int) $row['cronjob_notification'];
268 }
269
275 protected static function lookupExternalEmails(ilObjUser $user, ilMailOptions $mail_options)
276 {
277 $emailAddresses = array();
278
279 switch ($mail_options->getMailAddressOption()) {
281 if (strlen($user->getSecondEmail())) {
282 $emailAddresses[] = $user->getSecondEmail();
283 } elseif (strlen($user->getEmail())) {
284 // fallback, use first email address
285 $emailAddresses[] = $user->getEmail();
286 }
287 break;
288
289 case self::BOTH_EMAIL:
290 if (strlen($user->getEmail())) {
291 $emailAddresses[] = $user->getEmail();
292 }
293 if (strlen($user->getSecondEmail())) {
294 $emailAddresses[] = $user->getSecondEmail();
295 }
296 break;
297
299 default:
300 if (strlen($user->getEmail())) {
301 $emailAddresses[] = $user->getEmail();
302 } elseif (strlen($user->getSecondEmail())) {
303 // fallback, use first email address
304 $emailAddresses[] = $user->getSecondEmail();
305 }
306 break;
307 }
308
309 return $emailAddresses;
310 }
311
317 public static function getExternalEmailsByUser(ilObjUser $user, ilMailOptions $mail_options = null)
318 {
319 if (!($mail_options instanceof ilMailOptions)) {
320 $mail_options = new self($user->getId());
321 }
322
323 return self::lookupExternalEmails($user, $mail_options);
324 }
325
331 public static function getExternalEmailsByUserId($user_id, ilMailOptions $mail_options = null)
332 {
333 return self::getExternalEmailsByUser(new ilObjUser($user_id), $mail_options);
334 }
335}
An exception for terminatinating execution or to throw for unit testing.
Class ilMailOptions this class handles user mails.
static lookupExternalEmails(ilObjUser $user, ilMailOptions $mail_options)
__construct($a_user_id, ilMailTransportSettings $mailTransportSettings=null)
static getExternalEmailsByUser(ilObjUser $user, ilMailOptions $mail_options=null)
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()
setIncomingType($incoming_type)
setSignature($signature)
static getExternalEmailsByUserId($user_id, ilMailOptions $mail_options=null)
setLinebreak($linebreak)
setCronjobNotification($cronjob_notification)
setMailAddressOption($mail_address_option)
static lookupNotificationSetting($usr_id)
$user
Definition: migrateto20.php:57
$row
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
$data
Definition: bench.php:6