<?php 

/*
** SellaNMS
** $Id: notifications_template.php,v 1.4 2005/03/31 18:57:12 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

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

$data = getFormData();

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

if ($data['function'] == 'modify') {
	$function = 'modify_commit';

	$sql = sprintf("SELECT id, UNIX_TIMESTAMP(start) AS start, UNIX_TIMESTAMP(end) AS end, summary, detail FROM gui_event_message WHERE id = '%d'", $data['id']);
	$result = mysql_query($sql, $db);
	$row = mysql_fetch_array($result);

	$id = $row['id'];	

	$s_stamp = $row['start'];
	$s_choice = 'time';

	if ($row['end'] == 0) {
		$e_stamp = $s_stamp;
		$e_choice = 'never';
	} else {
		$e_stamp = $row['end'];
		$e_choice = 'time';
	}

	$summary = $row['summary'];
	$detail = $row['detail'];

	$title = $summary;
} else {
	$id = false;
	$function = 'create';

	$summary = false;
	$detail = false;

	$title = 'New Message';

	$s_choice = 'now';
	$e_choice = 'never';

	if ($data['function'] == 'create') {
		if ($data['start_time_now'] == 'yes') {
			$s_stamp = 'NOW()';
		} else {
			$s_stamp = sprintf("FROM_UNIXTIME(%d)", mktime($data['shour'], $data['sminute'], 0, $data['smonth'], $data['sday'], $data['syear']));
		}
		if ($data['stop_time_unknown'] == 'yes') {
			$e_stamp = '0';
		} else {
			$e_stamp = mktime($data['ehour'], $data['eminute'], 0, $data['emonth'], $data['eday'], $data['eyear']);
		}
		$sql = sprintf("INSERT INTO gui_event_message SET start = %s, end = FROM_UNIXTIME(%d), summary = '%s', detail = '%s'", $s_stamp, $e_stamp, $data['summary'], $data['detail']);

		mysql_query($sql, $db);
	} else if ($data['function'] == 'delete') {
		$sql = sprintf("DELETE FROM gui_event_message WHERE id = '%d'", $data['id']);
		mysql_query($sql, $db);
	} else if ($data['function'] == 'modify_commit') {
		if ($data['start_time_now'] == 'yes') {
			$s_stamp = 'NOW()';
		} else {
			$s_stamp = sprintf("FROM_UNIXTIME(%d)", mktime($data['shour'], $data['sminute'], 0, $data['smonth'], $data['sday'], $data['syear']));
		}
		if ($data['stop_time_unknown'] == 'yes') {
			$e_stamp = '0';
		} else {
			$e_stamp = mktime($data['ehour'], $data['eminute'], 0, $data['emonth'], $data['eday'], $data['eyear']);
		}
		$sql = sprintf("UPDATE gui_event_message SET start = %s, end = FROM_UNIXTIME(%d), summary = '%s', detail = '%s' WHERE id = '%d'", $s_stamp, $e_stamp, $data['summary'], $data['detail'], $data['id']);
		mysql_query($sql, $db);
	}
	$sql = sprintf("SELECT UNIX_TIMESTAMP(NOW()) AS time");
	$result = mysql_query($sql, $db);

	$row = mysql_fetch_array($result);
	$s_stamp = $row['time'];
	$e_stamp = $row['time'];
}
$sql = sprintf("SELECT id, UNIX_TIMESTAMP(start) AS start, UNIX_TIMESTAMP(end) AS end, summary FROM gui_event_message WHERE (end >= NOW()) OR (end = 0) ORDER BY start, end");
$result = mysql_query($sql, $db);

$messages = array();
for ($i = 0; $row = mysql_fetch_array($result); $i++) {
	$messages[$i]['start_time'] = $row['start'];
	$messages[$i]['end_time'] = $row['end'];
	$messages[$i]['summary'] = $row['summary'];
	$messages[$i]['id'] = $row['id'];
}
mysql_close($db);

?>

<CENTER>
<FORM>
	<INPUT TYPE="hidden" NAME="function" VALUE="<?php printf('%s', $function); ?>">

	<?php if ($id) { ?>
		<INPUT TYPE="hidden" NAME="id" VALUE="<?php printf('%s', $id); ?>">
	<?php } ?>

	<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR>
			<TD CLASS="tbltitle" COLSPAN="3" NOWRAP>
				Event Notification: <?php printf('%s', $title); ?>
			</TD>
		</TR>
		<TR>
			<TD CLASS="tblentry" NOWRAP><B>Start Time</B></TD>
			<TD NOWRAP CLASS="tblentry">
				<INPUT TYPE="radio" NAME="start_time_now" VALUE="yes" CLASS="radiobutton"<?php printf('%s', $s_choice == 'now' ? ' CHECKED' : ''); ?>>
			</TD>
			<TD NOWRAP CLASS="tblentry"><B>Use Current Time</B></TD>
		</TR>
		<TR>
			<TD NOWRAP CLASS="tblentry">&nbsp;</TD>
			<TD NOWRAP CLASS="tblentry">
				<INPUT TYPE="radio" VALUE="no" NAME="start_time_now" CLASS="radiobutton"<?php printf('%s', $s_choice == 'time' ? ' CHECKED' : ''); ?>>
			</TD>
			<TD NOWRAP CLASS="tblentry">
				<SELECT name="smonth" CLASS="combobox">
					<?php
						for ($i = 1; $i < 13; $i++) {
							printf('<OPTION VALUE="%d"%s>%s</OPTION>', $i, ($i == date('n', $s_stamp)) ? ' SELECTED' : '', date('M', mktime(0, 0, 0, $i)));
						}
					?>
				</SELECT>
				<SELECT name="sday" CLASS="combobox">
					<?php
						for ($i = 1; $i < 32; $i++) {
							printf('<OPTION VALUE="%d"%s>%d</OPTION>', $i, ($i == date('j', $s_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>,</B>
				<SELECT name="syear" CLASS="combobox">
					<?php
						for ($i = (date('Y', $e_stamp) - 1); $i < (date('Y', $e_stamp) + 2); $i++) {
							printf('<OPTION VALUE="%d"%s>%d</OPTION>', $i, ($i == date('Y', $s_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>&nbsp;&nbsp;at&nbsp;&nbsp;</B>
				<SELECT name="shour" CLASS="combobox">
					<?php
						for ($i = 0; $i < 24; $i++) {
							printf('<OPTION VALUE="%d"%s>%02d</OPTION>', $i, ($i == date('G', $s_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>:</B>
				<SELECT name="sminute" CLASS="combobox">
					<?php
						for ($i = 0; $i < 60; $i++) {
							printf('<OPTION VALUE="%d"%s>%02d</OPTION>', $i, ($i == date('i', $s_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
			</TD>
		</TR>

		<TR> <TD CLASS="tblentry">&nbsp;</TD> <TD COLSPAN="2" CLASS="tblentry">&nbsp;</TD> </TR>

		<TR>
			<TD CLASS="tblentry" NOWRAP><B>End Time</B></TD>
			<TD NOWRAP CLASS="tblentry">
				<INPUT TYPE="radio" NAME="stop_time_unknown" VALUE="yes" CLASS="radiobutton"<?php printf('%s', $e_choice == 'never' ? ' CHECKED' : ''); ?>>
			</TD>
			<TD NOWRAP CLASS="tblentry"><B>Display Until Deleted</B></TD>
		</TR>
		<TR>
			<TD NOWRAP CLASS="tblentry">&nbsp;</TD>
			<TD NOWRAP CLASS="tblentry">
				<INPUT TYPE="radio" NAME="stop_time_unknown" VALUE="no" CLASS="radiobutton"<?php printf('%s', $e_choice == 'time' ? ' CHECKED' : ''); ?>>
			</TD>
			<TD NOWRAP CLASS="tblentry">
				<SELECT name="emonth" CLASS="combobox">
					<?php
						for ($i = 1; $i < 13; $i++) {
							printf('<OPTION VALUE="%d"%s>%s</OPTION>', $i, ($i == date('n', $e_stamp)) ? ' SELECTED' : '', date('M', mktime(0, 0, 0, $i)));
						}
					?>
				</SELECT>
				<SELECT name="eday" CLASS="combobox">
					<?php
						for ($i = 1; $i < 32; $i++) {
							printf('<OPTION VALUE="%d"%s>%d</OPTION>', $i, ($i == date('j', $e_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>,</B>
				<SELECT name="eyear" CLASS="combobox">
					<?php
						for ($i = (date('Y', $e_stamp) - 1); $i < (date('Y', $e_stamp) + 2); $i++) {
							printf('<OPTION VALUE="%d"%s>%d</OPTION>', $i, ($i == date('Y', $e_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>&nbsp;&nbsp;at&nbsp;&nbsp;</B>
				<SELECT name="ehour" CLASS="combobox">
					<?php
						for ($i = 0; $i < 24; $i++) {
							printf('<OPTION VALUE="%d"%s>%02d</OPTION>', $i, ($i == date('G', $e_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
				<B>:</B>
				<SELECT name="eminute" CLASS="combobox">
					<?php
						for ($i = 0; $i < 60; $i++) {
							printf('<OPTION VALUE="%d"%s>%02d</OPTION>', $i, ($i == date('i', $e_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
			</TD>
			<TD COLSPAN="1">
				&nbsp;
			</TD>
		</TR>

		<TR> <TD CLASS="tblentry">&nbsp;</TD> <TD COLSPAN="2" CLASS="tblentry">&nbsp;</TD> </TR>

		<TR>
			<TD CLASS="tblentry" VALIGN="middle" NOWRAP><B>Summary</B></TD>
			<TD COLSPAN="2">
				<INPUT NAME="summary" TYPE="TEXT" VALUE="<?php printf('%s', $summary); ?>" SIZE="82" CLASS="textfield">
			</TD>
			<TD>
				&nbsp;
			</TD>
		</TR>
		<TR>
			<TD CLASS="tblentry" VALIGN="top" NOWRAP><B>Detail</B></TD>
			<TD ROWSPAN="10" COLSPAN="2">
				<TEXTAREA NAME="detail" ROWS="10" COLS="80" CLASS="textarea"><?php printf('%s', $detail); ?></TEXTAREA>
			</TD>
			<TD ROWSPAN="10">
				&nbsp;
			</TD>
		</TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR><TD CLASS="tblentry">&nbsp;</TD></TR>
		<TR>
			<TD COLSPAN="3" ALIGN="center">
				<INPUT TYPE="submit" VALUE="Submit">&nbsp;<INPUT TYPE="reset" VALUE="Reset">
			</TD>
			<TD>
				&nbsp;
			</TD>
		</TR>
	</TABLE>

	<BR>

	<?php if (count($messages) > 0) { ?>
	<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR CLASS="tbltitle">
			<TD COLSPAN="4">
				Scheduled Messages
			</TD>
		</TR>
		<TR CLASS="tblhdr">
			<TD NOWRAP>
				&nbsp;
			</TD>
			<TD NOWRAP>
				Start Time
			</TD>
			<TD NOWRAP>
				End Time
			</TD>
			<TD>
				Summary
			</TD>
		</TR>
		<?php for ($i = 0; $i < count($messages); $i++) { ?>
			<TR CLASS="tblentry">
				<TD NOWRAP>
					[<A HREF="?function=delete&id=<?php printf('%s', $messages[$i]['id']); ?>">delete</A>] [<A HREF="?function=modify&id=<?php printf('%s', $messages[$i]['id']); ?>">modify</A>]
				</TD>
				<TD NOWRAP>
					<?php printf('%s', date('M j, Y H:i', $messages[$i]['start_time'])); ?>
				</TD>
				<TD NOWRAP>
					<?php printf('%s', ($messages[$i]['end_time'] != 0) ? date('M j, Y H:i', $messages[$i]['end_time']) : ''); ?>
				</TD>
				<TD NOWRAP>
					<?php printf('%s', $messages[$i]['summary']); ?>
				</TD>
			</TR>
		<?php } ?>
	</TABLE>
	<?php } ?>
</FORM>
</CENTER>
