<?php
///////////// Events Code ///////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// Created: 12/30/2018

	$month_count = 0;
	$month = array();
	$day = array();
	$year = array();
	$event_name = array();
	$event_description = array();
	$event_start_date = array();
	$event_end_date = array();
	$event_start_time = array();
	$event_end_time = array();
    $URL = array();
	$target = array();
	$event_private = array();
	
	if (SEF_URL == 1)
		include($php_dir_rel . "get_params_from_sef_url.php");
		
	include($php_dir_rel . "clean_var.php");
	foreach ($_POST as $key => $value)	{
		if ((!is_array($value)) && (!is_object($value)))
			$_POST[$key] = CleanVar($value);
		else	{
			foreach ($value as $k => $val)	{
				$value[$k]= CleanVar($val);
			}
			$_POST[$key] = $value;
		}
	}
	
	$show_hebrew = $_GET['show_hebrew'];
	if (!isset($show_hebrew)) $show_hebrew = 0;
	$show_past = $_GET['show_past'];
	if (!isset($show_past)) $show_past = 0;
	$events_user_table = $_GET['user_table'];
	$events_UserID = $_GET['UserID'];
	$events_AccessLevel = $_GET['AccessLevel'];
	$use_logged_in_credentials = $_GET['use_logged_in_credentials'];
	if ($_GET['descending'])
		$descending = "DESC";
	
	$where = " WHERE (event_active = 1)";
	$today = date("Y-m-d");
	$curr_day = intval(substr($today, strlen($today) - 2));
	if ($show_past != 1)
		$where .= " AND (event_start_date >= '$today')";
	
	if ($use_logged_in_credentials)
	{
		if ($events_user_table)
			$where .= " AND (user_table = '$user_table')";
		if ($events_UserID)
			$where .= " AND (UserID = '$userid')";
		if ($events_AccessLevel)
			$where .= " AND (AccessLevel = '$AccessLevel')";
	}
	else
	{
		if ($events_user_table != 1)
			$user_table = $events_user_table;
		if ($events_UserID != 1)
			$userid = $events_UserID;
		if ($events_AccessLevel != 1)
			$AccessLevel = $events_AccessLevel;
			
		if (($events_user_table) && ($events_UserID) && ($events_AccessLevel))
			$where .= " AND ((user_table = '$user_table') AND ((UserID = '$userid') OR (AccessLevel = '$AccessLevel')))";
		elseif (($events_user_table) && (!$events_UserID) && (!$events_AccessLevel))
			$where .= " AND (user_table = '$user_table')";
		elseif ((!$events_user_table) && ($events_UserID) && (!$events_AccessLevel))
			$where .= " AND (UserID = '$userid')";
		elseif ((!$events_user_table) && (!$events_UserID) && ($events_AccessLevel))
			$where .= " AND (AccessLevel = '$AccessLevel')";
		elseif (($events_user_table) && ($events_UserID) && (!$events_AccessLevel))
			$where .= " AND (user_table = '$user_table') AND (UserID = '$userid')";
		elseif (($events_user_table) && (!$events_UserID) && ($events_AccessLevel))
			$where .= " AND (user_table = '$user_table') AND (AccessLevel = '$AccessLevel')";
		elseif ((!$events_user_table) && ($events_UserID) && ($events_AccessLevel))
			$where .= " AND ((UserID = '$userid') OR (AccessLevel = '$AccessLevel'))";
	}
	
	$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 . " ORDER BY `event_start_date`";
	if ($descending) $query .= " $descending";
	
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	
	echo "<script>\n";
	echo "	var location_name = new Array();\n";
	echo "	var address = new Array();\n";
	echo "	var address2 = new Array();\n";
	echo "	var city = new Array();\n";
	echo "	var state = new Array();\n";
	echo "	var postal_code = new Array();\n";
	echo "	var country = new Array();\n";
	echo "	var phone = new Array();\n";
	echo "	var phone2 = new Array();\n";
	echo "	var fax = new Array();\n";
	echo "	var email = new Array();\n";
	echo "	var contact_first = new Array();\n";
	echo "	var contact_middle = new Array();\n";
	echo "	var contact_last = new Array();\n";
	echo "	var website = new Array();\n";
	echo "	var web_link_text = new Array();\n";
	echo "	var notes = new Array();\n";
	echo "	var location_categories = new Array();\n";
	for ($i = 0; $i < $num_results; $i++)
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$event_name[$i] = stripslashes($row["event_name"]);
		$event_description[$i] = stripslashes($row["event_description"]);
		$event_start_date[$i] = $row["event_start_date"];
		$event_start_time[$i] = $row["event_start_time"];
		$event_end_time[$i] = $row["event_end_time"];
		$URL[$i] = $row["URL"];
		$target[$i] = $row["target"];
		$event_private[$i] = $row["event_private"];
		
		if ((substr($event_start_date[$i], 2, 1) ==  "/") && (substr($event_start_date[$i], 5, 1) ==  "/"))
			list($month[$i], $day[$i], $year[$i]) = explode("/", $event_start_date[$i]);
		else
			list($year[$i], $month[$i], $day[$i]) = preg_split("/[\/.-]/", $event_start_date[$i]);
		if ($month[$i] != $month[$i-1])
			$month_count++;
		if ((substr($event_start_time[$i], 2, 1) ==  ":") && (substr($event_start_time[$i], 5, 1) ==  ":"))	{
			list($hour, $minute, $second) = explode(":", $event_start_time[$i]);
			$am_pm = "am";
			if (intval($hour) >= 12)	{
				if (intval($hour) > 12)
					$hour = intval($hour) - 12;
				$am_pm = "pm";
			}
			elseif (intval($hour) == 0)	{
				$hour = "12";
				$am_pm = "am";
			}
			$event_start_time[$i] = intval($hour) . ":" . $minute . " " . $am_pm;
		}
		if ((substr($event_end_time[$i], 2, 1) ==  ":") && (substr($event_end_time[$i], 5, 1) ==  ":"))	{
			list($hour, $minute, $second) = explode(":", $event_end_time[$i]);
			$am_pm = "am";
			if (intval($hour) >= 12)	{
				if (intval($hour) > 12)
					$hour = intval($hour) - 12;
				$am_pm = "pm";
			}
			elseif (intval($hour) == 0)	{
				$hour = "12";
				$am_pm = "am";
			}
			$event_end_time[$i] = intval($hour) . ":" . $minute . " " . $am_pm;
		}
		
		$location_name[$i] = $row["location_name"];
		if (isset($row["location_name"]) && ($row["location_name"] != ""))	{
			echo "	location_name[$i] = \"" . $row["location_name"] . "\";\n";
			echo "	address[$i] = \"" . $row["address"] . "\";\n";
			echo "	address2[$i] = \"" . $row["address2"] . "\";\n";
			echo "	city[$i] = \"" . $row["city"] . "\";\n";
			echo "	state[$i] = \"" . $row["state"] . "\";\n";
			echo "	postal_code[$i] = \"" . $row["postal_code"] . "\";\n";
			echo "	country[$i] = \"" . $row["country"] . "\";\n";
			echo "	phone[$i] = \"" . $row["phone"] . "\";\n";
			echo "	phone2[$i] = \"" . $row["phone2"] . "\";\n";
			echo "	fax[$i] = \"" . $row["fax"] . "\";\n";
			echo "	email[$i] = \"" . $row["email"] . "\";\n";
			echo "	contact_first[$i] = \"" . $row["contact_first"] . "\";\n";
			echo "	contact_middle[$i] = \"" . $row["contact_middle"] . "\";\n";
			echo "	contact_last[$i] = \"" . $row["contact_last"] . "\";\n";
			echo "	website[$i] = \"" . $row["website"] . "\";\n";
			echo "	web_link_text[$i] = \"" . $row["web_link_text"] . "\";\n";
			echo "	notes[$i] = \"" . $row["notes"] . "\";\n";
			echo "	location_categories[$i] = \"" . $row["location_categories"] . "\";\n";
		}
	}
	echo "</script>\n";
	
	echo "<div class='events_div'>\n";
	echo "<table class='events_table'>\n";
	echo "<tr class='events_tr_head'>\n";
	echo "<td class='events_td_head'>Description</td>\n";
	echo "<td class='events_td_head'>Date</td>\n";
	echo "<td class='events_td_head'>Start Time</td>\n";
	echo "<td class='events_td_head'>End Time</td>\n";
	if ($show_hebrew == 1)
		echo "<td class='events_td_head'>Hebrew Date</td>\n";
	echo "</tr>\n";
	for ($i = 0; $i < $num_results; $i++)
	{
		if ((substr($event_start_date[$i], 2, 1) ==  "/") && (substr($event_start_date[$i], 5, 1) ==  "/"))
			list($month, $day, $year) = explode("/", $event_start_date[$i]);
		else
			list($year, $month, $day) = preg_split("/[\/.-]/", $event_start_date[$i]);
		$event_start_date_str = $month . "/" . $day . "/" . $year;
			
		if ($show_hebrew == 1)
		{
			// Get Jewish date
			$julian_date = gregoriantojd($month, $day, $year);
			$jewishdate = jdtojewish($julian_date);
			// Find the Jewish calendar date and parse out the day and year values
			list ($jc_month, $jc_day, $jc_year) = explode ('/', $jewishdate);
			// Find the Jewish calendar month name
			$jc_month = jdmonthname ($julian_date, CAL_MONTH_JEWISH);
			$HebrewDate = "$jc_day $jc_month";
		}
		
		if ($bgclr_class == "events_tr_a")
			$bgclr_class = "events_tr_b";
		else
			$bgclr_class = "events_tr_a";
			
		$events_highlight = "";
		$events_past_dates = "";
		if ($event_start_date[$i] < $today)
			$events_past_dates = " events_past_dates";
		if ($event_start_date[$i] == $today)
			$bgclr_class = " events_curr_day";
			
		if (isset($location_name[$i]) && ($location_name[$i] != ""))
			$location_link_html = "<br /><div class='location_link' onclick='ShowLocationInfo(" . $i . ");' onmouseover='ShowLocationInfo(" . $i . ");'>Location</div>";
		else
			$location_link_html = "";
		
		$private_html = "";
		if ($event_private[$i] == 1)
			$private_html = "<div class='events_private'>Private</div>\n";
		echo "<tr class='events_tr $bgclr_class$events_past_dates'>\n";
		if ($URL[$i])
			echo "<td class='events_td'><a href='" . $URL[$i] . "' target='" . $target[$i] . "'>" . $event_name[$i] . "<br />" . $event_description[$i] . "</a>" . $location_link_html . $private_html . "</td>\n";
		else
			echo "<td class='events_td'>" . $event_name[$i] . "<br />" . $event_description[$i] . $location_link_html . $private_html . "</td>\n";
			
		echo "<td class='events_td'>" . $event_start_date_str . "</td>\n";
		echo "<td class='events_td'>" . $event_start_time[$i] . "</td>\n";
		echo "<td class='events_td'>" . $event_end_time[$i] . "</td>\n";
		if ($show_hebrew == 1)
			echo "<td class='events_td'>$HebrewDate</td>\n";
		echo "</tr>\n";
	}
	echo "</table>\n";
	
	$toggle_show_past = 1;
	$show_hide_past_btn_label = "Show Past Events";
	if ($show_past == 1)	{
		$toggle_show_past = 0;
		$show_hide_past_btn_label = "Hide Past Events";
	}
		
	if (SEF_URL == 1)	{
		$show_hide_past_url = str_replace(".php", "", $this_page) . "/show_past/" . $toggle_show_past . "/show_hebrew/" . $show_hebrew;
		if (isset($events_user_table) && ($events_user_table != ""))
			$show_hide_past_url .= "/user_table/" . $events_user_table;
		if (isset($events_UserID) && ($events_UserID != ""))
			$show_hide_past_url .= "/UserID/" . $events_UserID;
		if (isset($events_AccessLevel) && ($events_AccessLevel != ""))
			$show_hide_past_url .= "/AccessLevel/" . $events_AccessLevel;
		if (isset($use_logged_in_credentials) && ($use_logged_in_credentials != ""))
			$show_hide_past_url .= "/use_logged_in_credentials/" . $use_logged_in_credentials;
	}
	else	{
		$show_hide_past_url = $this_page . "?show_past=" . $toggle_show_past . "&show_hebrew=" . $show_hebrew;
		if (isset($events_user_table) && ($events_user_table != ""))
			$show_hide_past_url .= "&user_table=" . $events_user_table;
		if (isset($events_UserID) && ($events_UserID != ""))
			$show_hide_past_url .= "&UserID=" . $events_UserID;
		if (isset($events_AccessLevel) && ($events_AccessLevel != ""))
			$show_hide_past_url .= "&AccessLevel=" . $events_AccessLevel;
		if (isset($use_logged_in_credentials) && ($use_logged_in_credentials != ""))
			$show_hide_past_url .= "&use_logged_in_credentials=" . $use_logged_in_credentials;
	}
	
	echo "\n<div id='show_hide_past_btn'><a href='" . $show_hide_past_url . "'>" . $show_hide_past_btn_label . "</a></div>\n";

	echo "</div>\n";
	
	include($scripts_dir_rel . "/location_info.html");
	/////////////////////////////////////////////////////////////////////////
?>