<?php
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03 
// $Id: action.php,v 1.11 2004/02/06 21:22:07 nobse Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Nobse, Bama
// Author Website : http://www.e-xoops.de,  http://service.bama-webdesign.de
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include("header.php");
//Delete***************************************************
function DelEvent() {
global $db, $xoopsUser, $_POST, $_GET;
$id = !empty($_POST['id']) ? $_POST['id'] : $_GET['id'];
if (!$id) {
	redirect_header("index.php",1,_ERRORMSG1);
break;
}
$calusern =$xoopsUser->getVar("uname", "E");
$query = $db->query("SELECT username FROM ".$db->prefix("ecal")." WHERE id = $id");
$row = $db->fetchArray($query);
if ( $calusern == $row[username]){
	$db->query("DELETE FROM ".$db->prefix("ecal")." WHERE id = '$id'");
	redirect_header("index.php",1,_ERRORMSG3);
} else {
	redirect_header("index.php",1,_ERRORMSG2);
}
}
//Modify*****************************************************
function ModifEvent() {
global $db, $myts, $xoopsUser, $_POST;
$id			 = intval($_POST['id']);
$cid		 = intval($_POST['cid']);
$username 	 = $_POST['username'];
$umail 		 = $_POST['umail'];
$subject	 = $myts->makeTboxData4Save($_POST['subject']);
$description = $myts->makeTboxData4Save($_POST['description1']);
$url 		 = $_POST['url'];
$valid		 = $_POST['valid'];
$flayer		 = $_POST['flayer'];
$privat		 = intval($_POST['privat']);
$locationid	 = intval($_POST['locationid']);
$heure		 = $_POST['heure'];
$minute		 = $_POST['minute'];
$heureend	 = $_POST['heureend'];
$minuteend	 = $_POST['minuteend'];
$month		 = $_POST['month'];
$year		 = $_POST['year'];
$day		 = $_POST['day'];
$high		 = intval($_POST['high']);
if (!$id) {
	redirect_header("index.php",1,_ERRORMSG1);
break;
}
$eventend = "$heureend:$minuteend";
$time = "$heure:$minute:00";
$query = $db->query("SELECT username FROM ".$db->prefix("ecal")." WHERE id = $id");
$row = $db->fetchArray($query);
if ( $xoopsUser) {
	if ( $xoopsUser->isAdmin() || $privat == 1 ) {
	$valid = "yes";
}
}	
	$db->query("UPDATE ".$db->prefix("ecal")." SET cid = $cid, username = '$username' , umail = '$umail', stamp = '$year-$month-$day $time' , eventend = '$eventend', subject = '$subject', description = '$description', url = '$url', valid = '$valid', flayer = '$flayer', privat = $privat, locationid = $locationid, high=$high WHERE id=$id");
	redirect_header("index.php",1,_ERRORMSG5);
}
//Add************************************************************ 
function AddEvent(){
global $db, $myts, $xoopsUser, $_POST;
$cid		 = intval($_POST['cid']);
$username 	 = $_POST['username'];
$umail 		 = $_POST['umail'];
$subject	 = $myts->makeTboxData4Save($_POST['subject']);
$description = $myts->makeTboxData4Save($_POST['description1']);
$url 		 = $_POST['url'];
$valid		 = $_POST['valid'];
$flayer		 = $_POST['flayer'];
$privat		 = intval($_POST['privat']);
$locationid	 = intval($_POST['locationid']);
$heure		 = $_POST['heure'];
$minute		 = $_POST['minute'];
$heureend	 = $_POST['heureend'];
$minuteend	 = $_POST['minuteend'];
$month		 = $_POST['month'];
$year		 = $_POST['year'];
$Day		 = $_POST['Day'];
$high		 = intval($_POST['high']);
$wiederholen = intval($_POST['wiederholen']);
$wiedertag   = intval($_POST['wiedertag']);
$wiedermonat = intval($_POST['wiedermonat']);
$wiederjahr  = intval($_POST['wiederjahr']);
$wiederholen1  = intval($_POST['wiederholen1']);
 if (count($Day) == 0){
            include(XOOPS_ROOT_PATH."/header.php");
            OpenTable();
            echo "<center><b><font color='red'>"._CAL_VALIDERORMSG."</font><br><br>"._CAL_NODAY."</b><br><br>";
            echo "<form method='post' action='addevent.php'>";
            echo "<input class='button' type='submit' name='retadd' value='"._RETURN2."'></center>";
            echo "<input type='hidden' name='month' value='".$month."'>";
            echo "<input type='hidden' name='year' value='".$year."'>";
            echo "<input type='hidden' name='cid' value='".$cid."'>";
            echo "<input type='hidden' name='username' value='".$username."'>";
            echo "<input type='hidden' name='umail' value='".$umail."'>";
            echo "<input type='hidden' name='subject' value='".$subject."'>";
            echo "<input type='hidden' name='description1' value='".$description."'>";
            echo "<input type='hidden' name='url' value='".$url."'>";
            echo "<input type='hidden' name='flayer' value='".$flayer."'>";
            echo "<input type='hidden' name='privat' value='".$privat."'>";
            echo "<input type='hidden' name='locationid' value='".$locationid."'>";
            echo "<input type='hidden' name='heure' value='".$heure."'>";
            echo "<input type='hidden' name='minute' value='".$minute."'>";
            echo "<input type='hidden' name='heureend' value='".$heureend."'>";
            echo "<input type='hidden' name='minuteend' value='".$minuteend."'>";
            echo "<input type='hidden' name='high' value='".$high."'>";
			echo "<input type='hidden' name='wiederholen' value='".$wiederholen."'>";
        	echo "<input type='hidden' name='wiedertag' value='".$wiedertag."'>";
			echo "<input type='hidden' name='wiedermonat' value='".$wiedermonat."'>";
			echo "<input type='hidden' name='wiederjahr' value='".$wiederjahr."'>";
			echo "<input type='hidden' name='wiederholen1' value='".$wiederholen1."'>";
			echo "</form>";            
            CloseTable();
            include(XOOPS_ROOT_PATH."/footer.php");
            exit();
        }
if ( $xoopsUser) {
	if ( $xoopsUser->isAdmin() || $privat == 1 ) {
	$valid = "yes";
}
}
$times = "$heure:$minute:00";
$eventend = "$heureend:$minuteend";
$i=0;
while ($Day[$i]) {
	if ($wiederholen > 0) {
		$ng_wiederstart = strtotime("$year/$month/$Day[$i]");
		$ng_wiederend = strtotime("$wiederjahr/$wiedermonat/$wiedertag");
		while($ng_wiederstart < $ng_wiederend){
			$newid = $db->genId($db->prefix('ecal')."_id_seq");
			$temp = $db->query("INSERT INTO ".$db->prefix("ecal")." SET id=$newid, cid=$cid, username='$username', umail='$umail', stamp='".date("Y-m-d",$ng_wiederstart)." $times', eventend='$eventend', subject='$subject', description='$description', url='$url', valid='$valid', flayer='$flayer', privat=$privat, locationid=$locationid, high=$high");
			$ng_wiederstart = date("Y-m-d",strtotime("+$wiederholen week",$ng_wiederstart));
			$ng_wiederstart = strtotime("$ng_wiederstart");
		} 
	}elseif ($wiederholen1 > 0){
		$ng_wiederstart = strtotime("$year/$month/$Day[$i]");
		$wochentag = date("l",$ng_wiederstart);
		$ng_wiederend = strtotime("$wiederjahr/$wiedermonat/$wiedertag");
		$month1 = $month;
		if ($wiederholen1 == 5) {
		    $wiederholen1 = "last";
			$month1 = $month + 1;
		}
		while($ng_wiederstart < $ng_wiederend){
			$newid = $db->genId($db->prefix('ecal')."_id_seq");
			$temp = $db->query("INSERT INTO ".$db->prefix("ecal")." SET id=$newid, cid=$cid, username='$username', umail='$umail', stamp='".date("Y-m-d",$ng_wiederstart)." $times', eventend='$eventend', subject='$subject', description='$description', url='$url', valid='$valid', flayer='$flayer', privat=$privat, locationid=$locationid, high=$high");
			$month1 = $month1 + 1;
			$ng_wiederstart = strtotime("$year/$month1/01");
			$ng_wiederstart = date("Y-m-d",strtotime("$wiederholen1 $wochentag",$ng_wiederstart));
			$ng_wiederstart = strtotime("$ng_wiederstart");
		}
	}else{
		$newid = $db->genId($db->prefix('ecal')."_id_seq");
		$temp = $db->query("INSERT INTO ".$db->prefix("ecal")." SET id=$newid, cid=$cid, username='$username', umail='$umail', stamp='$year-$month-$Day[$i] $times', eventend='$eventend', subject='$subject', description='$description', url='$url', valid='$valid', flayer='$flayer', privat=$privat, locationid=$locationid, high=$high");
	}	
	$i++;
}
redirect_header("index.php",1,_MSG1);
}
//**********************************************************************+
$pa = !empty($_POST['pa']) ? $_POST['pa'] : $_GET['pa'];
switch($pa) {
    case "ModifEvent":
		ModifEvent();
	break;
    case "AddEvent":
		AddEvent();
	break;
		
    case "DelEvent":
		DelEvent();
	break;
	default:
	redirect_header("index.php",1,_NO_AUTHORIZ);
	break;
}
?>