ILIAS  Release_5_0_x_branch Revision 61816
 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  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  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 
253  function _countNewMails($a_user_id)
254  {
255  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
257  }
258 
264  {
265  global $ilDB;
266 
267 /* $root_id = $this->getLastInsertId();
268  ++$root_id;
269 */
270  $root_id = $ilDB->nextId($this->table_mail_obj_data);
271 
272  $res = $ilDB->manipulateF('
273  INSERT INTO '. $this->table_mail_obj_data .'
274  ( obj_id,
275  user_id,
276  title,
277  m_type
278  )
279  VALUES( %s, %s, %s, %s)',
280  array('integer','integer', 'text', 'text'),
281  array($root_id, $this->user_id, 'a_root', 'root'));
282 
283  $this->mtree->addTree($this->user_id,$root_id);
284 
285  foreach ($this->default_folder as $key => $folder)
286  {
287  /*$last_id = $this->getLastInsertId();
288  ++$last_id;
289  */
290  $last_id = $ilDB->nextId($this->table_mail_obj_data);
291  $statement = $ilDB->manipulateF('
292  INSERT INTO '. $this->table_mail_obj_data .'
293  ( obj_id,
294  user_id,
295  title,
296  m_type
297  )
298  VALUES( %s, %s, %s, %s)',
299  array('integer','integer', 'text', 'text'),
300  array($last_id,$this->user_id, $key, $folder));
301 
302  $this->mtree->insertNode($last_id,$root_id);
303  }
304  }
312  function addFolder($a_parent_id,$a_folder_name)
313  {
314  global $ilDB;
315 
316  if ($this->folderNameExists($a_folder_name))
317  {
318  return 0;
319  }
320  // ENTRY IN mail_obj_data
321  $next_id = $ilDB->nextId($this->table_mail_obj_data);
322  $statement = $ilDB->manipulateF('
323  INSERT INTO '. $this->table_mail_obj_data .'
324  ( obj_id,
325  user_id,
326  title,
327  m_type
328  )
329  VALUES(%s,%s,%s,%s)',
330  array('integer','integer', 'text', 'text'),
331  array($next_id, $this->user_id, $a_folder_name, 'user_folder'));
332 
333  // ENTRY IN mail_tree
334  $this->mtree->insertNode($next_id,$a_parent_id);
335  return $next_id;
336  }
337 
345  function renameFolder($a_obj_id, $a_new_folder_name)
346  {
347  global $ilDB;
348 
349  if ($this->folderNameExists($a_new_folder_name))
350  {
351  return false;
352  }
353 
354  $statement = $ilDB->manipulateF('
355  UPDATE '. $this->table_mail_obj_data .'
356  SET title = %s
357  WHERE obj_id = %s',
358  array('text', 'integer'),
359  array($a_new_folder_name, $a_obj_id));
360 
361  return true;
362  }
363 
370  function folderNameExists($a_folder_name)
371  {
372  global $ilDB;
373 
374  $res = $ilDB->queryf('
375  SELECT obj_id FROM '. $this->table_mail_obj_data .'
376  WHERE user_id = %s
377  AND title = %s',
378  array('integer', 'text'),
379  array($this->user_id, $a_folder_name));
380 
381  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
382 
383  return $row->obj_id ? true : false;
384  }
385 
391  function deleteFolder($a_folder_id)
392  {
393  global $ilDB;
394 
395  $query = $ilDB->queryf('
396  SELECT title FROM mail_obj_data
397  WHERE obj_id = %s',
398  array('integer'),
399  array($a_folder_id)
400  );
401 
402  $row = $ilDB->fetchAssoc($query);
403 
404  if( array_key_exists($row['title'], $this->default_folder) )
405  {
406  return false;
407  }
408 
409  include_once("Services/Mail/classes/class.ilMail.php");
410  $umail = new ilMail($this->user_id);
411 
412  // SAVE SUBTREE DATA
413  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
414 
415  // DELETE ENTRY IN TREE
416  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
417 
418  // DELETE ENTRY IN mobj_data
419  foreach($subtree as $node)
420  {
421  // DELETE mail(s) of folder(s)
422  $mails = $umail->getMailsOfFolder($node["obj_id"]);
423 
424  foreach ($mails as $mail)
425  {
426  $mail_ids[] = $mail["mail_id"];
427  }
428 
429  if (is_array($mail_ids))
430  {
431  $umail->deleteMails($mail_ids);
432  }
433 
434  // DELETE mobj_data entries
435  $statement = $ilDB->manipulateF('
436  DELETE FROM '. $this->table_mail_obj_data .'
437  WHERE obj_id = %s',
438  array('integer'),
439  array($node['obj_id']));
440  }
441 
442  return true;
443  }
444 
445  // DONE: can be substituted by ilUtil::getLastInsertId
446  function getLastInsertId()
447  {
448  global $ilDB;
449 
450  return $ilDB->getLastInsertId();
451  }
452 
458  function getFolderData($a_obj_id)
459  {
460  global $ilDB;
461 
462  $res = $ilDB->queryf('
463  SELECT * FROM '. $this->table_mail_obj_data .'
464  WHERE user_id = %s
465  AND obj_id = %s',
466  array('integer', 'integer'),
467  array($this->user_id, $a_obj_id));
468 
469  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
470 
471  return array(
472  "obj_id" => $row->obj_id,
473  "title" => stripslashes($row->title),
474  "type" => $row->m_type
475  );
476  }
482  function getParentFolderId($a_obj_id)
483  {
484  global $ilDB;
485 
486  $res = $ilDB->queryf('
487  SELECT * FROM '. $this->table_tree .'
488  WHERE child = %s',
489  array('integer'),
490  array($a_obj_id));
491 
492  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
493 
494 
495  return $row->parent;
496  }
503  function getSubFolders($a_folder = 0,$a_folder_parent = 0)
504  {
505 
506  global $ilDB;
507 
508  if (!$a_folder)
509  {
510  $a_folder = $this->getRootFolderId();
511  }
512 
513  foreach ($this->default_folder as $key => $value)
514  {
515  $res = $ilDB->queryf('
516  SELECT obj_id,m_type FROM '. $this->table_mail_obj_data .'
517  WHERE user_id = %s
518  AND title = %s',
519  array('integer', 'text'),
520  array($this->user_id, $key));
521 
522  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
523 
524  $user_folder[] = array(
525  "title" => $key,
526  "type" => $row->m_type,
527  "obj_id" => $row->obj_id);
528  }
529 
530  $res = $ilDB->queryf('
531  SELECT * FROM '. $this->table_tree. ', '. $this->table_mail_obj_data .'
532  WHERE '. $this->table_mail_obj_data.'.obj_id = '. $this->table_tree.'.child
533  AND '. $this->table_tree.'.depth > %s
534  AND '. $this->table_tree.'.tree = %s
535  ORDER BY '. $this->table_mail_obj_data.'.title ',
536  array('integer', 'integer'),
537  array('2', $this->user_id));
538 
539  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
540  {
541  $user_folder[] = array(
542  "title" => stripslashes($row->title),
543  "type" => $row->m_type,
544  "obj_id" => $row->child);
545  }
546 
547  return $user_folder;
548  }
549 
555  function setUserId($a_user_id)
556  {
557  $this->user_id = $a_user_id;
558  }
559 
568  function delete()
569  {
573  global $ilDB;
574 
575  $ilDB->manipulateF('
576  DELETE FROM mail_obj_data WHERE user_id = %s',
577  array('integer'), array($this->user_id)
578  );
579 
580  $ilDB->manipulateF('
581  DELETE FROM mail_options WHERE user_id = %s',
582  array('integer'), array($this->user_id)
583  );
584 
585  $ilDB->manipulateF('
586  DELETE FROM mail_saved WHERE user_id = %s',
587  array('integer'), array($this->user_id)
588  );
589 
590  $ilDB->manipulateF('
591  DELETE FROM mail_tree WHERE tree = %s',
592  array('integer'), array($this->user_id)
593  );
594 
595  // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
596  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
597  $fdm = new ilFileDataMail($this->user_id);
598  $fdm->onUserDelete();
599 
600  // Delete mails of deleted user
601  $ilDB->manipulateF(
602  'DELETE FROM mail WHERE user_id = %s',
603  array('integer'),
604  array($this->user_id)
605  );
606 
607  return true;
608  }
609 
617  public function updateMailsOfDeletedUser($nameToShow)
618  {
622  global $ilDB;
623 
624  $ilDB->manipulateF('
625  UPDATE mail
626  SET sender_id = %s,
627  import_name = %s
628  WHERE sender_id = %s',
629  array('integer', 'text', 'integer'),
630  array(0, $nameToShow, $this->user_id));
631  }
632 }
633 ?>