<?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
*/

// Tree-inc
// Moregroupware.org


function get_folders($sql,$user_id,$make_fields) {
  global $conn,$messagecount,$fields,$folders,$folders2,$trash_id,$inbox_id,$draft_id,$sent_id;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

  while($row = $res->FetchRow()) {
    $thiscount = getmessagecount($row["mbox_id"]);
    $messagecount = $messagecount + $thiscount;
  
    if ($row["mboxname"] == "inbox" and $row["mboxparent"]==0) { $inbox_id = $row["mbox_id"]; }
    if ($row["mboxname"] == "sent" and $row["mboxparent"]==0) { $sent_id = $row["mbox_id"]; }
    if ($row["mboxname"] == "draft" and $row["mboxparent"]==0) { $draft_id = $row["mbox_id"]; }
    if ($row["mboxname"] == "trash" and $row["mboxparent"]==0) { $trash_id = $row["mbox_id"]; }

    if ($thiscount != 0) {
      $thiscount_unread = getmessagecount_unread($row["mbox_id"]);
    } else { $thiscount_unread = 0; }

    $mbox_old = $row["mboxname"];
    if ($row["mbox_protect"]==1) {
      $row["mboxname"] = mboxname_replace($row["mboxname"]);
      $row["mbox_protect"] = "true";
    } else {
      $row["mbox_protect"] = "false";
    }
  
    if ($row["isintrash"]==1) { $confirm="true"; } else { $confirm="false"; }

    $isinsidebar="false";
    if ($row["isinsidebar"]=='1') { 
      $sql = "select id from mgw_webmail2_sidebar where account=0 and user_id=".$_SESSION["MGW"]->userid." and mbox_id=".(int)$row["mbox_id"];
      if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      $messagecount = $res2->RecordCount();
      if ($messagecount != 0) { $isinsidebar="true"; }
    }

    if ($user_id == 0 or ($mbox_old=="spam")) { $ispublic=1; } else { $ispublic=0; }

    if ($mbox_old=="inbox" and $row["mboxparent"]==0 and $user_id != 0) { $isinbox = 1; } else {$isinbox = 0;}
    $folders[$row["mbox_id"]] = array($row["mboxparent"],$row["mbox_id"],$row["mboxname"],$row["mbox_protect"],$confirm,$isinsidebar,$thiscount,$thiscount_unread,$isinbox,$row["user_id"],$ispublic);

    if ($row["haslinks"]=='1') { 
      $sql = "select id,description,href,newwindow from mgw_webmail2_links where (user_id=".$_SESSION["MGW"]->userid." or user_id=0) and mbox_id=".(int)$row["mbox_id"];
      if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
      while($row2 = $res2->FetchRow()) {
        if ($row2["newwindow"] == 1) { $row2["newwindow"] = "B"; } else { $row2["newwindow"] = "R"; }
        $folders2[$row2["description"].$row["mbox_id"]] = array($row["mbox_id"],$row2["newwindow"],$row2["description"],$row2["href"],$row2["id"]);
      }
    }

  }

  if ($make_fields) {
    if ($user_id==0) {$mbox_id2 = "-2";} else {$mbox_id2 = "-1";}
    $sql = "select id,description,href,newwindow from mgw_webmail2_links where (user_id=".$_SESSION["MGW"]->userid." or user_id=0) and mbox_id=".(int)$mbox_id2;
    if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));

    while($row2 = $res2->FetchRow()) {
      if ($row2["newwindow"] == 1) { $row2["newwindow"] = "B"; } else { $row2["newwindow"] = "R"; }
      $folders2[$row2["description"].$mbox_id2] = array($mbox_id2,$row2["newwindow"],$row2["description"],$row2["href"],$row2["id"]);
    }
  }

  if ($make_fields) {
    if (is_array($folders) and count($folders) != 0) {
      foreach ($folders as $key=>$key2) {
        if (isset($folders[$key2[0]]) or $key2[0] == 0) {
          if ($key2[0] == 0) {
            if ($key2[9] == 0) { $father = "fieldpublic"; } else { $father = "foldersTree"; }
          } else {$father = "field_0_".$key2[0];}
          $fields[] = 'field_0_'.$key.' = insFld('.$father.', gFld("'.addslashes($key2[2]).'", 0, '.$key.','.$key2[3].','.$key2[4].','.$key2[5].',"#",'.$key2[6].','.$key2[7].','.$key2[8].','.$key2[10].'))';
        }
      }
      $folders = array();
    }
    if (is_array($folders2) and count($folders2) != 0) {
      ksort($folders2);
      foreach ($folders2 as $key=>$key2) {
        if ($key2[0] == -2) { $father = "fieldpublic"; } elseif ($key2[0] == -1) { $father = "foldersTree"; } else { $father = 'field_0_'.$key2[0]; }
        $fields[] = 'insDoc('.$father.', gLnk("'.$key2[1].'", "'.str_replace(array("'","\"","\\"),array("","",""),$key2[2]).'", "'.str_replace(array("'","\"","\\"),array("","",""),$key2[3]).'","'.$key2[4].'"))';
      }
      $folders2 = array();
    }
  }
}

function getpath($tree,$mbox_id,$list) {

  global $fathers;
    
  if (is_array($tree) and count($tree) != 0) {
    foreach ($tree as $key=>$key2) {
      if ($key == $mbox_id) {
        $fathers = $list;
      } else if (is_array($key2) and count($key2) != 0) {
        $list2 = $list;
        $list2[] = $key;
        getpath($key2,$mbox_id,$list2);
      }
    }
  }
  
}  
  


?>