<?php
	require_once('../config/config.php');
	require_once('../libs/dgsutil/cgi.php');
	require_once('../libs/util.php');

	global $config;

	$args = getFormData();

	if (isset($args['detail'])) {
		printNotification($event_title, $config, $args['detail']);	
	} else {
		printNotificationList($event_title, $config);
	}
?>

<?php
	function printNotificationList($event_title, $config) {
		global $event_new_color;

		$buffer_time = 15;

		$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
		mysql_select_db($config['databaseName'], $db);

		$sql = sprintf("SELECT id, UNIX_TIMESTAMP(start) as start, UNIX_TIMESTAMP(end) AS end, summary, detail FROM gui_event_message WHERE (start = 0 AND end = 0) OR (start = 0 AND NOW() < (end + INTERVAL %d MINUTE)) OR ((NOW() > start - INTERVAL %d MINUTE) AND end = 0) OR ((NOW() > start - INTERVAL %d MINUTE) AND (NOW() < end + INTERVAL %d MINUTE)) ORDER BY start DESC, end, id", $buffer_time, $buffer_time, $buffer_time, $buffer_time);
	
		$result = mysql_query($sql);

		for ($i = 0; $row = mysql_fetch_array($result); $i++) {
			$data[$i]['state'] = 'informational';
	
			$data[$i]['id'] = $row['id'];	
			$data[$i]['summary'] = $row['summary'];	
			$data[$i]['detail'] = $row['detail'];
			$data[$i]['start'] = $row['start'] != 0 ? date('M j H:i', $row['start']) : 'Unspecified';
			$data[$i]['end'] = $row['end'] != 0 ? date('M j H:i', $row['end']) : 'Unspecified';
		}
		mysql_close($db);

		printf("<TABLE CELLSPACING=\"1\" CELLPADDING=\"0\" BORDER=\"0\" WIDTH=\"100%%\">\n");
		printf("	<TR CLASS=\"tbltitle\">\n");
		printf("		<TD COLSPAN=\"2\" NOWRAP>\n");
		printf("			%s: %d<B>[<A CLASS=\"tbltitle\" HREF=\"\">Snapshot</A>]</B>\n", $event_title, count($data));
		printf("		</TD>\n");
		printf("	</TR>\n");

		printf("	<TR CLASS=\"tblhdr\">\n");
		printf("		<TD NOWRAP>\n");
		printf("			Time Stamp\n");
		printf("		</TD>\n");
		printf("		<TD WIDTH=\"100%%\" NOWRAP>\n");
		printf("			Summary\n");
		printf("		</TD>\n");
		printf("	</TR>\n");

		for ($i = 0; $i < count($data); $i++) {
			printf("	<TR CLASS=\"%s\">\n", $event_new_color[$data[$i]['state']][$i % 2]);
			printf("		<TD NOWRAP>\n");
			printf("			%s\n", $data[$i]['start']);
			printf("		</TD>\n");
			printf("		<TD NOWRAP>\n");
			printf("			<A HREF=\"?detail=%d&listing=%s\">%s</A>\n", $data[$i]['id'], $_SERVER['PHP_SELF'], $data[$i]['summary']);
			printf("		</TD>\n");
			printf("	</TR>\n");
		}
		printf("</TABLE>\n");
	}

	function printNotification($event_title, $config, $id) {
		$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
		mysql_select_db($config['databaseName'], $db);

		$sql = sprintf("SELECT id, UNIX_TIMESTAMP(start) as start, UNIX_TIMESTAMP(end) AS end, summary, detail FROM gui_event_message WHERE id = '%d'", $id);

		$result = mysql_query($sql);

		for ($i = 0; $row = mysql_fetch_array($result); $i++) {
			$data['state'] = 'informational';

			$data['id'] = $row['id'];	
			$data['summary'] = str_replace("\n", "<BR>\n", $row['summary']);	
			$data['detail'] = str_replace("\n", "<BR>\n", $row['detail']);
			$data['start'] = $row['start'] != 0 ? date('M j H:i', $row['start']) : 'Unspecified';
			$data['end'] = $row['end'] != 0 ? date('M j H:i', $row['end']) : 'Unspecified';
		}
		mysql_close($db);

		printf("<TABLE CELLSPACING=\"1\" CELLPADDING=\"0\" BORDER=\"0\" WIDTH=\"100%%\">\n");
		printf("	<TR CLASS=\"tbltitle\">\n");
		printf("		<TD COLSPAN=\"2\" NOWRAP>\n");
		printf("			%s: %s\n", $event_title, $data['summary']);
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("	<TR CLASS=\"tblentry\">\n");
		printf("		<TD VALIGN=\"top\" NOWRAP>\n");
		printf("			<B>Start Time</B>&nbsp;&nbsp;&nbsp;\n");
		printf("		</TD>\n");
		printf("		<TD WIDTH=\"100%%\">\n");
		printf("			%s\n", $data['start']);
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("	<TR CLASS=\"tblentry\">\n");
		printf("		<TD VALIGN=\"top\" NOWRAP>\n");
		printf("			<B>Stop Time</B>&nbsp;&nbsp;&nbsp;\n");
		printf("		</TD>\n");
		printf("		<TD WIDTH=\"100%%\">\n");
		printf("			%s\n", $data['end']);
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("	<TR CLASS=\"tblentry\">\n");
		printf("		<TD VALIGN=\"top\" NOWRAP>\n");
		printf("			<B>Summary</B>&nbsp;&nbsp;&nbsp;\n");
		printf("		</TD>\n");
		printf("		<TD WIDTH=\"100%%\">\n");
		printf("			%s\n", $data['summary']);
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("	<TR CLASS=\"tblentry\">\n");
		printf("		<TD VALIGN=\"top\" NOWRAP>\n");
		printf("			<B>Detail</B>&nbsp;&nbsp;&nbsp;\n");
		printf("		</TD>\n");
		printf("		<TD WIDTH=\"100%%\">\n");
		printf("			%s\n", $data['detail']);
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("	<TR CLASS=\"tblentry\">\n");
		printf("		<TD COLSPAN=\"2\" ALIGN=\"center\" NOWRAP>\n");
		printf("			[<A HREF=\"?\">Return To Listing</A>]\n");
		printf("		</TD>\n");
		printf("	</TR>\n");
		printf("</TABLE>\n");
	}
?>

