<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Folders
// Moregroupware.org

$myEnv["module"] = "webmail2";
$myEnv["stdsmenu"] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
include(WM2_INC_DIR.'folders_inc.php');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$MGW = $_SESSION["MGW"];
$user_id = $MGW->userid;

$myvars = array("subfolder","action","account");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

if ($action == "add" and $subfolder!="" and $account!="") {
  $sql = "select id from mgw_webmail2_sidebar where mbox_id=".(int)$subfolder." and account=".(int)$account." and user_id=".(int)$user_id;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $messagecount = $res->RecordCount();
  if ($messagecount == 0) {
  
    $mboxname = "";
    if ($account != 0) {
      include CACHE_FOLDER."imap/tree/".$account.".php";
      foreach ($folders as $folder) {
        if ($folder[1] == $subfolder) {
          $mboxname = $folder[7];
        }
      }
    }

    $sql = "insert into mgw_webmail2_sidebar (id, user_id, account, mbox_id,mboxname) values (".mgw_genID('mgw__seq_sidebar').",".(int)$user_id.",".(int)$account.",".(int)$subfolder.",'".$mboxname."')";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    $sql = "update mgw_webmail2_mailboxes set isinsidebar='1' where mbox_id=".(int)$subfolder;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  }
}
if ($action == "remove" and $subfolder!="" and $account!="") {
  $sql = "delete from mgw_webmail2_sidebar where user_id=".(int)$user_id." and account=".(int)$account." and mbox_id=".(int)$subfolder;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  $sql = "select id from mgw_webmail2_sidebar where account=".(int)$account." and mbox_id=".(int)$subfolder;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $messagecount = $res->RecordCount();
  if ($messagecount == 0) {
    $sql = "update mgw_webmail2_mailboxes set isinsidebar='0' where mbox_id=".(int)$subfolder;
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  }
}


$mbox_id_inbox = getboxid("inbox",0,$user_id);
$mbox_id_draft = getboxid("draft",0,$user_id);
$mbox_id_sent = getboxid("sent",0,$user_id);
$mbox_id_trash = getboxid("trash",0,$user_id);

$cnt_inbox = getmessagecount_unread2($mbox_id_inbox);
$cnt_draft = getmessagecount_unread2($mbox_id_draft);
$cnt_sent = getmessagecount_unread2($mbox_id_sent);
$cnt_trash = getmessagecount_unread2($mbox_id_trash);

$inbox = mboxname_replace("inbox");
$draft = mboxname_replace("draft");
$sent = mboxname_replace("sent");
$trash = mboxname_replace("trash");

$inbox_link = "parent.rightframe.location=\"index2.php?account=0&mbox_id=".(int)$mbox_id_inbox."&".SID."\";";
$compose_link = "parent.rightframe.location=\"sendmail.php?".SID."\";";
$folders_link = "parent.rightframe.location=\"myfolders.php?mode=my&".SID."\";";
$folders2_link = "parent.rightframe.location=\"myfolders.php?mode=public&".SID."\";";

$draft_link = "parent.rightframe.location=\"index2.php?account=0&mbox_id=".(int)$mbox_id_draft."&".SID."\";";
$sent_link = "parent.rightframe.location=\"index2.php?account=0&mbox_id=".(int)$mbox_id_sent."&".SID."\";";
$trash_link = "parent.rightframe.location=\"index2.php?account=0&mbox_id=".(int)$mbox_id_trash."&".SID."\";";

$buttons = array();
$buttons[] ="p.addButton('templates/default/media/n_mail-newmail.gif','".$hashmap["webmail2_func_new_message"]."','".$compose_link."');\n";
$buttons[] ="p.addButton('templates/default/media/n_mail-inbox.gif','".$inbox." ".$cnt_inbox."','".$inbox_link."');\n";
$buttons[] ="p.addButton('templates/default/media/n_mail-ordner2.gif','".$hashmap["myfolders_folders"]."','".$folders_link."');\n";
$buttons[] ="p.addButton('templates/default/media/n_mail-drafts.gif','".$draft." ".$cnt_draft."','".$draft_link."');\n";
$buttons[] ="p.addButton('templates/default/media/n_mail-sent.gif','".$sent." ".$cnt_sent."','".$sent_link."');\n";
$buttons[] ="p.addButton('templates/default/media/n_mail-deleted.gif','".$trash." ".$cnt_trash."','".$trash_link."');\n";

if (!isset($MGW->settings["wm2_disable_public_folders"]) or !$MGW->settings["wm2_disable_public_folders"]) {
  $buttons[] ="p.addButton('templates/default/media/n_mail-ordner.gif','".$hashmap["publicfolders_folders"]."','".$folders2_link."');\n";
}

$smarty->assign("buttons2",$buttons);

$sql = "select * from mgw_webmail2_sidebar where user_id=".(int)$user_id." order by id";
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
	  
$buttons = array();
while($row = $res->FetchRow()) {

  if ($row["account"]==0) {
    $sql = "select user_id,mbox_protect,mboxname from mgw_webmail2_mailboxes where mbox_id=".(int)$row["mbox_id"];
    if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
    $row2 = $res2->FetchRow();

    if ($row2["mboxname"] != "") {
      if ($row2["mbox_protect"]==1) {$row2["mboxname"] = mboxname_replace($row2["mboxname"]); }
      $buttons[] ="p.addButton('templates/default/media/n_mail-ordner2.gif','".addslashes($row2["mboxname"])."','parent.rightframe.location=\"index2.php?account=".(int)$row["account"]."&mbox_id=".(int)$row["mbox_id"]."&".SID."\"');\n";
    }
  } else {
    include CACHE_FOLDER."imap/tree/".$row["account"].".php";
    foreach ($folders as $folder) {
      if ($folder[7] == $row["mboxname"]) {
        if ($folder[1]!=$row["mbox_id"]) {
          $sql = "update mgw_webmail2_sidebar set mbox_id='".(int)$folder[1]."' where mboxname='".$row["mboxname"]."' and account=".(int)$row["account"];
          if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
        }
        $mboxname = $folder[2];
        $buttons[] ="p.addButton('templates/default/media/n_mail-ordner2.gif','".$mboxname."','parent.rightframe.location=\"index2.php?account=".(int)$row["account"]."&mbox_id=".(int)$folder[1]."&".SID."\"');\n";
      }
    } 
  }
}

$smarty->assign("buttons",$buttons);

$smarty->display("folders.tpl");

?>
