<?php

/*
** SellaNMS
** $Id: eventviewer.php,v 1.30 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

class EventViewer extends Module {
	function EventViewer($viewer_title) {
		$this->viewerTitle = $viewer_title;

		$this->cache_ext = 'php';
	}

	function addState($state, $new_color1, $new_color2, $ack_color1, $ack_color2) {
		$this->state[] = $state;

		$this->new_color[$state][0] = $new_color1;
		$this->new_color[$state][1] = $new_color2;
		$this->ack_color[$state][0] = $ack_color1;
		$this->ack_color[$state][1] = $ack_color2;
	}	

	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/eventviewer_template.php'));

		return $content;
	}

	function shouldUpdate() {
		global $config;

		$module_name = get_class($this);

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

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

		reset($this->state);

		$sql = sprintf("SELECT gui_event.id AS events FROM gui_event WHERE ");
		for ($i = count($this->state); list($i, $s) = each($this->state); $i--) {
			if ($i > 0) {
				$sql .= sprintf(" OR ");
			}
			$sql .= sprintf("gui_event.state = '%s'", $s);
		}
		$sql .= sprintf(" GROUP BY ip, interface, element, brief ORDER BY timestamp");
		$result = mysql_query($sql, $db);

		while ($row = mysql_fetch_array($result)) {
			$current_events += $row['events'];
		}

		mysql_close($db);

		if ($current_events == $old_events) {
			$ret_val = false;
		} else {
			$ret_val = true;
			$_SESSION[$module_name]['old_events'] = $current_events;
		}

		$_SESSION[$module_name]['refresh'] = $ret_val;

		return $ret_val;
	}
}

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

?>
