<?php
/*
 *      PHP Frontend for pocsag monitor
 *
 *      Copyright (C) 2004-2005
 *          Manuel Weiser (manuelw@fire-devils.org)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

require('config.inc.php');

if (!isset($count_rd_dme)) $count_rd_dme="";
if (!isset($count_ff_dme)) $count_ff_dme="";
if (!isset($count_rd_zvei)) $count_rd_zvei="";
if (!isset($count_ffvei)) $count_ff_zvei="";
if (!isset($count_rd_kfz)) $count_rd_kfz="";
if (!isset($count_ff_kfz)) $count_ff_kfz="";
if (!isset($stats_ff)) $stats_ff="";
if (!isset($stats_rd)) $stats_rd="";
if (!isset($stats_thw)) $stats_thw="";
if (!isset($stats_kat)) $stats_kat="";


/**************************/
// Seite zusammenstellen
//

	$result	= mysqli_query($dbconn,  "SELECT a.rec_typ, b.org
	                        FROM ric_zvei a
	                        LEFT JOIN organisation b ON b.id = a.org_id") or die (mysqli_error());
	while($row = mysqli_fetch_array($result))
	{
		if( $row["org"] == 'RD' )
		{
			if( $row["rec_typ"] == 'DME' ) $count_rd_dme++;
			else $count_rd_zvei++;
		}
		elseif( $row["org"] != 'RD' && $row["org"] != 'RLS' )
		{
			if( $row["rec_typ"] == 'DME' ) $count_ff_dme++;
			else $count_ff_zvei++;
  		}
	}
	
	$result	= mysqli_query($dbconn,  "SELECT a.org_id, b.org
	                        FROM kfz_fms a
	                        LEFT JOIN organisation b ON b.id = a.org_id") or die (mysqli_error());
	while($row = mysqli_fetch_array($result))
	{
		if( $row["org"] == 'RD' ) $count_rd_kfz++;
		elseif( $row["org"] != 'RD' ) $count_ff_kfz++;
	}

	$result	= mysqli_query($dbconn,  "SELECT org_name, alarm_count, DATE_FORMAT(last_alarm, '%Y%m%d%H%i%s') AS last_alarm FROM organisation WHERE org = 'FF' AND alarm_count != 0 OR org = 'KBM' AND alarm_count != 0 OR org = 'BF' AND alarm_count != 0 ORDER BY alarm_count DESC, last_alarm DESC, org ASC, org_name ASC") or die (mysql_error());
	while($row = mysqli_fetch_array($result))
	{
		if( !isset($row["org"])) $row["org"]="";
		$stats_ff .= '
				      <tr>
        				<td bgcolor="'.$orgcolor[6].'">&nbsp;'.$row["org"].' '.$row["org_name"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[6].'">&nbsp;'.$row["alarm_count"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[6].'">&nbsp;'.get_date($row["last_alarm"]).'</td>
				      </tr>';
	}
	
	$result	= mysqli_query($dbconn,  "SELECT org_name, alarm_count, DATE_FORMAT(last_alarm, '%Y%m%d%H%i%s') AS last_alarm FROM organisation WHERE org = 'RD' AND alarm_count != 0 ORDER BY alarm_count DESC, last_alarm DESC, org ASC, org_name ASC") or die (mysql_error());
	while($row = mysqli_fetch_array($result))
	{
	 	if( !isset($row["org"])) $row["org"]="";
		$stats_rd .= '
				      <tr>
        				<td bgcolor="'.$orgcolor[8].'">&nbsp;'.$row["org"].' '.$row["org_name"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[8].'">&nbsp;'.$row["alarm_count"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[8].'">&nbsp;'.get_date($row["last_alarm"]).'</td>
				      </tr>';
	}
	
	$result	= mysqli_query($dbconn,  "SELECT org_name, alarm_count, DATE_FORMAT(last_alarm, '%Y%m%d%H%i%s') AS last_alarm FROM organisation WHERE org = 'THW' AND alarm_count != 0 ORDER BY alarm_count DESC, last_alarm DESC, org ASC, org_name ASC") or die (mysql_error());
	while($row = mysqli_fetch_array($result))
	{
		if( !isset($row["org"])) $row["org"]="";
		$stats_thw .= '
				      <tr>
        				<td bgcolor="'.$orgcolor[7].'">&nbsp;'.$row["org"].' '.$row["org_name"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[7].'">&nbsp;'.$row["alarm_count"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[7].'">&nbsp;'.get_date($row["last_alarm"]).'</td>
				      </tr>';
	}
	
	$result	= mysqli_query($dbconn,  "SELECT org_name, alarm_count, DATE_FORMAT(last_alarm, '%Y%m%d%H%i%s') AS last_alarm FROM organisation WHERE org = 'KAT' AND alarm_count != 0 ORDER BY alarm_count DESC, last_alarm DESC, org ASC, org_name ASC") or die (mysql_error());
	while($row = mysqli_fetch_array($result))
	{
		$stats_kat .= '
				      <tr>
        				<td bgcolor="'.$orgcolor[4].'">&nbsp;'.$row["org"].' '.$row["org_name"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[4].'">&nbsp;'.$row["alarm_count"].'</td>
				        <td align="center" bgcolor="'.$orgcolor[4].'">&nbsp;'.get_date($row["last_alarm"]).'</td>
				      </tr>';
	}
	
	// Seiteninhalt ausgeben
	eval ("\$page_middle .= \"".gettemplate($template_dir."body_stats")."\";");

/**************************/
// Ausgabe Rahmen
//
eval ("dooutput(\"".gettemplate($template_dir."rahmen")."\");");

/**************************/
// Datenbank schliessen
//
mysqli_close($dbconn);
?>