<?php

/*
** SellaNMS
** $Id: currentnotifications.php,v 1.3 2005/05/02 06:37:37 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

class CurrentNotifications extends EventViewer {
	function CurrentNotifications() {
		parent::EventViewer('Current Notifications');

		$this->addState('informational', 'eventalert1', 'eventalert2', 'eventacknowlege1', 'eventacknowlege2');
	}   

	function getContent($tabs) {
		$content = sprintf("<?php \$event_title = '%s'; ?>\n", $this->viewerTitle);

		reset($this->state);
		while (list($i, $s) = each($this->state)) {
			$content .= sprintf("<?php \$event_state[%d] = '%s'; ?>\n", $i, $s);

			$content .= sprintf("<?php \$event_new_color['%s'][0] = '%s'; ?>\n", $s, $this->new_color[$s][0]);
			$content .= sprintf("<?php \$event_new_color['%s'][1] = '%s'; ?>\n", $s, $this->new_color[$s][1]);
			$content .= sprintf("<?php \$event_ack_color['%s'][0] = '%s'; ?>\n", $s, $this->ack_color[$s][0]);
			$content .= sprintf("<?php \$event_ack_color['%s'][1] = '%s'; ?>\n", $s, $this->ack_color[$s][1]);
		}
		$content .= join('', file('modules/events/modules/templates/currentnotifications_template.php'));

		return $content;
	}

	function shouldUpdate() {
		global $config;

		$module_name = get_class($this);

		global ${$module_name};

		$current_events = array();
		$old_events = ${$module_name}['old_events'];
		if (!isset($old_events)) {
			$old_events = array();
		}

		$sql = sprintf("SELECT id, summary FROM gui_event_message WHERE (NOW() > start - INTERVAL %d MINUTE) AND (NOW() < end + INTERVAL %d MINUTE) ORDER BY start DESC, end, id", 15, 15);

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

		$result = mysql_query($sql);

		$changed = false;
		while ($row = mysql_fetch_array($result)) {
			$current_events[$row['id']] = true;

			if ($old_events[$row['id']] == true) {
				$old_events[$row['id']] = false;
			} else {
				$changed = true;
			}
		}
		mysql_close($db);

		while (list(,$i) = each($old_events)) {
			if ($i == true) {
				$changed = true;
			}
		}
		${$module_name}['old_events'] = $current_events;

		if ($changed == true) {
			return true;
		} else {
			return false;
		}
	}
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/

?>
