<?
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ALGeo
//
// Author: Christian Mähler
// Version: 9.5
// Last update: April 2003
//
// Base library for map&guide geocoding functionality.
// Please note that only English speaking properties are used
// there is no support for German speaking properties as
// known by the original COM objects under Windows
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//++++++++++ INCLUDE ++++++++++++++++++++++++++//" 
@include_once(dirname(__FILE__)."/albasic.inc.php");
//+++++++++++++++++++++++++++++++++++++++++++++//"

class ALGeo extends ALBasic
{
 	  // public variables for reading and writing data
		 
		var $GeoZIP;  					 // ZIP (put + get)
    var $GeoTown;						 // town
    var $GeoDistrict;				 // district (put + get)
    var $GeoStreet;					 // street (put + get)
    var $GeoCoordinate;			 // coordinates in format "x/y" (put + get)
    var $GeoCoordinateX;		 // x coordinate (put + get)
    var $GeoCoordinateY;		 // y coordinate (put + get)		
    var $GeoCountry;  			 // country (put + get)
    var $GeoTownID;					 // town ID (put + get)
   	var $GeoHousenumber;		 // house number (put + get)					 
    var $GeoResponse;				 // response from geo object (get)
		var $ParaRequestType;		 // type of option list to be built in res_town_2 (put)
    var $ParaUseHNr;				 // use the house number in a single field? (put)
    var $ParaMap; 					 // generate map? (put)
    var $ShowDistrict;			 // show detailed information while geocoding (put) 
    var $MaxNumNearest;			 // how many geocoding results at maximum?? (put) 
		
		// private variables
		
		var $BPGeoServiceNum;		 // internal service number
		
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Constructor
// set all parameters, necessary for geocoding. Contructor sets default
// parameter 

function ALGeo()
{
 $this->ALBasic();

 $this->ParaMap=false;
 $this->ShowDistrict=false;
 $this->MaxNumNearest=1000;
 $this->NR_GEOREQUEST=7; // don't change this value!!
 $this->Log_to_file("ALGeo.ALGeo: Constructor passed successfully");
 $this->BPGeoServiceNum=1099;		// base number for internal service 
}
		
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SetOrder
// build request string depending on internal values as ACTION and
// based on the request as defined by the user

function SetOrder()
{    
		$this->BPRequest["CLIENT"]["MAP"] 	 	 	 	 = (($this->ParaMap)  ? "TRUE" : "FALSE");
    $this->BPRequest["CLIENT"]["SHOWDISTRICT"] = (($this->ShowDistrict) ? "TRUE" : "FALSE");
		$this->BPRequest["CLIENT"]["MAPSBYSTREAM"] = (($this->BPMapsByStream) ? "TRUE" : "FALSE");;
    if ($this->ShowDistrict && $this->MaxNumNearest) 
		    $this->BPRequest["CLIENT"]["MAXNUMNEAREST"] = $this->MaxNumNearest;
 	  $this->BPRequest["CLIENT"]["SEARCH"]  = $this->GeoCountry . $this->GeoZIP . "," . $this->GeoTown .",". $this->GeoDistrict .",". $this->GeoStreet ." ". $this->GeoHousenumber .",,";
		$this->BPRequest["CLIENT"]["SUBACTION"]  	 = 1;
		$this->BPRequest["SERVER"]["ACTION"] 			 = 7;					
    $this->BPRequest["CLIENT"]["REQUEST"] 		 = "TRUE";

 		$this->Log_to_file("ALGeo.SetOrder: Passed successfully");		
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Res_town_0
// no hits found, answer with error code

function res_town_0()
{
     $this->GeoResponse = "Error: Your geocoding request could not be handled.";
     $this->Log_to_file("ALGeo.res_town_0: Passed successfully. No results found for geocoding");
		 return $this->BPStat_Error;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Res_town_1
// exactly one hit found, answer is presented at object properties and
// not as a string in one property 

function res_town_1($res_array_)
{   		
	  $errco = $this->BFGetParam(0,"SERVER","ERRORCODE","0");
		if($errco=="0")
		{
		 		$temp = explode(",", $this->BFGetParam(0,"VORSCHLAG1","ORTE1",","));
    		// fill public variables with values (list(<valuelist>)=array(values))
      	list($this->GeoZIP,$this->GeoTown,$this->GeoDistrict,$this->GeoStreet,$this->GeoTownID,$this->GeoCoordinateX,$this->GeoCoordinateY)=$temp;
        $this->GeoCoordinate = $this->GeoCoordinateX ."/" . $this->GeoCoordinateY;				
		}
		
	  // build a list as known for multiple results (in HTML format = 1)
  	$this->res_town_2($res_array_,1); 

    $this->Log_to_file("ALGeo.res_town_1: Passed successfully. One hit found for geocoding");
  	
    return $this->BPStat_Success;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Res_town_2
// more than one hit found, build result string according to prefered format 

function res_town_2($res_array_, $countorte_)
{
   $pararequest_jc       = 0;
   $pararequest_html     = 1;
   $pararequest_full     = 2;
   $pararequest_coord    = 3;
   $pararequest_id_coord = 4;
  
   $split = "~";
   $num   = 2;
   $begin = 1;
   $end   = 0;

   switch ($this->ParaRequestType)
   {
  	 	 // +++++++++++++++++++++++++++ $pararequest_jc = 0
       case $pararequest_jc : 
       {
			 		 // for every hit
           for($i=$begin; $i<=$countorte_+$end ; $i++)
           {
              $temp = explode($split, $res_array_[$i]);
							// build answer string
              if(substr($temp[1],0,4)=="ORTE")
        			{
        			  $temp = explode(",", $temp[$num]);
                $erg .= "$temp[0] $temp[1] $temp[2] $temp[3] $temp[4] $temp[5] $temp[6] <br>";
        			}
            } // for
  
           $this->GeoResponse = $erg;

			     $this->Log_to_file("ALGeo.res_town_2: Passed successfully: pararequest_jc ");

           return $this->BPGeoStat_Select;
           break;
        }

  	 	 // +++++++++++++++++++++++++++ $pararequest_html = 1
       case $pararequest_html :
       {
			     // for every hit found
           for($i=$begin; $i<=$countorte_+$end ; $i++)
           {
              $temp = explode($split, $res_array_[$i]);
              if(substr($temp[1],0,4)=="ORTE")
              {
                $temp = explode(",", $temp[$num]);
    						
								// build option tag which can be used directly in 
								// HTML select statements. Use fpr values only the
								// town id
                if ($i == $begin)
                {
                  $select_tag =  "<option selected value=\"$temp[4]\">";
                  $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                }
                else
                {
                  $select_tag = $select_tag . "<option value=\"$temp[4]\">";
                  $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                }
    					}
           } // for
    
			     $this->Log_to_file("ALGeo.res_town_2: Passed successfully: pararequest_html ");

           $this->GeoResponse = $select_tag;
           return $this->BPGeoStat_Select;
           break;
       }

       // +++++++++++++++++++++++++++ $pararequest_full = 2
        case $pararequest_full :
        {
           for($i=$begin; $i<=$countorte_+$end ; $i++)
           {
              $temp = explode($split, $res_array_[$i]);
          		if(substr($temp[1],0,4)=="ORTE")
          		{
                $temp = explode(",", $temp[$num]);

   							// build option tag which can be used directly in 
								// HTML select statements. Use for values all
								// parameters

                if ($i == $begin)
                 {
                  $select_tag =  "<option selected value=\"$temp[0]|$temp[1]|$temp[2]|$temp[3]|$temp[4]|$temp[5]|$temp[6]|$this->GeoPLZ|$this->GeoHauptOrt|$this->GeoTeilOrt|$this->GeoStrasse|$this->GeoHausnummer\">";
                  $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                 }
                 else
                 {
                  $select_tag = $select_tag . "<option value=\"$temp[0]|$temp[1]|$temp[2]|$temp[3]|$temp[4]|$temp[5]|$temp[6]\">";
                  $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                 }
          		 }
            } // for
  
            $this->GeoResponse = $select_tag;
			     
					  $this->Log_to_file("ALGeo.res_town_2: Passed successfully: pararequest_full ");

            return $this->BPGeoStat_Select;
            break;
          } // case 2
                
				 // +++++++++++++++++++++++++++ $pararequest_coord = 3
         case $pararequest_coord :
         {
             for($i=$begin; $i<=$countorte_+$end ; $i++)
             {
                  $temp = explode($split, $res_array_[$i]);
      					
								  // build option tag which can be used directly in 
								  // HTML select statements. Use for values 
								  // the coordinates only

									if(substr($temp[1],0,4)=="ORTE")
      						{									
                      $temp = explode(",", $temp[$num]);
                      if ($i == $begin)
                       {
                        $select_tag =  "<option selected value=\"$temp[5]/$temp[6]\">";
                        $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                       }
                       else
                       {
                        $select_tag = $select_tag . "<option value=\"$temp[5]/$temp[6]\">";
                        $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                       }
      						 }
              } // for
  
              $this->GeoResponse = $select_tag;

							$this->Log_to_file("ALGeo.res_town_2: Passed successfully: pararequest_coord ");
							
              return $this->BPGeoStat_Select;
              break;
          }

					// +++++++++++++++++++++++++++ $pararequest_id_coord = 4
         
          case $pararequest_id_coord :
          {
              for($i=$begin; $i<=$countorte_+$end ; $i++)
               {
                  $temp = explode($split, $res_array_[$i]);
									
								  // build option tag which can be used directly in 
								  // HTML select statements. Use for values 
								  // the coordinates and the town id only						
												
  								if(substr($temp[1],0,4)=="ORTE")
  								{
                    $temp = explode(",", $temp[$num]);
                    if ($i == $begin)
                     {
                      $select_tag =  "<option selected value=\"$temp[4]|$temp[5]/$temp[6]\">";
                      $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                     }
                     else
                     {
                      $select_tag = $select_tag . "<option value=\"$temp[4]|$temp[5]/$temp[6]\">";
                      $select_tag = $select_tag . "$temp[0] $temp[1] $temp[2] $temp[3]";
                     }
  								 }
              } // for

              $this->GeoResponse = $select_tag;

						  $this->Log_to_file("ALGeo.res_town_2: Passed successfully: pararequest_id_coord ");
							
              return $this->BPGeoStat_Select;
              break;
         }
    }// switch pararequesttype
}//function res_town_2

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// CheckRes
// check how many hits were found and build answer accordingly 

function CheckRes($countorte)
{
    // +++++++++++++++  no hit found ++++++++++++++++++++
    if ($countorte == 0) return $this->res_town_0();

    // +++++++++++++++  exactly one hit found +++++++++++++++++++
    if ($countorte == 1) return $this->res_town_1($this->BPTCPAnswer["ANS"]);

    // +++++++++++++++ multiple hits found ++++++++++++++++++
    if ($countorte >= 2) return $this->res_town_2($this->BPTCPAnswer["ANS"], $countorte);
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// GeoRequest
// send geocoding request to server. the geocoding is based on the
// geocoding algorithm for iNETServer. for map&guide-like
// geocoding please use the function MGSearchLocation in class MGClient 

function GeoRequest()
{
    // check parameter
    if (($this->BPStat_Error == $this->BFCheckParam($this->NR_GEOREQUEST)) || ($this->BPMapsByStream && $this->BPStreamDirectory==''))
		{
		 		$this->Log_to_file("ALGeo.GeoRequest: Too less parameters ");
		    return $this->BPStat_Error;
		}
    
    // set request type
    $this->SetOrder();
	 
	  // build request
    $reqerr=$request = $this->BFBuildOrder(); 

    // send request to server
    $answerlength=$this->BFWriteOrder($request);

		// if length is less than 45 byte, we had an error
		if($answerlength>45)
		{
 	    // return result code
	  	// count number of hits
			$reqerr=$this->BFReadALSAnswer($this->NR_GEOREQUEST);
			$countorte = intval($this->BFGetParam($this->eATAns, "VORSCHLAG1","NUMORTE",-1));
			return $this->CheckRes($countorte);
		}
		else
		  return $this->BPStat_Error;
}

} //end of class definition
 
?>