<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'logs' ) ) access_error();

if( isset( $_POST['send_form'] ) || isset( $_POST['send_form_newwindow'] ) )
{
    $form_errors = array();
    
    if(!check_fields('charid') &&
    	!check_fields('charnames') &&
    	!check_fields('accountnames') &&
    	!check_fields( 'x', 'y', 'radius' ) &&    	
    	!check_fields( 'suchtext' ) &&
    	!check_fields( 'time_min_day', 'time_min_month', 'time_min_hour', 'time_min_minute' ) &&
    	!check_fields( 'time_max_day', 'time_max_month', 'time_max_hour', 'time_max_minute' )
    )
    	$form_errors[] = form_error( 'no_searchfields' );

    if( count( $form_errors ) > 0 )
    {
        $Smarty->assign( 'form_errors', $form_errors );
    }
    else
    {
    	$search_conditions = array();

        if( check_fields( 'charid' ) )
        	$search_conditions[] = "me_logs.charid = ".$_REQUEST['charid'];
        if( check_fields( 'accountid_log' ) )
        	$search_conditions[] = "me_logs.accountid = ".$_REQUEST['accountid_log'];
        if( check_fields( 'time_min_day', 'time_min_month', 'time_min_hour', 'time_min_minute' ) )
        	$search_conditions[] = "zeit >= '".date( "Y" )."-".$_REQUEST['time_min_month']."-".$_REQUEST['time_min_day']." ".$_REQUEST['time_min_hour'].":".$_REQUEST['time_min_minute'].":00'";
		if( check_fields( 'time_max_day', 'time_max_month', 'time_max_hour', 'time_max_minute' ) )
        	$search_conditions[] = "zeit <= '".date( "Y" )."-".$_REQUEST['time_max_month']."-".$_REQUEST['time_max_day']." ".$_REQUEST['time_max_hour'].":".$_REQUEST['time_max_minute'].":59'";
		if( check_fields( 'suchtext' ) )
        	$search_conditions[] = "(service='sprache' AND meldung LIKE '%".$_REQUEST['suchtext']."%')";        	
        if( check_fields( 'accountnames' ) ) {
        	$names = array();
        	$exploded = explode(',', $_REQUEST['accountnames']);
        	foreach($exploded as $e)
        		$names[] = trim($e);
        		
        	$accsel = "server.accounts.name,";
        	$accjoin = "JOIN server.accounts ON(logdb.me_logs.accountid=server.accounts.accountid)";
        		
        	$search_conditions[] = "(accounts.name = '".implode("' OR accounts.name = '", $names)."')";
        }
        if( check_fields( 'charnames' ) ) {
        	$names = array();
        	$exploded = explode(',', $_REQUEST['charnames']);
        	foreach($exploded as $e)
        		$names[] = trim($e);
        		
        	$charsel = "server.chars.name,";
        	$charjoin = "JOIN server.chars ON(logdb.me_logs.charid=server.chars.charid)";
        		
        	$search_conditions[] = "(chars.name = '".implode("' OR chars.name = '", $names)."')";
        }
               
        $search_conditions[] = "(service='server' OR service='sprache')";
        if( check_fields( 'x', 'y', 'radius' ) ) {
        	$x1 = intval(max(0, $_REQUEST['x'] - $_REQUEST['radius']));  // mehr x
        	$y1 = intval(max(0, $_REQUEST['y'] - $_REQUEST['radius']));
        	
        	$x2 = intval($_REQUEST['x'] + $_REQUEST['radius']);  // mehr x
        	$y2 = intval($_REQUEST['y'] + $_REQUEST['radius']);
        	
        	$search_conditions[] = sprintf("(y >= %d AND y<=%d) AND (x >= %d AND x <= %d)", $y1, $y2, $x1, $x2);
        	//$search_conditions[] = "( (x-'".$_REQUEST['x']."')*(x-'".$_REQUEST['x']."') + (y-'".$_REQUEST['y']."')*(y-'".$_REQUEST['y']."') <= '".$_REQUEST['radius']."'*'".$_REQUEST['radius']."')";
        }
        $search_conditions[] = 1;
        $search_condstring = implode( ' AND ', $search_conditions );
echo "SELECT me_logs.*, logging_type.hp_string, ".$accsel." ".$charsel." logging_type.name AS typname FROM logdb.me_logs JOIN logdb.logging_type ON(logdb.me_logs.typ=logdb.logging_type.id) ".$accjoin." ".$charjoin." WHERE ".$search_condstring." ORDER BY zeit LIMIT 500";        
        $logs = $MySQL->fetch_complete( "SELECT me_logs.*, logging_type.hp_string, ".$accsel." ".$charsel." logging_type.name AS typname FROM logdb.me_logs JOIN logdb.logging_type ON(logdb.me_logs.typ=logdb.logging_type.id) ".$accjoin." ".$charjoin." WHERE ".$search_condstring." ORDER BY zeit LIMIT 500");
        $Smarty->assign('logs', $logs);        
    }
    
    assign_formvalues('charid', 'charnames', 'accountid_log', 'accountnames', 'x', 'y', 'radius',
    	'time_min_day', 'time_min_month','time_min_hour', 'time_min_minute',
    	'time_max_day', 'time_max_month','time_max_hour', 'time_max_minute',
    	'suchtext'
    );
}

$logging_types = $MySQL->fetch_complete("SELECT id, name FROM logdb.logging_type ORDER BY `name` ASC");
$Smarty->assign('logging_types', $logging_types);

$page['content'] = $Smarty->fetch( 'logs_simpel.tpl' );

$Smarty->assign( 'page', $page );
if( isset( $_POST['send_form_newwindow'] ) )
{
	$Smarty->display( 'main_light.tpl' );
}
else
{
	$Smarty->display( 'main.tpl' );
}

?>