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

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act","mode","modeFlag","csId","opener","orderByClause","statusMessage","zipcodeFrom","zipcodeTo", "deactivateMenu"));

$pageTitel = "ANFAHRTSZEITEN";
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");

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

// ****************************************************
// * Selection of all zipcodes in the specified range *
// ****************************************************

function mcIsSetX (&$var, $initVal = "") { if (!isset($var)) { $var = $initVal; }; return $var; };

$zipcodeArray = array();
$travelTimeArray = array();
$serviceOutput = "";
$maxEnitriesToBeDisplayed = "4000";
if ($modeFlag == "") : $modeFlag = "0"; endif;  // Switch for "CLASSIC" [= 0] and "BWV" [= 1]

// Check mode for syntax check of the zipcodes
$parMaskZipcodeCheckSyntax = getParameterValue("0", "MASK_ZIP_CHK_SYNTAX_DISABLED", $hq_id);
if ($parMaskZipcodeCheckSyntax == "") :
   $parMaskZipcodeCheckSyntax = getParameterValue("0", "MASK_ZIP_CHK_SYNTAX_DISABLED", "0");
endif;

if ($f_act != "" && ($zipcodeFrom == "" || $zipcodeTo == "")) :
   $statusMessage = "Bitte geben Sie einen Bereich an!";
else :
   if ($parMaskZipcodeCheckSyntax != "1" && $f_act != "" && (!is_numeric($zipcodeFrom) || !is_numeric($zipcodeTo))) :
      $statusMessage = "Bitte geben Sie eine gültige PLZ für den Bereich an!";
   else :
      // Sorting of the services to be displayed      
      if ($orderByClause == "") : $orderByClause = "srvp.srvp_plz" ; endif;

      if ($modeFlag == "1") :
         // Use traveltime of table "serviceplz" (BWV mode)
         $sqlquery = "SELECT srvp.srvp_id, srvp.srvp_plz, srvp.srvp_traveltime"
                     . " FROM serviceplz AS srvp"
                     . " WHERE srvp.srvp_plz >= '" . $zipcodeFrom . "' AND"
                     . "       srvp.srvp_plz <= '" . $zipcodeTo . "' AND"
                     . "       srvp.srvp_valid = '1' "
                     . " ORDER BY " . $orderByClause
                     . " LIMIT 0," . $maxEnitriesToBeDisplayed;
      else :
         // Use traveltime of table "serviceplztraveltime" (CLASSIC mode)
         $sqlquery = "SELECT srvp.srvp_id, srvp.srvp_plz, srvpt.srvpt_traveltime AS srvp_traveltime"
                     . " FROM serviceplz AS srvp LEFT JOIN serviceplztraveltime AS srvpt ON srvp.srvp_id = srvpt.srvp_id AND srvpt.hq_id = '" . $hq_id . "'"
                     . " WHERE srvp.srvp_plz >= '" . $zipcodeFrom . "' AND"
                     . "       srvp.srvp_plz <= '" . $zipcodeTo . "' AND"
                     . "       srvp.srvp_valid = '1'"
                     . " ORDER BY " . $orderByClause
                     . " LIMIT 0," . $maxEnitriesToBeDisplayed;
      endif;
// echo $sqlquery; die();
      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

      while ($row = $result->fetch_assoc()):

            $zipcodeArray[] = $row["srvp_plz"];
            $travelTimeArray[] = $row["srvp_traveltime"];
      endwhile;
      $result->free();
   endif;
endif;

$lenZipcodeArray = count($zipcodeArray);



// *************************
// *** BEGIN STORING *******
// *************************

// Storing prices and discounts if requested
// Using the results of the statements before!!!
if ($f_act != "" && $f_act == "save") :

   if ($zipcodeFrom == "" || $zipcodeTo == "") :
      $statusMessage = "Bitte geben Sie einen Bereich an!";
   else :
      if ($parMaskZipcodeCheckSyntax != "1" && (!is_numeric($zipcodeFrom) || !is_numeric($zipcodeTo))) :
         $statusMessage = "Bitte geben Sie eine gültige PLZ für den Bereich an!";
      else :
      	 // Set the current timestamp to store the data
      	 // $currentTime = getDateTime("0");

      	 TA("B");
      	 for ($j = 0; $j < $lenZipcodeArray; $j++) :    

      	     // Get current input-type-text-field with minutes
      	     list ($newValueToBeStored) = getHttpVars(array("f_". $zipcodeArray[$j]));
      	     
      	     // Check the value to be numeric
      	     if ($newValueToBeStored == "" || !is_numeric($newValueToBeStored)) : $newValueToBeStored = "0"; endif;
      	     
      	     // Set default traveltime if value equals to 0
      	     if ($newValueToBeStored == "0") : $newValueToBeStored = LATEST_TAKETIME_IN_MINUTES; endif;

             // Update traveltime
             if ($modeFlag == "1") :
                // BWV mode
                updateStmt("serviceplz", "srvp_plz", $zipcodeArray[$j], array("srvp_traveltime", $newValueToBeStored));
             else :
                // CLASSIC mode
                $srvpId = getFieldValueFromId("serviceplz", "srvp_plz", $zipcodeArray[$j], "srvp_id");
                
                if (existsEntry("serviceplztraveltime",array("srvp_id",$srvpId, "hq_id", $hq_id))) :
                   updateStmt("serviceplztraveltime", "srvp_id", $srvpId, array("srvpt_traveltime", $newValueToBeStored), "hq_id = '" . $hq_id . "'");
                else :
                   insertStmt("serviceplztraveltime", array("srvp_id", $srvpId, "hq_id", $hq_id, "srvpt_traveltime", $newValueToBeStored));
                endif;
             endif;
             
             // Override $travelTimeArray[]
             $travelTimeArray[$j] = $newValueToBeStored;
      	 endfor;
      
      	 TA("C");
      	 TA("E");
      endif;   
   endif;
endif;

// *************************
// *** END STORING *********
// *************************


// Table-header
$serviceOutput = "<tr>\n";
$serviceOutput .= "<td>PLZ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
$serviceOutput .= "<td>Zeit in Minuten</td>\n";
$serviceOutput .= "</tr>\n";

// Table-data
for ($j = 0; $j < $lenZipcodeArray; $j++) :
    $serviceOutput .= "<tr>\n";
    $serviceOutput .= "<td>" . $zipcodeArray[$j] . "</td>\n";
    $serviceOutput .= "<td><input type=\"text\" name=\"f_" . $zipcodeArray[$j] . "\" value=\"" . $travelTimeArray[$j] . "\" size=\"4\" maxlength=\"4\" align=\"right\" onChange=\"checkIsNaN(this.value, 'Achtung: Keine PLZ')\"></td>\n";
    $serviceOutput .= "</tr>\n";
endfor;
?>


<html lang="de">
<head>
<title><?php echo $pageTitel ?></title>

<style type="text/css">
  <?php include_once ("../css/phoenix.css.php"); ?>
  <?php include_once ("../css/navigation.css.php"); ?>
  <?php include_once ("../css/mc.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script src="../include/checkFormTags.js" type="text/javascript"></script>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

function finishPage(f_act) { 
 document.forms[0].f_act.value = f_act;
 document.forms[0].submit();
};
-->
</script>
<noscript>
  <center>
  <b><br>JavaScript ist nicht verf&uuml;gbar. Bitte aktivieren Sie JavaScript<br><br>
        in Ihrem Browser, damit diese Seite ordnungsgem&auml;&szlig; funktioniert!</b><br><br>
  </center>
</noscript>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="mc_page-header">
  <?php echo getLngt("PLZ-Bereich") ?>
</div>

<div class="maincontent mc_elem" name="maincontent" id="maincontent">

<form action="../admin/traveltime.php" method="post">
  
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="mode" value="<?php echo ec($mode) ?>">
  <input type="hidden" name="orderByClause" value="<?php echo $orderByClause ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
      <input class="f10np1" type="radio" name="modeFlag" value="0" <?php echo ($modeFlag == "0" ? "checked" : "") ?>> <?php echo getLngt("CLASSIC") ?>
      <input class="f10np1" type="radio" name="modeFlag" value="1" <?php echo ($modeFlag == "1" ? "checked" : "") ?>> <?php echo getLngt("BWV") ?>
  </div>
  <?php echo htmlDivLineSpacer("30px"); ?>

  <div>
    <div <?php echo setStyleHtmlDiv("100px","left"); ?>>VON:</div>
    <div>
      <input type="text" name="zipcodeFrom" value="<?php echo $zipcodeFrom ?>" size="6">
    </div>
  </div>
  <?php echo htmlDivLineSpacer("5px"); ?>
  <div>
    <div <?php echo setStyleHtmlDiv("100px","left"); ?>>BIS:</div>
    <div>
      <input type="text" name="zipcodeTo" value="<?php echo $zipcodeTo ?>" size="6">
    </div>
  </div>
  <?php echo htmlDivLineSpacer("10px", "", "left"); ?>

  <?php echo defineButtonType10("Liste&nbsp;zusammenstellen", "action_list", "finishPage('showlist');", "170", "left", "5"); ?>
  <?php echo defineButtonType10("Speichern", "action_list", "finishPage('save');", "100", "left"); ?>
  <?php echo htmlDivLineSpacer("30px", "", "left"); ?>

  <div>
    Anzahl gefundener Bereiche: <?php echo $lenZipcodeArray ?><br><br>(Max. Anzeige z.Z. eingestellt: <?php echo $maxEnitriesToBeDisplayed ?>)
  </div>
  
  <?php echo htmlDivLineSpacer("30px"); ?>
  
  <div>
    <table align="left" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
      <?php echo $serviceOutput ?>
    </table>    
  </div>

</form>
</div>
</body>
</html>
