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

classes/class.ilMailbox.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 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 
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                 $this->lng->loadLanguageModule("mail");
00119                 $this->actions = array(
00120                         "move"        => $this->lng->txt("mail_move_to"),
00121                         "mark_read"   => $this->lng->txt("mail_mark_read"),
00122                         "mark_unread" => $this->lng->txt("mail_mark_unread"),
00123                         "delete"      => $this->lng->txt("delete"));
00124 
00125                 
00126                 // array contains basic folders and there lng translation for every new user
00127                 $this->default_folder = array(
00128                         "b_inbox"     => "inbox",
00129                         "c_trash"     => "trash",
00130                         "d_drafts"    => "drafts",
00131                         "e_sent"      => "sent",
00132                         "z_local"     => "local");
00133 
00134         }
00139         function getInboxFolder()
00140         {
00141                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00142                                  "WHERE user_id = '".$this->user_id."' ".
00143                                  "AND type = 'inbox'";
00144                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00145 
00146                 return $row->obj_id;
00147         }
00148 
00153         function getDraftsFolder()
00154         {
00155                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00156                                  "WHERE user_id = '".$this->user_id."' ".
00157                                  "AND type = 'drafts'";
00158                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00159 
00160                 return $row->obj_id;
00161         }
00162 
00167         function getTrashFolder()
00168         {
00169                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00170                                  "WHERE user_id = '".$this->user_id."' ".
00171                                  "AND type = 'trash'";
00172                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00173 
00174                 return $row->obj_id;
00175         }
00176 
00181         function getSentFolder()
00182         {
00183                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00184                                  "WHERE user_id = '".$this->user_id."' ".
00185                                  "AND type = 'sent'";
00186                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00187 
00188                 return $row->obj_id;
00189         }
00190 
00195         function getRootFolderId()
00196         {
00197                 return $this->mtree->getRootID($this->user_id);
00198         }
00199 
00207         function getActions($a_mobj_id)
00208         {
00209                 if ($a_mobj_id)
00210                 {
00211                         $folder_data = $this->getFolderData($a_mobj_id);
00212 
00213                         if ($folder_data["type"] == "user_folder" or $folder_data["type"] == "local")
00214                         {
00215                                 return array_merge($this->actions,array("add" => $this->lng->txt("mail_add_subfolder")));
00216                         }
00217                 }
00218 
00219                 return $this->actions;
00220         }
00221 
00229         function hasNewMail($a_user_id)
00230         {
00231                 global $ilias;
00232 
00233                 if (!$a_user_id)
00234                 {
00235                         return 0;
00236                 }
00237 
00238                 // CHECK FOR SYSTEM MAIL
00239                 $query = "SELECT mail_id FROM mail WHERE folder_id = 0 AND user_id = '".$a_user_id."' ".
00240                         "AND m_status = 'unread'";
00241 
00242                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00243                 
00244                 if($row->mail_id)
00245                 {
00246                         return $row->mail_id;
00247                 }
00248 
00249                 $query = "SELECT m.mail_id FROM mail AS m,mail_obj_data AS mo ".
00250                                  "WHERE m.user_id = mo.user_id ".
00251                                  "AND m.folder_id = mo.obj_id ".
00252                                  "AND mo.type = 'inbox' ".
00253                                  "AND m.user_id = '".$a_user_id."' ".
00254                                  "AND m.m_status = 'unread'";
00255                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00256 
00257                 // CHECK FOR SYSTEM MAIL
00258                 $query = "SELECT mail_id FROM mail WHERE folder_id = 0 AND user_id = '".$a_user_id."' ".
00259                         "AND m_status = 'unread'";
00260 
00261                 $row = $ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00262 
00263                 return $row ? $row->mail_id : 0;
00264         }
00265 
00270         function createDefaultFolder()
00271         {
00272                 $root_id = $this->getLastInsertId();
00273                 ++$root_id;
00274 
00275                 $query = "INSERT INTO $this->table_mail_obj_data ".
00276                                  "SET obj_id = '".$root_id."',".
00277                                  "user_id = '$this->user_id',".
00278                                  "title = 'a_root',".
00279                                  "type = 'root'";
00280                 $res = $this->ilias->db->query($query);
00281                 $this->mtree->addTree($this->user_id,$root_id);
00282                 
00283                 foreach ($this->default_folder as $key => $folder)
00284                 {
00285                         $last_id = $this->getLastInsertId();
00286                         ++$last_id;
00287 
00288                         $query = "INSERT INTO $this->table_mail_obj_data ".
00289                                          "SET obj_id = '".$last_id."',".
00290                                          "user_id = '$this->user_id',".
00291                                          "title = '$key',".
00292                                          "type = '$folder'";
00293                         $res = $this->ilias->db->query($query);
00294                         $this->mtree->insertNode($last_id,$root_id);
00295                 }
00296         }
00304         function addFolder($a_parent_id,$a_folder_name)
00305         {
00306                 if ($this->folderNameExists($a_folder_name))
00307                 {
00308                         return 0;
00309                 }
00310                 // ENTRY IN mail_obj_data
00311                 $query = "INSERT INTO $this->table_mail_obj_data ".
00312                                  "SET user_id = '$this->user_id',".
00313                                  "title = '".addslashes($a_folder_name)."',".
00314                                  "type = 'user_folder'";
00315                 $res = $this->ilias->db->query($query);
00316 
00317                 // ENTRY IN mail_tree
00318                 $new_id = $this->getLastInsertId();
00319                 $this->mtree->insertNode($new_id,$a_parent_id);
00320 
00321                 return $new_id;
00322         }
00323 
00331         function renameFolder($a_obj_id, $a_new_folder_name)
00332         {
00333                 if ($this->folderNameExists($a_new_folder_name))
00334                 {
00335                         return false;
00336                 }
00337 
00338                 $query = "UPDATE $this->table_mail_obj_data ".
00339                                  "SET title = '".addslashes($a_new_folder_name)."' ".
00340                                  "WHERE obj_id = '".$a_obj_id."'";
00341                 $res = $this->ilias->db->query($query);
00342                 
00343                 return true;
00344         }
00345 
00352         function folderNameExists($a_folder_name)
00353         {
00354                 $query = "SELECT obj_id FROM $this->table_mail_obj_data ".
00355                                  "WHERE user_id = '".$this->user_id."' ".
00356                                  "AND title = '".addslashes($a_folder_name)."'";
00357                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00358 
00359                 return $row->obj_id ? true : false;
00360         }
00361 
00367         function deleteFolder($a_folder_id)
00368         {
00369                 include_once("classes/class.ilMail.php");
00370                 $umail = new ilMail($this->user_id);
00371 
00372                 // SAVE SUBTREE DATA
00373                 $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($a_folder_id));
00374 
00375                 // DELETE ENTRY IN TREE
00376                 $this->mtree->deleteTree($this->mtree->getNodeData($a_folder_id));
00377 
00378                 // DELETE ENTRY IN mobj_data
00379                 foreach($subtree as $node)
00380                 {
00381                         // DELETE mail(s) of folder(s)
00382                         $mails = $umail->getMailsOfFolder($node["obj_id"]);
00383 
00384                         foreach ($mails as $mail)
00385                         {
00386                                 $mail_ids[] = $mail["mail_id"];
00387                         }
00388 
00389                         if (is_array($mail_ids))
00390                         {
00391                                 $umail->deleteMails($mail_ids);
00392                         }
00393 
00394                         // DELETE mobj_data entries
00395                         $query = "DELETE FROM $this->table_mail_obj_data ".
00396                                          "WHERE obj_id = '".$node["obj_id"]."'";
00397                         $res = $this->ilias->db->query($query);
00398                 }
00399 
00400                 return true;
00401         }
00402 
00403         // TODO: can be substituted by ilUtil::getLastInsertId
00404         function getLastInsertId()
00405         {
00406                 $query = "SELECT MAX(obj_id) FROM $this->table_mail_obj_data ";
00407                 $res = $this->ilias->db->query($query);
00408 
00409                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00410                 {
00411                         return $row["MAX(obj_id)"] ? $row["MAX(obj_id)"] : 0;
00412                 }
00413         }
00414         
00420         function getFolderData($a_obj_id)
00421         {
00422                 $query = "SELECT * FROM $this->table_mail_obj_data ".
00423                                  "WHERE user_id = '".$this->user_id."' ".
00424                                  "AND obj_id = '".$a_obj_id."'";
00425 
00426                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00427 
00428                 return array(
00429                                         "title"    => stripslashes($row->title),
00430                                         "type"     => $row->type
00431                                         );
00432         }
00438         function getParentFolderId($a_obj_id)
00439         {
00440                 $query = "SELECT * FROM $this->table_tree ".
00441                                  "WHERE child = '".$a_obj_id."'";
00442                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00443         
00444                 return $row->parent;
00445         }
00452         function getSubFolders($a_folder = 0,$a_folder_parent = 0)
00453         {
00454                 if (!$a_folder)
00455                 {
00456                         $a_folder = $this->getRootFolderId();
00457                 }
00458                 
00459                 foreach ($this->default_folder as $key => $value)
00460                 {
00461                         $query = "SELECT obj_id,type FROM $this->table_mail_obj_data ".
00462                                 "WHERE user_id = $this->user_id ".
00463                                 "AND title = '".$key."'";
00464                         $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00465                         
00466                         $user_folder[] = array(
00467                                 "title"    => $key,
00468                                 "type"     => $row->type,
00469                                 "obj_id"   => $row->obj_id);
00470                 } 
00471 
00472                 $query = "SELECT * FROM $this->table_tree, $this->table_mail_obj_data ".
00473                         "WHERE $this->table_mail_obj_data.obj_id = $this->table_tree.child ".
00474                         "AND $this->table_tree.depth > '2' ".
00475                         "AND $this->table_tree.tree = '".$this->user_id."' ".
00476                         "ORDER BY $this->table_mail_obj_data.title";
00477 
00478                 $res = $this->ilias->db->query($query);
00479 
00480                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00481                 {
00482                         $user_folder[] = array(
00483                                 "title"      => stripslashes($row->title),
00484                                 "type"    => $row->type,
00485                                 "obj_id"  => $row->child);
00486                 }
00487 
00488                 return $user_folder;
00489         }
00490 
00496         function setUserId($a_user_id)
00497         {
00498                 $this->user_id = $a_user_id;
00499         }
00500         
00509         function delete()
00510         {
00511                 $q = "DELETE FROM mail_obj_data WHERE user_id='".$this->user_id."'";
00512                 $this->ilias->db->query($q);
00513 
00514                 $q = "DELETE FROM mail_options WHERE user_id='".$this->user_id."'";
00515                 $this->ilias->db->query($q);
00516 
00517                 $q = "DELETE FROM mail_saved WHERE user_id='".$this->user_id."'";
00518                 $this->ilias->db->query($q);
00519 
00520                 $q = "DELETE FROM mail_tree WHERE tree='".$this->user_id."'";
00521                 $this->ilias->db->query($q);
00522                 
00523                 return true;
00524         }
00525 
00533         function updateMailsOfDeletedUser()
00534         {
00535                 $tmp_user =& ilObjectFactory::getInstanceByObjId($this->user_id,false);
00536 
00537                 $query = "UPDATE mail SET sender_id = '0',import_name = '".$tmp_user->getLogin()."' ".
00538                         "WHERE sender_id = '".$this->user_id."'";
00539 
00540                 $this->ilias->db->query($query);
00541 
00542                 return true;
00543         }
00544                 
00545 }
00546 ?>

Generated on Fri Dec 13 2013 09:06:34 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1