ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  function getInboxFolder()
123  {
124  global $ilDB;
125 
126  $res = $ilDB->queryf('
127  SELECT * FROM '.$this->table_mail_obj_data.'
128  WHERE user_id = %s
129  AND m_type = %s',
130  array('integer', 'text'),
131  array($this->user_id, 'inbox'));
132 
133  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
134 
135  return $row->obj_id;
136  }
137 
142  function getDraftsFolder()
143  {
144  global $ilDB;
145 
146  $res = $ilDB->queryf('
147  SELECT * FROM '.$this->table_mail_obj_data.'
148  WHERE user_id = %s
149  AND m_type = %s',
150  array('integer', 'text'),
151  array($this->user_id, 'drafts'));
152 
153  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
154 
155 
156  return $row->obj_id;
157  }
158 
163  function getTrashFolder()
164  {
165  global $ilDB;
166 
167  $res = $ilDB->queryf('
168  SELECT * FROM '.$this->table_mail_obj_data.'
169  WHERE user_id = %s
170  AND m_type = %s',
171  array('integer', 'text'),
172  array($this->user_id, 'trash'));
173 
174  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
175 
176  return $row->obj_id;
177  }
178 
183  function getSentFolder()
184  {
185  global $ilDB;
186 
187  $res = $ilDB->queryf('
188  SELECT * FROM '.$this->table_mail_obj_data.'
189  WHERE user_id = %s
190  AND m_type = %s',
191  array('integer', 'text'),
192  array($this->user_id, 'sent'));
193 
194  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
195 
196 
197  return $row->obj_id;
198  }
199 
204  function getRootFolderId()
205  {
206  return $this->mtree->getRootID($this->user_id);
207  }
208 
216  function getActions($a_mobj_id)
217  {
218  if ($a_mobj_id)
219  {
220  $folder_data = $this->getFolderData($a_mobj_id);
221 
222  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
223  {
224  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
225  return $this->actions;
226  }
227  }
228 
229  return $this->actions;
230  }
231 
239  function _countNewMails($a_user_id)
240  {
241  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
243  }
244 
250  {
251  global $ilDB;
252 
253 /* $root_id = $this->getLastInsertId();
254  ++$root_id;
255 */
256  $root_id = $ilDB->nextId($this->table_mail_obj_data);
257 
258  $res = $ilDB->manipulateF('
259  INSERT INTO '. $this->table_mail_obj_data .'
260  ( obj_id,
261  user_id,
262  title,
263  m_type
264  )
265  VALUES( %s, %s, %s, %s)',
266  array('integer','integer', 'text', 'text'),
267  array($root_id, $this->user_id, 'a_root', 'root'));
268 
269  $this->mtree->addTree($this->user_id,$root_id);
270 
271  foreach ($this->default_folder as $key => $folder)
272  {
273  /*$last_id = $this->getLastInsertId();
274  ++$last_id;
275  */
276  $last_id = $ilDB->nextId($this->table_mail_obj_data);
277  $statement = $ilDB->manipulateF('
278  INSERT INTO '. $this->table_mail_obj_data .'
279  ( obj_id,
280  user_id,
281  title,
282  m_type
283  )
284  VALUES( %s, %s, %s, %s)',
285  array('integer','integer', 'text', 'text'),
286  array($last_id,$this->user_id, $key, $folder));
287 
288  $this->mtree->insertNode($last_id,$root_id);
289  }
290  }
298  function addFolder($a_parent_id,$a_folder_name)
299  {
300  global $ilDB;
301 
302  if ($this->folderNameExists($a_folder_name))
303  {
304  return 0;
305  }
306  // ENTRY IN mail_obj_data
307  $next_id = $ilDB->nextId($this->table_mail_obj_data);
308  $statement = $ilDB->manipulateF('
309  INSERT INTO '. $this->table_mail_obj_data .'
310  ( obj_id,
311  user_id,
312  title,
313  m_type
314  )
315  VALUES(%s,%s,%s,%s)',
316  array('integer','integer', 'text', 'text'),
317  array($next_id, $this->user_id, $a_folder_name, 'user_folder'));
318 
319  // ENTRY IN mail_tree
320  $this->mtree->insertNode($next_id,$a_parent_id);
321  return $next_id;
322  }
323 
331  function renameFolder($a_obj_id, $a_new_folder_name)
332  {
333  global $ilDB;
334 
335  if ($this->folderNameExists($a_new_folder_name))
336  {
337  return false;
338  }
339 
340  $statement = $ilDB->manipulateF('
341  UPDATE '. $this->table_mail_obj_data .'
342  SET title = %s
343  WHERE obj_id = %s',
344  array('text', 'integer'),
345  array($a_new_folder_name, $a_obj_id));
346 
347  return true;
348  }
349 
356  function folderNameExists($a_folder_name)
357  {
358  global $ilDB;
359 
360  $res = $ilDB->queryf('
361  SELECT obj_id FROM '. $this->table_mail_obj_data .'
362  WHERE user_id = %s
363  AND title = %s',
364  array('integer', 'text'),
365  array($this->user_id, $a_folder_name));
366 
367  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
368 
369  return $row->obj_id ? true : false;
370  }
371 
377  function deleteFolder($a_folder_id)
378  {
379  global $ilDB;
380 
381  $query = $ilDB->queryf('
382  SELECT title FROM mail_obj_data
383  WHERE obj_id = %s',
384  array('integer'),
385  array($a_folder_id)
386  );
387 
388  $row = $ilDB->fetchAssoc($query);
389 
390  if( array_key_exists($row['title'], $this->default_folder) )
391  {
392  return false;
393  }
394 
395  include_once("Services/Mail/classes/class.ilMail.php");
396  $umail = new ilMail($this->user_id);
397 
398  // SAVE SUBTREE DATA
399  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
400 
401  // DELETE ENTRY IN TREE
402  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
403 
404  // DELETE ENTRY IN mobj_data
405  foreach($subtree as $node)
406  {
407  // DELETE mail(s) of folder(s)
408  $mails = $umail->getMailsOfFolder($node["obj_id"]);
409 
410  foreach ($mails as $mail)
411  {
412  $mail_ids[] = $mail["mail_id"];
413  }
414 
415  if (is_array($mail_ids))
416  {
417  $umail->deleteMails($mail_ids);
418  }
419 
420  // DELETE mobj_data entries
421  $statement = $ilDB->manipulateF('
422  DELETE FROM '. $this->table_mail_obj_data .'
423  WHERE obj_id = %s',
424  array('integer'),
425  array($node['obj_id']));
426  }
427 
428  return true;
429  }
430 
431  // DONE: can be substituted by ilUtil::getLastInsertId
432  function getLastInsertId()
433  {
434  global $ilDB;
435 
436  return $ilDB->getLastInsertId();
437  }
438 
444  function getFolderData($a_obj_id)
445  {
446  global $ilDB;
447 
448  $res = $ilDB->queryf('
449  SELECT * FROM '. $this->table_mail_obj_data .'
450  WHERE user_id = %s
451  AND obj_id = %s',
452  array('integer', 'integer'),
453  array($this->user_id, $a_obj_id));
454 
455  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
456 
457  return array(
458  "title" => stripslashes($row->title),
459  "type" => $row->m_type
460  );
461  }
467  function getParentFolderId($a_obj_id)
468  {
469  global $ilDB;
470 
471  $res = $ilDB->queryf('
472  SELECT * FROM '. $this->table_tree .'
473  WHERE child = %s',
474  array('integer'),
475  array($a_obj_id));
476 
477  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
478 
479 
480  return $row->parent;
481  }
488  function getSubFolders($a_folder = 0,$a_folder_parent = 0)
489  {
490 
491  global $ilDB;
492 
493  if (!$a_folder)
494  {
495  $a_folder = $this->getRootFolderId();
496  }
497 
498  foreach ($this->default_folder as $key => $value)
499  {
500  $res = $ilDB->queryf('
501  SELECT obj_id,m_type FROM '. $this->table_mail_obj_data .'
502  WHERE user_id = %s
503  AND title = %s',
504  array('integer', 'text'),
505  array($this->user_id, $key));
506 
507  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
508 
509  $user_folder[] = array(
510  "title" => $key,
511  "type" => $row->m_type,
512  "obj_id" => $row->obj_id);
513  }
514 
515  $res = $ilDB->queryf('
516  SELECT * FROM '. $this->table_tree. ', '. $this->table_mail_obj_data .'
517  WHERE '. $this->table_mail_obj_data.'.obj_id = '. $this->table_tree.'.child
518  AND '. $this->table_tree.'.depth > %s
519  AND '. $this->table_tree.'.tree = %s
520  ORDER BY '. $this->table_mail_obj_data.'.title ',
521  array('integer', 'integer'),
522  array('2', $this->user_id));
523 
524  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
525  {
526  $user_folder[] = array(
527  "title" => stripslashes($row->title),
528  "type" => $row->m_type,
529  "obj_id" => $row->child);
530  }
531 
532  return $user_folder;
533  }
534 
540  function setUserId($a_user_id)
541  {
542  $this->user_id = $a_user_id;
543  }
544 
553  function delete()
554  {
558  global $ilDB;
559 
560  $ilDB->manipulateF('
561  DELETE FROM mail_obj_data WHERE user_id = %s',
562  array('integer'), array($this->user_id)
563  );
564 
565  $ilDB->manipulateF('
566  DELETE FROM mail_options WHERE user_id = %s',
567  array('integer'), array($this->user_id)
568  );
569 
570  $ilDB->manipulateF('
571  DELETE FROM mail_saved WHERE user_id = %s',
572  array('integer'), array($this->user_id)
573  );
574 
575  $ilDB->manipulateF('
576  DELETE FROM mail_tree WHERE tree = %s',
577  array('integer'), array($this->user_id)
578  );
579 
580  // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
581  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
582  $fdm = new ilFileDataMail($this->user_id);
583  $fdm->onUserDelete();
584 
585  // Delete mails of deleted user
586  $ilDB->manipulateF(
587  'DELETE FROM mail WHERE user_id = %s',
588  array('integer'),
589  array($this->user_id)
590  );
591 
592  return true;
593  }
594 
602  public function updateMailsOfDeletedUser($nameToShow)
603  {
607  global $ilDB;
608 
609  $ilDB->manipulateF('
610  UPDATE mail
611  SET sender_id = %s,
612  import_name = %s
613  WHERE sender_id = %s',
614  array('integer', 'text', 'integer'),
615  array(0, $nameToShow, $this->user_id));
616  }
617 }
618 ?>