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


// Gets all distance data inside the radius defined in "$distance" regarding to the center of the circle
function getDistanceData ($distance, $long, $lat, $distance2 = "") {
  global $db, $PHP_SELF, $hq_id;
  $retArray = array();
  if ($distance != "" && is_numeric($distance) && $long != "" && is_numeric($long) && $lat != "" && is_numeric($lat)) :

     $geoEarthRadius = getParameterValue("0", "GEO_EARTH_RADIUS", $hq_id);
     if ($geoEarthRadius == "") : $geoEarthRadius = 6371.0; endif;

     $boolDeltaDistance = false;
     if ($distance2 != "" && is_numeric($distance2)) : $boolDeltaDistance = true; endif;

     $whereClauseDistance = " (ACOS((SIN(" . deg2rad($lat) . ")*SIN(RADIANS(crd.lat))) + (COS(" . deg2rad($lat) . ")*COS(RADIANS(crd.lat))*COS(RADIANS(crd.lng) - " . deg2rad($long) . "))) * " . $geoEarthRadius . ") < " . $distance . " ";
     if ($boolDeltaDistance) :
        $whereClauseDistance = " (" . $whereClauseDistance . " AND (ACOS((SIN(" . deg2rad($lat) . ")*SIN(RADIANS(crd.lat))) + (COS(" . deg2rad($lat) . ")*COS(RADIANS(crd.lat))*COS(RADIANS(crd.lng) - " . deg2rad($long) . "))) * " . $geoEarthRadius . ") >= " . $distance2 . ") ";
     endif;
     // $whereClauseDistance = " AND " . $whereClauseDistance;

     $sqlStmt = "SELECT crd.cr_zipcode, crd.cr_city, crd.cr_district, lat, lng,"
              . "       (ACOS((SIN(" . deg2rad($lat) . ")*SIN(RADIANS(crd.lat))) + (COS(" . deg2rad($lat) . ")*COS(RADIANS(crd.lat))*COS(RADIANS(crd.lng) - " . deg2rad($long) . "))) * " . $geoEarthRadius . ") AS distance"
              . " FROM phoenix_special.cityradius AS crd"
              . " WHERE " . $whereClauseDistance
              . " ORDER BY crd.cr_city, crd.cr_zipcode, crd.cr_district";
// echo "<br><br>" . $sqlStmt . "<br><br>"; die();
     $result = $db->query($sqlStmt);
     if (DB::isError($result)) die ("$PHP_SELF: <br>$sqlStmt<br>" . $result->getMessage());
     while ($row = $result->fetch_assoc()):
           $retArray[] = array($row["cr_city"],$row["cr_zipcode"],$row["cr_district"],$row["lat"],$row["lng"],$row["distance"]);
     endwhile;
     $result->free();
  endif;
  return $retArray;
}


// Inserts a row into table "serviceradius"
function insertServiceRadius ($dataArray = array(), $hq_id = "0", $cs_id = "0", $radiusAreaNo = "", $dataIncArray = array()) {
  global $db, $PHP_SELF;
  $srvr_id_new = "0";
  $dataArrayLen = count($dataArray);
  $dataIncArrayLen = count($dataIncArray);
  if ($dataArrayLen > 0) :
     // ARRAY: (cr_city, cr_zipcode, cr_district, lat, lng, distance)
     $city = $dataArray[0];
     $zipcode = $dataArray[1];
     $district = $dataArray[2];

     // [1.] Check for empty district regarding the following row having the same city and the same zipcode.
     //      If the following data row has a district (!= ""), then do NOT use the current row WITHOUT a district name!
     // [2.] Check for potential doublets.
     $execInsert = true;
     if ($dataIncArrayLen > 0) :
        $cityInc = $dataIncArray[0];
        $zipcodeInc = $dataIncArray[1];
        $districtInc = $dataIncArray[2];
        // [1.]
        if ($district == "") :
           if ($city == $cityInc && $zipcode == $zipcodeInc && $districtInc != "") :
              $execInsert = false;
           endif;
        endif;
        // [2.]
        if ($execInsert) :
           if ($city == $cityInc && $zipcode == $zipcodeInc && $district == $districtInc) :
              $execInsert = false;
           endif;
        endif;
     endif;

     if ($execInsert) :
        if ($radiusAreaNo != "" && is_numeric($radiusAreaNo)) :
           if ($district == "") : $district = $city; endif;
           insertStmt("serviceradius", array("hq_id", $hq_id, "cs_id", $cs_id, "srvr_zipcode", $zipcode, "srvr_city_district", $district, "srvr_radiusarea_no", $radiusAreaNo));
           $srvr_id_new = getLastInsertId();
        endif;
     endif;
  endif;
  return $srvr_id_new;
}

?>