ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 {
19  protected $ilias;
20 
24  protected $lng;
25 
29  protected $db;
30 
34  protected $mtree;
35 
40  protected $user_id;
41 
46  protected $actions = array();
47 
52  protected $default_folder = array();
53 
59 
64  protected $table_tree;
65 
70  public function __construct($a_user_id = 0)
71  {
72  global $DIC;
73 
74  $this->ilias = $DIC['ilias'];
75  $this->lng = $DIC->language();
76  $this->db = $DIC->database();
77 
78  $this->user_id = $a_user_id;
79 
80  $this->table_mail_obj_data = 'mail_obj_data';
81  $this->table_tree = 'mail_tree';
82 
83  if ($a_user_id) {
84  $this->mtree = new ilTree($this->user_id);
85  $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
86  }
87 
88  // i added this, becaus if i create a new user automatically during
89  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
90  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
91  if (is_object($this->lng)) {
92  $this->lng->loadLanguageModule("mail");
93 
94  $this->actions = array(
95  "moveMails" => $this->lng->txt("mail_move_to"),
96  "markMailsRead" => $this->lng->txt("mail_mark_read"),
97  "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
98  "deleteMails" => $this->lng->txt("delete"));
99  }
100 
101  // array contains basic folders and there lng translation for every new user
102  $this->default_folder = array(
103  "b_inbox" => "inbox",
104  "c_trash" => "trash",
105  "d_drafts" => "drafts",
106  "e_sent" => "sent",
107  "z_local" => "local");
108  }
109 
113  public function getInboxFolder()
114  {
115  $res = $this->db->queryF(
116  '
117  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
118  WHERE user_id = %s
119  AND m_type = %s',
120  array('integer', 'text'),
121  array($this->user_id, 'inbox')
122  );
123 
124  $row = $this->db->fetchAssoc($res);
125 
126  return $row['obj_id'];
127  }
128 
132  public function getDraftsFolder()
133  {
134  $res = $this->db->queryF(
135  '
136  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
137  WHERE user_id = %s
138  AND m_type = %s',
139  array('integer', 'text'),
140  array($this->user_id, 'drafts')
141  );
142 
143  $row = $this->db->fetchAssoc($res);
144 
145  return $row['obj_id'];
146  }
147 
151  public function getTrashFolder()
152  {
153  $res = $this->db->queryf(
154  '
155  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
156  WHERE user_id = %s
157  AND m_type = %s',
158  array('integer', 'text'),
159  array($this->user_id, 'trash')
160  );
161 
162  $row = $this->db->fetchAssoc($res);
163 
164  return $row['obj_id'];
165  }
166 
170  public function getSentFolder()
171  {
172  $res = $this->db->queryf(
173  '
174  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
175  WHERE user_id = %s
176  AND m_type = %s',
177  array('integer', 'text'),
178  array($this->user_id, 'sent')
179  );
180 
181  $row = $this->db->fetchAssoc($res);
182 
183  return $row['obj_id'];
184  }
185 
189  public function getRootFolderId()
190  {
191  return $this->mtree->getRootID($this->user_id);
192  }
193 
201  public function getActions($a_mobj_id)
202  {
203  if ($a_mobj_id) {
204  $folder_data = $this->getFolderData($a_mobj_id);
205 
206  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local") {
207  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
208  return $this->actions;
209  }
210  }
211 
212  return $this->actions;
213  }
214 
218  public function createDefaultFolder()
219  {
220  $root_id = $this->db->nextId($this->table_mail_obj_data);
221  $this->db->manipulateF(
222  '
223  INSERT INTO ' . $this->table_mail_obj_data . '
224  ( obj_id,
225  user_id,
226  title,
227  m_type
228  )
229  VALUES(%s, %s, %s, %s)',
230  array('integer','integer', 'text', 'text'),
231  array($root_id, $this->user_id, 'a_root', 'root')
232  );
233  $this->mtree->addTree($this->user_id, $root_id);
234 
235  foreach ($this->default_folder as $key => $folder) {
236  $last_id = $this->db->nextId($this->table_mail_obj_data);
237  $this->db->manipulateF(
238  '
239  INSERT INTO ' . $this->table_mail_obj_data . '
240  ( obj_id,
241  user_id,
242  title,
243  m_type
244  )
245  VALUES(%s, %s, %s, %s)',
246  array('integer','integer', 'text', 'text'),
247  array($last_id, $this->user_id, $key, $folder)
248  );
249  $this->mtree->insertNode($last_id, $root_id);
250  }
251  }
252 
259  public function addFolder($a_parent_id, $a_folder_name)
260  {
261  if ($this->folderNameExists($a_folder_name)) {
262  return 0;
263  }
264 
265  $next_id = $this->db->nextId($this->table_mail_obj_data);
266  $this->db->manipulateF(
267  '
268  INSERT INTO ' . $this->table_mail_obj_data . '
269  ( obj_id,
270  user_id,
271  title,
272  m_type
273  )
274  VALUES(%s,%s,%s,%s)',
275  array('integer','integer', 'text', 'text'),
276  array($next_id, $this->user_id, $a_folder_name, 'user_folder')
277  );
278  $this->mtree->insertNode($next_id, $a_parent_id);
279 
280  return $next_id;
281  }
282 
289  public function renameFolder($a_obj_id, $a_new_folder_name)
290  {
291  if ($this->folderNameExists($a_new_folder_name)) {
292  return false;
293  }
294 
295  $this->db->manipulateF(
296  '
297  UPDATE ' . $this->table_mail_obj_data . '
298  SET title = %s
299  WHERE obj_id = %s AND user_id = %s',
300  array('text', 'integer', 'integer'),
301  array($a_new_folder_name, $a_obj_id, $this->user_id)
302  );
303 
304  return true;
305  }
306 
312  protected function folderNameExists($a_folder_name)
313  {
314  $res = $this->db->queryF(
315  '
316  SELECT obj_id FROM ' . $this->table_mail_obj_data . '
317  WHERE user_id = %s
318  AND title = %s',
319  array('integer', 'text'),
320  array($this->user_id, $a_folder_name)
321  );
322  $row = $this->db->fetchAssoc($res);
323 
324  return is_array($row) && $row['obj_id'] > 0 ? true : false;
325  }
326 
332  public function deleteFolder($a_folder_id)
333  {
334  $query = $this->db->queryf(
335  '
336  SELECT obj_id, title FROM mail_obj_data
337  WHERE obj_id = %s AND user_id = %s',
338  array('integer', 'integer'),
339  array($a_folder_id, $this->user_id)
340  );
341  $row = $this->db->fetchAssoc($query);
342 
343  if (!is_array($row) || array_key_exists($row['title'], $this->default_folder)) {
344  return false;
345  }
346 
347  require_once 'Services/Mail/classes/class.ilMail.php';
348  $umail = new ilMail($this->user_id);
349 
350  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
351  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
352 
353  foreach ($subtree as $node) {
354  $mails = $umail->getMailsOfFolder($node["obj_id"]);
355  $mail_ids = array();
356  foreach ($mails as $mail) {
357  $mail_ids[] = $mail["mail_id"];
358  }
359 
360  $umail->deleteMails($mail_ids);
361 
362  $this->db->manipulateF(
363  '
364  DELETE FROM ' . $this->table_mail_obj_data . '
365  WHERE obj_id = %s AND user_id = %s',
366  array('integer', 'integer'),
367  array($node['obj_id'], $this->user_id)
368  );
369  }
370 
371  return true;
372  }
373 
379  public function getFolderData($a_obj_id)
380  {
381  $res = $this->db->queryF(
382  '
383  SELECT * FROM ' . $this->table_mail_obj_data . '
384  WHERE user_id = %s
385  AND obj_id = %s',
386  array('integer', 'integer'),
387  array($this->user_id, $a_obj_id)
388  );
389  $row = $this->db->fetchAssoc($res);
390 
391  return array(
392  'obj_id' => $row['obj_id'],
393  'title' => $row['title'],
394  'type' => $row['m_type']
395  );
396  }
397 
403  public function getParentFolderId($a_obj_id)
404  {
405  $res = $this->db->queryF(
406  '
407  SELECT * FROM ' . $this->table_tree . '
408  WHERE child = %s AND tree = %s',
409  array('integer', 'integer'),
410  array($a_obj_id, $this->user_id)
411  );
412  $row = $this->db->fetchAssoc($res);
413 
414  return is_array($row) ? $row['parent'] : 0;
415  }
416 
423  public function getSubFolders($a_folder = 0, $a_folder_parent = 0)
424  {
425  if (!$a_folder) {
426  $a_folder = $this->getRootFolderId();
427  }
428 
429  $user_folder = array();
430 
431  foreach ($this->default_folder as $key => $value) {
432  $res = $this->db->queryF(
433  '
434  SELECT obj_id, m_type
435  FROM ' . $this->table_mail_obj_data . '
436  WHERE user_id = %s
437  AND title = %s',
438  array('integer', 'text'),
439  array($this->user_id, $key)
440  );
441  $row = $this->db->fetchAssoc($res);
442 
443  $user_folder[] = array(
444  'title' => $key,
445  'type' => $row['m_type'],
446  'obj_id' => $row['obj_id']
447  );
448  }
449 
450  $res = $this->db->queryF(
451  '
452  SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data . '
453  WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child
454  AND ' . $this->table_tree . '.depth > %s
455  AND ' . $this->table_tree . '.tree = %s
456  ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title ',
457  array('integer', 'integer'),
458  array(2, $this->user_id)
459  );
460  while ($row = $this->db->fetchAssoc($res)) {
461  $user_folder[] = array(
462  'title' => $row['title'],
463  'type' => $row['m_type'],
464  'obj_id' => $row['child']
465  );
466  }
467 
468  return $user_folder;
469  }
470 
474  public function setUserId($a_user_id)
475  {
476  $this->user_id = $a_user_id;
477  }
478 
485  public function delete()
486  {
487  $this->db->manipulateF(
488  '
489  DELETE FROM mail_obj_data WHERE user_id = %s',
490  array('integer'),
491  array($this->user_id)
492  );
493 
494  $this->db->manipulateF(
495  '
496  DELETE FROM mail_options WHERE user_id = %s',
497  array('integer'),
498  array($this->user_id)
499  );
500 
501  $this->db->manipulateF(
502  '
503  DELETE FROM mail_saved WHERE user_id = %s',
504  array('integer'),
505  array($this->user_id)
506  );
507 
508  $this->db->manipulateF(
509  '
510  DELETE FROM mail_tree WHERE tree = %s',
511  array('integer'),
512  array($this->user_id)
513  );
514 
515  // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
516  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
517  $fdm = new ilFileDataMail($this->user_id);
518  $fdm->onUserDelete();
519 
520  // Delete mails of deleted user
521  $this->db->manipulateF(
522  'DELETE FROM mail WHERE user_id = %s',
523  array('integer'),
524  array($this->user_id)
525  );
526 
527  return true;
528  }
529 
535  public function updateMailsOfDeletedUser($nameToShow)
536  {
537  $this->db->manipulateF(
538  '
539  UPDATE mail
540  SET sender_id = %s, import_name = %s
541  WHERE sender_id = %s',
542  array('integer', 'text', 'integer'),
543  array(0, $nameToShow, $this->user_id)
544  );
545  }
546 }
updateMailsOfDeletedUser($nameToShow)
Update existing mails.
global $DIC
Definition: saml.php:7
This class handles all operations on files (attachments) in directory ilias_data/mail.
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.
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) ...
$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...
Create styles array
The data for the language used.
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 ...
__construct($a_user_id=0)
ilMailbox constructor.
getDraftsFolder()
get Id of the inbox folder of an user
deleteFolder($a_folder_id)
$key
Definition: croninfo.php:18