• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Mail/classes/class.ilMailbox.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00033 require_once("Services/Mail/classes/class.ilMail.php");
00034 
00035 class ilMailbox
00036 {
00042         var $ilias;
00043 
00049         var $lng;
00050 
00056         var $mtree;
00057 
00063         var $user_id;
00064 
00071         var $actions;
00072 
00078         var $default_folder;
00079 
00085         var $table_mail_obj_data;
00086 
00092         var $table_tree;
00093 
00099         function ilMailbox($a_user_id = 0)
00100         {
00101                 global $ilias,$lng;
00102 
00103 
00104                 $this->ilias = &$ilias;
00105                 $this->lng = &$lng;
00106                 $this->user_id = $a_user_id;
00107 
00108                 $this->table_mail_obj_data = 'mail_obj_data';
00109                 $this->table_tree = 'mail_tree';
00110 
00111                 if ($a_user_id)
00112                 {
00113                         $this->mtree = new ilTree($this->user_id);
00114                         $this->mtree->setTableNames($this->table_tree,$this->table_mail_obj_data);
00115                 }
00116 
00117                 // i added this, becaus if i create a new user automatically during
00118                 // CAS authentication, we have no $lng variable (alex, 16.6.2006)
00119                 // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
00120                 if (is_object($this->lng))
00121                 {
00122                         $this->lng->loadLanguageModule("mail");
00123 
00124                         $this->actions = array(
00125                                 "moveMails"        => $this->lng->txt("mail_move_to"),
00126                                 "markMailsRead"   => $this->lng->txt("mail_mark_read"),
00127                                 "markMailsUnread" => $this->lng->txt("mail_mark_unread"),
00128                                 "deleteMails"      => $this->lng->txt("delete"));
00129                 }
00130                 
00131                 // array contains basic folders and there lng translation for every new user
00132                 $this->default_folder = array(
00133                         "b_inbox"     => "inbox",
00134                         "c_trash"     => "trash",
00135                         "d_drafts"    => "drafts",
00136                         "e_sent"      => "sent",
00137                         "z_local"     => "local");
00138 
00139         }
00144         function getInboxFolder()
00145         {
00146                 global $ilDB;
00147                 
00148                 $query = "SELECT * FROM ".$this->table_mail_obj_data." ".
00149                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00150                                  "AND type = 'inbox'";
00151                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00152 
00153                 return $row->obj_id;
00154         }
00155 
00160         function getDraftsFolder()
00161         {
00162                 global $ilDB;
00163 
00164                 $query = "SELECT * FROM ".$this->table_mail_obj_data ." ".
00165                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00166                                  "AND type = 'drafts'";
00167                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00168 
00169                 return $row->obj_id;
00170         }
00171 
00176         function getTrashFolder()
00177         {
00178                 global $ilDB;
00179 
00180                 $query = "SELECT * FROM ".$this->table_mail_obj_data ." ".
00181                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00182                                  "AND type = 'trash'";
00183                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00184 
00185                 return $row->obj_id;
00186         }
00187 
00192         function getSentFolder()
00193         {
00194                 global $ilDB;
00195 
00196                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00197                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00198                                  "AND type = 'sent'";
00199                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00200 
00201                 return $row->obj_id;
00202         }
00203 
00208         function getRootFolderId()
00209         {
00210                 return $this->mtree->getRootID($this->user_id);
00211         }
00212 
00220         function getActions($a_mobj_id)
00221         {
00222                 if ($a_mobj_id)
00223                 {
00224                         $folder_data = $this->getFolderData($a_mobj_id);
00225 
00226                         if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
00227                         {
00228                                 #return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
00229                                 return $this->actions;
00230                         }
00231                 }
00232 
00233                 return $this->actions;
00234         }
00235 
00243         function hasNewMail($a_user_id)
00244         {
00245                 global $ilDB;
00246                 global $ilias;
00247 
00248                 if (!$a_user_id)
00249                 {
00250                         return 0;
00251                 }
00252 
00253                 // CHECK FOR SYSTEM MAIL
00254                 $query = "SELECT mail_id FROM mail WHERE folder_id = 0 AND user_id = ".$ilDB->quote($a_user_id)." ".
00255                         "AND m_status = 'unread'";
00256 
00257                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00258                 
00259                 if($row->mail_id)
00260                 {
00261                         return $row->mail_id;
00262                 }
00263 
00264                 $query = "SELECT m.mail_id FROM mail AS m,mail_obj_data AS mo ".
00265                                  "WHERE m.user_id = mo.user_id ".
00266                                  "AND m.folder_id = mo.obj_id ".
00267                                  "AND mo.type = 'inbox' ".
00268                                  "AND m.user_id = ".$ilDB->quote($a_user_id)." ".
00269                                  "AND m.m_status = 'unread'";
00270                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00271 
00272                 return $row ? $row->mail_id : 0;
00273         }
00274 
00282         function _countNewMails($a_user_id)
00283         {
00284                 global $ilDB;
00285                 global $ilias;
00286 
00287                 if (!$a_user_id)
00288                 {
00289                         return 0;
00290                 }
00291 
00292                 // CHECK FOR SYSTEM MAIL
00293                 $query = "SELECT count(*) as cnt FROM mail WHERE folder_id = 0 AND user_id = ".$ilDB->quote($a_user_id)." ".
00294                         "AND m_status = 'unread'";
00295 
00296                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00297 
00298                 $query = "SELECT count(*) as cnt FROM mail AS m,mail_obj_data AS mo ".
00299                                  "WHERE m.user_id = mo.user_id ".
00300                                  "AND m.folder_id = mo.obj_id ".
00301                                  "AND mo.type = 'inbox' ".
00302                                  "AND m.user_id = ".$ilDB->quote($a_user_id)." ".
00303                                  "AND m.m_status = 'unread'";
00304                 $row2 = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00305                 
00306                 return $row->cnt + $row2->cnt;
00307         }
00308 
00313         function createDefaultFolder()
00314         {
00315                 global $ilDB;
00316 
00317                 $root_id = $this->getLastInsertId();
00318                 ++$root_id;
00319 
00320                 $query = "INSERT INTO $this->table_mail_obj_data ".
00321                                  "SET obj_id = ".$ilDB->quote($root_id).",".
00322                                  "user_id = ".$ilDB->quote($this->user_id).", ".
00323                                  "title = 'a_root',".
00324                                  "type = 'root'";
00325                 $res = $this->ilias->db->query($query);
00326                 $this->mtree->addTree($this->user_id,$root_id);
00327                 
00328                 foreach ($this->default_folder as $key => $folder)
00329                 {
00330                         $last_id = $this->getLastInsertId();
00331                         ++$last_id;
00332 
00333                         $query = "INSERT INTO $this->table_mail_obj_data ".
00334                                          "SET obj_id = ".$ilDB->quote($last_id).", ".
00335                                          "user_id = ".$ilDB->quote($this->user_id).", ".
00336                                          "title = ".$ilDB->quote($key).", ".
00337                                          "type = ".$ilDB->quote($folder);
00338                         $res = $this->ilias->db->query($query);
00339                         $this->mtree->insertNode($last_id,$root_id);
00340                 }
00341         }
00349         function addFolder($a_parent_id,$a_folder_name)
00350         {
00351                 global $ilDB;
00352 
00353                 if ($this->folderNameExists($a_folder_name))
00354                 {
00355                         return 0;
00356                 }
00357                 // ENTRY IN mail_obj_data
00358                 $query = "INSERT INTO $this->table_mail_obj_data ".
00359                                  "SET user_id = ".$ilDB->quote($this->user_id).", ".
00360                                  "title = ".$ilDB->quote($a_folder_name).",".
00361                                  "type = 'user_folder'";
00362                 $res = $this->ilias->db->query($query);
00363 
00364                 // ENTRY IN mail_tree
00365                 $new_id = $this->getLastInsertId();
00366                 $this->mtree->insertNode($new_id,$a_parent_id);
00367 
00368                 return $new_id;
00369         }
00370 
00378         function renameFolder($a_obj_id, $a_new_folder_name)
00379         {
00380                 global $ilDB;
00381 
00382                 if ($this->folderNameExists($a_new_folder_name))
00383                 {
00384                         return false;
00385                 }
00386 
00387                 $query = "UPDATE $this->table_mail_obj_data ".
00388                                  "SET title = ".$ilDB->quote($a_new_folder_name)." ".
00389                                  "WHERE obj_id = ".$ilDB->quote($a_obj_id)." ";
00390                 $res = $this->ilias->db->query($query);
00391                 
00392                 return true;
00393         }
00394 
00401         function folderNameExists($a_folder_name)
00402         {
00403                 global $ilDB;
00404 
00405                 $query = "SELECT obj_id FROM $this->table_mail_obj_data ".
00406                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00407                                  "AND title = ".$ilDB->quote($a_folder_name)." ";
00408                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00409 
00410                 return $row->obj_id ? true : false;
00411         }
00412 
00418         function deleteFolder($a_folder_id)
00419         {
00420                 global $ilDB;
00421 
00422                 include_once("Services/Mail/classes/class.ilMail.php");
00423                 $umail = new ilMail($this->user_id);
00424 
00425                 // SAVE SUBTREE DATA
00426                 $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
00427 
00428                 // DELETE ENTRY IN TREE
00429                 $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
00430 
00431                 // DELETE ENTRY IN mobj_data
00432                 foreach($subtree as $node)
00433                 {
00434                         // DELETE mail(s) of folder(s)
00435                         $mails = $umail->getMailsOfFolder($node["obj_id"]);
00436 
00437                         foreach ($mails as $mail)
00438                         {
00439                                 $mail_ids[] = $mail["mail_id"];
00440                         }
00441 
00442                         if (is_array($mail_ids))
00443                         {
00444                                 $umail->deleteMails($mail_ids);
00445                         }
00446 
00447                         // DELETE mobj_data entries
00448                         $query = "DELETE FROM $this->table_mail_obj_data ".
00449                                          "WHERE obj_id = ".$ilDB->quote($node["obj_id"])." ";
00450                         $res = $this->ilias->db->query($query);
00451                 }
00452 
00453                 return true;
00454         }
00455 
00456         // TODO: can be substituted by ilUtil::getLastInsertId
00457         function getLastInsertId()
00458         {
00459                 global $ilDB;
00460 
00461                 $query = "SELECT MAX(obj_id) FROM $this->table_mail_obj_data ";
00462                 $res = $this->ilias->db->query($query);
00463 
00464                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00465                 {
00466                         return $row["MAX(obj_id)"] ? $row["MAX(obj_id)"] : 0;
00467                 }
00468         }
00469         
00475         function getFolderData($a_obj_id)
00476         {
00477                 global $ilDB;
00478 
00479                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00480                                  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00481                                  "AND obj_id = ".$ilDB->quote($a_obj_id)." ";
00482 
00483                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00484 
00485                 return array(
00486                                         "title"    => stripslashes($row->title),
00487                                         "type"     => $row->type
00488                                         );
00489         }
00495         function getParentFolderId($a_obj_id)
00496         {
00497                 global $ilDB;
00498 
00499                 $query = "SELECT * FROM $this->table_tree ".
00500                                  "WHERE child = ".$ilDB->quote($a_obj_id)." ";
00501                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00502         
00503                 return $row->parent;
00504         }
00511         function getSubFolders($a_folder = 0,$a_folder_parent = 0)
00512         {
00513                 global $ilDB;
00514 
00515                 if (!$a_folder)
00516                 {
00517                         $a_folder = $this->getRootFolderId();
00518                 }
00519                 
00520                 foreach ($this->default_folder as $key => $value)
00521                 {
00522                         $query = "SELECT obj_id,type FROM $this->table_mail_obj_data ".
00523                                 "WHERE user_id = ".$ilDB->quote($this->user_id). " ".
00524                                 "AND title = ".$ilDB->quote($key)." ";
00525                         $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00526                         
00527                         $user_folder[] = array(
00528                                 "title"    => $key,
00529                                 "type"     => $row->type,
00530                                 "obj_id"   => $row->obj_id);
00531                 } 
00532 
00533                 $query = "SELECT * FROM $this->table_tree, $this->table_mail_obj_data ".
00534                         "WHERE $this->table_mail_obj_data.obj_id = $this->table_tree.child ".
00535                         "AND $this->table_tree.depth > '2' ".
00536                         "AND $this->table_tree.tree = ".$ilDB->quote($this->user_id)." ".
00537                         "ORDER BY $this->table_mail_obj_data.title";
00538 
00539                 $res = $this->ilias->db->query($query);
00540 
00541                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00542                 {
00543                         $user_folder[] = array(
00544                                 "title"      => stripslashes($row->title),
00545                                 "type"    => $row->type,
00546                                 "obj_id"  => $row->child);
00547                 }
00548 
00549                 return $user_folder;
00550         }
00551 
00557         function setUserId($a_user_id)
00558         {
00559                 $this->user_id = $a_user_id;
00560         }
00561         
00570         function delete()
00571         {
00572                 global $ilDB;
00573 
00574                 $q = "DELETE FROM mail_obj_data WHERE user_id=".$ilDB->quote($this->user_id)." ";
00575                 $this->ilias->db->query($q);
00576 
00577                 $q = "DELETE FROM mail_options WHERE user_id= ".$ilDB->quote($this->user_id)." ";
00578                 $this->ilias->db->query($q);
00579 
00580                 $q = "DELETE FROM mail_saved WHERE user_id= ".$ilDB->quote($this->user_id)." ";
00581                 $this->ilias->db->query($q);
00582 
00583                 $q = "DELETE FROM mail_tree WHERE tree=".$ilDB->quote($this->user_id)." ";
00584                 $this->ilias->db->query($q);
00585                 
00586                 return true;
00587         }
00588 
00596         function updateMailsOfDeletedUser()
00597         {
00598                 global $ilDB;
00599 
00600                 $tmp_user =& ilObjectFactory::getInstanceByObjId($this->user_id,false);
00601 
00602                 $query = "UPDATE mail SET sender_id = '0',import_name = ".$ilDB->quote($tmp_user->getLogin())." ".
00603                         "WHERE sender_id = ".$ilDB->quote($this->user_id)." ";
00604 
00605                 $this->ilias->db->query($query);
00606 
00607                 return true;
00608         }
00609                 
00610 }
00611 ?>

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1