<?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");

$filter_city = $_POST['filter_city'];
$filter_location = $_POST['filter_location'];
$filter_date = $_POST['filter_date'];

$data = "";
$row_delim = "";

$children_where = " AND ((" . BWB_TABLE_PREFIX . "events.group_id IS NULL) OR (" . BWB_TABLE_PREFIX . "events.group_id = " . BWB_TABLE_PREFIX . "events.event_id))";
$children_where2 = " AND ((group_id IS NULL) OR (group_id = event_id))";

if (!isset($filter_date) || ($filter_date == ""))	{
	$date_where = "";
	$date_where2 = "";
}
else	{
	$date_where = " AND (DATEDIFF(" . BWB_TABLE_PREFIX . "events.event_start_date, '$filter_date') >= 0)";
	$date_where2 = " AND (DATEDIFF(event_start_date, '$filter_date') >= 0)";
}

$filter_city = str_replace("'", "&#39;", $filter_city);
$filter_location = str_replace("'", "&#39;", $filter_location);

// NOT City AND Location
if (((!isset($filter_city)) || ($filter_city == "") || ($filter_city == "-not selected-")) && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events INNER JOIN locations ON " . BWB_TABLE_PREFIX . "events.location_id = locations.location_id WHERE (locations.location_name = '$filter_location')$date_where2$children_where";
	
// City AND Location
elseif ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events INNER JOIN locations ON " . BWB_TABLE_PREFIX . "events.location_id = locations.location_id WHERE (locations.location_name = '$filter_location') OR (locations.location_city = '$filter_city')$date_where$children_where";
	
// City AND NOT Location
elseif ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && ((!isset($filter_location)) || ($filter_location == "") || ($filter_location == "-not selected-")))
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events INNER JOIN locations ON " . BWB_TABLE_PREFIX . "events.location_id = locations.location_id WHERE (locations.location_city = '$filter_city')$date_where$children_where";
	
// No filtering
else
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events WHERE (group_id IS NULL) OR (group_id = event_id) ORDER BY event_name, event_start_date";
		
//	echo $query;
//	exit;	
$result = $bwb_dbh->query($query);
$data = $result->rowCount() . "#####";

if ($result->rowCount() > 0)
{
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$event_id = $row['event_id'];
		$location_id = $row['location_id'];
		
		$query = "SELECT * FROM locations WHERE location_id=$location_id";
		$result2 = $bwb_dbh->query($query);
		$row2 = $result2->fetch(PDO::FETCH_ASSOC);
		$row['location_name'] = $row2['location_name'];
		$row['location_city'] = $row2['location_city'];
		
		$query = "SELECT * FROM event_tracking WHERE event_id = " . $event_id;
		$result3 = $bwb_dbh->query($query);
		if ($result3->rowCount() > 0)
			$row['clicked'] = 1;
		else
			$row['clicked'] = 0;
			
		$data .= $row_delim . json_encode($row);
		$row_delim = "@@@@@";
	}
	echo $data;
}
else
	echo "-no data-";
?>