<?php
/*=======================================================================
 *
 * jb_longhaul_map.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

include_once ("../include/global.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../include/image.inc.php");
include_once ("../include/bwv.inc.php");


getSecHttpVars("1",array("mapSizeMode", "federalStateMode", "transparency", "distance", "singleZipcode", "specialJob",
                         "displayDirectionArrows", "displayJobIDs", "displayCrSIDs", "displayZipcodes", 
                         "displayCouriers", "f_hq_id_string", "useRemoteDB"));

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
authCheckEmployeeRights($emp_id, "10", "1");

// ******************************************************
// Very special treatment for access to remote database
$remoteDBisActive = false;
if (BWV2_checkEmpRemoteDBAccess($useRemoteDB, $emp_id)) :
   $remoteDBisActive = BWV2_setRemoteDBAccessParameter();
endif;
// ******************************************************

if ($mapSizeMode != "1" && $mapSizeMode != "2" && $mapSizeMode != "3") : $mapSizeMode = "2"; endif;
if ($federalStateMode != "1") : $federalStateMode = "0"; endif;
if ($transparency == "") : $transparency = "0"; endif;
if ($distance == "") : $distance = "80"; endif;
if ($displayDirectionArrows != "1") : $displayDirectionArrows = "0"; endif;
if ($displayJobIDs != "1") : $displayJobIDs = "0"; endif;
if ($displayCrSIDs != "1") : $displayCrSIDs = "0"; endif;
if ($displayZipcodes != "1") : $displayZipcodes = "0"; endif;
if ($displayCouriers != "1") : $displayCouriers = "0"; endif;

// Special conversion for map script (array => string)
$f_hq_id = spliti(",", $f_hq_id_string);
// Mandator filter
if ($f_hq_id == "") : $f_hq_id = array(); endif;
if (count($f_hq_id) == 0) : array_push($f_hq_id, $hq_id); endif;

$singleZipcode = pad(trim($singleZipcode), 5, "0");   // Use "pad()" because e.g. "01067" is interpreted as "1067" !!!
$zipGeoCoord = getGeoCoordinatesFromZipcode($singleZipcode);
$zipLong = $zipGeoCoord[0];
$zipLat = $zipGeoCoord[1];
if ($zipLong == "" || $zipLat == "") : $singleZipcode = ""; endif;


// Check map file
if (($mapSizeMode == "1" || $mapSizeMode == "2" || $mapSizeMode == "3") && ($federalStateMode == "0" || $federalStateMode == "1")) : 
   $mapFileName = "germany_" . $mapSizeMode . $federalStateMode . ".jpg";
else :
   $mapFileName = "germany_21.jpg";   // Resolution 673 x 900 with federal states [Default]
   $mapSizeMode = "2";
   $federalStateMode = "1";
endif;

$textFontPath = "../include/arial.ttf";


header ("Content-type: image/jpg");

// Create image from file
$im = createMapImageFromFile($mapFileName);

// Set modifications according to transparency mode
if ($transparency != "0") :
   $im = getTransparency($im, $transparency);
endif;

if ($im) :
   // Define colors
   $lineColBlack = imagecolorallocate ($im, 0, 0, 0);
   $lineColRed = imagecolorallocate ($im, 255, 0, 0);
   $lineColGreen = imagecolorallocate ($im, 0, 255, 0);
   $lineColBlue = imagecolorallocate ($im, 0, 0, 255);       

   // ****************************************

   // Display a found zipcode   
   $point = getMapImagePoint($mapSizeMode, $zipLong, $zipLat);
   if ($point[0] != "0" && $point[1] != "0") :
      imageellipse($im, $point[0], $point[1], 29, 29, $lineColRed);
      imageellipse($im, $point[0], $point[1], 30, 30, $lineColRed);
      imageellipse($im, $point[0], $point[1], 31, 31, $lineColRed);
      imageellipse($im, $point[0], $point[1], 32, 32, $lineColRed);
      // imageline ($im, $point[0], $point[1] - 50, $point[0], $point[1] + 50, $lineColRed);
      // imageline ($im, $point[0] - 50, $point[1], $point[0] + 50, $point[1], $lineColRed);
   endif;

   // ****************************************

   for ($d = 0; $d <= 2; $d++) {
       if ($d == "0") :
          // Display longhaul jobs (jb_status = "9")
          $jbArray = getLongHaulJobData("9", $distance, $specialJob);
          $lineCol = $lineColRed;
       endif;
       if ($d == "1") :
          // Display longhaul jobs taken by couriers (jb_status = "1")
          $jbArray = getLongHaulJobData("1", $distance, $specialJob);
          $lineCol = $lineColBlue;
       endif;
       if ($d == "2") :
          // Display done longhaul with couriers on the way home
          $jbArray = getLongHaulJobData("2", $distance, $specialJob);
          $lineCol = $lineColGreen;
       endif;
       $jbArrayLen = count($jbArray);     
       
       for ($i = 0; $i < $jbArrayLen; $i++) {
	         $jbId = $jbArray[$i][0];           // Get ID of the tour
	         $CrSid = $jbArray[$i][1];          // Get SID of the courier
           $jbZipcodes = $jbArray[$i][3];     // Get zipcodes of the tour
           $jbZipcodes = trim(substr($jbZipcodes, 0, strpos($jbZipcodes, "|")));   // Remove cities
           if (substr($jbZipcodes, -1) == ";") :    // Remove last character if ";" again
              $jbZipcodes = substr($jbZipcodes, 0, -1);
           endif;                      
           $jbZipcodes = str_replace (";", "-,-", $jbZipcodes);
           $jbZipcodesArray = spliti("-,-",$jbZipcodes);
           $jbZipcodesArrayLen = count($jbZipcodesArray);
           $jbZipcodes = str_replace ("-,-", ",", $jbZipcodes);
           $vhtName = $jbArray[$i][4];        // Get vehicle name
           $vhtID = $jbArray[$i][5];          // Get vehicle ID
           $crGpsLong = $jbArray[$i][11];     // Last GPS longitude of the courier
           $crGpsLat = $jbArray[$i][12];      // Last GPS latitude of the courier
           $crGpsTime = $jbArray[$i][13];     // Last timestamp of courier locating

           $pointArray = array();
           $drawPolygonFlag = true;
       	   for ($j = 0; $j < $jbZipcodesArrayLen; $j++) :
               $currZipcode = trim($jbZipcodesArray[$j]);
               if ($currZipcode != "." && is_numeric($currZipcode)) :
                  // Get point of the current zipcode
                  $point = getMapCoordinatesFromZipcode($currZipcode, $mapSizeMode);
                  if ($point[0] != "0" && $point[1] != "0") :
                     $pointArray = array_merge($pointArray, $point);
                  endif;
               else :
                 $drawPolygonFlag = false;
                 break;   // Abort because at least one zipcode is not valid
               endif;
           endfor;
           // Draw polygon of the tour
           if ($drawPolygonFlag) :
              if (count($pointArray) > 4) :   // function "imagepolygon" aborts if not at least 3 pairs of coordinates do exist !!!
                 imagepolygon ($im, $pointArray, $jbZipcodesArrayLen, $lineCol);
              else :
                 imageline ($im, $pointArray[0], $pointArray[1], $pointArray[2], $pointArray[3], $lineCol);
                 imageline ($im, $pointArray[0] + 1, $pointArray[1] + 1, $pointArray[2] + 1, $pointArray[3] + 1, $lineCol);
              endif;
             
              // Display tour start and tour end with ellipses
              $pointArrayStart = array_slice($pointArray, 0, 2);
              $pointArrayEnd = array_slice($pointArray, -2, 2);
              imagefilledellipse($im, $pointArrayStart[0], $pointArrayStart[1], 10, 10, $lineCol);
              imagefilledellipse($im, $pointArrayEnd[0], $pointArrayEnd[1], 10, 10, $lineCol);

              if ($displayJobIDs == "1" || $displayCrSIDs == "1" || $displayZipcodes == "1" || 
                  $displayCouriers == "1" || $displayDirectionArrows == "1") :

                 $textToBeDisplayed = "";
                 if ($displayJobIDs == "1") : $textToBeDisplayed .= $jbId; endif;
                 if ($textToBeDisplayed != "") : $textToBeDisplayed .= "  "; endif;
                 if ($displayCrSIDs == "1") : $textToBeDisplayed .= "[" . $CrSid . ", " . $vhtName . "]"; endif;
                 if ($textToBeDisplayed != "") : $textToBeDisplayed .= "  "; endif;
                 if ($displayZipcodes == "1") : $textToBeDisplayed .= "(" . $jbZipcodes . ")"; endif;
                 // if ($textToBeDisplayed != "") : $textToBeDisplayed .= "  "; endif;

                 // Display job data
                 // Central point of the first line of the tour
                 $pointMiddleX = (($pointArray[2] + $pointArray[0]) / 2);  // Middle X-coord. of the first stations
                 $pointMiddleY = (($pointArray[3] + $pointArray[1]) / 2);  // Middle Y-coord. of the first stations
                 // Top-left-corner of the textbox associated to the tour
                 $pointTextX = $pointMiddleX + 20;
                 $pointTextY = $pointMiddleY + 20;
                 
                 // Gradient of the first line of the tour (=> take care for the y-axis because of orientation [(top, left) = (0, 0)] !!!)
                 // $gradient = ($pointArray[3] - $pointArray[1]) / ($pointArray[2] - $pointArray[0]);
                 
                 // Arrow points of the first line of the tour
                 if ($displayDirectionArrows == "1") :
                    $pointArrowX = (($pointArray[2] + $pointMiddleX) / 2);    // Arrow X-coord. of the first stations
                    $pointArrowY = (($pointArray[3] + $pointMiddleY) / 2);    // Arrow Y-coord. of the first stations
                    
                    $rScalar = 0.04;
                    $sScalar = sqrt( ($pointArray[2] - $pointArray[0]) * ($pointArray[2] - $pointArray[0]) + ($pointArray[3] - $pointArray[1]) * ($pointArray[3] - $pointArray[1]) );
                    $sScalar = 1;
                    // $rScalar = $sScalar;
                    
                    $pointArrowX2 = $pointArrowX + $rScalar * ($pointArray[2] - $pointArray[0]);    // Arrow X2-coord.
                    $pointArrowY2 = $pointArrowY + $rScalar * ($pointArray[3] - $pointArray[1]);    // Arrow Y2-coord.
                    
                    $pointArrowXR = $pointArrowX + $rScalar * ($pointArray[1] - $pointArray[3]);    // Arrow right X2-coord.
                    $pointArrowYR = $pointArrowY + $rScalar * ($pointArray[2] - $pointArray[0]);    // Arrow right Y2-coord.
                    
                    $pointArrowXL = $pointArrowX - $rScalar * ($pointArray[1] - $pointArray[3]);    // Arrow left X2-coord.
                    $pointArrowYL = $pointArrowY - $rScalar * ($pointArray[2] - $pointArray[0]);    // Arrow left Y2-coord.
                    
                    $arrowPointArray = array($pointArrowXL, $pointArrowYL, $pointArrowXR, $pointArrowYR, $pointArrowX2, $pointArrowY2);
                    imagefilledpolygon ($im, $arrowPointArray, 3, $lineCol);
                 endif;

                 // Set circle in the middle of the line of the first both stations for connection with textbox or vehicle icon
                 imagefilledellipse($im, $pointMiddleX, $pointMiddleY, 5, 5, $lineColBlack);
                 
                 if ($displayJobIDs == "1" || $displayCrSIDs == "1" || $displayZipcodes == "1") :
                    $boxPointsArray = imagettftext ($im, 10, 0, $pointTextX, $pointTextY, $lineColBlack, $textFontPath, $textToBeDisplayed);

                    // Draws a box around the text
                    $boxBorder = "2";
                    setTextBox ($im, $boxPointsArray, $boxBorder, $lineColBlack);
                    imageline ($im, $pointMiddleX, $pointMiddleY, $boxPointsArray[6] - $boxBorder, $boxPointsArray[7] - $boxBorder, $lineColBlack);
                 endif;

                 // Display courier with vehicle icon
                 $vhtFileName = "";
                 if ($vhtID == "2") : $vhtFileName = "pkw_mc_1.jpg"  ; endif;
                 if ($vhtID == "3") : $vhtFileName = "kombi_mc_1.jpg"  ; endif;
                 if ($vhtID == "4") : $vhtFileName = "kasten_mc_1.jpg"  ; endif;
                 if ($vhtID == "5") : $vhtFileName = "bus_mc_1.jpg"  ; endif;
                 if ($vhtID == "6") : $vhtFileName = "grbus_mc_1.jpg"  ; endif;
                 if ($vhtID == "7") : $vhtFileName = "lkw_mc_1.jpg"  ; endif;
                 if ($vhtID > "7")  : $vhtFileName = "lkw_mc_1.jpg"  ; endif;
                 
                 if ($displayCouriers == "1" && $vhtFileName != "") :
                    $imVht = createMapImageFromFile($vhtFileName);
                    $imVhtSizeX = imagesx($imVht);
                    $imVhtSizeY = imagesy($imVht);

                    // Set transparency of the vehicle icon
                    $currSeconds = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
                    $crGpsTimeSeconds = mktime(substr($crGpsTime,11,2), substr($crGpsTime,14,2), substr($crGpsTime,17,2), substr($crGpsTime,5,2), substr($crGpsTime,8,2), substr($crGpsTime,0,4));
                    $diffMinutes = (($currSeconds - $crGpsTimeSeconds) / 60);
                    $transparencyScaleVehicleIcon = min((floor($diffMinutes / 15) * 10) - 10, 80);
                    // $boxPointsArray = imagettftext ($im, 10, 0, 300, 500, $lineColBlack, $textFontPath, $transparencyScaleVehicleIcon);
                    // setTextBox ($im, $boxPointsArray, "2", $lineColBlack); 
                    if ($transparencyScaleVehicleIcon > 0) :
                       $imVht = getTransparency($imVht, $transparencyScaleVehicleIcon);
                    endif;

                    // Get coordinates where the vehicle image has to be set
                    $pointVhtImgX = $pointMiddleX - 20;
                    $pointVhtImgY = $pointMiddleY - 20;
                    
                    $currPointVht = getMapImagePoint($mapSizeMode, $crGpsLong, $crGpsLat);
                    if ($currPointVht[0] > 0 && $currPointVht[1] > 0) :
                       $pointVhtImgX = $currPointVht[0] - 20;
                       $pointVhtImgY = $currPointVht[1] - 20;
                    endif;

                    // int Imagecopy ( int dst_im, int src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h);
                    imagecopy ($im, $imVht, $pointVhtImgX, $pointVhtImgY, 0, 0, $imVhtSizeX, $imVhtSizeY);
                 
                    // Connect text box with the middle of the first stations
                    // imagefilledellipse($im, $pointMiddleX, $pointMiddleY, 5, 5, $lineColBlack);
                    imageline ($im, $pointMiddleX, $pointMiddleY, $pointVhtImgX + ($imVhtSizeX / 2), $pointVhtImgY + ($imVhtSizeY / 2), $lineColBlack);
                 endif;
              endif;
           endif;
       }
   }

   // ****************************************
 
endif;


/*
   // -----------------------------------------------------------------------------------------
   // Frankfurt am Main  	8.60462075288717  	50.1263940837884  	60489  
   $geoCoordX = "8.60462075288717";
   $geoCoordY = "50.1263940837884";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");

   // -----------------------------------------------------------------------------------------
   // Kiel  	10.1167469407959  	54.3626350523619  	24106  
   $geoCoordX = "10.1167469407959";
   $geoCoordY = "54.3626350523619";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");
   
   // -----------------------------------------------------------------------------------------
   // München  	11.5721994266954  	48.1378593490146  	80331
   $geoCoordX = "11.5721994266954";
   $geoCoordY = "48.1378593490146";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");
   
   // -----------------------------------------------------------------------------------------
   // Essen  	7.00774636216092  	51.5141464430345  	45329
   $geoCoordX = "7.00774636216092";
   $geoCoordY = "51.5141464430345";

   $mapPointX = $mapPointConstX + (($geoCoordX - $geoCoordConstX) * $mapPointConstDeltaX);
   $mapPointY = $mapPointConstY + (($geoCoordConstY - $geoCoordY) * $mapPointConstDeltaY);

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");

   // -----------------------------------------------------------------------------------------
   // Dresden  	13.6911443969423  	51.0805574259239  	01139
   $geoCoordX = "13.6911443969423";
   $geoCoordY = "51.0805574259239";

   $mapPointX = $mapPointConstX + (($geoCoordX - $geoCoordConstX) * $mapPointConstDeltaX);
   $mapPointY = $mapPointConstY + (($geoCoordConstY - $geoCoordY) * $mapPointConstDeltaY);

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");
   
   // -----------------------------------------------------------------------------------------
   // Nürnberg  	11.0665446831496  	49.4793815205307  	90425
   $geoCoordX = "11.0665446831496";
   $geoCoordY = "49.4793815205307";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");

   // -----------------------------------------------------------------------------------------
   // Hamburg  	10.0011036114924  	53.5541579534295  	20095
   $geoCoordX = "10.0011036114924";
   $geoCoordY = "53.5541579534295";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);
// imagettftext ($im, 8, 0, 500, 500, $lineColBlack, $textFontPath, $point[0] . " " . $point[1]);
   setCrosshairs ($im, $point, "30");
   
   // -----------------------------------------------------------------------------------------
   // Magdeburg  	11.6407479374166  	52.1217786614182  	39104
   $geoCoordX = "11.6407479374166";
   $geoCoordY = "52.1217786614182";

   $point = getMapImagePoint($mapSizeMode, $geoCoordX, $geoCoordY);

   setCrosshairs ($im, $point, "30");
*/


// Output DEBUG
// imagettftext ($im, 10, 0, 10, 10, $lineColBlack, $textFontPath, $debug); 

imagepng ($im);
?>
   
