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


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../geo/geocode.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_id", "f_street", "f_zipcode", "f_city", "f_country", 
                  "f_street_new", "f_zipcode_new", "f_city_new", "f_country_new",
                  "f_st_id_remove",
                  "orderClauseDefault", "statusMessage", "deactivateMenu"));

getLanguage(__FILE__);

$pageTitel = getLngt("ADRESSEN");
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);
if (!authCheckEmployeeRights($emp_id, "0")) : die ("$PHP_SELF: Access denied!"); endif;

$numOfRows = 0;
$numOfRows2 = 0;
mcTrim($f_street); mcTrim($f_zipcode); mcTrim($f_city);
$searchValues = $f_street . $f_zipcode . $f_city;  // If called e.g. by the first time with empty search-fields

// If no search-field has content then take default-city of the headquarter
$hqAddress = getAddress($hq_id, "hq");
if ($f_city == "") :
   $f_city = mcArrTrim($hqAddress, "city");
endif;

// Initialize output
$out = "";
$out2 = "";


// Update single address
if ($f_act == "update") :
   if ($f_id != "") :
      if ($f_street_new != "" && $f_zipcode_new != "" && $f_city_new != "") :
      
         if ($f_country_new == "") : $f_country_new = "DE"; endif;
         
         updateStmt("address", "ad_id", $f_id, array("ad_street", $f_street_new, "ad_zipcode", $f_zipcode_new, "ad_city", $f_city_new, "ad_country", $f_country_new));
      else :
         $statusMessage = getLngt("Unvollständige Adressen werden nicht abgespeichert!");
      endif;
   else :
      $statusMessage = getLngt("Wählen Sie bitte eine Adresse aus! Wählen Sie dazu bitte in dem Suchergebnis den jeweiligen Bearbeitungs-Link!");
   endif;

   // Init new search with updated address
   $f_act = "search";
endif;


// Inserts address to table "street" for correct validation in the future
if ($f_act == "validated") :
   if ($f_street_new != "" && $f_zipcode_new != "" && $f_city_new != "") :
      if (!existsEntry("phoenix_special.street",array("st_street",$f_street_new,"st_zipcode",$f_zipcode_new,"st_city",$f_city_new))) :
         insertStmt("phoenix_special.street", array("st_street",$f_street_new,"st_zipcode",$f_zipcode_new,"st_city",$f_city_new));
      else :
         $statusMessage = getLngt("Die Adresse existiert schon für die Validierung!");
      endif;
   else :
      $statusMessage = getLngt("Unvollständige Adressen werden nicht abgespeichert!");
   endif;

   // Init new search with updated address
   $f_act = "search";
endif;


// Removes entry from table "street"
if ($f_act == "removeEntryFromStreet") :
   if ($f_st_id_remove != "") :
      deleteStmt("phoenix_special.street","st_id = ".$f_st_id_remove);
   else :
      $statusMessage = getLngt("Der Eintrag kann nicht gelöscht werden!");
   endif;
   
   // Init new search with updated address
   $f_act = "search";
endif;


// Generate search-resultset
if ($f_act == "search" && $searchValues != "") :
        
   if (strlen($f_street) > 4 || (strlen($f_street) > 1 && (strlen($f_zipcode) > 1 || strlen($f_city) > 1))) :

      // *********************************************************************
      // * Selection of the costcenters of the current customer for the list *
      // *********************************************************************
      $whereClause = "";
      if ($f_street != "") : $whereClause .= "ad_street LIKE '" . $f_street . "%'"; endif;      
      if ($whereClause != "" && $f_zipcode != "") : $whereClause .= " AND "; endif;
      if ($f_zipcode != "") : $whereClause .= "ad_zipcode LIKE '" . $f_zipcode . "%'"; endif;                  
      if ($whereClause != "" && $f_city != "") : $whereClause .= " AND "; endif;      
      if ($f_city != "") : $whereClause .= "ad_city LIKE '" . $f_city . "%'"; endif;      
      if ($whereClause != "" && $f_country != "") : $whereClause .= " AND "; endif;      
      if ($f_country != "") : $whereClause .= "ad_country LIKE '" . $f_country . "%'"; endif;

      // if ($whereClause != "") : $whereClause = " AND " . $whereClause; endif;

      if ($orderClauseDefault == "") : 
         $orderClause = "ad_street, ad_zipcode";
      else :
         $orderClause = "ad_" . $orderClauseDefault;
      endif;


      // *******************
      // * TABLE "address" *
      // *******************         
      $sqlquery = "SELECT ad.ad_id, ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country" 
                  . " FROM address AS ad"
                  . " WHERE " . $whereClause
                  . " ORDER BY " . $orderClause;

      $lineToggler = 0;
      $result = $db->dbQ($sqlquery);
      while ($row = $result->fetch_assoc()):            
            $numOfRows++;

            if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;                
            $cellColor = getListColor($numOfRows, $lineToggler);

            $v_ad_id        = $row["ad_id"];
            $v_ad_street    = $row["ad_street"];
            $v_ad_zipcode   = $row["ad_zipcode"];
            $v_ad_city      = $row["ad_city"];
            $v_ad_country   = $row["ad_country"];

            $out .= "<tr class=\"f10bp1\">";
            $out .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_street &nbsp;</td> ";
            $out .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_zipcode &nbsp;</td> ";
            $out .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_city &nbsp;</td> ";            
            $out .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_country &nbsp;</td> ";
            // $out .= "<td class=\"f8np1_red\" bgcolor=\"" . $cellColor ."\"> $v_ad_district &nbsp;</td> ";
            $out .= "<td bgcolor=\"" . $cellColor ."\">&nbsp;" . "<a href=\"javascript:changeAddress('".$v_ad_id."','".my_str_check_js($v_ad_street)."','".my_str_check_js($v_ad_zipcode)."','".my_str_check_js($v_ad_city)."','".my_str_check_js($v_ad_country)."');\">"
                                 . getLngt("Bearbeiten") . "</a>&nbsp;</td>";
            $out .= "</tr>\n";
      endwhile;
      $result->free();     


      // **************************************************************************************
      // * TABLE "street" Selection of the costcenter based on a search by companyname-prefix *
      // **************************************************************************************

      if (TRUE) :
         $whereClause = "";
         if ($f_street != "") : $whereClause .= "st_street LIKE '" . $f_street . "%'"; endif;      
         if ($whereClause != "" && $f_zipcode != "") : $whereClause .= " AND "; endif;
         if ($f_zipcode != "") : $whereClause .= "st_zipcode LIKE '" . $f_zipcode . "%'"; endif;                  
         if ($whereClause != "" && $f_city != "") : $whereClause .= " AND "; endif;      
         if ($f_city != "") : $whereClause .= "st_city LIKE '" . $f_city . "%'"; endif;      
         if ($whereClause != "" && $f_country != "") : $whereClause .= " AND "; endif;      
         if ($f_country != "") : $whereClause .= "st_country LIKE '" . $f_country . "%'"; endif;

         // if ($whereClause != "") : $whereClause = " AND " . $whereClause; endif;

         if ($orderClauseDefault == "") : 
            $orderClause = "st_street, st_zipcode";
         else :
            $orderClause = "st_" . $orderClauseDefault;
         endif;      

         $sqlquery = "SELECT st_id, st_district, st_street, st_zipcode, st_city, st_country" 
                      . " FROM phoenix_special.street"
                      . " WHERE "    . $whereClause
                      . " ORDER BY " . $orderClause;

         $result = $db->query($sqlquery);
         if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

         $lineToggler = 0;
         while ($row = $result->fetch_assoc()):            
               $numOfRows2++;

   	           if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;                
               $cellColor = getListColor($numOfRows2, $lineToggler);

               $v_ad_id        = $row["st_id"];
               $v_ad_district  = $row["st_district"];
               $v_ad_street    = $row["st_street"];
               $v_ad_zipcode   = $row["st_zipcode"];
               $v_ad_city      = $row["st_city"];
               $v_ad_country   = $row["st_country"];

               $out2 .= "<tr class=\"f10bp1\">";
               // $out2 .= "<td class=\"f10bp1_red\" bgcolor=\"" . $cellColor ."\"> $v_csc_name </td> ";
               $out2 .= "<td bgcolor=\"" . $cellColor ."\">&nbsp;" . "<a href=\"javascript:finishPage('".my_str_check_js($v_ad_district)."','".my_str_check_js($v_ad_street)."','".my_str_check_js($v_ad_zipcode)."','".my_str_check_js($v_ad_city)."','".my_str_check_js($v_ad_country)."','".mcIsSet($tourno)."');\">"
                                         . $v_ad_street . "</a>" . "&nbsp;</td>";
               $out2 .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_zipcode &nbsp;</td> ";
               $out2 .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_city &nbsp;</td> ";            
               // $out2 .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_country &nbsp;</td> ";
               // $out2 .= "<td bgcolor=\"" . $cellColor ."\"> $v_ad_district &nbsp;</td> ";
               $out2 .= "<td bgcolor=\"" . $cellColor ."\">&nbsp;" . "<a href=\"javascript:removeEntryFromStreet('".$v_ad_id."');\">" . getLngt("Löschen") . "</a>&nbsp;</td>";

               $out2 .= "</tr>\n";
         endwhile;
         $result->free();
      endif;  // if (FALSE)
   else :
      $statusMessage = getLngt("Eingabe entw. mind. 5 Zeichen bei Straße oder mind. 2 Zeichen bei Straße in Verbindung mit mind. 2 Zeichen bei Ort oder PLZ!");
   endif;
endif;   
?>

<html>

<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 type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

var v_district = '';
var v_street = '';
var v_zipcode = '';
var v_city = '';
var v_country = '';

function changeAddress(id,street,zipcode,city,country) {
  document.forms[0].f_id.value = id;
  document.forms[0].f_street_new.value = street;
  document.forms[0].f_zipcode_new.value = zipcode;
  document.forms[0].f_city_new.value = city;
  document.forms[0].f_country_new.value = country;
};

function removeEntryFromStreet(id) {
  if (confirm('<?php echo getLngt("Soll der Eintrag gelöscht werden?") ?>')) {
     document.forms[0].f_st_id_remove.value = id;
     document.forms[0].f_act.value = 'removeEntryFromStreet';
     document.forms[0].submit();
  }
};

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
};

function clearFields() {
  document.forms[0].f_id.value = '';
  document.forms[0].f_street.value = '';
  document.forms[0].f_zipcode.value = '';
  document.forms[0].f_city.value = '<?php echo $hqAddress["city"] ?>';
  document.forms[0].f_country.value = '';
  if (document.forms[0].f_district) {document.forms[0].f_district.value = '';};
};
-->
</script>
</head>

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

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

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

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

  <form action="ad_admin.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">  
  <input type="hidden" name="orderClauseDefault" value="<?php echo $orderClauseDefault ?>">
  <input type="hidden" name="f_id" value="">
  <input type="hidden" name="f_st_id_remove" value="">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">

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

  <?php echo defineButtonType08(getLngt("Suchen"), "action_search", "javascript:document.forms[0].f_act.value='search';document.forms[0].submit();", "180", "left", "3"); ?>
  <?php echo defineButtonType08(getLngt("Felder zurücksetzen"), "action_clear", "javascript:clearFields();", "180"); ?>
  <?php echo htmlDivLineSpacer("5px"); ?>

  <div>
    <table class="f8np1" border="0" cellpadding="0">
      <tr>
        <td><input type="text" name="f_street" value="<?php echo $f_street ?>" size="25" placeholder="<?php echo getLngt("Straße") ?>"></td>
        <td><input type="text" name="f_zipcode" value="<?php echo $f_zipcode ?>" size="5" placeholder="<?php echo getLngt("PLZ") ?>"></td>
        <td><input type="text" name="f_city" value="<?php echo $f_city ?>" size="20" placeholder="<?php echo getLngt("Ort") ?>"></td>                  
        <td><input type="text" name="f_country" value="<?php echo $f_country ?>" size="3" placeholder="<?php echo getLngt("Land") ?>"></td>
   <!-- <td><input type="text" name="f_district" value="<?php echo mcIsSet($f_district) ?>" size="20"></td> -->
        <td><?php // echo getLngt("Bearbeiten") ?></td>
      </tr>
<?php if (false) : ?>
      <tr>
        <td><a href="javascript:document.forms[0].orderClauseDefault.value='street';document.forms[0].f_act.value='search';document.forms[0].submit();"><?php echo getLngt("Straße") ?></a></td>
        <td><a href="javascript:document.forms[0].orderClauseDefault.value='zipcode';document.forms[0].f_act.value='search';document.forms[0].submit();"><?php echo getLngt("PLZ") ?></a></td>
        <td><a href="javascript:document.forms[0].orderClauseDefault.value='city';document.forms[0].f_act.value='search';document.forms[0].submit();"><?php echo getLngt("Ort") ?></a></td>                  
        <td><a href="javascript:document.forms[0].orderClauseDefault.value='country';document.forms[0].f_act.value='search';document.forms[0].submit();"><?php echo getLngt("Land") ?></a></td>
   <!-- <td><a href="javascript:document.forms[0].orderClauseDefault.value='district';document.forms[0].f_act.value='search';document.forms[0].submit();"><?php echo getLngt("Ortsteil") ?></a></td> -->
        <td>&nbsp;</td>
      </tr>
<?php endif; ?>
      <tr>
      	<td colspan="5" height="10px"></td>
      </tr>
    <?php echo $out ?>
    </table>    
  </div>
  <?php echo htmlDivLineSpacer("15px"); ?>

  <div>
    <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " (Keine Einträge gefunden.)"; endif; ?>
  </div>
  <?php echo htmlDivLineSpacer("50px"); ?>

  <?php echo defineButtonType08(getLngt("Änderung speichern"), "action_store", "javascript:document.forms[0].f_act.value='update';document.forms[0].submit();", "180", "left", "3"); ?>
  <?php echo defineButtonType08(getLngt("Für Validierung hinzufügen"), "action_validated", "javascript:document.forms[0].f_act.value='validated';document.forms[0].submit();", "180"); ?>
  <?php echo htmlDivLineSpacer("15px"); ?>

  <div>
    <table border="0">
      <tr>
        <td>
          <table class="f8np1" border="0" cellpadding="0">
            <tr>
              <td><input type="text" name="f_street_new" value="" size="25" placeholder="<?php echo getLngt("Straße") ?>"></td>
              <td><input type="text" name="f_zipcode_new" value="" size="5" placeholder="<?php echo getLngt("PLZ") ?>"></td>
              <td><input type="text" name="f_city_new" value="" size="20" placeholder="<?php echo getLngt("Ort") ?>"></td>                  
              <td><input type="text" name="f_country_new" value="" size="3" placeholder="<?php echo getLngt("Land") ?>"></td>
         <!-- <td><input type="text" name="f_district_new" value="" size="20"></td> -->
         <!-- <td>&nbsp;</td> -->
            </tr>
          </table>
        </td>
      </tr> 
      <?php
        if ($out2 != "") :
      ?>
         <tr>
           <td>
             <br><br><br>
             <span class="f8np1_blue"><?php echo getLngt("Bestehende Adressen für Validierungprüfung:") ?></span>
             <br><br>
             <table class="f8np1" border="0" cellpadding="0">
               <?php echo $out2 ?>
             </table>            
           </td>
         </tr>                                
      <?php
        endif;
      ?>
    </table>
  </div>
  <?php echo htmlDivLineSpacer("15px"); ?>

  <div>
    <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows2 ?><?php if ($numOfRows2 == "0" && $f_act == "search" && $statusMessage == "") : echo " " . getLngt("(Keine Einträge gefunden.)"); endif; ?>
  </div>
  <?php echo htmlDivLineSpacer("40px"); ?>

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

</html>