ILIAS  Release_4_2_x_branch Revision 61807
 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  $res = $ilDB->queryf('
149  SELECT * FROM '.$this->table_mail_obj_data.'
150  WHERE user_id = %s
151  AND m_type = %s',
152  array('integer', 'text'),
153  array($this->user_id, 'inbox'));
154 
155  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
156 
157  return $row->obj_id;
158  }
159 
164  function getDraftsFolder()
165  {
166  global $ilDB;
167 
168  $res = $ilDB->queryf('
169  SELECT * FROM '.$this->table_mail_obj_data.'
170  WHERE user_id = %s
171  AND m_type = %s',
172  array('integer', 'text'),
173  array($this->user_id, 'drafts'));
174 
175  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
176 
177 
178  return $row->obj_id;
179  }
180 
185  function getTrashFolder()
186  {
187  global $ilDB;
188 
189  $res = $ilDB->queryf('
190  SELECT * FROM '.$this->table_mail_obj_data.'
191  WHERE user_id = %s
192  AND m_type = %s',
193  array('integer', 'text'),
194  array($this->user_id, 'trash'));
195 
196  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
197 
198  return $row->obj_id;
199  }
200 
205  function getSentFolder()
206  {
207  global $ilDB;
208 
209  $res = $ilDB->queryf('
210  SELECT * FROM '.$this->table_mail_obj_data.'
211  WHERE user_id = %s
212  AND m_type = %s',
213  array('integer', 'text'),
214  array($this->user_id, 'sent'));
215 
216  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
217 
218 
219  return $row->obj_id;
220  }
221 
226  function getRootFolderId()
227  {
228  return $this->mtree->getRootID($this->user_id);
229  }
230 
238  function getActions($a_mobj_id)
239  {
240  if ($a_mobj_id)
241  {
242  $folder_data = $this->getFolderData($a_mobj_id);
243 
244  if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
245  {
246  #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
247  return $this->actions;
248  }
249  }
250 
251  return $this->actions;
252  }
253 
261  function _countNewMails($a_user_id)
262  {
263  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
265  }
266 
272  {
273  global $ilDB;
274 
275 /* $root_id = $this->getLastInsertId();
276  ++$root_id;
277 */
278  $root_id = $ilDB->nextId($this->table_mail_obj_data);
279 
280  $res = $ilDB->manipulateF('
281  INSERT INTO '. $this->table_mail_obj_data .'
282  ( obj_id,
283  user_id,
284  title,
285  m_type
286  )
287  VALUES( %s, %s, %s, %s)',
288  array('integer','integer', 'text', 'text'),
289  array($root_id, $this->user_id, 'a_root', 'root'));
290 
291  $this->mtree->addTree($this->user_id,$root_id);
292 
293  foreach ($this->default_folder as $key => $folder)
294  {
295  /*$last_id = $this->getLastInsertId();
296  ++$last_id;
297  */
298  $last_id = $ilDB->nextId($this->table_mail_obj_data);
299  $statement = $ilDB->manipulateF('
300  INSERT INTO '. $this->table_mail_obj_data .'
301  ( obj_id,
302  user_id,
303  title,
304  m_type
305  )
306  VALUES( %s, %s, %s, %s)',
307  array('integer','integer', 'text', 'text'),
308  array($last_id,$this->user_id, $key, $folder));
309 
310  $this->mtree->insertNode($last_id,$root_id);
311  }
312  }
320  function addFolder($a_parent_id,$a_folder_name)
321  {
322  global $ilDB;
323 
324  if ($this->folderNameExists($a_folder_name))
325  {
326  return 0;
327  }
328  // ENTRY IN mail_obj_data
329  $next_id = $ilDB->nextId($this->table_mail_obj_data);
330  $statement = $ilDB->manipulateF('
331  INSERT INTO '. $this->table_mail_obj_data .'
332  ( obj_id,
333  user_id,
334  title,
335  m_type
336  )
337  VALUES(%s,%s,%s,%s)',
338  array('integer','integer', 'text', 'text'),
339  array($next_id, $this->user_id, $a_folder_name, 'user_folder'));
340 
341  // ENTRY IN mail_tree
342  $this->mtree->insertNode($next_id,$a_parent_id);
343  return $next_id;
344  }
345 
353  function renameFolder($a_obj_id, $a_new_folder_name)
354  {
355  global $ilDB;
356 
357  if ($this->folderNameExists($a_new_folder_name))
358  {
359  return false;
360  }
361 
362  $statement = $ilDB->manipulateF('
363  UPDATE '. $this->table_mail_obj_data .'
364  SET title = %s
365  WHERE obj_id = %s',
366  array('text', 'integer'),
367  array($a_new_folder_name, $a_obj_id));
368 
369  return true;
370  }
371 
378  function folderNameExists($a_folder_name)
379  {
380  global $ilDB;
381 
382  $res = $ilDB->queryf('
383  SELECT obj_id FROM '. $this->table_mail_obj_data .'
384  WHERE user_id = %s
385  AND title = %s',
386  array('integer', 'text'),
387  array($this->user_id, $a_folder_name));
388 
389  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
390 
391  return $row->obj_id ? true : false;
392  }
393 
399  function deleteFolder($a_folder_id)
400  {
401  global $ilDB;
402 
403  $query = $ilDB->queryf('
404  SELECT title FROM mail_obj_data
405  WHERE obj_id = %s',
406  array('integer'),
407  array($a_folder_id)
408  );
409 
410  $row = $ilDB->fetchAssoc($query);
411 
412  if( array_key_exists($row['title'], $this->default_folder) )
413  {
414  return false;
415  }
416 
417  include_once("Services/Mail/classes/class.ilMail.php");
418  $umail = new ilMail($this->user_id);
419 
420  // SAVE SUBTREE DATA
421  $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
422 
423  // DELETE ENTRY IN TREE
424  $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
425 
426  // DELETE ENTRY IN mobj_data
427  foreach($subtree as $node)
428  {
429  // DELETE mail(s) of folder(s)
430  $mails = $umail->getMailsOfFolder($node["obj_id"]);
431 
432  foreach ($mails as $mail)
433  {
434  $mail_ids[] = $mail["mail_id"];
435  }
436 
437  if (is_array($mail_ids))
438  {
439  $umail->deleteMails($mail_ids);
440  }
441 
442  // DELETE mobj_data entries
443  $statement = $ilDB->manipulateF('
444  DELETE FROM '. $this->table_mail_obj_data .'
445  WHERE obj_id = %s',
446  array('integer'),
447  array($node['obj_id']));
448  }
449 
450  return true;
451  }
452 
453  // DONE: can be substituted by ilUtil::getLastInsertId
454  function getLastInsertId()
455  {
456  global $ilDB;
457 
458  return $ilDB->getLastInsertId();
459  }
460 
466  function getFolderData($a_obj_id)
467  {
468  global $ilDB;
469 
470  $res = $ilDB->queryf('
471  SELECT * FROM '. $this->table_mail_obj_data .'
472  WHERE user_id = %s
473  AND obj_id = %s',
474  array('integer', 'integer'),
475  array($this->user_id, $a_obj_id));
476 
477  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
478 
479  return array(
480  "title" => stripslashes($row->title),
481  "type" => $row->m_type
482  );
483  }
489  function getParentFolderId($a_obj_id)
490  {
491  global $ilDB;
492 
493  $res = $ilDB->queryf('
494  SELECT * FROM '. $this->table_tree .'
495  WHERE child = %s',
496  array('integer'),
497  array($a_obj_id));
498 
499  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
500 
501 
502  return $row->parent;
503  }
510  function getSubFolders($a_folder = 0,$a_folder_parent = 0)
511  {
512 
513  global $ilDB;
514 
515  if (!$a_folder)
516  {
517  $a_folder = $this->getRootFolderId();
518  }
519 
520  foreach ($this->default_folder as $key => $value)
521  {
522  $res = $ilDB->queryf('
523  SELECT obj_id,m_type FROM '. $this->table_mail_obj_data .'
524  WHERE user_id = %s
525  AND title = %s',
526  array('integer', 'text'),
527  array($this->user_id, $key));
528 
529  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
530 
531  $user_folder[] = array(
532  "title" => $key,
533  "type" => $row->m_type,
534  "obj_id" => $row->obj_id);
535  }
536 
537  $res = $ilDB->queryf('
538  SELECT * FROM '. $this->table_tree. ', '. $this->table_mail_obj_data .'
539  WHERE '. $this->table_mail_obj_data.'.obj_id = '. $this->table_tree.'.child
540  AND '. $this->table_tree.'.depth > %s
541  AND '. $this->table_tree.'.tree = %s
542  ORDER BY '. $this->table_mail_obj_data.'.title ',
543  array('integer', 'integer'),
544  array('2', $this->user_id));
545 
546  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
547  {
548  $user_folder[] = array(
549  "title" => stripslashes($row->title),
550  "type" => $row->m_type,
551  "obj_id" => $row->child);
552  }
553 
554  return $user_folder;
555  }
556 
562  function setUserId($a_user_id)
563  {
564  $this->user_id = $a_user_id;
565  }
566 
575  function delete()
576  {
577  global $ilDB;
578 
579  $ilDB->manipulateF('
580  DELETE FROM mail_obj_data WHERE user_id = %s',
581  array('integer'), array($this->user_id)
582  );
583 
584  $ilDB->manipulateF('
585  DELETE FROM mail_options WHERE user_id = %s',
586  array('integer'), array($this->user_id)
587  );
588 
589  $ilDB->manipulateF('
590  DELETE FROM mail_saved WHERE user_id = %s',
591  array('integer'), array($this->user_id)
592  );
593 
594  $ilDB->manipulateF('
595  DELETE FROM mail_tree WHERE tree = %s',
596  array('integer'), array($this->user_id)
597  );
598 
599  return true;
600  }
601 
609  public function updateMailsOfDeletedUser($nameToShow)
610  {
614  global $ilDB;
615 
616  $ilDB->manipulateF('
617  UPDATE mail
618  SET sender_id = %s,
619  import_name = %s
620  WHERE sender_id = %s',
621  array('integer', 'text', 'integer'),
622  array(0, $nameToShow, $this->user_id));
623  }
624 }
625 ?>