<?php
// $Id: action.php,v 1.1 2004/07/11 10:27:50 Farsus 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, $HTTP_POST_VARS, $HTTP_GET_VARS;

$id = !empty($HTTP_POST_VARS['id']) ? $HTTP_POST_VARS['id'] : $HTTP_GET_VARS['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->fetch_array($query);

if ( $calusern == $row[username]){
	$db->queryF("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, $HTTP_POST_VARS;

$id			 = intval($HTTP_POST_VARS['id']);
$cid		 = intval($HTTP_POST_VARS['cid']);
$username 	 = $HTTP_POST_VARS['username'];
$umail 		 = $HTTP_POST_VARS['umail'];
$subject	 = $myts->makeTboxData4Save($HTTP_POST_VARS['subject']);
$description = $myts->makeTboxData4Save($HTTP_POST_VARS['description1']);
$url 		 = $HTTP_POST_VARS['url'];
$valid		 = $HTTP_POST_VARS['valid'];
$flayer		 = $HTTP_POST_VARS['flayer'];
$privat		 = intval($HTTP_POST_VARS['privat']);
$locationid	 = intval($HTTP_POST_VARS['locationid']);

$heure		 = $HTTP_POST_VARS['heure'];
$minute		 = $HTTP_POST_VARS['minute'];
$heureend	 = $HTTP_POST_VARS['heureend'];
$minuteend	 = $HTTP_POST_VARS['minuteend'];
$month		 = $HTTP_POST_VARS['month'];
$year		 = $HTTP_POST_VARS['year'];
$day		 = $HTTP_POST_VARS['day'];
$high		 = intval($HTTP_POST_VARS['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->fetch_array($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, $HTTP_POST_VARS;

$cid		 = intval($HTTP_POST_VARS['cid']);
$username 	 = $HTTP_POST_VARS['username'];
$umail 		 = $HTTP_POST_VARS['umail'];
$subject	 = $myts->makeTboxData4Save($HTTP_POST_VARS['subject']);
$description = $myts->makeTboxData4Save($HTTP_POST_VARS['description1']);
$url 		 = $HTTP_POST_VARS['url'];
$valid		 = $HTTP_POST_VARS['valid'];
$flayer		 = $HTTP_POST_VARS['flayer'];
$privat		 = intval($HTTP_POST_VARS['privat']);
$locationid	 = intval($HTTP_POST_VARS['locationid']);

$heure		 = $HTTP_POST_VARS['heure'];
$minute		 = $HTTP_POST_VARS['minute'];
$heureend	 = $HTTP_POST_VARS['heureend'];
$minuteend	 = $HTTP_POST_VARS['minuteend'];
$month		 = $HTTP_POST_VARS['month'];
$year		 = $HTTP_POST_VARS['year'];
$Day		 = $HTTP_POST_VARS['Day'];
$high		 = intval($HTTP_POST_VARS['high']);
$wiederholen = intval($HTTP_POST_VARS['wiederholen']);
$wiedertag   = intval($HTTP_POST_VARS['wiedertag']);
$wiedermonat = intval($HTTP_POST_VARS['wiedermonat']);
$wiederjahr  = intval($HTTP_POST_VARS['wiederjahr']);
$wiederholen1  = intval($HTTP_POST_VARS['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($HTTP_POST_VARS['pa']) ? $HTTP_POST_VARS['pa'] : $HTTP_GET_VARS['pa'];
switch($pa) {
    case "ModifEvent":
		ModifEvent();
	break;

    case "AddEvent":
		AddEvent();
	break;
		
    case "DelEvent":
		DelEvent();
	break;

	default:
	redirect_header("index.php",1,_NO_AUTHORIZ);
	break;
}
?>