ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
12 require_once("Services/Mail/classes/class.ilMail.php");
13 
14 class ilMailbox
15 {
21  var $ilias;
22 
28  var $lng;
29 
35  var $mtree;
36 
42  var $user_id;
43 
50  var $actions;
51 
58 
65 
72 
78  public function __construct($a_user_id = 0)
79  {
80  global $ilias,$lng;
81 
82  $this->ilias = $ilias;
83  $this->lng = $lng;
84  $this->user_id = $a_user_id;
85 
86  $this->table_mail_obj_data = 'mail_obj_data';
87  $this->table_tree = 'mail_tree';
88 
89  if ($a_user_id)
90  {
91  $this->mtree = new ilTree($this->user_id);
92  $this->mtree->setTableNames($this->table_tree,$this->table_mail_obj_data);
93  }
94 
95  // i added this, becaus if i create a new user automatically during
96  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
97  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
98  if (is_object($this->lng))
99  {
100  $this->lng->loadLanguageModule("mail");
101 
102  $this->actions = array(
103  "moveMails" => $this->lng->txt("mail_move_to"),
104  "markMailsRead" => $this->lng->txt("mail_mark_read"),
105  "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
106  "deleteMails" => $this->lng->txt("delete"));
107  }
108 
109  // array contains basic folders and there lng translation for every new user
110  $this->default_folder = array(
111  "b_inbox" => "inbox",
112  "c_trash" => "trash",
113  "d_drafts" => "drafts",
114  "e_sent" => "sent",
115  "z_local" => "local");
116 
117  }
122  public function getInboxFolder()
123  {
127  global $ilDB;
128 
129  $res = $ilDB->queryF('
130  SELECT obj_id FROM '.$this->table_mail_obj_data.'
131  WHERE user_id = %s
132  AND m_type = %s',
133  array('integer', 'text'),
134  array($this->user_id, 'inbox')
135  );
136 
137  $row = $ilDB->fetchAssoc($res);
138 
139  return $row['obj_id'];
140  }
141 
146  public function getDraftsFolder()
147  {
151  global $ilDB;
152 
153  $res = $ilDB->queryF('
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, 'drafts')
159  );
160 
161  $row = $ilDB->fetchAssoc($res);
162 
163  return $row['obj_id'];
164  }
165 
170  public function getTrashFolder()
171  {
175  global $ilDB;
176 
177  $res = $ilDB->queryf('
178  SELECT obj_id FROM '.$this->table_mail_obj_data.'
179  WHERE user_id = %s
180  AND m_type = %s',
181  array('integer', 'text'),
182  array($this->user_id, 'trash')
183  );
184 
185  $row = $ilDB->fetchAssoc($res);
186 
187  return $row['obj_id'];
188  }
189 
194  public function getSentFolder()
195  {
199  global $ilDB;
200 
201  $res = $ilDB->queryf('
202  SELECT obj_id FROM '.$this->table_mail_obj_data.'
203  WHERE user_id = %s
204  AND m_type = %s',
205  array('integer', 'text'),
206  array($this->user_id, 'sent')
207  );
208 
209  $row = $ilDB->fetchAssoc($res);
210 
211  return $row['obj_id'];
212  }
213 
218  function getRootFolderId()
219  {
220  return $this->mtree->getRootID($this->user_id);
221  }
222 
230  public function getActions($a_mobj_id)
231  {
232  if ($a_mobj_id)
233  {
234  $folder_data = $this->getFolderData($a_mobj_id);
235 
236  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
237  {
238  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
239  return $this->actions;
240  }
241  }
242 
243  return $this->actions;
244  }
245 
249  public function createDefaultFolder()
250  {
251  global $ilDB;
252 
253  $root_id = $ilDB->nextId($this->table_mail_obj_data);
254  $ilDB->manipulateF('
255  INSERT INTO '. $this->table_mail_obj_data .'
256  ( obj_id,
257  user_id,
258  title,
259  m_type
260  )
261  VALUES(%s, %s, %s, %s)',
262  array('integer','integer', 'text', 'text'),
263  array($root_id, $this->user_id, 'a_root', 'root')
264  );
265  $this->mtree->addTree($this->user_id, $root_id);
266 
267  foreach($this->default_folder as $key => $folder)
268  {
269  $last_id = $ilDB->nextId($this->table_mail_obj_data);
270  $ilDB->manipulateF('
271  INSERT INTO '. $this->table_mail_obj_data .'
272  ( obj_id,
273  user_id,
274  title,
275  m_type
276  )
277  VALUES(%s, %s, %s, %s)',
278  array('integer','integer', 'text', 'text'),
279  array($last_id, $this->user_id, $key, $folder)
280  );
281  $this->mtree->insertNode($last_id, $root_id);
282  }
283  }
284 
291  public function addFolder($a_parent_id, $a_folder_name)
292  {
293  global $ilDB;
294 
295  if($this->folderNameExists($a_folder_name))
296  {
297  return 0;
298  }
299 
300  $next_id = $ilDB->nextId($this->table_mail_obj_data);
301  $ilDB->manipulateF('
302  INSERT INTO '. $this->table_mail_obj_data .'
303  ( obj_id,
304  user_id,
305  title,
306  m_type
307  )
308  VALUES(%s,%s,%s,%s)',
309  array('integer','integer', 'text', 'text'),
310  array($next_id, $this->user_id, $a_folder_name, 'user_folder')
311  );
312  $this->mtree->insertNode($next_id, $a_parent_id);
313 
314  return $next_id;
315  }
316 
323  public function renameFolder($a_obj_id, $a_new_folder_name)
324  {
325  global $ilDB;
326 
327  if($this->folderNameExists($a_new_folder_name))
328  {
329  return false;
330  }
331 
332  $ilDB->manipulateF('
333  UPDATE '. $this->table_mail_obj_data .'
334  SET title = %s
335  WHERE obj_id = %s AND user_id = %s',
336  array('text', 'integer', 'integer'),
337  array($a_new_folder_name, $a_obj_id, $this->user_id)
338  );
339 
340  return true;
341  }
342 
348  protected function folderNameExists($a_folder_name)
349  {
350  global $ilDB;
351 
352  $res = $ilDB->queryF('
353  SELECT obj_id FROM '. $this->table_mail_obj_data .'
354  WHERE user_id = %s
355  AND title = %s',
356  array('integer', 'text'),
357  array($this->user_id, $a_folder_name)
358  );
359  $row = $ilDB->fetchAssoc($res);
360 
361  return is_array($row) && $row['obj_id'] > 0 ? true : false;
362  }
363 
369  public function deleteFolder($a_folder_id)
370  {
371  global $ilDB;
372 
373  $query = $ilDB->queryf('
374  SELECT obj_id, title FROM mail_obj_data
375  WHERE obj_id = %s AND user_id = %s',
376  array('integer', 'integer'),
377  array($a_folder_id, $this->user_id)
378  );
379  $row = $ilDB->fetchAssoc($query);
380 
381  if(!is_array($row) || array_key_exists($row['title'], $this->default_folder))
382  {
383  return false;
384  }
385 
386  require_once 'Services/Mail/classes/class.ilMail.php';
387  $umail = new ilMail($this->user_id);
388 
389  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
390  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
391 
392  foreach($subtree as $node)
393  {
394  $mails = $umail->getMailsOfFolder($node["obj_id"]);
395  $mail_ids = array();
396  foreach($mails as $mail)
397  {
398  $mail_ids[] = $mail["mail_id"];
399  }
400 
401  $umail->deleteMails($mail_ids);
402 
403  $ilDB->manipulateF('
404  DELETE FROM '. $this->table_mail_obj_data .'
405  WHERE obj_id = %s AND user_id = %s',
406  array('integer', 'integer'),
407  array($node['obj_id'], $this->user_id)
408  );
409  }
410 
411  return true;
412  }
413 
419  public function getFolderData($a_obj_id)
420  {
421  global $ilDB;
422 
423  $res = $ilDB->queryF('
424  SELECT * FROM ' . $this->table_mail_obj_data . '
425  WHERE user_id = %s
426  AND obj_id = %s',
427  array('integer', 'integer'),
428  array($this->user_id, $a_obj_id)
429  );
430  $row = $ilDB->fetchAssoc($res);
431 
432  return array(
433  'obj_id' => $row['obj_id'],
434  'title' => $row['title'],
435  'type' => $row['m_type']
436  );
437  }
438 
444  public function getParentFolderId($a_obj_id)
445  {
446  global $ilDB;
447 
448  $res = $ilDB->queryF('
449  SELECT * FROM '. $this->table_tree .'
450  WHERE child = %s AND tree = %s',
451  array('integer', 'integer'),
452  array($a_obj_id, $this->user_id)
453  );
454  $row = $ilDB->fetchAssoc($res);
455 
456  return is_array($row) ? $row['parent'] : 0;
457  }
458 
465  public function getSubFolders($a_folder = 0, $a_folder_parent = 0)
466  {
467  global $ilDB;
468 
469  if(!$a_folder)
470  {
471  $a_folder = $this->getRootFolderId();
472  }
473 
474  $user_folder = array();
475 
476  foreach($this->default_folder as $key => $value)
477  {
478  $res = $ilDB->queryF('
479  SELECT obj_id, m_type
480  FROM ' . $this->table_mail_obj_data . '
481  WHERE user_id = %s
482  AND title = %s',
483  array('integer', 'text'),
484  array($this->user_id, $key)
485  );
486  $row = $ilDB->fetchAssoc($res);
487 
488  $user_folder[] = array(
489  'title' => $key,
490  'type' => $row['m_type'],
491  'obj_id' => $row['obj_id']
492  );
493  }
494 
495  $res = $ilDB->queryF('
496  SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data . '
497  WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child
498  AND ' . $this->table_tree . '.depth > %s
499  AND ' . $this->table_tree . '.tree = %s
500  ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title ',
501  array('integer', 'integer'),
502  array(2, $this->user_id)
503  );
504  while($row = $ilDB->fetchAssoc($res))
505  {
506  $user_folder[] = array(
507  'title' => $row['title'],
508  'type' => $row['m_type'],
509  'obj_id' => $row['child']
510  );
511  }
512 
513  return $user_folder;
514  }
515 
519  public function setUserId($a_user_id)
520  {
521  $this->user_id = $a_user_id;
522  }
523 
530  public function delete()
531  {
535  global $ilDB;
536 
537  $ilDB->manipulateF('
538  DELETE FROM mail_obj_data WHERE user_id = %s',
539  array('integer'), array($this->user_id)
540  );
541 
542  $ilDB->manipulateF('
543  DELETE FROM mail_options WHERE user_id = %s',
544  array('integer'), array($this->user_id)
545  );
546 
547  $ilDB->manipulateF('
548  DELETE FROM mail_saved WHERE user_id = %s',
549  array('integer'), array($this->user_id)
550  );
551 
552  $ilDB->manipulateF('
553  DELETE FROM mail_tree WHERE tree = %s',
554  array('integer'), array($this->user_id)
555  );
556 
557  // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
558  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
559  $fdm = new ilFileDataMail($this->user_id);
560  $fdm->onUserDelete();
561 
562  // Delete mails of deleted user
563  $ilDB->manipulateF(
564  'DELETE FROM mail WHERE user_id = %s',
565  array('integer'),
566  array($this->user_id)
567  );
568 
569  return true;
570  }
571 
577  public function updateMailsOfDeletedUser($nameToShow)
578  {
582  global $ilDB;
583 
584  $ilDB->manipulateF('
585  UPDATE mail
586  SET sender_id = %s, import_name = %s
587  WHERE sender_id = %s',
588  array('integer', 'text', 'integer'),
589  array(0, $nameToShow, $this->user_id)
590  );
591  }
592 }
This class handles all operations on files (attachments) in directory ilias_data/mail.
getRootFolderId()
get Id of the root folder of an user public
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)
Mail Box class Base class for creating and handling mail boxes.
Class Mail this class handles base functions for mail handling.
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) ...
renameFolder($a_obj_id, $a_new_folder_name)
Rename a folder and check if the name already exists.
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.
getActions($a_mobj_id)
get all possible actions if no mobj_id is given or folder specific actions if mobj_id is given ...
global $ilDB
__construct($a_user_id=0)
Constructor.
deleteFolder($a_folder_id)