ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailbox.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 class ilMailbox
14 {
18  protected $ilias;
19 
23  protected $lng;
24 
28  protected $db;
29 
33  protected $mtree;
34 
39  protected $user_id;
40 
45  protected $actions = array();
46 
51  protected $default_folder = array();
52 
58 
63  protected $table_tree;
64 
69  public function __construct($a_user_id = 0)
70  {
71  global $DIC;
72 
73  $this->ilias = $DIC['ilias'];
74  $this->lng = $DIC->language();
75  $this->db = $DIC->database();
76 
77  $this->user_id = $a_user_id;
78 
79  $this->table_mail_obj_data = 'mail_obj_data';
80  $this->table_tree = 'mail_tree';
81 
82  if ($a_user_id) {
83  $this->mtree = new ilTree($this->user_id);
84  $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
85  }
86 
87  // i added this, becaus if i create a new user automatically during
88  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
89  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
90  if (is_object($this->lng)) {
91  $this->lng->loadLanguageModule("mail");
92 
93  $this->actions = array(
94  "moveMails" => $this->lng->txt("mail_move_to"),
95  "markMailsRead" => $this->lng->txt("mail_mark_read"),
96  "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
97  "deleteMails" => $this->lng->txt("delete"));
98  }
99 
100  // array contains basic folders and there lng translation for every new user
101  $this->default_folder = array(
102  "b_inbox" => "inbox",
103  "c_trash" => "trash",
104  "d_drafts" => "drafts",
105  "e_sent" => "sent",
106  "z_local" => "local");
107  }
108 
112  public function getInboxFolder()
113  {
114  $res = $this->db->queryF(
115  '
116  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
117  WHERE user_id = %s
118  AND m_type = %s',
119  array('integer', 'text'),
120  array($this->user_id, 'inbox')
121  );
122 
123  $row = $this->db->fetchAssoc($res);
124 
125  return $row['obj_id'];
126  }
127 
131  public function getDraftsFolder()
132  {
133  $res = $this->db->queryF(
134  '
135  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
136  WHERE user_id = %s
137  AND m_type = %s',
138  array('integer', 'text'),
139  array($this->user_id, 'drafts')
140  );
141 
142  $row = $this->db->fetchAssoc($res);
143 
144  return $row['obj_id'];
145  }
146 
150  public function getTrashFolder()
151  {
152  $res = $this->db->queryf(
153  '
154  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
155  WHERE user_id = %s
156  AND m_type = %s',
157  array('integer', 'text'),
158  array($this->user_id, 'trash')
159  );
160 
161  $row = $this->db->fetchAssoc($res);
162 
163  return $row['obj_id'];
164  }
165 
169  public function getSentFolder()
170  {
171  $res = $this->db->queryf(
172  '
173  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
174  WHERE user_id = %s
175  AND m_type = %s',
176  array('integer', 'text'),
177  array($this->user_id, 'sent')
178  );
179 
180  $row = $this->db->fetchAssoc($res);
181 
182  return $row['obj_id'];
183  }
184 
188  public function getRootFolderId()
189  {
190  return $this->mtree->getRootID($this->user_id);
191  }
192 
200  public function getActions($a_mobj_id)
201  {
202  if ($a_mobj_id) {
203  $folder_data = $this->getFolderData($a_mobj_id);
204 
205  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local") {
206  return $this->actions;
207  }
208  }
209 
210  return $this->actions;
211  }
212 
216  public function createDefaultFolder()
217  {
218  $root_id = $this->db->nextId($this->table_mail_obj_data);
219  $this->db->manipulateF(
220  '
221  INSERT INTO ' . $this->table_mail_obj_data . '
222  ( obj_id,
223  user_id,
224  title,
225  m_type
226  )
227  VALUES(%s, %s, %s, %s)',
228  array('integer','integer', 'text', 'text'),
229  array($root_id, $this->user_id, 'a_root', 'root')
230  );
231  $this->mtree->addTree($this->user_id, $root_id);
232 
233  foreach ($this->default_folder as $key => $folder) {
234  $last_id = $this->db->nextId($this->table_mail_obj_data);
235  $this->db->manipulateF(
236  '
237  INSERT INTO ' . $this->table_mail_obj_data . '
238  ( obj_id,
239  user_id,
240  title,
241  m_type
242  )
243  VALUES(%s, %s, %s, %s)',
244  array('integer','integer', 'text', 'text'),
245  array($last_id, $this->user_id, $key, $folder)
246  );
247  $this->mtree->insertNode($last_id, $root_id);
248  }
249  }
250 
257  public function addFolder($a_parent_id, $a_folder_name)
258  {
259  if ($this->folderNameExists($a_folder_name)) {
260  return 0;
261  }
262 
263  $next_id = $this->db->nextId($this->table_mail_obj_data);
264  $this->db->manipulateF(
265  '
266  INSERT INTO ' . $this->table_mail_obj_data . '
267  ( obj_id,
268  user_id,
269  title,
270  m_type
271  )
272  VALUES(%s,%s,%s,%s)',
273  array('integer','integer', 'text', 'text'),
274  array($next_id, $this->user_id, $a_folder_name, 'user_folder')
275  );
276  $this->mtree->insertNode($next_id, $a_parent_id);
277 
278  return $next_id;
279  }
280 
287  public function renameFolder($a_obj_id, $a_new_folder_name)
288  {
289  if ($this->folderNameExists($a_new_folder_name)) {
290  return false;
291  }
292 
293  $this->db->manipulateF(
294  '
295  UPDATE ' . $this->table_mail_obj_data . '
296  SET title = %s
297  WHERE obj_id = %s AND user_id = %s',
298  array('text', 'integer', 'integer'),
299  array($a_new_folder_name, $a_obj_id, $this->user_id)
300  );
301 
302  return true;
303  }
304 
310  protected function folderNameExists($a_folder_name)
311  {
312  $res = $this->db->queryF(
313  '
314  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
315  WHERE user_id = %s
316  AND title = %s',
317  array('integer', 'text'),
318  array($this->user_id, $a_folder_name)
319  );
320  $row = $this->db->fetchAssoc($res);
321 
322  return is_array($row) && $row['obj_id'] > 0 ? true : false;
323  }
324 
330  public function deleteFolder($a_folder_id)
331  {
332  $query = $this->db->queryf(
333  '
334  SELECT obj_id, title FROM mail_obj_data
335  WHERE obj_id = %s AND user_id = %s',
336  array('integer', 'integer'),
337  array($a_folder_id, $this->user_id)
338  );
339  $row = $this->db->fetchAssoc($query);
340 
341  if (!is_array($row) || array_key_exists($row['title'], $this->default_folder)) {
342  return false;
343  }
344 
345  require_once 'Services/Mail/classes/class.ilMail.php';
346  $umail = new ilMail($this->user_id);
347 
348  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
349  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
350 
351  foreach ($subtree as $node) {
352  $mails = $umail->getMailsOfFolder($node["obj_id"]);
353  $mail_ids = array();
354  foreach ($mails as $mail) {
355  $mail_ids[] = $mail["mail_id"];
356  }
357 
358  $umail->deleteMails($mail_ids);
359 
360  $this->db->manipulateF(
361  '
362  DELETE FROM ' . $this->table_mail_obj_data . '
363  WHERE obj_id = %s AND user_id = %s',
364  array('integer', 'integer'),
365  array($node['obj_id'], $this->user_id)
366  );
367  }
368 
369  return true;
370  }
371 
377  public function getFolderData($a_obj_id)
378  {
379  $res = $this->db->queryF(
380  '
381  SELECT * FROM ' . $this->table_mail_obj_data . '
382  WHERE user_id = %s
383  AND obj_id = %s',
384  array('integer', 'integer'),
385  array($this->user_id, $a_obj_id)
386  );
387  $row = $this->db->fetchAssoc($res);
388 
389  return array(
390  'obj_id' => $row['obj_id'],
391  'title' => $row['title'],
392  'type' => $row['m_type']
393  );
394  }
395 
401  public function getParentFolderId($a_obj_id)
402  {
403  $res = $this->db->queryF(
404  '
405  SELECT * FROM ' . $this->table_tree . '
406  WHERE child = %s AND tree = %s',
407  array('integer', 'integer'),
408  array($a_obj_id, $this->user_id)
409  );
410  $row = $this->db->fetchAssoc($res);
411 
412  return is_array($row) ? $row['parent'] : 0;
413  }
414 
421  public function getSubFolders($a_folder = 0, $a_folder_parent = 0)
422  {
423  if (!$a_folder) {
424  $a_folder = $this->getRootFolderId();
425  }
426 
427  $user_folder = array();
428 
429  foreach ($this->default_folder as $key => $value) {
430  $res = $this->db->queryF(
431  '
432  SELECT obj_id, m_type
433  FROM ' . $this->table_mail_obj_data . '
434  WHERE user_id = %s
435  AND title = %s',
436  array('integer', 'text'),
437  array($this->user_id, $key)
438  );
439  $row = $this->db->fetchAssoc($res);
440 
441  $user_folder[] = array(
442  'title' => $key,
443  'type' => $row['m_type'],
444  'obj_id' => $row['obj_id']
445  );
446  }
447 
448  $res = $this->db->queryF(
449  '
450  SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data . '
451  WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child
452  AND ' . $this->table_tree . '.depth > %s
453  AND ' . $this->table_tree . '.tree = %s
454  ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title ',
455  array('integer', 'integer'),
456  array(2, $this->user_id)
457  );
458  while ($row = $this->db->fetchAssoc($res)) {
459  $user_folder[] = array(
460  'title' => $row['title'],
461  'type' => $row['m_type'],
462  'obj_id' => $row['child']
463  );
464  }
465 
466  return $user_folder;
467  }
468 
472  public function setUserId($a_user_id)
473  {
474  $this->user_id = $a_user_id;
475  }
476 
480  public function getUserId() : int
481  {
482  return (int) $this->user_id;
483  }
484 
491  public function delete()
492  {
493  $this->db->manipulateF(
494  '
495  DELETE FROM mail_obj_data WHERE user_id = %s',
496  array('integer'),
497  array($this->user_id)
498  );
499 
500  $this->db->manipulateF(
501  '
502  DELETE FROM mail_options WHERE user_id = %s',
503  array('integer'),
504  array($this->user_id)
505  );
506 
507  $this->db->manipulateF(
508  '
509  DELETE FROM mail_saved WHERE user_id = %s',
510  array('integer'),
511  array($this->user_id)
512  );
513 
514  $this->db->manipulateF(
515  '
516  DELETE FROM mail_tree WHERE tree = %s',
517  array('integer'),
518  array($this->user_id)
519  );
520 
521  // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
522  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
523  $fdm = new ilFileDataMail($this->user_id);
524  $fdm->onUserDelete();
525 
526  // Delete mails of deleted user
527  $this->db->manipulateF(
528  'DELETE FROM mail WHERE user_id = %s',
529  array('integer'),
530  array($this->user_id)
531  );
532 
533  return true;
534  }
535 
541  public function updateMailsOfDeletedUser($nameToShow)
542  {
543  $this->db->manipulateF(
544  '
545  UPDATE mail
546  SET sender_id = %s, import_name = %s
547  WHERE sender_id = %s',
548  array('integer', 'text', 'integer'),
549  array(0, $nameToShow, $this->user_id)
550  );
551  }
552 
557  public function isOwnedFolder(int $folderId) : bool
558  {
559  $folderData = $this->getFolderData($folderId);
560 
561  return $folderData['obj_id'] == $folderId;
562  }
563 }
updateMailsOfDeletedUser($nameToShow)
Update existing mails.
global $DIC
Definition: saml.php:7
Class ilFileDataMail.
getSentFolder()
get Id of the sent folder of an user
getRootFolderId()
get Id of the root folder of an user
addFolder($a_parent_id, $a_folder_name)
Adds a new mail folder with the passed name under the given parent folder.
folderNameExists($a_folder_name)
Checks whether or not the passed folder name exists in the context of the folder owner.
getFolderData($a_obj_id)
Fetches the data of a specific folder.
setUserId($a_user_id)
foreach($_POST as $key=> $value) $res
getTrashFolder()
get Id of the trash folder of an user
Mail Box class Base class for creating and handling mail boxes.
createDefaultFolder()
Creates all default folders for a user.
getParentFolderId($a_obj_id)
Get id of parent folder.
redirection script todo: (a better solution should control the processing via a xml file) ...
$query
renameFolder($a_obj_id, $a_new_folder_name)
Rename a folder and check if the name already exists.
getInboxFolder()
get Id of the inbox folder of an user
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
getSubFolders($a_folder=0, $a_folder_parent=0)
Get all folders under a given folder/node id.
$row
getActions($a_mobj_id)
get all possible actions if no mobj_id is given or folder specific actions if mobj_id is given ...
__construct($a_user_id=0)
ilMailbox constructor.
getDraftsFolder()
get Id of the inbox folder of an user
deleteFolder($a_folder_id)
isOwnedFolder(int $folderId)
$key
Definition: croninfo.php:18