<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/3/2014
//
	
	include ("check_session.php");
	
	$filter_city = $_REQUEST['filter_city'];
	$filter_event_name = addslashes($_REQUEST['filter_event_name']);
	$filter_location = $_REQUEST['filter_location'];
	$filter_date = $_REQUEST['filter_date'];
	if ((!isset($filter_date)) || ($filter_date == ""))
		$filter_date = date("Y-m-d");
	$userid = $_REQUEST['userid'];
	$show_children = $_REQUEST['show_children'];
	$show_pending = $_REQUEST['show_pending'];
	
	$filter_city = str_replace("'", "&#39;", $filter_city);
	$filter_location = str_replace("'", "&#39;", $filter_location);
		
	if ($show_children != 1)	{
		$children_where = " AND ((t1.group_id IS NULL) OR (t1.group_id = t1.event_id))";
		$children_where2 = " AND ((group_id IS NULL) OR (group_id = event_id))";
	}
	else	{
		$children_where = "";
		$children_where2 = "";
	}
	
	if ($show_pending == 1)	{
		$pending_where = " AND (t1.event_status = 0)";
		$pending_where2 = " AND (event_status = 0)";
	}
	else	{
		$pending_where = "";
		$pending_where2 = "";
	}
	
	
	// City AND Event AND Location
	if ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && (isset($filter_event_name)) && ($filter_event_name != "") && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.city = '$filter_city') AND (t2.location_name = '$filter_location') AND (t1.event_name LIKE '%$filter_event_name%') AND (DATEDIFF(t1.event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// City AND Event AND NOT Location
	elseif ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && (isset($filter_event_name)) && ($filter_event_name != "") && ((!isset($filter_location)) || ($filter_location == "") || ($filter_location == "-not selected-")))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.city = '$filter_city') AND (t1.event_name LIKE '%$filter_event_name%') AND (DATEDIFF(t1.event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// NOT City AND Event AND NOT Location
	elseif (((!isset($filter_city)) || ($filter_city == "") || ($filter_city == "-not selected-")) && (isset($filter_event_name)) && ($filter_event_name != "") && ((!isset($filter_location)) || ($filter_location == "") || ($filter_location == "-not selected-")))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events WHERE (event_name LIKE '%$filter_event_name%') AND (DATEDIFF(event_start_date, '$filter_date') >= 0)$children_where2$pending_where2 ORDER BY event_start_date";
		
	// NOT City AND Event AND Location
	elseif (((!isset($filter_city)) || ($filter_city == "") || ($filter_city == "-not selected-")) && (isset($filter_event_name)) && ($filter_event_name != "") && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.location_name = '$filter_location') AND (event_name LIKE '%$filter_event_name%') AND (DATEDIFF(event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// NOT City AND NOT Event AND Location
	elseif (((!isset($filter_city)) || ($filter_city == "") || ($filter_city == "-not selected-")) && ((!isset($filter_event_name)) || ($filter_event_name == "")) && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.location_name = '$filter_location') AND (DATEDIFF(event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// City AND NOT Event AND Location
	elseif ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && ((!isset($filter_event_name)) || ($filter_event_name == "")) && (isset($filter_location)) && ($filter_location != "") && ($filter_location != "-not selected-"))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.location_name = '$filter_location') AND (t2.city = '$filter_city') AND (DATEDIFF(t1.event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// City AND NOT Event AND NOT Location
	elseif ((isset($filter_city)) && ($filter_city != "") && ($filter_city != "-not selected-") && ((!isset($filter_event_name)) || ($filter_event_name == "")) && ((!isset($filter_location)) || ($filter_location == "") || ($filter_location == "-not selected-")))
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS t1 INNER JOIN " . BWB_TABLE_PREFIX . "locations AS t2 ON t1.location_id = t2.location_id WHERE (t2.city = '$filter_city') AND (DATEDIFF(t1.event_start_date, '$filter_date') >= 0)$children_where$pending_where";
		
	// No filtering
	else
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events WHERE DATEDIFF(event_start_date, '$filter_date') >= 0$children_where2$pending_where2 ORDER BY event_name, event_start_date";
//echo $query;
//exit;
	$row_delim = "";
	$col_delim = ":::";
	$result = $bwb_dbh->query($query);
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{		
		if (!isset($row["event_name"]) || ($row["event_name"] == ""))
			$event_name = " ";
		else
			$event_name = addslashes($row["event_name"]);
		echo $row_delim . $row["event_id"] . $col_delim . $event_name . $col_delim . $row["event_start_date"];
		$row_delim = "::::";
	}
	
//	echo "|||||" . $query;
?>