<?php
/*=======================================================================
 *
 * geocode.inc
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

if (substr(phpversion(), 0, 1) >= "5") :
   // include('../geo/geoglobal.php');
else :
   include('../geo/geoglobal.php');
endif;


// Following include_once is important for function getGeoCode()
@include_once($Application['OBJECT_BASE'].'algeo.inc.php');         


function GetNextRequestParameter($tcpip,$path,$requestname,$imageurl,$map,$language)
{
  global $Application;
 
  $path=$Application[strtoupper("MAP".$map."_".$language."_MAPPATH")];
  $requestname=$Application["ALS_FILE"].date('his')."_".$Application["CURRENTREQUEST"];	
  $imageurl=$Application["ALS_URL"].$requestname.".gif";
  $tcpip=$Application["ALS_LIST_D7_D"];
	
  return array($tcpip,$path,$requestname,$imageurl);
}; 


// Generates the result-object containing all informations (distance, time, costs, etc.)
function getGeoCode($adStreet, $adZipcode, $adCity, $adHsno, $adCountry = "D", $adDistrict = "") {
      
      global $MGObject;
	
      $MGObject = new ALGeo;
      list($tcpip,$path,$requestname,$imageurl) = GetNextRequestParameter($tcpip,$path,$requestname,$imageurl2,$map,$language);

      $MGObject->BPUseTCPIP=true;
      $MGObject->BPALS=$tcpip;
 
      if ($Application["COM_LOG"]) $MGObject->BPLogFilePath=$Application["COM_LOG_FILE"];
				
      // pass all available parameters to the ALS
      if ($adZipcode!="") $MGObject->GeoZIP=$adZipcode;
      if ($adCity!="") $MGObject->GeoTown=$adCity;
      if ($adDistrict!="") $MGObject->GeoDistrict=$adDistrict;
      if ($adStreet!="") $MGObject->GeoStreet=$adStreet;
      if ($adHsno!="") $MGObject->GeoHousenumber=$adHsno;
      if ($adCountry!="") $MGObject->GeoCountry=$adCountry;

      // try to geocode as exactly as possible and ask the user as detailed as possible
      // if the location can't be geocoded clearly 

      $MGObject->ShowDistrict=true;
      //$MGObject->BFAddParam("CLIENT","USEHNR","TRUE");
 
      // we don't need a map for the geocoding process
      $MGObject->ParaMap=false;

      // as a result we want all the parameters the ALS can provide us with
      $MGObject->ParaRequestType=2;

      // ++++++++++++++ send request to ALS ++++++++++++++++++++
      $resultType=$MGObject->GeoRequest();
      
      return $resultType;
};      


// Function to check the validity of an address and to get the coordinates if it is valid.
// Returns "" if not valid else returns a two-component-array with x- and y-coordinate.
function getGeoCoordinates($adStreet, $adZipcode, $adCity, $adHsno, $adCountry = "D", $adDistrict = "") {

    global $MGObject, $hq_id;

    $retCoordinates = "";
    
    // Checks for M&G-Status is enabled or disabled
    // If enabled the check has to be done by the M6G-Sever,
    // if disabled all addresses entered to the system would be valid    

    // if (MG_STATUS == "0") : return "DISABLED"; endif;    // Do NOT return empty string!
    $parMgStatus = getParameterValue("0", "MG_STATUS", $hq_id);
    if ($parMgStatus == "") : $parMgStatus = getParameterValue("0", "MG_STATUS", "0"); endif;

    if ($parMgStatus == "" || $parMgStatus == "0") : return "DISABLED"; endif;    // Do NOT return empty string!

    $resultType = getGeoCode($adStreet,$adZipcode,$adCity,$adHsno,$adCountry,$adDistrict);

    // resultType == 2 -> exact match
    if ($resultType == 2) :   

       // Geo-Coordinates
       $xcoord=$MGObject->GeoCoordinateX;
       $ycoord=$MGObject->GeoCoordinateY;         	

       // Check for setting
       if ($xcoord != "" && $xcoord != 0 && $ycoord != "" && $ycoord != 0):
          
          $retCoordinates = array($xcoord,$ycoord);          
       endif;         
       
       // Initialize object
       $MGObject=null;                   
    endif;

    return $retCoordinates;
};


// Generates the result-address-object according to the specified GEO-coordinates
function getAddressFromGeoCode($x, $y, $coordType = "0") {
      
      global $MGObject;

      // calculate logical coordinates out of pixel coordinates      
      if ($coordType != "0") :					
         $width_pix=(int)$Application["MAP_WIDTH"];
         $height_pix=(int)$Application["MAP_HEIGHT"];	
         $rect=explode(",",$external_logicalrect);
					
         $x=(int)($external_x*abs((int)$rect[0]-(int)$rect[2])/$width_pix);
         $y=(int)($external_y*abs((int)$rect[1]-(int)$rect[3])/$height_pix);
      endif;

      // instantiate object for reverse geocoding      
      list($tcpip,$path,$requestname,$imageurl) = GetNextRequestParameter($tcpip,$path,$requestname,$imageurl2,$map,$language);

      $MiscObject=null;
      // included above ... look top of script
      // include_once($Application['OBJECT_BASE'].'albasic.inc.php');

      $MiscObject = new ALBasic;
      $MiscObject->BPUseTCPIP=true;
      $MiscObject->BPALS=$tcpip;				 

      if ($Application["COM_LOG"]) $MiscObject->BPLogFilePath=$Application["COM_LOG_FILE"];

      $MiscObject->BFAddParam("SERVER","ACTION","7");
      $MiscObject->BFAddParam("CLIENT","SUBACTION","10");
      $MiscObject->BFAddParam("CLIENT","COORDINATES",$x.",".$y);	

      $MiscObject->BFRequest();

      $result=$MiscObject->BFGetParam(0,"SERVER","NEARBY","");								      	

      $resultAddress = "";
      if ($result!="" && $MiscObject->BFGetParam(0,"SERVER","ERRORTEXT","")=="") {

         //<LayerName>|<VisibleLayerName>|<ID>|<Description>|<x logical>|<y logical>|<x Pixel>|<y Pixel>|??|ZIP|Town|District
         $data=explode("|",$result[$i]);
         
         $resultAddress[0] = $data[9];      // Zipcode
         $resultAddress[1] = $data[10];     // Town
         $resultAddress[2] = $data[11];     // District
         
       };
         
      $MiscObject=null;
      
      return $resultAddress;
};

?>


