ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailNotification.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 include_once './Services/Language/classes/class.ilLanguageFactory.php';
6 include_once './Services/Mail/classes/class.ilMail.php';
7 
16 abstract class ilMailNotification
17 {
19 
20  protected $type = null;
21  protected $sender = null;
22 
23  protected $mail = null;
24  protected $subject = '';
25  protected $body = '';
26 
27  protected $attachments = array();
28 
29  protected $language = null;
30  protected $lang_modules = array();
31 
32  protected $recipients = array();
33 
34  protected $ref_id = null;
35  protected $obj_id = null;
36  protected $obj_type = null;
37 
38  protected $additional_info = array();
39 
40  protected $is_in_wsp;
41  protected $wsp_tree;
43 
47  public function __construct($a_is_personal_workspace = false)
48  {
49  global $DIC;
50 
51  $this->is_in_wsp = (bool) $a_is_personal_workspace;
52 
53  $this->setSender(ANONYMOUS_USER_ID);
54  $this->language = ilLanguageFactory::_getLanguage($DIC->language()->getDefaultLanguage());
55 
56  if ($this->is_in_wsp) {
57  include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php";
58  include_once "Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php";
59  $this->wsp_tree = new ilWorkspaceTree($DIC->user()->getId()); // owner of tree is irrelevant
60  $this->wsp_access_handler = new ilWorkspaceAccessHandler($this->wsp_tree);
61  }
62  }
63 
68  public function setType($a_type)
69  {
70  $this->type = $a_type;
71  }
72 
77  public function getType()
78  {
79  return $this->type;
80  }
81 
86  public function setSender($a_usr_id)
87  {
88  $this->sender = $a_usr_id;
89  }
90 
95  public function getSender()
96  {
97  return $this->sender;
98  }
99 
104  protected function setSubject($a_subject)
105  {
106  return $this->subject = $a_subject;
107  }
108 
112  protected function getSubject()
113  {
114  return $this->subject;
115  }
116 
120  protected function setBody($a_body)
121  {
122  $this->body = $a_body;
123  }
124 
130  protected function appendBody($a_body)
131  {
132  return $this->body .= $a_body;
133  }
134 
138  protected function getBody()
139  {
140  return $this->body;
141  }
142 
146  public function setRecipients(array $a_rcp)
147  {
148  $this->recipients = $a_rcp;
149  }
150 
155  public function getRecipients()
156  {
157  return $this->recipients;
158  }
159 
164  public function setAttachments($a_att)
165  {
166  $this->attachments = $a_att;
167  }
168 
173  public function getAttachments()
174  {
175  return (array) $this->attachments;
176  }
177 
182  public function setLangModules(array $a_modules)
183  {
184  $this->lang_modules = $a_modules;
185  }
186 
191  protected function initLanguage($a_usr_id)
192  {
193  $this->language = $this->getUserLanguage($a_usr_id);
194  }
195 
202  public function getUserLanguage($a_usr_id)
203  {
205  $language->loadLanguageModule('mail');
206 
207  if (sizeof($this->lang_modules)) {
208  foreach ($this->lang_modules as $lmod) {
209  $language->loadLanguageModule($lmod);
210  }
211  }
212 
213  return $language;
214  }
215 
220  protected function initLanguageByIso2Code($a_code = '')
221  {
222  $this->language = ilLanguageFactory::_getLanguage($a_code);
223  $this->language->loadLanguageModule('mail');
224 
225  if (sizeof($this->lang_modules)) {
226  foreach ($this->lang_modules as $lmod) {
227  $this->language->loadLanguageModule($lmod);
228  }
229  }
230  }
231 
235  protected function setLanguage($a_language)
236  {
237  $this->language = $a_language;
238  }
239 
243  protected function getLanguage()
244  {
245  return $this->language;
246  }
247 
252  protected function getLanguageText($a_keyword)
253  {
254  return str_replace('\n', "\n", $this->getLanguage()->txt($a_keyword));
255  }
256 
260  public function setRefId($a_id)
261  {
262  if (!$this->is_in_wsp) {
263  $this->ref_id = $a_id;
264  $obj_id = ilObject::_lookupObjId($this->ref_id);
265  } else {
266  $this->ref_id = (int) $a_id;
267  $obj_id = $this->wsp_tree->lookupObjectId($this->getRefId());
268  }
269 
270  $this->setObjId($obj_id);
271  }
272 
276  public function getRefId()
277  {
278  return $this->ref_id;
279  }
280 
284  public function getObjId()
285  {
286  return $this->obj_id;
287  }
288 
292  public function setObjId($a_obj_id)
293  {
294  $this->obj_id = $a_obj_id;
295  $this->obj_type = ilObject::_lookupType($this->obj_id);
296  }
297 
302  public function getObjType()
303  {
304  return $this->obj_type;
305  }
306 
311  public function setAdditionalInformation(array $a_info)
312  {
313  $this->additional_info = $a_info;
314  }
315 
319  public function getAdditionalInformation()
320  {
321  return (array) $this->additional_info;
322  }
323 
328  protected function getObjectTitle($a_shorten = false)
329  {
330  if (!$this->getObjId()) {
331  return '';
332  }
334  if ((bool) $a_shorten) {
335  $txt = ilUtil::shortenText($txt, self::SUBJECT_TITLE_LENGTH, true);
336  }
337  return $txt;
338  }
339 
345  public function sendMail(array $a_rcp, $a_type, $a_parse_recipients = true)
346  {
347  $recipients = array();
348  foreach ($a_rcp as $rcp) {
349  if ($a_parse_recipients) {
351  } else {
352  $recipients[] = $rcp;
353  }
354  }
355  $recipients = implode(',', $recipients);
356  $errors = $this->getMail()->sendMail(
357  $recipients,
358  '',
359  '',
360  $this->getSubject(),
361  $this->getBody(),
362  $this->getAttachments(),
363  $a_type
364  );
365  // smeyer: 19.5.16 fixed strlen warning, since $error is of type array
366  if (count($errors) > 0) {
367  require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
369  //ilLoggerFactory::getLogger('mail')->error($error);
370  }
371  }
372 
376  protected function initMail()
377  {
378  return $this->mail = new ilMail($this->getSender());
379  }
380 
384  protected function getMail()
385  {
386  return is_object($this->mail) ? $this->mail : $this->initMail();
387  }
388 
394  protected function createPermanentLink($a_params = array(), $a_append = '')
395  {
396  include_once './Services/Link/classes/class.ilLink.php';
397 
398  if ($this->getRefId()) {
399  if (!$this->is_in_wsp) {
400  return ilLink::_getLink($this->ref_id, $this->getObjType(), $a_params, $a_append);
401  } else {
402  return ilWorkspaceAccessHandler::getGotoLink($this->getRefId(), $this->getObjId(), $a_append);
403  }
404  } else {
405  // Return root
406  return ilLink::_getLink(ROOT_FOLDER_ID, 'root');
407  }
408  }
409 
414  protected function userToString($a_usr_id)
415  {
416  $name = ilObjUser::_lookupName($a_usr_id);
417  return ($name['title'] ? $name['title'] . ' ' : '') .
418  ($name['firstname'] ? $name['firstname'] . ' ' : '') .
419  ($name['lastname'] ? $name['lastname'] . ' ' : '');
420  }
421 
430  protected function isRefIdAccessible($a_user_id, $a_ref_id, $a_permission = "read")
431  {
432  global $DIC;
433 
434  // no given permission == accessible
435 
436  if (!$this->is_in_wsp) {
437  if (trim($a_permission) &&
438  !$DIC->access()->checkAccessOfUser($a_user_id, $a_permission, "", $a_ref_id, $this->getObjType())) {
439  return false;
440  }
441  } else {
442  if (trim($a_permission) &&
443  !$this->wsp_access_handler->checkAccessOfUser($this->wsp_tree, $a_user_id, $a_permission, "", $a_ref_id, $this->getObjType())) {
444  return false;
445  }
446  }
447  return true;
448  }
449 
454  public function getBlockBorder()
455  {
456  return "----------------------------------------\n";
457  }
458 }
static _lookupLogin($a_user_id)
lookup login
static _lookupName($a_user_id)
lookup user name
getType()
Get notification type.
static shortenText( $a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
global $DIC
Definition: saml.php:7
initLanguageByIso2Code($a_code='')
Init language by ISO2 code.
setSender($a_usr_id)
Set sender of mail.
initLanguage($a_usr_id)
Init language.
setAttachments($a_att)
Set attachments.
isRefIdAccessible($a_user_id, $a_ref_id, $a_permission="read")
Check if ref id is accessible for user.
Access handler for personal workspace.
static _lookupTitle($a_id)
lookup object title
mail($to, $subject, $message, $additional_headers=null, $additional_parameters=null)
Tree handler for personal workspace.
$a_type
Definition: workflow.php:92
__construct($a_is_personal_workspace=false)
static _getLanguage($a_lang_key='')
Get langauge object.
Base class for course/group mail notifications.
setType($a_type)
Set notification type.
static getGotoLink($a_node_id, $a_obj_id, $a_additional=null)
setLangModules(array $a_modules)
Set lang modules.
static _lookupObjId($a_id)
getObjectTitle($a_shorten=false)
getAttachments()
Get attachments.
getObjType()
Get object type.
$txt
Definition: error.php:11
static _lookupType($a_id, $a_reference=false)
lookup object type
sendMail(array $a_rcp, $a_type, $a_parse_recipients=true)
static _getLanguageOfUser($a_usr_id)
Get language object of user.
getRecipients()
get array of recipients
getBlockBorder()
Get (ascii) block border.
$errors
Definition: index.php:6
createPermanentLink($a_params=array(), $a_append='')
getSender()
get sender of mail
appendBody($a_body)
Append body text.
static getLogger($a_component_id)
Get component logger.
getUserLanguage($a_usr_id)
Get user language.
setAdditionalInformation(array $a_info)
Additional information for creating notification mails.