ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
20  const DEFAULT_LINE_BREAK = 60;
21 
25  protected $ilias;
26 
30  protected $db;
31 
35  protected $user_id;
36 
40  protected $settings;
41 
45  protected $table_mail_options = 'mail_options';
46 
50  protected $linebreak;
51 
55  protected $signature;
56 
61 
65  protected $incoming_type = self::INCOMING_LOCAL;
66 
70  protected $mail_address_option = self::FIRST_EMAIL;
71 
72 
77 
82  public function __construct($a_user_id, ilMailTransportSettings $mailTransportSettings = null)
83  {
84  global $DIC;
85 
86  $this->user_id = $a_user_id;
87 
88  $this->db = $DIC->database();
89  $this->settings = $DIC->settings();
90 
91  if ($mailTransportSettings === null) {
93  }
94  $this->mailTransportSettings = $mailTransportSettings;
95 
96  $this->read();
97  }
98 
103  public function createMailOptionsEntry()
104  {
105  $incomingMail = strlen($this->settings->get('mail_incoming_mail')) ? (int) $this->settings->get('mail_incoming_mail') : self::INCOMING_LOCAL;
106  $mail_address_option = strlen($this->settings->get('mail_address_option')) ? (int) $this->settings->get('mail_address_option') : self::FIRST_EMAIL;
107 
108  $this->db->replace(
109  $this->table_mail_options,
110  array(
111  'user_id' => array('integer', $this->user_id),
112  ),
113  array(
114  'linebreak' => array('integer', (int) self::DEFAULT_LINE_BREAK),
115  'signature' => array('text', null),
116  'incoming_type' => array('integer', $incomingMail),
117  'mail_address_option' => array('integer', $mail_address_option),
118  'cronjob_notification' => array('integer', 0)
119  )
120  );
121  }
122 
123  protected function read()
124  {
125  $res = $this->db->queryF(
126  'SELECT mail_options.cronjob_notification,
127  mail_options.signature,
128  mail_options.linebreak,
129  mail_options.incoming_type,
130  mail_options.mail_address_option,
131  usr_data.email,
132  usr_data.second_email
133  FROM mail_options
134  LEFT JOIN usr_data ON mail_options.user_id = usr_data.usr_id
135  WHERE mail_options.user_id = %s',
136  array('integer'),
137  array($this->user_id)
138  );
140 
141  $this->cronjob_notification = $row->cronjob_notification;
142  $this->signature = $row->signature;
143  $this->linebreak = $row->linebreak;
144  $this->incoming_type = $row->incoming_type;
145  $this->mail_address_option = (int) $row->mail_address_option >= 3 ? $row->mail_address_option : self::FIRST_EMAIL;
146 
147  $firstMailAddress = $row->email;
148 
149  $secondMailAddress = $row->second_email;
150 
151  $this->mailTransportSettings->adjust($firstMailAddress, $secondMailAddress);
152  }
153 
156  public function updateOptions()
157  {
158  $data = array(
159  'signature' => array('text', $this->getSignature()),
160  'linebreak' => array('integer', (int) $this->getLinebreak()),
161  'incoming_type' => array('integer', $this->getIncomingType()),
162  'mail_address_option' => array('integer', $this->getMailAddressOption())
163  );
164 
165  if ($this->settings->get('mail_notification')) {
166  $data['cronjob_notification'] = array('integer', (int) $this->getCronjobNotification());
167  } else {
168  $data['cronjob_notification'] = array('integer', (int) self::lookupNotificationSetting($this->user_id));
169  }
170 
171  return $this->db->replace(
172  $this->table_mail_options,
173  array(
174  'user_id' => array('integer', $this->user_id)
175  ),
176  $data
177  );
178  }
179 
183  public function getLinebreak()
184  {
185  return $this->linebreak;
186  }
187 
191  public function getSignature()
192  {
193  return $this->signature;
194  }
195 
199  public function getIncomingType()
200  {
201  return $this->incoming_type;
202  }
203 
207  public function setLinebreak($linebreak)
208  {
209  $this->linebreak = $linebreak;
210  }
211 
215  public function setSignature($signature)
216  {
217  $this->signature = $signature;
218  }
219 
224  {
225  $this->incoming_type = $incoming_type;
226  }
227 
232  {
233  $this->cronjob_notification = $cronjob_notification;
234  }
235 
239  public function getCronjobNotification()
240  {
242  }
243 
247  public function getMailAddressOption()
248  {
250  }
251 
256  {
257  $this->mail_address_option = $mail_address_option;
258  }
259 
264  protected static function lookupNotificationSetting($usr_id)
265  {
266  global $DIC;
267 
268  $query = "SELECT cronjob_notification FROM mail_options WHERE user_id = " . $DIC->database()->quote($usr_id, 'integer');
269  $row = $DIC->database()->fetchAssoc($DIC->database()->query($query));
270  return (int) $row['cronjob_notification'];
271  }
272 
278  protected static function lookupExternalEmails(ilObjUser $user, ilMailOptions $mail_options)
279  {
280  $emailAddresses = array();
281 
282  switch ($mail_options->getMailAddressOption()) {
283  case self::SECOND_EMAIL:
284  if (strlen($user->getSecondEmail())) {
285  $emailAddresses[] = $user->getSecondEmail();
286  } elseif (strlen($user->getEmail())) {
287  // fallback, use first email address
288  $emailAddresses[] = $user->getEmail();
289  }
290  break;
291 
292  case self::BOTH_EMAIL:
293  if (strlen($user->getEmail())) {
294  $emailAddresses[] = $user->getEmail();
295  }
296  if (strlen($user->getSecondEmail())) {
297  $emailAddresses[] = $user->getSecondEmail();
298  }
299  break;
300 
301  case self::FIRST_EMAIL:
302  default:
303  if (strlen($user->getEmail())) {
304  $emailAddresses[] = $user->getEmail();
305  } elseif (strlen($user->getSecondEmail())) {
306  // fallback, use first email address
307  $emailAddresses[] = $user->getSecondEmail();
308  }
309  break;
310  }
311 
312  return $emailAddresses;
313  }
314 
320  public static function getExternalEmailsByUser(ilObjUser $user, ilMailOptions $mail_options = null)
321  {
322  if (!($mail_options instanceof ilMailOptions)) {
323  $mail_options = new self($user->getId());
324  }
325 
326  return self::lookupExternalEmails($user, $mail_options);
327  }
328 
334  public static function getExternalEmailsByUserId($user_id, ilMailOptions $mail_options = null)
335  {
336  return self::getExternalEmailsByUser(new ilObjUser($user_id), $mail_options);
337  }
338 }
Class ilMailOptions this class handles user mails.
setMailAddressOption($mail_address_option)
setCronjobNotification($cronjob_notification)
__construct($a_user_id, ilMailTransportSettings $mailTransportSettings=null)
global $DIC
Definition: saml.php:7
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()...
setSignature($signature)
static getExternalEmailsByUser(ilObjUser $user, ilMailOptions $mail_options=null)
static lookupNotificationSetting($usr_id)
getEmail()
get email address public
foreach($_POST as $key=> $value) $res
getId()
get object id public
setLinebreak($linebreak)
$query
Create styles array
The data for the language used.
setIncomingType($incoming_type)
settings()
Definition: settings.php:2
static getExternalEmailsByUserId($user_id, ilMailOptions $mail_options=null)
static lookupExternalEmails(ilObjUser $user, ilMailOptions $mail_options)