ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailbox.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
33 require_once("Services/Mail/classes/class.ilMail.php");
34 
35 class ilMailbox
36 {
42  var $ilias;
43 
49  var $lng;
50 
56  var $mtree;
57 
63  var $user_id;
64 
71  var $actions;
72 
79 
86 
93 
99  function ilMailbox($a_user_id = 0)
100  {
101  global $ilias,$lng;
102 
103 
104  $this->ilias = &$ilias;
105  $this->lng = &$lng;
106  $this->user_id = $a_user_id;
107 
108  $this->table_mail_obj_data = 'mail_obj_data';
109  $this->table_tree = 'mail_tree';
110 
111  if ($a_user_id)
112  {
113  $this->mtree = new ilTree($this->user_id);
114  $this->mtree->setTableNames($this->table_tree,$this->table_mail_obj_data);
115  }
116 
117  // i added this, becaus if i create a new user automatically during
118  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
119  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
120  if (is_object($this->lng))
121  {
122  $this->lng->loadLanguageModule("mail");
123 
124  $this->actions = array(
125  "moveMails" => $this->lng->txt("mail_move_to"),
126  "markMailsRead" => $this->lng->txt("mail_mark_read"),
127  "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
128  "deleteMails" => $this->lng->txt("delete"));
129  }
130 
131  // array contains basic folders and there lng translation for every new user
132  $this->default_folder = array(
133  "b_inbox" => "inbox",
134  "c_trash" => "trash",
135  "d_drafts" => "drafts",
136  "e_sent" => "sent",
137  "z_local" => "local");
138 
139  }
144  function getInboxFolder()
145  {
146  global $ilDB;
147 
148  $query = "SELECT * FROM ".$this->table_mail_obj_data." ".
149  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
150  "AND type = 'inbox'";
151  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
152 
153  return $row->obj_id;
154  }
155 
160  function getDraftsFolder()
161  {
162  global $ilDB;
163 
164  $query = "SELECT * FROM ".$this->table_mail_obj_data ." ".
165  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
166  "AND type = 'drafts'";
167  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
168 
169  return $row->obj_id;
170  }
171 
176  function getTrashFolder()
177  {
178  global $ilDB;
179 
180  $query = "SELECT * FROM ".$this->table_mail_obj_data ." ".
181  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
182  "AND type = 'trash'";
183  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
184 
185  return $row->obj_id;
186  }
187 
192  function getSentFolder()
193  {
194  global $ilDB;
195 
196  $query = "SELECT * FROM $this->table_mail_obj_data ".
197  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
198  "AND type = 'sent'";
199  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
200 
201  return $row->obj_id;
202  }
203 
208  function getRootFolderId()
209  {
210  return $this->mtree->getRootID($this->user_id);
211  }
212 
220  function getActions($a_mobj_id)
221  {
222  if ($a_mobj_id)
223  {
224  $folder_data = $this->getFolderData($a_mobj_id);
225 
226  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
227  {
228  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
229  return $this->actions;
230  }
231  }
232 
233  return $this->actions;
234  }
235 
243  function hasNewMail($a_user_id)
244  {
245  global $ilDB;
246  global $ilias;
247 
248  if (!$a_user_id)
249  {
250  return 0;
251  }
252 
253  // CHECK FOR SYSTEM MAIL
254  $query = "SELECT mail_id FROM mail WHERE folder_id = 0 AND user_id = ".$ilDB->quote($a_user_id)." ".
255  "AND m_status = 'unread'";
256 
257  $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
258 
259  if($row->mail_id)
260  {
261  return $row->mail_id;
262  }
263 
264  $query = "SELECT m.mail_id FROM mail AS m,mail_obj_data AS mo ".
265  "WHERE m.user_id = mo.user_id ".
266  "AND m.folder_id = mo.obj_id ".
267  "AND mo.type = 'inbox' ".
268  "AND m.user_id = ".$ilDB->quote($a_user_id)." ".
269  "AND m.m_status = 'unread'";
270  $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
271 
272  return $row ? $row->mail_id : 0;
273  }
274 
282  function _countNewMails($a_user_id)
283  {
284  global $ilDB;
285  global $ilias;
286 
287  if (!$a_user_id)
288  {
289  return 0;
290  }
291 
292  // CHECK FOR SYSTEM MAIL
293  $query = "SELECT count(*) as cnt FROM mail WHERE folder_id = 0 AND user_id = ".$ilDB->quote($a_user_id)." ".
294  "AND m_status = 'unread'";
295 
296  $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
297 
298  $query = "SELECT count(*) as cnt FROM mail AS m,mail_obj_data AS mo ".
299  "WHERE m.user_id = mo.user_id ".
300  "AND m.folder_id = mo.obj_id ".
301  "AND mo.type = 'inbox' ".
302  "AND m.user_id = ".$ilDB->quote($a_user_id)." ".
303  "AND m.m_status = 'unread'";
304  $row2 = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
305 
306  return $row->cnt + $row2->cnt;
307  }
308 
314  {
315  global $ilDB;
316 
317  $root_id = $this->getLastInsertId();
318  ++$root_id;
319 
320  $query = "INSERT INTO $this->table_mail_obj_data ".
321  "SET user_id = ".$ilDB->quote($this->user_id).", ".
322  "title = 'a_root',".
323  "type = 'root'";
324  $res = $this->ilias->db->query($query);
325  $this->mtree->addTree($this->user_id,$root_id);
326 
327  foreach ($this->default_folder as $key => $folder)
328  {
329  $last_id = $this->getLastInsertId();
330  ++$last_id;
331 
332  $query = "INSERT INTO $this->table_mail_obj_data ".
333  "SET user_id = ".$ilDB->quote($this->user_id).", ".
334  "title = ".$ilDB->quote($key).", ".
335  "type = ".$ilDB->quote($folder);
336  $res = $this->ilias->db->query($query);
337  $this->mtree->insertNode($last_id,$root_id);
338  }
339  }
347  function addFolder($a_parent_id,$a_folder_name)
348  {
349  global $ilDB;
350 
351  if ($this->folderNameExists($a_folder_name))
352  {
353  return 0;
354  }
355  // ENTRY IN mail_obj_data
356  $query = "INSERT INTO $this->table_mail_obj_data ".
357  "SET user_id = ".$ilDB->quote($this->user_id).", ".
358  "title = ".$ilDB->quote($a_folder_name).",".
359  "type = 'user_folder'";
360  $res = $this->ilias->db->query($query);
361 
362  // ENTRY IN mail_tree
363  $new_id = $this->getLastInsertId();
364  $this->mtree->insertNode($new_id,$a_parent_id);
365 
366  return $new_id;
367  }
368 
376  function renameFolder($a_obj_id, $a_new_folder_name)
377  {
378  global $ilDB;
379 
380  if ($this->folderNameExists($a_new_folder_name))
381  {
382  return false;
383  }
384 
385  $query = "UPDATE $this->table_mail_obj_data ".
386  "SET title = ".$ilDB->quote($a_new_folder_name)." ".
387  "WHERE obj_id = ".$ilDB->quote($a_obj_id)." ";
388  $res = $this->ilias->db->query($query);
389 
390  return true;
391  }
392 
399  function folderNameExists($a_folder_name)
400  {
401  global $ilDB;
402 
403  $query = "SELECT obj_id FROM $this->table_mail_obj_data ".
404  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
405  "AND title = ".$ilDB->quote($a_folder_name)." ";
406  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
407 
408  return $row->obj_id ? true : false;
409  }
410 
416  function deleteFolder($a_folder_id)
417  {
418  global $ilDB;
419 
420  include_once("Services/Mail/classes/class.ilMail.php");
421  $umail = new ilMail($this->user_id);
422 
423  // SAVE SUBTREE DATA
424  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
425 
426  // DELETE ENTRY IN TREE
427  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
428 
429  // DELETE ENTRY IN mobj_data
430  foreach($subtree as $node)
431  {
432  // DELETE mail(s) of folder(s)
433  $mails = $umail->getMailsOfFolder($node["obj_id"]);
434 
435  foreach ($mails as $mail)
436  {
437  $mail_ids[] = $mail["mail_id"];
438  }
439 
440  if (is_array($mail_ids))
441  {
442  $umail->deleteMails($mail_ids);
443  }
444 
445  // DELETE mobj_data entries
446  $query = "DELETE FROM $this->table_mail_obj_data ".
447  "WHERE obj_id = ".$ilDB->quote($node["obj_id"])." ";
448  $res = $this->ilias->db->query($query);
449  }
450 
451  return true;
452  }
453 
454  // DONE: can be substituted by ilUtil::getLastInsertId
455  function getLastInsertId()
456  {
457  global $ilDB;
458 
459  return $ilDB->getLastInsertId();
460  }
461 
467  function getFolderData($a_obj_id)
468  {
469  global $ilDB;
470 
471  $query = "SELECT * FROM $this->table_mail_obj_data ".
472  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
473  "AND obj_id = ".$ilDB->quote($a_obj_id)." ";
474 
475  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
476 
477  return array(
478  "title" => stripslashes($row->title),
479  "type" => $row->type
480  );
481  }
487  function getParentFolderId($a_obj_id)
488  {
489  global $ilDB;
490 
491  $query = "SELECT * FROM $this->table_tree ".
492  "WHERE child = ".$ilDB->quote($a_obj_id)." ";
493  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
494 
495  return $row->parent;
496  }
503  function getSubFolders($a_folder = 0,$a_folder_parent = 0)
504  {
505  global $ilDB;
506 
507  if (!$a_folder)
508  {
509  $a_folder = $this->getRootFolderId();
510  }
511 
512  foreach ($this->default_folder as $key => $value)
513  {
514  $query = "SELECT obj_id,type FROM $this->table_mail_obj_data ".
515  "WHERE user_id = ".$ilDB->quote($this->user_id). " ".
516  "AND title = ".$ilDB->quote($key)." ";
517  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
518 
519  $user_folder[] = array(
520  "title" => $key,
521  "type" => $row->type,
522  "obj_id" => $row->obj_id);
523  }
524 
525  $query = "SELECT * FROM $this->table_tree, $this->table_mail_obj_data ".
526  "WHERE $this->table_mail_obj_data.obj_id = $this->table_tree.child ".
527  "AND $this->table_tree.depth > '2' ".
528  "AND $this->table_tree.tree = ".$ilDB->quote($this->user_id)." ".
529  "ORDER BY $this->table_mail_obj_data.title";
530 
531  $res = $this->ilias->db->query($query);
532 
533  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
534  {
535  $user_folder[] = array(
536  "title" => stripslashes($row->title),
537  "type" => $row->type,
538  "obj_id" => $row->child);
539  }
540 
541  return $user_folder;
542  }
543 
549  function setUserId($a_user_id)
550  {
551  $this->user_id = $a_user_id;
552  }
553 
562  function delete()
563  {
564  global $ilDB;
565 
566  $q = "DELETE FROM mail_obj_data WHERE user_id=".$ilDB->quote($this->user_id)." ";
567  $this->ilias->db->query($q);
568 
569  $q = "DELETE FROM mail_options WHERE user_id= ".$ilDB->quote($this->user_id)." ";
570  $this->ilias->db->query($q);
571 
572  $q = "DELETE FROM mail_saved WHERE user_id= ".$ilDB->quote($this->user_id)." ";
573  $this->ilias->db->query($q);
574 
575  $q = "DELETE FROM mail_tree WHERE tree=".$ilDB->quote($this->user_id)." ";
576  $this->ilias->db->query($q);
577 
578  return true;
579  }
580 
589  {
590  global $ilDB;
591 
592  $tmp_user =& ilObjectFactory::getInstanceByObjId($this->user_id,false);
593 
594  $query = "UPDATE mail SET sender_id = '0',import_name = ".$ilDB->quote($tmp_user->getLogin())." ".
595  "WHERE sender_id = ".$ilDB->quote($this->user_id)." ";
596 
597  $this->ilias->db->query($query);
598 
599  return true;
600  }
601 
602 }
603 ?>