<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 2/21/2014
//
	include ("check_session.php");
	include ("get_recurring_dates.php");
	
	$event_id = $_REQUEST["event_id"];
	$event_name = addslashes($_REQUEST["event_name"]);
	$event_name = str_replace("AMPERSAND", "&", $event_name);
	if (isset($_REQUEST["event_categories"]))
		$event_categories = $_REQUEST["event_categories"];
	else
		$event_categories = "";
	$event_start_date = $_REQUEST["event_start_date"];
	$event_end_date = $_REQUEST["event_end_date"];
	
	$recurrence = $_REQUEST["recurrence"];
	$recurrence_freq = $_REQUEST["recurrence_freq"];
//	$recurrence_interval = $_REQUEST["recurrence_interval"];
	$recurrence_byweekno = $_REQUEST["recurrence_byweekno"];
	$recurrence_byday = $_REQUEST["recurrence_byday"];
	$recurrence_days = $_REQUEST["recurrence_days"];
	if ($recurrence == 1)
		$event_group_start_date = $event_start_date;
	elseif (isset($event_end_date))
		unset($event_end_date);
	
	$event_start_time = $_REQUEST["event_start_time"];
	$event_end_time = $_REQUEST["event_end_time"];
	if (isset($_REQUEST["event_all_day"]) && ($_REQUEST["event_all_day"] != "") && ($_REQUEST["event_all_day"] != 0))
		$event_all_day = 1;
	else
		$event_all_day = 0;
	$location_id = $_REQUEST["location_id"];
	$event_description = addslashes($_REQUEST["event_description"]);
	$event_description = str_replace("AMPERSAND", "&", $event_description);
	$event_status = $_REQUEST["event_status"];
	if (isset($_REQUEST["event_owner"]) && ($_REQUEST["event_owner"] != ""))
		$event_owner = $_REQUEST["event_owner"];
	else
		$event_owner = "";
	$event_active = $_REQUEST["event_active"];
		
	$today = date("Y-m-d H:i:s");
		
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events WHERE event_id='$event_id'";
	$result = $bwb_dbh->query($query);
	if (($event_id > -1) && ($result->rowCount() > 0))	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "events SET event_name = '$event_name', event_categories = '$event_categories', event_start_date = '$event_start_date', event_end_date = '$event_end_date', event_group_start_date = '$event_group_start_date', recurrence = '$recurrence', recurrence_freq = '$recurrence_freq', recurrence_byweekno = '$recurrence_byweekno', recurrence_byday = '$recurrence_byday', recurrence_days = '$recurrence_days', event_start_time = '$event_start_time', event_end_time = '$event_end_time', event_all_day = $event_all_day,";
		if ((isset($location_id)) && ($location_id > 0))
			$query .= " location_id = $location_id,";
		$query .= " event_description = '$event_description', event_status = $event_status, event_active = $event_active WHERE event_id = $event_id";
		$action = "Update event ID $event_id: ";
	}
	else	{
		$query = "INSERT INTO " . BWB_TABLE_PREFIX . "events SET event_name = '$event_name', event_categories = '$event_categories', event_start_date = '$event_start_date', event_end_date = '$event_end_date', event_group_start_date = '$event_group_start_date', recurrence = '$recurrence', recurrence_freq = '$recurrence_freq', recurrence_byweekno = '$recurrence_byweekno', recurrence_byday = '$recurrence_byday', recurrence_days = '$recurrence_days', event_start_time = '$event_start_time', event_end_time = '$event_end_time', event_all_day = $event_all_day, location_id = $location_id, event_description = '$event_description', event_status = $event_status, event_active = $event_active, event_owner = '$event_owner', `created` = '$today'";
		$action = "Insert: ";
	}
	echo $query . "<br><br>\n\n";
	$result = $bwb_dbh->exec($query);
	
	if (strpos($action, "Insert") > -1)
		$event_id = $bwb_dbh->lastInsertId();
	echo $event_id;

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if ((strpos($action, "Insert") > -1) && ($recurrence == 1) && isset($recurrence_freq) && ($recurrence_freq != ""))
	{
		$group_id = $event_id;
		$query = "UPDATE " . BWB_TABLE_PREFIX . "events SET group_id = '$group_id' WHERE event_id = '$event_id'";
		$result = $bwb_dbh->exec($query);
	}	
	elseif ((strpos($action, "Update") > -1) && ($recurrence == 1) && isset($recurrence_freq) && ($recurrence_freq != ""))
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events WHERE event_id='$event_id'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$group_id = $row['group_id'];
		
		if (!isset($group_id) || ($group_id == 0))
			$group_id = $event_id;
		
		$query = "DELETE FROM " . BWB_TABLE_PREFIX . "events WHERE event_id != $group_id AND group_id = $group_id";
		$result = $bwb_dbh->exec($query);
	}
	
	if (($recurrence == 1) && ($recurrence_freq == "daily"))
	{	
		$dates = GetRecurringDates($event_start_date, $event_end_date, $recurrence_freq, $recurrence_byday, $recurrence_byweekno, $recurrence_days);
		foreach($dates as $event_date)
		{
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "events SET event_name = '$event_name', group_id = '$group_id', event_categories = '$event_categories', event_start_date = '$event_date', event_end_date = '$event_end_date', event_group_start_date = '$event_start_date', recurrence = '1', recurrence_freq = 'daily', event_start_time = '$event_start_time', event_end_time = '$event_end_time', event_all_day = $event_all_day, location_id = $location_id, event_description = '$event_description', event_status = $event_status, event_active = $event_active, event_owner = '$event_owner', `created` = '$today'";
		//	echo $query . "<br><br>\n\n";
			$result = $bwb_dbh->exec($query);
		}
	}
	elseif (($recurrence == 1) && (($recurrence_freq == "weekly") || ($recurrence_freq == "monthly")))
	{	
		$dates = GetRecurringDates($event_start_date, $event_end_date, $recurrence_freq, $recurrence_byday, $recurrence_byweekno, $recurrence_days);
		foreach($dates as $event_date)
		{
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "events SET event_name = '$event_name', group_id = '$group_id', event_categories = '$event_categories', event_start_date = '$event_date', recurrence = '0', event_start_time = '$event_start_time', event_end_time = '$event_end_time', event_all_day = $event_all_day, location_id = $location_id, event_description = '$event_description', event_status = $event_status, event_active = $event_active, event_owner = '$event_owner', `created` = '$today'";
		//	echo $query . "<br><br>\n\n";
			$result = $bwb_dbh->exec($query);
		}
	}
	elseif (($recurrence == 1) && ($recurrence_freq == "yearly"))	// Yearly is daily-recurring events that happen every year.
	{		
		$dates = GetRecurringDates($event_start_date, $event_end_date, $recurrence_freq, $recurrence_byday, $recurrence_byweekno, $recurrence_days);
		$event_start_date_arr = explode("-", $event_start_date);
		$last_year = $event_start_date_arr[0];
		$month = $event_start_date_arr[1];
		$day = $event_start_date_arr[2] + $recurrence_days -1;
		foreach($dates as $event_date)
		{
			$year = substr($event_date, 0, 4);
			if ($year != $last_year)
			{
				$end_date = $year . "-" . $month . "-" . $day;
				$query = "INSERT INTO " . BWB_TABLE_PREFIX . "events SET event_name = '$event_name', group_id = '$group_id', event_categories = '$event_categories', event_start_date = '$event_date', event_end_date = '$end_date', event_group_start_date = '$event_start_date', recurrence = '1', recurrence_freq = 'daily', event_start_time = '$event_start_time', event_end_time = '$event_end_time', event_all_day = $event_all_day, location_id = $location_id, event_description = '$event_description', event_status = $event_status, event_active = $event_active, event_owner = '$event_owner', `created` = '$today'";
			//	echo $query . "<br><br>\n\n";
				$result = $bwb_dbh->exec($query);
				$last_year = $year;
			}
		}
	}
?>