<?php

include './core.php';

if( !isset( $user ) ) access_error();

if( isset( $_REQUEST['show_map_image'] ) )
{
    switch( $_REQUEST['show_map_image'] )
	{
		default:
		case 'd':
			$map_url = "images/maps/germany.gif"; break;
		case 'ch':
			$map_url = "images/maps/swizerland.gif"; break;
		case 'at':
			$map_url = "images/maps/austria.gif"; break;
	}
    $map_size = @getimagesize( $map_url );

    $img = imagecreate( $map_size[0], $map_size[1] );
	$bg = imagecolorallocate( $img, 255, 255, 255 );
	$color = imagecolorallocate( $img, 0, 0, 255 );
	ImageCopy ( $img, ImageCreateFromGIF( $map_url ), 0, 0, 0, 0, $map_size[0], $map_size[1] );

    $locations = $MySQL->query( "SELECT * FROM user_locations WHERE  country = '".$_REQUEST['show_map_image']."'" );
    while($row = $MySQL->fetch_array( $locations ) )
	{
        imagerectangle( $img, $row['map_x']-2, $row['map_y']-2, $row['map_x']+2, $row['map_y']+2, $color );
	}

	header( "Content-Type: image/png" );
	imagepng( $img );
}
elseif( isset( $_REQUEST['show_map'] ) )
{
    $locations = $MySQL->fetch_complete( "SELECT user_locations.*, server.accounts.* FROM user_locations, server.accounts WHERE user_locations.accountID = server.accounts.accountid && country = '".$_REQUEST['show_map']."'" );
    $Smarty->assign( 'locations', $locations );
    $Smarty->assign( 'country', $_REQUEST['show_map'] );
    $Smarty->display( 'user_locations_show_map.tpl' );

    die();
}
elseif( isset( $_REQUEST['show_plz'] ) )
{
    $plz_from = $_REQUEST['show_plz'] - 1000;
    $Smarty->assign( 'plz_from', $plz_from );
	$plz_to = $_REQUEST['show_plz'] + 1000;
    $Smarty->assign( 'plz_to', $plz_to );

    $locations_res = $MySQL->query( "SELECT * FROM user_locations WHERE plz >= ".$plz_from." && plz <= ".$plz_to." && country = '".$_REQUEST['country']."' ORDER BY plz" );
    $locations = array();
    while( $row = $MySQL->fetch_array( $locations_res ) )
    {
    	$row['account'] = new Account( (int) $row['accountID'] );
        $locations[] = $row;
    }
    $Smarty->assign( 'locations', $locations );
    $Smarty->assign( 'country', $_REQUEST['country'] );
    $Smarty->display( 'user_locations_show_plz.tpl' );

    die();
}
elseif( isset( $_REQUEST['show_table'] ) )
{
    $locations_res = $MySQL->query( "SELECT * FROM user_locations WHERE country = '".$_REQUEST['show_table']."' ORDER BY plz" );
    $locations = array();
    while( $row = $MySQL->fetch_array( $locations_res ) )
    {
    	$row['account'] = @new Account( (int) $row['accountID'] );
        $locations[] = $row;
    }
    $Smarty->assign( 'locations', $locations );
    $page['content'] = $Smarty->fetch( 'user_locations_show_table.tpl' );
}
elseif( isset( $_REQUEST['edit'] ) && $_REQUEST['edit'] == '1' )
{
    if( isset( $_REQUEST['delete_entry'] ) )
    {
    	$MySQL->query( "DELETE FROM user_locations WHERE accountid = ".$user->ID );

        set_location( 'user_locations.php?'.SID );
    }
    elseif( isset( $_REQUEST['send_form'] ) && $_REQUEST['step'] == 2 )
    {
        $form_errors = array();

	    if( !check_fields( 'country', 'town' ) )
	        $form_errors[] = form_error( 'uncomplete' );

	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
            $page['content'] = $Smarty->fetch( 'user_locations_edit_1.tpl' );
	    }
	    else
	    {
        	$values = array();
            $values['country'] = $_POST['country'];
            $values['town'] = $_POST['town'];
            $values['plz'] = $_POST['plz'];
            switch( $values['country'] )
	        {
	            default:
	            case 'd':
	                $values['map_url'] = "images/maps/germany.gif"; break;
	            case 'ch':
	                $values['map_url'] = "images/maps/swizerland.gif"; break;
	            case 'at':
	                $values['map_url'] = "images/maps/austria.gif"; break;
	        }
            $Smarty->assign( 'values', $values );

            $Smarty->display( 'user_locations_edit_2.tpl' );
			die();
        }
    }
    elseif( isset( $_REQUEST['step'] ) && $_REQUEST['step'] == 3 )
    {
    	list( $user_entry ) = $MySQL->query_first( "SELECT COUNT(*) FROM user_locations WHERE accountid = ".$user->ID );
        if( $user_entry > 0 )
        	$MySQL->query( "UPDATE user_locations SET country = '".$_POST['country']."', town = '".$_POST['town']."', plz = '".$_POST['plz']."', map_x = ".$_POST['x'].", map_y = ".$_POST['y']." WHERE accountid = ".$user->ID );
    	else
        	$MySQL->query( "INSERT INTO user_locations (accountID, country, town, plz, map_x, map_y) VALUES (".$user->ID.", '".$_POST['country']."', '".$_POST['town']."', '".$_POST['plz']."', ".$_POST['x'].", ".$_POST['y'].")" );

        set_location( 'user_locations.php?'.SID );
    }
    else
    {
        $ul_info = $MySQL->query_first( "SELECT * FROM user_locations WHERE accountID = ".$user->ID );
        $Smarty->assign( 'ul_info', $ul_info );
        $page['content'] = $Smarty->fetch( 'user_locations_edit_1.tpl' );
    }
}
else
	$page['content'] = $Smarty->fetch( 'user_locations.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>