00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00034 require_once("./classes/class.ilMail.php");
00035
00036 class ilMailbox
00037 {
00043 var $ilias;
00044
00050 var $lng;
00051
00057 var $mtree;
00058
00064 var $user_id;
00065
00072 var $actions;
00073
00079 var $default_folder;
00080
00086 var $table_mail_obj_data;
00087
00093 var $table_tree;
00094
00100 function ilMailbox($a_user_id = 0)
00101 {
00102 global $ilias,$lng;
00103
00104
00105 $this->ilias = &$ilias;
00106 $this->lng = &$lng;
00107 $this->user_id = $a_user_id;
00108
00109 $this->table_mail_obj_data = 'mail_obj_data';
00110 $this->table_tree = 'mail_tree';
00111
00112 if ($a_user_id)
00113 {
00114 $this->mtree = new ilTree($this->user_id);
00115 $this->mtree->setTableNames($this->table_tree,$this->table_mail_obj_data);
00116 }
00117
00118
00119
00120
00121 if (is_object($this->lng))
00122 {
00123 $this->lng->loadLanguageModule("mail");
00124
00125 $this->actions = array(
00126 "move" => $this->lng->txt("mail_move_to"),
00127 "mark_read" => $this->lng->txt("mail_mark_read"),
00128 "mark_unread" => $this->lng->txt("mail_mark_unread"),
00129 "delete" => $this->lng->txt("delete"));
00130 }
00131
00132
00133 $this->default_folder = array(
00134 "b_inbox" => "inbox",
00135 "c_trash" => "trash",
00136 "d_drafts" => "drafts",
00137 "e_sent" => "sent",
00138 "z_local" => "local");
00139
00140 }
00145 function getInboxFolder()
00146 {
00147 $query = "SELECT * FROM $this->table_mail_obj_data ".
00148 "WHERE user_id = '".$this->user_id."' ".
00149 "AND type = 'inbox'";
00150 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00151
00152 return $row->obj_id;
00153 }
00154
00159 function getDraftsFolder()
00160 {
00161 $query = "SELECT * FROM $this->table_mail_obj_data ".
00162 "WHERE user_id = '".$this->user_id."' ".
00163 "AND type = 'drafts'";
00164 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00165
00166 return $row->obj_id;
00167 }
00168
00173 function getTrashFolder()
00174 {
00175 $query = "SELECT * FROM $this->table_mail_obj_data ".
00176 "WHERE user_id = '".$this->user_id."' ".
00177 "AND type = 'trash'";
00178 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00179
00180 return $row->obj_id;
00181 }
00182
00187 function getSentFolder()
00188 {
00189 $query = "SELECT * FROM $this->table_mail_obj_data ".
00190 "WHERE user_id = '".$this->user_id."' ".
00191 "AND type = 'sent'";
00192 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00193
00194 return $row->obj_id;
00195 }
00196
00201 function getRootFolderId()
00202 {
00203 return $this->mtree->getRootID($this->user_id);
00204 }
00205
00213 function getActions($a_mobj_id)
00214 {
00215 if ($a_mobj_id)
00216 {
00217 $folder_data = $this->getFolderData($a_mobj_id);
00218
00219 if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
00220 {
00221 return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
00222 }
00223 }
00224
00225 return $this->actions;
00226 }
00227
00235 function hasNewMail($a_user_id)
00236 {
00237 global $ilias;
00238
00239 if (!$a_user_id)
00240 {
00241 return 0;
00242 }
00243
00244
00245 $query = "SELECT mail_id FROM mail WHERE folder_id = 0 AND user_id = '".$a_user_id."' ".
00246 "AND m_status = 'unread'";
00247
00248 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00249
00250 if($row->mail_id)
00251 {
00252 return $row->mail_id;
00253 }
00254
00255 $query = "SELECT m.mail_id FROM mail AS m,mail_obj_data AS mo ".
00256 "WHERE m.user_id = mo.user_id ".
00257 "AND m.folder_id = mo.obj_id ".
00258 "AND mo.type = 'inbox' ".
00259 "AND m.user_id = '".$a_user_id."' ".
00260 "AND m.m_status = 'unread'";
00261 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00262
00263 return $row ? $row->mail_id : 0;
00264 }
00265
00273 function _countNewMails($a_user_id)
00274 {
00275 global $ilias;
00276
00277 if (!$a_user_id)
00278 {
00279 return 0;
00280 }
00281
00282
00283 $query = "SELECT count(*) as cnt FROM mail WHERE folder_id = 0 AND user_id = '".$a_user_id."' ".
00284 "AND m_status = 'unread'";
00285
00286 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00287
00288 $query = "SELECT count(*) as cnt FROM mail AS m,mail_obj_data AS mo ".
00289 "WHERE m.user_id = mo.user_id ".
00290 "AND m.folder_id = mo.obj_id ".
00291 "AND mo.type = 'inbox' ".
00292 "AND m.user_id = '".$a_user_id."' ".
00293 "AND m.m_status = 'unread'";
00294 $row2 = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00295
00296 return $row->cnt + $row2->cnt;
00297 }
00298
00303 function createDefaultFolder()
00304 {
00305 $root_id = $this->getLastInsertId();
00306 ++$root_id;
00307
00308 $query = "INSERT INTO $this->table_mail_obj_data ".
00309 "SET obj_id = '".$root_id."',".
00310 "user_id = '$this->user_id',".
00311 "title = 'a_root',".
00312 "type = 'root'";
00313 $res = $this->ilias->db->query($query);
00314 $this->mtree->addTree($this->user_id,$root_id);
00315
00316 foreach ($this->default_folder as $key => $folder)
00317 {
00318 $last_id = $this->getLastInsertId();
00319 ++$last_id;
00320
00321 $query = "INSERT INTO $this->table_mail_obj_data ".
00322 "SET obj_id = '".$last_id."',".
00323 "user_id = '$this->user_id',".
00324 "title = '$key',".
00325 "type = '$folder'";
00326 $res = $this->ilias->db->query($query);
00327 $this->mtree->insertNode($last_id,$root_id);
00328 }
00329 }
00337 function addFolder($a_parent_id,$a_folder_name)
00338 {
00339 if ($this->folderNameExists($a_folder_name))
00340 {
00341 return 0;
00342 }
00343
00344 $query = "INSERT INTO $this->table_mail_obj_data ".
00345 "SET user_id = '$this->user_id',".
00346 "title = '".addslashes($a_folder_name)."',".
00347 "type = 'user_folder'";
00348 $res = $this->ilias->db->query($query);
00349
00350
00351 $new_id = $this->getLastInsertId();
00352 $this->mtree->insertNode($new_id,$a_parent_id);
00353
00354 return $new_id;
00355 }
00356
00364 function renameFolder($a_obj_id, $a_new_folder_name)
00365 {
00366 if ($this->folderNameExists($a_new_folder_name))
00367 {
00368 return false;
00369 }
00370
00371 $query = "UPDATE $this->table_mail_obj_data ".
00372 "SET title = '".addslashes($a_new_folder_name)."' ".
00373 "WHERE obj_id = '".$a_obj_id."'";
00374 $res = $this->ilias->db->query($query);
00375
00376 return true;
00377 }
00378
00385 function folderNameExists($a_folder_name)
00386 {
00387 $query = "SELECT obj_id FROM $this->table_mail_obj_data ".
00388 "WHERE user_id = '".$this->user_id."' ".
00389 "AND title = '".addslashes($a_folder_name)."'";
00390 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00391
00392 return $row->obj_id ? true : false;
00393 }
00394
00400 function deleteFolder($a_folder_id)
00401 {
00402 include_once("classes/class.ilMail.php");
00403 $umail = new ilMail($this->user_id);
00404
00405
00406 $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
00407
00408
00409 $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
00410
00411
00412 foreach($subtree as $node)
00413 {
00414
00415 $mails = $umail->getMailsOfFolder($node["obj_id"]);
00416
00417 foreach ($mails as $mail)
00418 {
00419 $mail_ids[] = $mail["mail_id"];
00420 }
00421
00422 if (is_array($mail_ids))
00423 {
00424 $umail->deleteMails($mail_ids);
00425 }
00426
00427
00428 $query = "DELETE FROM $this->table_mail_obj_data ".
00429 "WHERE obj_id = '".$node["obj_id"]."'";
00430 $res = $this->ilias->db->query($query);
00431 }
00432
00433 return true;
00434 }
00435
00436
00437 function getLastInsertId()
00438 {
00439 $query = "SELECT MAX(obj_id) FROM $this->table_mail_obj_data ";
00440 $res = $this->ilias->db->query($query);
00441
00442 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00443 {
00444 return $row["MAX(obj_id)"] ? $row["MAX(obj_id)"] : 0;
00445 }
00446 }
00447
00453 function getFolderData($a_obj_id)
00454 {
00455 $query = "SELECT * FROM $this->table_mail_obj_data ".
00456 "WHERE user_id = '".$this->user_id."' ".
00457 "AND obj_id = '".$a_obj_id."'";
00458
00459 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00460
00461 return array(
00462 "title" => stripslashes($row->title),
00463 "type" => $row->type
00464 );
00465 }
00471 function getParentFolderId($a_obj_id)
00472 {
00473 $query = "SELECT * FROM $this->table_tree ".
00474 "WHERE child = '".$a_obj_id."'";
00475 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00476
00477 return $row->parent;
00478 }
00485 function getSubFolders($a_folder = 0,$a_folder_parent = 0)
00486 {
00487 if (!$a_folder)
00488 {
00489 $a_folder = $this->getRootFolderId();
00490 }
00491
00492 foreach ($this->default_folder as $key => $value)
00493 {
00494 $query = "SELECT obj_id,type FROM $this->table_mail_obj_data ".
00495 "WHERE user_id = $this->user_id ".
00496 "AND title = '".$key."'";
00497 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00498
00499 $user_folder[] = array(
00500 "title" => $key,
00501 "type" => $row->type,
00502 "obj_id" => $row->obj_id);
00503 }
00504
00505 $query = "SELECT * FROM $this->table_tree, $this->table_mail_obj_data ".
00506 "WHERE $this->table_mail_obj_data.obj_id = $this->table_tree.child ".
00507 "AND $this->table_tree.depth > '2' ".
00508 "AND $this->table_tree.tree = '".$this->user_id."' ".
00509 "ORDER BY $this->table_mail_obj_data.title";
00510
00511 $res = $this->ilias->db->query($query);
00512
00513 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00514 {
00515 $user_folder[] = array(
00516 "title" => stripslashes($row->title),
00517 "type" => $row->type,
00518 "obj_id" => $row->child);
00519 }
00520
00521 return $user_folder;
00522 }
00523
00529 function setUserId($a_user_id)
00530 {
00531 $this->user_id = $a_user_id;
00532 }
00533
00542 function delete()
00543 {
00544 $q = "DELETE FROM mail_obj_data WHERE user_id='".$this->user_id."'";
00545 $this->ilias->db->query($q);
00546
00547 $q = "DELETE FROM mail_options WHERE user_id='".$this->user_id."'";
00548 $this->ilias->db->query($q);
00549
00550 $q = "DELETE FROM mail_saved WHERE user_id='".$this->user_id."'";
00551 $this->ilias->db->query($q);
00552
00553 $q = "DELETE FROM mail_tree WHERE tree='".$this->user_id."'";
00554 $this->ilias->db->query($q);
00555
00556 return true;
00557 }
00558
00566 function updateMailsOfDeletedUser()
00567 {
00568 $tmp_user =& ilObjectFactory::getInstanceByObjId($this->user_id,false);
00569
00570 $query = "UPDATE mail SET sender_id = '0',import_name = '".$tmp_user->getLogin()."' ".
00571 "WHERE sender_id = '".$this->user_id."'";
00572
00573 $this->ilias->db->query($query);
00574
00575 return true;
00576 }
00577
00578 }
00579 ?>