<?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($_REQUEST["search_what"])) $_REQUEST["search_what"]="";
if (!isset($search_word)) $search_word="";
if (!isset($search_word_org)) $search_word_org="";
if (!isset($_REQUEST["search_org"])) $_REQUEST["search_org"]="";
if (!isset($_REQUEST["search_ric"])) $_REQUEST["search_ric"]="";
if (!isset($_REQUEST["search_max_results"])) $_REQUEST["search_max_results"]="";
if (!isset($_REQUEST["search_time"])) $_REQUEST["search_time"]="";
if (!isset($_REQUEST["search_time_from"])) $_REQUEST["search_time_from"]="";
if (!isset($_REQUEST["search_time_to"])) $_REQUEST["search_time_to"]="";
if (!isset($_REQUEST["Submit"])) $_REQUEST["Submit"]="";
if (!isset($_REQUEST["select_tag_from"])) $_REQUEST["select_tag_from"]="";
if (!isset($_REQUEST["select_monat_from"])) $_REQUEST["select_monat_from"]="";
if (!isset($_REQUEST["select_tag_to"])) $_REQUEST["select_tag_to"]="";
if (!isset($_REQUEST["select_monat_to"])) $_REQUEST["select_monat_to"]="";
if (!isset($show)) $show="";

/**************************/
// Seite zusammenstellen
//
	// Selects
	$select_search_what1 = '<input name="search_what" type="radio" value="word"'; if($_REQUEST["search_what"]=='word' || !$_REQUEST["search_what"]) {$select_search_what1.=' checked';}else{$select_search_what1.='';}$select_search_what1.='>';
	$select_search_what2 = '<input name="search_what" type="radio" value="ric"'; if($_REQUEST["search_what"]=='ric') {$select_search_what2.=' checked';}else{$select_search_what2.='';}$select_search_what2.='>';
	
	$select_search_org = '
		<select name="search_org" id="search_org">
                  <option value="0"'; if($_REQUEST["search_org"]=='0') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>Alles</option>
                  <option value="FF"'; if($_REQUEST["search_org"]=='FF') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>FF</option>
                  <option value="BF"'; if($_REQUEST["search_org"]=='BF') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>BF</option>
                  <option value="RD"'; if($_REQUEST["search_org"]=='RD') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>RD</option>
                  <option value="THW"'; if($_REQUEST["search_org"]=='THW') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>THW</option>
                  <option value="KBM"'; if($_REQUEST["search_org"]=='KBM') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>KBM</option>
                  <option value="RLS"'; if($_REQUEST["search_org"]=='RLS') {$select_search_org.=' selected';}else{$select_search_org.='';} $select_search_org.='>RLS</option>
                </select>
	';
	
	$select_search_ric = '
		<select name="search_ric" id="search_ric">';

	$result	= mysqli_query($dbconn, "SELECT id, org_name, org FROM organisation WHERE org != '' ORDER BY org ASC, org_name ASC") or die (mysqli_error($dbconn));
	while($row = mysqli_fetch_array($result))
	{
		$select_search_ric .= '
			<option value="'.$row["id"].'"'; if($_REQUEST["search_ric"]==$row["id"]) {$select_search_ric.=' selected';}else{$select_search_ric.='';} $select_search_ric.='>'.$row["org"].' &nbsp;&nbsp;'.$row["org_name"].'</option>
		';
	}
	
	$select_search_ric .= '
                </select>	
	';
	
	$select_max_results = '
		<select name="search_max_results" id="search_max_results">
                  <option value="20"'; if(!$_REQUEST["search_max_results"] || $_REQUEST["search_max_results"]=='20') {$select_max_results.=' selected';}else{$select_max_results.='';} $select_max_results.='>20</option>
                  <option value="50"'; if($_REQUEST["search_max_results"]=='50') {$select_max_results.=' selected';}else{$select_max_results.='';} $select_max_results.='>50</option>
                  <option value="100"'; if($_REQUEST["search_max_results"]=='100') {$select_max_results.=' selected';}else{$select_max_results.='';} $select_max_results.='>100</option>
                  <option value="300"'; if($_REQUEST["search_max_results"]=='300') {$select_max_results.=' selected';}else{$select_max_results.='';} $select_max_results.='>300</option>
                  <option value="500"'; if($_REQUEST["search_max_results"]=='500') {$select_max_results.=' selected';}else{$select_max_results.='';} $select_max_results.='>500</option>
                </select>
	';
	
			$checkbox_time = '
				<input name="search_time" type="checkbox" id="search_time" value="1"'; if($_REQUEST["search_time"]=='1') $checkbox_time.=' checked';$checkbox_time.='>
			';
	
			// Tage erzeugen
			$select_tag_from = '<select name="select_tag_from" id="select_tag_from">';
				for($i=1;$i<32;$i++)
				{
					if (!$_REQUEST["Submit"] && doublenum($i) == date("d", mktime(0,0,0, date("m"),date("d")-7,date("Y"))) || doublenum($i) == $_REQUEST["select_tag_from"] ) {$selected='selected';}else{$selected='';}
					$select_tag_from .= '<option value="'.doublenum($i).'" '.$selected.'>'.doublenum($i).'</option>';
				}
			$select_tag_from .= '</select>';
			
			// Monate erzeugen
			$select_monat_from = '<select name="select_monat_from" id="select_monat_from">';
				for($i=1;$i<13;$i++)
				{
					if (!$_REQUEST["Submit"] && doublenum($i) == date("m", mktime(0,0,0, date("m"),date("d"),date("Y"))) || doublenum($i) == $_REQUEST["select_monat_from"] ) {$selected='selected';}else{$selected='';}
					$select_monat_from .= '<option value="'.doublenum($i).'" '.$selected.'>'.strftime("%B", mktime(0,0,0,doublenum($i),1,2000)).'</option>';
				}
			$select_monat_from .= '</select>';
			
			// Tage erzeugen
			$select_tag_to = '<select name="select_tag_to" id="select_tag_to">';
				for($i=1;$i<32;$i++)
				{
					if (!$_REQUEST["Submit"] && doublenum($i) == date("d", mktime(0,0,0, date("m"),date("d"),date("Y"))) || doublenum($i) == $_REQUEST["select_tag_to"] ) {$selected='selected';}else{$selected='';}
					$select_tag_to .= '<option value="'.doublenum($i).'" '.$selected.'>'.doublenum($i).'</option>';
				}
			$select_tag_to .= '</select>';
			
			// Monate erzeugen
			$select_monat_to = '<select name="select_monat_to" id="select_monat_to">';
				for($i=1;$i<13;$i++)
				{
					if (!$_REQUEST["Submit"] && doublenum($i) == date("m", mktime(0,0,0, date("m"),date("d"),date("Y"))) || doublenum($i) == $_REQUEST["select_monat_to"] ) {$selected='selected';}else{$selected='';}
					$select_monat_to .= '<option value="'.doublenum($i).'" '.$selected.'>'.strftime("%B", mktime(0,0,0,doublenum($i),1,2000)).'</option>';
				}
			$select_monat_to .= '</select>';
			
		if( !$_REQUEST["search_time_from"] ) { $search_time_from = '00:00'; } else { $search_time_from = $_REQUEST["search_time_from"]; }
		if( !$_REQUEST["search_time_to"] ) { $search_time_to = '00:00'; } else { $search_time_to = $_REQUEST["search_time_to"]; }


	// Wenn User Adminrechte hat
	if( $_SESSION["admin"] == 1 ) $nav_menu .= '<a href="admin.php">ADMIN</a>&nbsp;|&nbsp;';

	// Filter
	if( $_REQUEST["search_what"] == 'word' )
	{
		if( $_REQUEST["search_word"] )
		{
			$show_filter = 'WHERE a.text LIKE "%'.$_REQUEST[search_word].'%"';
			
			if( $_REQUEST["search_word_org"] )
			{
				$show_filter .= 'AND d.org_name LIKE "%'.$_REQUEST[search_word_org].'%"';
			}
		}
		else{
			$show_filter = 'WHERE d.org_name LIKE "%'.$_REQUEST[search_word_org].'%"';
		}
		
	}
	else{
		$show_filter = 'WHERE d.id = "'.$_REQUEST["search_ric"].'"';
	}
	
	if( $_REQUEST["search_org"] != '0' )
	{
		$show_filter .= ' AND d.org = "'.$_REQUEST["search_org"].'"';
	}
	
        if( $_REQUEST["search_time"] == '1' )
	{
		if( $_REQUEST["select_monat_from"] > $_REQUEST["select_monat_to"] ) { $year_from = $yearnow-1; } else { $year_from = $yearnow; }
		//if( $search_time_to == '00:00' && $_REQUEST["search_time"] == '1' ) $_REQUEST["select_tag_to"] = $_REQUEST["select_tag_to"]+1;
		
		$time_from	= $year_from.$_REQUEST["select_monat_from"].$_REQUEST["select_tag_from"].search_time_small($_REQUEST["search_time_from"]).'00';
		$time_to	= $yearnow.$_REQUEST["select_monat_to"].$_REQUEST["select_tag_to"].search_time_small($_REQUEST["search_time_to"]).'00';
		
		$show_filter .= ' AND zeit > "'.$time_from.'" AND zeit < "'.$time_to.'"';
	}

	// Auslesen pocsag
if( $_REQUEST["search_what"] == 'ric' && $_REQUEST["search_ric"] != '' || $_REQUEST["search_what"] == 'word' && $_REQUEST["search_word"] != '' || $_REQUEST["search_what"] == 'word' && $_REQUEST["search_word_org"] != '' )
{
	$result	= mysqli_query($dbconn, "SELECT 	a.adresse AS adresse_neu, a.bezeichnung AS bezeichnung_neu, a.rec_typ, a.text, DATE_FORMAT(a.zeit, '%Y%m%d%H%i%s') AS zeit, a.funktion,
					b.adresse, b.formatierung_id, b.bezeichnung,
					c.id, c.bg_farbe, c.text_farbe, c.text_format, c.text_groesse, c.text_groesse_text,
					d.org_name, d.org
				FROM dme a
				LEFT JOIN ric_zvei b ON b.adresse = a.adresse
				LEFT JOIN formatierung c ON c.id = b.formatierung_id
				LEFT JOIN organisation d ON d.id = b.org_id
				$show_filter
				ORDER BY a.zeit DESC LIMIT 0,$_REQUEST[search_max_results]") or die (mysqli_error($dbconn));
	while($row = mysqli_fetch_array($result))
	{
		$bezeichnung	= $row["bezeichnung"];
		
		// Tageswechsel ausgeben
		if( get_day($row["zeit"]) != $lastday )
		{
			$nextday = '
					<tr>
					  <td colspan="5" bgcolor="black"><span style="color:white;font-size:16;font-weight:bold">&nbsp;'.get_date_long($row["zeit"]).'</span></td>
					</tr>
			';
		}
		else
		{
			$nextday = '';
		}
			$lastday	= get_day($row["zeit"]);
		
		// Wenn unbekannte Ric Wert setzen
		if( !$bezeichnung && !$row["org_name"] ) $bezeichnung = 'Unbekannt';
		
		// default Farben bestimmen
		if( !$row["bg_farbe"] )		$row["bg_farbe"]		= 'white';
		if( !$row["text_farbe"] )	$row["text_farbe"]		= 'black';
		if( !$row["text_farbe_text"] )	$row["text_farbe_text"] 	= 'black';
		if( !$row["bg_farbe_text"] )	$row["bg_farbe_text"]		= '#CCCCCC';
		if( !$row["text_groesse"] )	$row["text_groesse"]		= '13';
		if( !$row["text_groesse_text"] )$row["text_groesse_text"]	= '12';
		
/*                 // pocsag in Array schreiben
		$thisday = $nextday.'
				<tr bgcolor="'.$row["bg_farbe"].'">
				  <td width="60"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">&nbsp;'.get_time($row["zeit"]).'</span></td>
				  <td width="70%" height="2"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">&nbsp;'.$row["org"].'</span><span style="font-size:'.$row["text_groesse"].'"> | </span><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">'.$row["org_name"].'</span><span style="font-size:'.$row["text_groesse"].'"> | </span><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">'.$bezeichnung.'</span></td>
				  <td width="250"><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="3"><span style="color:'.$row["text_farbe_text"].';font-size:'.$row["text_groesse_text"].'">'.$row["text"].'</span></td>
				</tr>
			';
		}
*/
        // pocsag in Array schreiben
		$thisday = $nextday.'
				<tr bgcolor="'.$row["bg_farbe"].'">
	 			  <!-- <td width="60"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">&nbsp;'.get_time($row["zeit"]).'</span></td> -->
				  <td width="60"><span style="color:'.$row["text_farbe"].';font-size:'.$status_size.';font-weight:bold">&nbsp;'.get_time($row["zeit"]).'</span></td>
				  <td width="80" height="2" align="center"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">&nbsp;'.$row["org"].'</span></td>
				  <td width="100" align="center"><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'">'.$bezeichnung.'</span></td>
				  <td><span style="color:'.$row["text_farbe"].';font-size:'.$row["text_groesse"].';font-weight:'.$row["text_format"].'"> &nbsp;'.$row["org_name"].'</span></td>
				  <td width="250"><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>
			';
		}
		
		$show[$row["zeit"]] = $thisday;
	}
}
	
	// Array sortieren und Zeiger auf Anfang setzen
	if( $show )
	{
		krsort($show);
		reset($show);

		// Array in Variable fr Ausgabe schreiben
		while(list($k, $v) = each ($show))
		{
			$show_all .= $v;
		}
	}

	// Seiteninhalt ausgeben
	eval ("\$page_middle .= \"".gettemplate($template_dir."body_suche")."\";");

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

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