<?php 

/*
** SellaNMS
** $Id: notifications_template.php,v 1.2 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('../config/config.php');
require('../libs/dgsutil/cgi.php');
require('../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 messages WHERE id = '%d'", $data['id']);
	$result = mysql_query($sql, $db);
	$row = mysql_fetch_array($result);

	$id = $row['id'];	

	$s_stamp = $row['start'];
	$e_stamp = $row['end'];

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

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

	$summary = false;
	$detail = 'Submitted By:
Submitted On:

';

	$title = 'New Message';

	if ($data['function'] == 'create') {
		$s_stamp = mktime($data['shour'], $data['shour'], 0, $data['smonth'], $data['sday'], $data['syear']);
		$e_stamp = mktime($data['ehour'], $data['ehour'], 0, $data['emonth'], $data['eday'], $data['eyear']);

		$sql = sprintf("INSERT INTO messages SET start = FROM_UNIXTIME(%d), 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 messages WHERE id = '%d'", $data['id']);
		mysql_query($sql, $db);
	} else if ($data['function'] == 'modify_commit') {
		$s_stamp = mktime($data['shour'], $data['shour'], 0, $data['smonth'], $data['sday'], $data['syear']);
		$e_stamp = mktime($data['ehour'], $data['ehour'], 0, $data['emonth'], $data['eday'], $data['eyear']);

		$sql = sprintf("UPDATE messages SET start = FROM_UNIXTIME(%d), 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 messages WHERE end >= NOW() 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);

?>

<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" WIDTH="100%">
		<TR>
			<TD CLASS="tbltitle" COLSPAN="3" NOWRAP>
				Message Information: <?php printf('%s', $title); ?>
			</TD>
			<TD COLSPAN="1" WIDTH="100%">&nbsp;</TD>
		</TR>
		<TR>
			<TD CLASS="tblhdr" NOWRAP>Start Time</TD>
			<TD COLSPAN="2" NOWRAP>
				<SELECT name="smonth">
					<?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">
					<?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">
					<?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">
					<?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">
					<?php
						for ($i = 0; $i < 60; $i++) {
							printf('<OPTION VALUE="%d"%s>%02d</OPTION>', $i, ($i == date('i', $s_stamp)) ? ' SELECTED' : '', $i);
						}
					?>
				</SELECT>
			</TD>
			<TD>
				&nbsp;
			</TD>
		</TR>
		<TR>
			<TD CLASS="tblhdr" NOWRAP>End Time</TD>
			<TD COLSPAN="2" NOWRAP>
				<SELECT name="emonth">
					<?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">
					<?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">
					<?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">
					<?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">
					<?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="tblhdr" VALIGN="middle" NOWRAP>Summary</TD>
			<TD COLSPAN="2">
				<INPUT NAME="summary" TYPE="TEXT" VALUE="<?php printf('%s', $summary); ?>" SIZE="82">
			</TD>
			<TD>
				&nbsp;
			</TD>
		</TR>
		<TR>
			<TD CLASS="tblhdr" VALIGN="top" NOWRAP>Detail</TD>
			<TD ROWSPAN="10" COLSPAN="2">
				<TEXTAREA NAME="detail" ROWS="10" COLS="80"><?php printf('%s', $detail); ?></TEXTAREA>
			</TD>
			<TD ROWSPAN="10">
				&nbsp;
			</TD>
		</TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR><TD>&nbsp;</TD></TR>
		<TR>
			<TD COLSPAN="3" ALIGN="right">
				<INPUT TYPE="submit" VALUE="Submit"><INPUT TYPE="reset" VALUE="Reset">
			</TD>
			<TD WIDTH="100%">
				&nbsp;
			</TD>
		</TR>
	</TABLE>

	<BR>

	<?php if (count($messages) > 0) { ?>
	<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0" WIDTH="100%">
		<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 WIDTH="100%">
				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', 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>
