<?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($_SESSION["querystring"])) $_SESSION["querystring"] = "";
	if( $_SERVER['QUERY_STRING'] != $_SESSION["querystring"] )
	{
		//$_SESSION["lastupdate"] = "";
		$_SESSION["online_users"] = "";
		$_SESSION["querystring"]	= $_SERVER['QUERY_STRING'];
	}

	// Filter Datum/Zeit erstellen
	if( !isset($_GET["filter"]) || $_GET["filter"] == 1 )
	{ 
		$show_filter = 1;
	} 
	else
	{
		$filter = $_GET["filter"]; 
	}

if (!isset($_SESSION["lastupdate"])) $_SESSION["lastupdate"]="";
if (!isset($show_filter)) $show_filter="";

if( $show_filter == 1 && $_SESSION["lastupdate"] == "" && !isset($_GET["clean"]) ) {
	$show_filter 		= filter_zeit(1);
	$show_filter		= " WHERE a.zeit > '$show_filter' ";
} 
elseif( $show_filter == 1 && $_SESSION["lastupdate"] != "" && !isset($_GET["clean"]) ) {
	$show_filter		= " WHERE a.num > ".$_SESSION["lastupdate"];
}
elseif( $show_filter == 1 && $_SESSION["lastupdate"] != "" && isset($_GET["clean"]) && $_GET["clean"] == "yes" ) {
	$show_filter 		= filter_zeit(1);
	$show_filter		= " WHERE a.zeit > '$show_filter' ";
}
else { //if( $show_filter != 1 ) {
	$show_filter 		= filter_zeit((isset($filter)) ? $filter : 1);
	$show_filter		= " WHERE a.zeit > '$show_filter' ";
} 

/////////

		if (!isset($_GET["org_filter"])) $_GET["org_filter"] = "";
		if( $_GET["org_filter"] == 'ff' )
		{
			$sql_org_filter = 'AND (org = "BF" OR org = "FF" OR org = "THW")';
		}
		elseif ( $_GET["org_filter"] == 'rd' )
		{
			$sql_org_filter = 'AND org = "RD"';
		}
		else
		{
			$sql_org_filter = ''; 
		}
		
		$_SESSION["orgfiltered"] = $sql_org_filter;

		$show_filter = $show_filter.' '.$sql_org_filter;

	//
	// Daten f¸r Formatierung holen und in array->session schreiben
	if( (!isset($_SESSION["formatierung"]) || $_SESSION["formatierung"]=="") && (!isset($_GET["nooutput"]) || $_GET["nooutput"] == "") )
	{
		$result	= mysqli_query($dbconn, "SELECT id, bg_farbe, text_farbe, text_format, text_groesse, text_groesse_text FROM formatierung ORDER BY id ASC") or die (mysqli_error($dbconn));
		while($row = mysqli_fetch_array($result))
		{
			$formatierung[$row["id"]] = array(
				"bg_farbe"			=> $row["bg_farbe"],
				"text_farbe"		=> $row["text_farbe"],
				"text_format"		=> $row["text_format"],
				"text_groesse"		=> $row["text_groesse"],
				"text_groesse_text"	=> $row["text_groesse_text"]
			);
														
			$_SESSION["formatierung"] = $formatierung;
		}
	} else {
		$formatierung = $_SESSION["formatierung"];
	}
	
	//
	// Auslesen pocsag
	//
	$result	= mysqli_query($dbconn, "SELECT a.num, a.adresse AS adresse_neu, a.bezeichnung AS bezeichnung_neu, a.text, DATE_FORMAT(a.zeit, '%Y%m%d%H%i%s') AS zeit, a.funktion, a.REC_TYP, a.done,
					b.adresse, b.formatierung_id, b.bezeichnung,
					d.id AS org_id, d.org_name, d.org, d.alarm_count, d.last_alarm
				FROM dme a
				LEFT JOIN ric_zvei b ON b.adresse = a.adresse
				LEFT JOIN organisation d ON d.id = b.org_id
				$show_filter
				ORDER BY a.zeit DESC") or die (mysqli_error($dbconn));
	while($row = mysqli_fetch_array($result))
	{
		//echo "<br>-----------";
		if( !isset($_GET["filter"]) || $_GET["filter"] == 1 || (isset($_GET["clean"]) && $_GET["clean"] == "yes") )
		{
			if( $row["num"] > $_SESSION["lastupdate"] ) $_SESSION["lastupdate"] = $row["num"];
		}

		$bezeichnung	= $row["bezeichnung"];
		
		// Tageswechsel ausgeben
		if( isset($lastday) && get_date($row["zeit"]) != $lastday )
		{
			$tageswechsel = get_timestamp(str_replace("-","",date_dbformat($lastday))."000000");
			
			//$nextday = '
			$show[$tageswechsel] = '
					<tr>
					  <td colspan="5" bgcolor="black"><span style="color:white;font-size:16px;font-weight:bold">&nbsp;Tageswechsel: ' . get_date_long($row["zeit"]) . '</span></td>
					</tr>
			';
		}
		else
		{
			$nextday = '';
		}
			$lastday	= get_date($row["zeit"]);

		// Eintragen der Bezeichnung wenn Bezeichnung in monrc vorhanden und update 1
		if( $update_adresse_monrc && !$bezeichnung )
		{
			// prfen ob die ric schon vorhanden ist, wenn nicht eintragen
			$result_check = mysqli_query($dbconn, "SELECT id FROM ric_zvei WHERE adresse=".$row["adresse_neu"]);
			$row_check = mysqli_fetch_array($result_check);
			
			if( !$row_check["id"] )
			{
				$result_check 	= mysqli_query($dbconn, "INSERT INTO ric_zvei (adresse, bezeichnung, rec_typ, formatierung_id) VALUES ('$row[adresse_neu]', '$row[bezeichnung_neu]', '$row[REC_TYP]', '0')") or die(mysqli_error($dbconn));
				$bezeichnung	= $row["bezeichnung_neu"];
			}
		}
		
		// Wenn unbekannte Ric Wert setzen
		$isNew = false;
		if( empty($row["org_name"]) ) {
            $check_imp = mysqli_query($dbconn, "SELECT id, i_org, i_ort, i_zusatz FROM import_ric WHERE i_ric=".$row["adresse_neu"]);
            $imp = mysqli_fetch_array($check_imp);

            if( !empty($imp["id"])) {
            	$bezeichnung = $row["adresse_neu"];
				$row["org_name"] = $imp["i_org"]." ".$imp["i_ort"]." ".$imp["i_zusatz"];
				$isNew = true;
			}
            else {
                $bezeichnung = 'Unbekannt';
            }
        }
		if( !$row["org_name"] ) $row["org_name"] = $row["adresse_neu"];
		
		// Wenn ZVEI
		if( $row["REC_TYP"] == 'ZVEI' ) $row["funktion"] = $row["text"];
		
		if (isset($formatierung[$row["formatierung_id"]]["bg_farbe"])) $row["bg_farbe"]					= $formatierung[$row["formatierung_id"]]["bg_farbe"];
		if (isset($formatierung[$row["formatierung_id"]]["text_farbe"])) $row["text_farbe"] 			= $formatierung[$row["formatierung_id"]]["text_farbe"];
		if (isset($formatierung[$row["formatierung_id"]]["text_farbe_text"])) $row["text_farbe_text"]	= $formatierung[$row["formatierung_id"]]["text_farbe_text"];
		if (isset($formatierung[$row["formatierung_id"]]["bg_farbe_text"])) $row["bg_farbe_text"] 		= $formatierung[$row["formatierung_id"]]["bg_farbe_text"];
		if (isset($formatierung[$row["formatierung_id"]]["text_groesse"])) $row["text_groesse"] 		= $formatierung[$row["formatierung_id"]]["text_groesse"];
		if (isset($formatierung[$row["formatierung_id"]]["text_groesse_text"])) $row["text_groesse_text"] 	= $formatierung[$row["formatierung_id"]]["text_groesse_text"];
		if (isset($formatierung[$row["formatierung_id"]]["text_format"])) $row["text_format"]			= $formatierung[$row["formatierung_id"]]["text_format"];
		
		// default Farben bestimmen
        if( !isset($row["bg_farbe"]) && $isNew == false ) {
            $row["bg_farbe"] = '#FFFFFF';
        }
		elseif( !isset($row["bg_farbe"]) && $isNew == true ) {
            $row["bg_farbe"] = '#E8C700';
        }
		//if( !isset($row["bg_farbe"]) )				$row["bg_farbe"]			= 'white';
		if( !isset($row["text_farbe"]) )			$row["text_farbe"]			= 'black';
		if( !isset($row["text_farbe_text"]) )		$row["text_farbe_text"] 	= 'black';
		if( !isset($row["bg_farbe_text"]) )			$row["bg_farbe_text"] 		= '#CCCCCC';
		if( !isset($row["text_groesse"]) )			$row["text_groesse"]		= '13';
		if( !isset($row["text_groesse_text"]) )		$row["text_groesse_text"]	= '12';
        if( !isset($row["text_format"]) )			$row["text_format"]			= '1';
		
		// pocsag in Array schreiben
		//$thisday = $nextday.'
		$thisday = '
				<tr bgcolor="'.$row["bg_farbe"].'">
				  <td width="'.$pocsag_sp[1].'" align="center"><span style="color:white;font-size:'.$status_size.';font-weight:bold">'.get_time($row["zeit"]).'&nbsp;</span></td>
				  <td width="'.$pocsag_sp[2].'" align="center"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'"> '.$row["org"].'</span></td>
				  <td width="'.$pocsag_sp[3].'" align="center"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">'.$bezeichnung.'</span></td>
				  <td><span title="'.$row["adresse"].'" style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'"> &nbsp;&nbsp;'.$row["org_name"].'</span></td>
				  <td width="'.$pocsag_sp[5].'">
				  	<span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">&nbsp;'.$row["funktion"].'</span>
				  </td>
				</tr>
		';
		// wenn das ne ZVEI ist keine Textzeile ausgeben
		if( $row["REC_TYP"] != "ZVEI" )
		{
			$thisday .= '
				<tr bgcolor="'.$row["bg_farbe_text"].'">
				  <td colspan="5">
					<span style="color:'.$row["text_farbe_text"].';font-size:'.$row["text_groesse_text"].'">'.$row["text"].'</span>
				  </td>
				</tr>
			';
		}
		
			// Falls sich Zeit letzter Status mit pocsag berschneiden um eins erhÔøΩen
			$show_time = get_timestamp($row["zeit"]);
			if( isset($show) )
			{
				while( array_key_exists($show_time, $show) )
				{
					$show_time = ($show_time+1);
				}
			}
		
		$show[$show_time] = $thisday;
	}

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

// Ausgabe für Ajax
echo "
<html>
";

//if ($new_unwetter == 1) echo '<meta http-equiv="refresh" content="1">';

if ( isset($show) )
{
	echo '<table width="98%"  border="0" cellspacing="1" cellpadding="0" bgcolor="#000000" valign="top">';
	foreach ( $show as $item )
	{
		echo $item;
	}
	echo '</table>';
}

echo "
</html>
";
?>