<?php
////////////////////////////////////////////////////////////////////////
// EB Content Management System
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// Revised: 4/10/2010	ELL
// Added date picker and removed obsolete switch for HebrewDateConversion.
// Revised: 10/29/2010	ELL
// Complelely re-engineered for speed with many dates using AJAX.
// Revised:	2/17/2011	ELL
// Fixed bug in IE where jsDatePick did not work.  (DOCTYPE neede to be declared.)  Added sorting of dates.
// Revised: 2/8/2012	ELL
// Added table prefix handling.
// Revised: 4/26/2012	ELL
// Spare rows were missing table prefix for page list.  Added table prefix to fix missing page list.
// Revised: 7/3/2012	ELL
// Added user/group assigned events.
// Revised: 4/9/2013	ELL
// Changed links to move to plugins folder and off-loaded JavaScript into JS file.
// Revised: 7/1/2013	ELL
// Implemented popup message class.
// Revised: 11/30/2013	ELL
// Modified for version 5 of the Breeze Website Builder.
// Revised: 4/24/2014	ELL
// Added ability to set target for event URL.
//
	include ("check_session.php");
	
	$spares = 100;
	
	$query = "SELECT `language_folder` FROM " . BWB_TABLE_PREFIX . "plugins WHERE `name` = 'Calendar Grid'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$language_folder = $row['language_folder'];
	
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if ((defined("BWB_LANG")) && (file_exists($document_root . "/" . $language_folder . "/" . BWB_LANG . ".php")))
		include_once ($document_root . "/" . $language_folder . "/" . BWB_LANG . ".php");
	else
		include_once ($document_root . "/" . $language_folder . "/en.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Revise Schedule | Breeze Website Builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../theme/<?php echo $theme; ?>/manager.css" type="text/css">

<link rel="stylesheet" type="text/css" media="all" href="js/jsDatePick.css" />

<style type="text/css">
#btn_add_event	{
	width:230px;
}

.button_heading_sub_level_1	{
	margin-left: 0;
	margin-bottom: 3px;
}
</style>

<script type="text/javascript" src="js/jsDatePick.min.1.1.js"></script>

<script type="text/javascript" src="../js/json2.js"></script>
<script type="text/javascript" src="../js/ajax.js"></script>
<script type="text/javascript" src="../js/mouse_position.js"></script>
<script type="text/javascript" src="../js/popup_msg.js"></script>
<script type="text/javascript" src="js/edit_schedule.js"></script>

<script language="JavaScript"  type="text/javascript">
var pages = new Array();
<?php
	$pages = array();
	$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data ORDER BY page_name";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	for ($j = 0; $j < $num_results; $j++)
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$page_name = $row["page_name"];
		echo "	pages[$j] = '$page_name';\n";
		$pages[$j] = $page_name;
	}
	
	echo "\n	var CALENDAR_EVENTS_DELETE_EVENT_CONFIRM_1_MSG = '" . CALENDAR_EVENTS_DELETE_EVENT_CONFIRM_1_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_DELETE_EVENT_CONFIRM_2_MSG = '" . CALENDAR_EVENTS_DELETE_EVENT_CONFIRM_2_MSG . "';\n";
	echo "	var YES_WORD = '" . YES_WORD . "';\n";
	echo "	var NO_WORD = '" . NO_WORD . "';\n";
	echo "	var EDIT_MENU_ITEMS_STARTING_DELETE_MSG = '" . EDIT_MENU_ITEMS_STARTING_DELETE_MSG . "';\n";
	echo "	var EDITOR_STARTING_UPDATE_MSG = '" . EDITOR_STARTING_UPDATE_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_DATE_FORMAT_MSG = \"" . CALENDAR_EVENTS_DATE_FORMAT_MSG . "\";\n";
	echo "	var QUERY_READY_TO_SEND = '" . QUERY_READY_TO_SEND . "';\n";
	echo "	var QUERY_SENT = '" . QUERY_SENT . "';\n";
	echo "	var QUERY_COMPLETE = '" . QUERY_COMPLETE . "';\n";
	echo "	var QUERY_COMPLETE_NO_RECORD_FOUND = '" . QUERY_COMPLETE_NO_RECORD_FOUND . "';\n";
	echo "	var CALENDAR_EVENTS_LOCATING_EVENTS_MSG = '" . CALENDAR_EVENTS_LOCATING_EVENTS_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_COULD_NOT_DELETE_EVENT_MSG = '" . CALENDAR_EVENTS_COULD_NOT_DELETE_EVENT_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_EVENT_DELETED_MSG = '" . CALENDAR_EVENTS_EVENT_DELETED_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_DELETING_EVENT_MSG = '" . CALENDAR_EVENTS_DELETING_EVENT_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_COULD_NOT_UPDATE_EVENT_MSG = '" . CALENDAR_EVENTS_COULD_NOT_UPDATE_EVENT_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_EVENT_UPDATED_MSG = '" . CALENDAR_EVENTS_EVENT_UPDATED_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_UPDATING_EVENT_MSG = '" . CALENDAR_EVENTS_UPDATING_EVENT_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_COULD_NOT_UPDATE_USER_LISTS_MSG = '" . CALENDAR_EVENTS_COULD_NOT_UPDATE_USER_LISTS_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_USER_LIST_UPDATED_MSG = '" . CALENDAR_EVENTS_USER_LIST_UPDATED_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_UPDATING_USER_LIST_MSG = '" . CALENDAR_EVENTS_UPDATING_USER_LIST_MSG . "';\n";
	echo "	var CALENDAR_EVENTS_MEMORY_LIMIT_ERR = '" . CALENDAR_EVENTS_MEMORY_LIMIT_ERR . "';\n";
	echo "	var CALENDAR_EVENTS_STARTING_USER_QUERY_MSG = '" . CALENDAR_EVENTS_STARTING_USER_QUERY_MSG . "';\n";
?>
</script>

</head>

<?php
	$query = "SHOW TABLES";
	$result = $bwb_dbh->query($query);
	$tmp_table = "";
	$tables = array();
	$rows = $result->fetchAll(PDO::FETCH_NUM);
	foreach ($rows as $row)
	{
		$tmp_table = $row[0];
		if ((substr($tmp_table, 0, strlen(BWB_TABLE_PREFIX)) != BWB_TABLE_PREFIX) || (BWB_TABLE_PREFIX == ""))
			$tables[] = $tmp_table;
	}
	
	$access_levels = array();
	$query2 = "SELECT `access_level` FROM `" . BWB_TABLE_PREFIX . "access_levels`";
	$result2 = $bwb_dbh->query($query2);
	while ($row2 = $result2->fetch(PDO::FETCH_ASSOC))
	{
		$access_levels[] = $row2["access_level"];
	}
?>

<body id="workspace_body">

<div class="button_heading_sub_level_1"><div class="button_heading_text_sub_level_1"><?php echo CALENDAR_EVENTS_HEADING; ?></div><div class="button_heading_help" onClick="OpenHelpWindow();">&nbsp;</div></div>

<div align="center">

<div id="Schedule">
<table><tbody>
<?php
	echo "<tr bgcolor='#CCCCCC'>\n";
	echo "<td><b>" . DESCRIPTION_WORD . "</b></td>\n";
	echo "<td><b>" . DATE_WORD . " <font size=1>" . CALENDAR_EVENTS_DATE_FORMAT . "</font></b></td>\n";
	echo "<td><b>" . CALENDAR_EVENTS_START_TIME . "</b></td>\n";
	echo "<td><b>" . CALENDAR_EVENTS_END_TIME . "</b></td>\n";
	echo "<td><b>" . LINK_WORD . "</b></td>\n";
	echo "<td><b>" . TARGET_WORD . "</b></td>\n";
	echo "<td><b>" . ACTIVE_WORD . "</b></td>\n";
	echo "<td><b>" . USER_TABLE_WORD . "</b></td>\n";
	echo "<td><b>" . USERID_WORD . "</b></td>\n";
	echo "<td><b>" . ACCESS_LEVEL_WORD . "</b></td>\n";
	echo "<td>&nbsp;</td>\n";
	echo "<td>&nbsp;</td>\n";
	echo "</tr>\n";
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "schedule ORDER BY EventDateSort";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	for ($i = 0; $i < $num_results; $i++)
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		
		$ID = $row["ID"];
		$Description = $row["Description"];
		$EventDate = $row["EventDate"];
		$StartTime = $row["StartTime"];
		$EndTime = $row["EndTime"];
		$URL = $row["URL"];
		$target = $row["target"];
		$Active = $row["Active"];
		$user_table = $row["user_table"];
		$UserID = $row["UserID"];
		$AccessLevel = $row["AccessLevel"];
		$Description = stripslashes($Description);
		$EventDate = stripslashes($EventDate);
		$EventDateArr = explode("/", $EventDate);
		$EventDate = $EventDateArr[2] . "-" . $EventDateArr[0] . "-" . $EventDateArr[1];
		$StartTime = stripslashes($StartTime);
		$EndTime = stripslashes($EndTime);
		$URL = stripslashes($URL);
		
		if ($bgclr == "#DDDDDD")
			$bgclr = "#EEEEEE";
		else
			$bgclr = "#DDDDDD";
		
		echo "<tr bgcolor='$bgclr' id='tr_$i'>\n";
		echo "<td valign='top'><input type='hidden' id='ID$i' name='ID$i' value='$ID' /><textarea id='Description$i' name='Description$i'>$Description</textarea></td>\n";
		echo "<td valign='top'><input type='text' id='EventDate$i' name='EventDate$i' value='$EventDate' /></td>\n";
		echo "<td valign='top'><input type='text' id='StartTime$i' name='StartTime$i' value='$StartTime' /></td>\n";
		echo "<td valign='top'><input type='text' id='EndTime$i' name='EndTime$i' value='$EndTime' /></td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectPage$i\" onChange=\"document.getElementById('URL$i').value=this.value+'.php';\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($pages as $page_name)
		{
			$page_name_tmp = $page_name . ".php";
			if ($page_name_tmp == $URL)
				echo "<option value='$page_name' selected='selected'>$page_name</option>\n";
			else
				echo "<option value='$page_name'>$page_name</option>\n";
		}
		echo "</select>\n";
		echo "<input type='text' id='URL$i' name='URL$i' value='$URL'></td>\n";
		
		echo "<td valign='top'>\n<select name='target_select$i' id='target_select$i' onChange='PopulateTarget($i);'>\n";
		if ($target == "_self")
			echo "<option value='_self' selected='selected'>" . SELF_NORMAL . "</option>\n";
		else
			echo "<option value='_self'>" . SELF_NORMAL . "</option>\n";
		if ($target == "_blank")
			echo "<option value='_blank' selected='selected'>" . NEW_WINDOW . "</option>\n";
		else
			echo "<option value='_blank'>" . NEW_WINDOW . "</option>\n";
		if ((isset($target)) && ($target != ""))	{
			echo "<option value='enter frame name' selected='selected'>" . NAMED_FRAME . "</option>\n";
			echo "</select>\n";
			echo "<input type='text' name='target$i' id='target$i' value='$target' onclick=\"if (this.value == '" . ENTER_FRAME_NAME . "') this.value = '';\">\n</td>\n";
		}
		else	{
			echo "<option value='enter frame name'>" . NAMED_FRAME . "</option>\n";
			echo "</select>\n";
			echo "<input type='hidden' name='target$i' id='target$i' value='' onclick=\"if (this.value == '" . ENTER_FRAME_NAME . "') this.value = '';\">\n</td>\n";
		}
		
		echo "<td valign='top'>\n";
		if ($Active == 1)
			echo "<input id='Active$i' name='Active$i' type='checkbox' value='active' checked='checked'></td>\n";
		else
			echo "<input id='Active$i' name='Active$i' type='checkbox' value='active'></td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectUserTable$i\" id=\"selectUserTable$i\" onChange=\"RetrieveUsers(this.value, $i);\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($tables as $tmp_table)
		{
			if ($tmp_table == $user_table)
				echo "<option value='$tmp_table' selected='selected'>$tmp_table</option>\n";
			else
				echo "<option value='$tmp_table'>$tmp_table</option>\n";
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top' id='UserID" . $i . "_td'>\n";
		echo "<select name=\"selectUserID$i\" id=\"selectUserID$i\">\n";
		echo "<option value=''>-not selected-</option>\n";
		if ($user_table != "")
		{
			$query2 = "SELECT `UserID` FROM `" . $user_table . "`";
			$result2 = $bwb_dbh->query($query2);
			while ($row2 = $result2->fetch(PDO::FETCH_ASSOC))
			{
				$tmp_UserID = $row2["UserID"];
				if (($tmp_UserID != "") && (!is_null($tmp_UserID)))
				{
					if ($tmp_UserID == $UserID)
						echo "<option value='$tmp_UserID' selected='selected'>$tmp_UserID</option>\n";
					else
						echo "<option value='$tmp_UserID'>$tmp_UserID</option>\n";
				}
			}
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectAccessLevel$i\" id=\"selectAccessLevel$i\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($access_levels as $tmp_AccessLevel)
		{
			if ($tmp_AccessLevel == $AccessLevel)
				echo "<option value='$tmp_AccessLevel' selected='selected'>$tmp_AccessLevel</option>\n";
			else
				echo "<option value='$tmp_AccessLevel'>$tmp_AccessLevel</option>\n";
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top'><img src='images/save.png' border=0 alt='Update' name='Update$i' onClick=\"Update($i);\" style='cursor:pointer;' /></td>\n";
		$strSearch = array("\"", "'");
		$desc_remove = str_replace($strSearch, "", $Description);
		$desc_remove = str_replace("\n", " ", $desc_remove);
		echo "<td valign='top'><img src='images/delete.png' border=0 alt='Delete' name='Delete' onClick=\"DeleteConfirm('$desc_remove', $ID, $i);\" style='cursor:pointer;' /></td>\n";
		echo "</tr>\n";
	}
	
	/////////////////// Spare rows ////////////////////////
	for ($k = $i; $k < $i + $spares; $k++)
	{
		echo "<tr id='tr_$k' style='display:none;'>\n";
		echo "<td valign='top'><input type='hidden' id='ID$k' name='ID$k' value='-1' /><textarea id='Description$k' name='Description$k'></textarea></td>\n";
		echo "<td valign='top'><input type='text' id='EventDate$k' name='EventDate$k' value='' /></td>\n";
		echo "<td valign='top'><input type='text' id='StartTime$k' name='StartTime$k' value='' /></td>\n";
		echo "<td valign='top'><input type='text' id='EndTime$k' name='EndTime$k' value='' /></td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectPage$k\" onChange=\"document.getElementById('URL$k').value=this.value+'.php';\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($pages as $page_name)
		{
			echo "<option value='$page_name'>$page_name</option>\n";
		}
		echo "</select>\n";
		echo "<input type='text' id='URL$k' name='URL$k' value=''></td>\n";
		
		echo "<td valign='top'>\n<select name='target_select$k' id='target_select$k' onChange='PopulateTarget($k);'>\n";
		echo "<option value='_self'>" . SELF_NORMAL . "</option>\n";
		echo "<option value='_blank'>" . NEW_WINDOW . "</option>\n";
		echo "<option value='enter frame name'>" . NAMED_FRAME . "</option>\n";
		echo "</select>\n";
		echo "<input type='hidden' name='target$k' id='target$k' value='' onclick=\"if (this.value == '" . ENTER_FRAME_NAME . "') this.value = '';\">\n</td>\n";
		
		echo "<td valign='top'>\n";
		echo "<input id='Active$k' name='Active$k' type='checkbox' value='active' checked='checked'></td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectUserTable$k\" id=\"selectUserTable$k\" onChange=\"RetrieveUsers(this.value, $k);\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($tables as $tmp_table)
		{
			echo "<option value='$tmp_table'>$tmp_table</option>\n";
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top' id='UserID" . $i . "_td'>\n";
		echo "<select name=\"selectUserID$k\" id=\"selectUserID$k\">\n";
		echo "<option value=''>-not selected-</option>\n";
		if ($user_table != "")
		{
			$query2 = "SELECT `UserID` FROM `" . $user_table . "`";
			$result2 = $bwb_dbh->query($query2);
			while ($row2 = $result2->fetch(PDO::FETCH_ASSOC))
			{
				$tmp_UserID = $row2["UserID"];
				if (($tmp_UserID != "") && (!is_null($tmp_UserID)))
					echo "<option value='$tmp_UserID'>$tmp_UserID</option>\n";
			}
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top'>\n";
		echo "<select name=\"selectAccessLevel$k\" id=\"selectAccessLevel$k\">\n";
		echo "<option value=''>-not selected-</option>\n";
		foreach ($access_levels as $tmp_AccessLevel)
		{
			$tmp_AccessLevel = $row2["access_level"];
			echo "<option value='$tmp_AccessLevel'>$tmp_AccessLevel</option>\n";
		}
		echo "</select>\n";
		echo "</td>\n";
		
		echo "<td valign='top'><img src='images/save.png' border=0 alt='Update' name='Update$k' onClick=\"Update($k);\" style='cursor:pointer;' /></td>\n";
		echo "<td valign='top'><img src='images/delete.png' border=0 alt='Delete' name='Delete' onClick=\"DeleteConfirm('***new row***', -1, $k);\" style='cursor:pointer;' /></td>\n";
		echo "</tr>\n";
	}
?>
	</tbody></table>
</div>
<?php 
	echo "<input type='hidden' name='rec_cnt' id='rec_cnt' value='$i' />\n";
	$k = $i + $spares;
	echo "<input type='hidden' name='row_cnt' id='row_cnt' value='$k' />\n";
?>
	<div id="btn_add_event" class='button_console_add' onClick="AddEvent();"><span class='button_console_text_w_icon'><?php echo CALENDAR_EVENTS_ADD_EVENT_BTN; ?></span></div>
    <?php echo CALENDAR_EVENTS_SAVE_INSTRUCTIONS; ?> <img src="images/save.png" width="16" height="16" alt="Save" />.
</div>

<?php include("../lib/popup_msg.html"); ?>

<script language="JavaScript" type="text/javascript">
	popup_msg.HidePopupMsg();

window.onload = function(){
<?php
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "schedule ORDER BY EventDateSort";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	for ($i = 0; $i < $num_results + $spares; $i++)
		echo <<<END
		
	g_globalObject$i = new JsDatePick({
		useMode:2,
		target:"EventDate$i"
	});
	
END;

?>
};
</script>

</body>
</html>