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

// Index
// Moregroupware.org

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

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

checkdir(CACHE_FOLDER,false);

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

$sql = "select id from mgw_calendar2_cals where dcalendar=".$conn->Quote($_SESSION["MGW"]->username);
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$cal_count = $res->RecordCount();

if ($cal_count==0) {
  $id = mgw_genID('mgw__seq_cals',2);
  $sql = "INSERT INTO mgw_calendar2_cals (id,dcalendar) VALUES (".(int)$id.",".$conn->Quote($_SESSION["MGW"]->username).")";
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

  include_once(ROOTPATH."/modules/calendar2/inc/rights.inc.php");

  foreach ($calendar_rights as $item=>$temp_val) {	
    $sql = "INSERT INTO mgw_rights2 (id, id_string, allow_users, deny_users, allow_groups, deny_groups, cuser, cdate, default_value) VALUES (".(int)mgw_genID('mgw__seq_rights2').", ".$conn->Quote("root_modules_calendar2_personalcalendars_".$id."_".$item).", ',".(int)$_SESSION["MGW"]->userid.",', ',,', ',,', ',,', ".(int)$_SESSION["MGW"]->userid.", ".time().", 0);";
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }
}


$groups = array();
$sql = "SELECT groupid FROM mgw_groups_members where userid=".(int)$_SESSION["MGW"]->userid;
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
while($row = $res->FetchRow()) {
  $sql = "select id from mgw_calendar2_cals where dcalendar=".$conn->Quote($row["groupid"]);
  if(!$res2 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  $cal_count = $res2->RecordCount();

  if ($cal_count==0) {
    $id = mgw_genID('mgw__seq_cals',2);
	$sql = "INSERT INTO mgw_calendar2_cals (id,dcalendar) VALUES (".(int)$id.",".$conn->Quote($row["groupid"]).")";
	if(!$res3 = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

	include_once(ROOTPATH."/modules/calendar2/inc/rights.inc.php");
	
    foreach ($calendar_rights as $item=>$temp_val) {	
      $sql = "INSERT INTO mgw_rights2 (id, id_string, allow_users, deny_users, allow_groups, deny_groups, cuser, cdate, default_value) VALUES (".(int)mgw_genID('mgw__seq_rights2').", ".$conn->Quote("root_modules_calendar2_groupcalendars_".(int)$id."_".$item).", ',,', ',,', ',".(int)$row["groupid"].",', ',,', ".(int)$_SESSION["MGW"]->userid.", ".time().", 0);";
      if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	}
  }
}

if (isset($_SESSION["MGW"]->settings["cal2_default_view"])) $view = $_SESSION["MGW"]->settings["cal2_default_view"]; else $view = "day";
if ($view!="day" and $view!="month" and $view!="week" and $view!="year") $view = "day";
if (isset($_SESSION["MGW"]->settings["cal2_default_day"]) and $_SESSION["MGW"]->settings["cal2_default_day"]) $getdate=$_SESSION["MGW"]->settings["cal2_default_day"]; else $getdate="";

header("Location: ".$view.".php?getdate=".$getdate."&".SID);
?>