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

// Custom_view
// Moregroupware.org

$myEnv["module"] = "calendar2";
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
include(INCLUDEPATH . "mSelectBox.class.php");

include (CAL2_INC_DIR.'init.inc.php'); 

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

queryRights2("root_modules_calendar2_calendarmanager_listcalendars");

$myvars = array("action","id","dcalendar","dtype","edit_id","dholiday","dcalendar","dlocation");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}

if ($action=="delete" and $id!="" and is_numeric($id)) {
  queryRights2("root_modules_calendar2_calendarmanager_deletecalendars");
  
  $sql = "SELECT dcalendar FROM mgw_calendar2_cals where id=".(int)$id;
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();

  if (isset($row["dcalendar"]) and $row["dcalendar"]!="") {
    $sql = "delete FROM mgw_calendar2_ics where id=".(int)$id;
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  
    $sql = "delete FROM mgw_calendar2_cals where dcalendar=".$conn->Quote($row["dcalendar"]);
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }
}

if ($action=="edit" and $id!="" and is_numeric($id)) {
  $sql = "SELECT dcalendar,dlocation FROM mgw_calendar2_cals where id=".(int)$id;
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $row = $res->FetchRow();
  
  if (isset($row["dcalendar"])) {

    if ($row["dcalendar"][0]!="@") $smarty->assign("dholiday","checked");
    $row["dcalendar"] = str_replace(array("#","@"),"",$row["dcalendar"]);

    $smarty->assign("dcalendar",$row["dcalendar"]);
    $smarty->assign("dlocation",$row["dlocation"]);
    $smarty->assign("edit_id",$id);
  }
}


if ($action=="change" and $dcalendar!="") {

    $dcalendar = str_replace(array("#","@"),"",$dcalendar);
  
    if ($dholiday=="checked") $dcalendar = "#".$dcalendar;
      else $dcalendar = "@".$dcalendar;
  
  if ($edit_id=="") {
    queryRights2("root_modules_calendar2_calendarmanager_createcalendars");
  
    $sql = "INSERT INTO mgw_calendar2_cals (id,dcalendar,dlocation) VALUES (".(int)mgw_genID('mgw__seq_cals',2).",".$conn->Quote($dcalendar).",".$conn->Quote($dlocation).")";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  } elseif ($edit_id!="" and is_numeric($edit_id)) {
    queryRights2("root_modules_calendar2_calendarmanager_deletecalendars");

	$sql = "update mgw_calendar2_cals set dcalendar=".$conn->Quote($dcalendar).",dlocation=".$conn->Quote($dlocation)." where id=".$edit_id;
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }
}

$cals_users = array();
$cals_groups = array();
$cals_others = array();
$cals_holidays = array();

$sql = "SELECT id,dcalendar,dlocation FROM mgw_calendar2_cals order by dcalendar";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while($row = $res->FetchRow()) {
  if ($row["dcalendar"]!="") {
    if (is_numeric($row["dcalendar"])) {
	  $cals_groups[] = array($row["id"],$row["dlocation"],get_group_name($row["dcalendar"])." (".$row["dcalendar"].")");
	} elseif ($row["dcalendar"][0]=="#") {
	  $cals_holidays[] = array($row["id"],$row["dlocation"],substr($row["dcalendar"],1));
	} elseif ($row["dcalendar"][0]=="@") {
	  $cals_others[] = array($row["id"],$row["dlocation"],substr($row["dcalendar"],1));
	} else {
	  $cals_users[] = array($row["id"],$row["dlocation"],ucfirst($row["dcalendar"]));
	}
  }
}

$smarty->assign("cals_groups",$cals_groups);
$smarty->assign("cals_holidays",$cals_holidays);
$smarty->assign("cals_others",$cals_others);
$smarty->assign("cals_users",$cals_users);

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

?>
