<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 12/30/2018
//
include ("check_session.php");

$year = $_POST['year'];
$month = $_POST['month'];
$start_day = $_POST['start_day'];
$end_day = $_POST['end_day'];
$next_mnth = intval($month) + 1;
$next_yr = $year;
if ($next_mnth == 13)	{
	$next_mnth = 1;
	$next_yr = $year + 1;
}
	
if (intval($month) < 10)
	$month_str = "0" . strval($month); 
else
	$month_str = strval($month); 

if (intval($next_mnth) < 10)
	$next_mnth_str = "0" . strval($next_mnth); 
else
	$next_mnth_str = strval($next_mnth); 
if (!isset($start_day) || ($start_day == "") || ($start_day == 0) || !isset($end_day) || ($end_day == "") || ($end_day == 0))	{
	$begin_date = $year . "-" . $month_str . "-01";
	$end_date = $next_yr . "-" . $next_mnth_str . "-01";
	$end_compare = "<";
}
else	{
	if ($start_day < 10)
		$start_day_str = "0" . strval($start_day);
	else
		$start_day_str = strval($start_day);
	if ($end_day < 10)
		$end_day_str = "0" . strval($end_day);
	else
		$end_day_str = strval($end_day);
	$begin_date = $year . "-" . $month_str . "-" . $start_day_str;
	$end_date = $year . "-" . $month_str . "-" . $end_day_str;
	$end_compare = "<=";
}

$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS T1 LEFT JOIN " . BWB_TABLE_PREFIX . "locations AS T2 ON T1.location_id = T2.location_id WHERE (DATEDIFF(event_start_date, '$begin_date') >= 0) AND (DATEDIFF(event_start_date, '$end_date') $end_compare 0) ORDER BY event_start_date";

$result = $bwb_dbh->query($query);
if ($result->rowCount() > 0)
{
	$row_delim = "";
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		echo $row_delim . json_encode($row);
		$row_delim = "@@@@@";
	}
}
else
	echo "-no data-";
?>