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

include_once ("../include/mcglobal.inc.php");
include_once ("../include/inc_parseXML.inc.php");
// include_once ("../include/inc_customer.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "op"));

// Get the IP of the current client calling the page
$currentClientIP = trim($_SERVER['REMOTE_ADDR']);

$pageTitel = getLngt("Newsletter abbestellen");
$generalOutErrMsg = getLngt("Wir konnten Ihre Anfrage leider nicht bearbeiten. Eine automatische Mail an die Systemadministration wurde versendet.");

$out = $generalOutErrMsg;
$imgIdentifyer = "adsg";  // Init


// Write DB log
function writeToLogRemoteDB($logoId, $hqId, $jb_id, $usrId, $crId, $crSid, $csId, $logoDescription, $atId = "0", $ptId = "0", $empId = "0") {
  global $db_conn, $PHP_SELF;
  $tmpArray = array("logo_id", $logoId, "hq_id", $hqId, "jb_id", $jb_id, "usr_id", $usrId, "cr_id", $crId, "cr_sid", $crSid, "cs_id", $csId, "at_id", $atId, "pt_id", $ptId, "emp_id", $empId, "logo_description", $logoDescription);
  $result = $db_conn->query("INSERT INTO phoenix_log.log (logo_id, hq_id, cs_id, logo_description) VALUES ('" . $logoId . "','" . $hqId . "','" . $csId . "','" . $logoDescription . "')");
  return $result;
}

// Remove postparser tags
function convertSpecialMC($op) {
  $op = convertSpecial($op);
  if (substr($op, 0, 12) == "<postparser>") : $op = substr($op, 12); endif;
  if (substr($op, -13) == "</postparser>") : $op = substr($op, 0, -13); endif;
  if (substr($op, 0, 16) == "%3Cpostparser%3E") : $op = substr($op, 16); endif;
  if (substr($op, -17) == "%3C/postparser%3E") : $op = substr($op, 0, -17); endif;
  return $op;
}

$op = convertSpecialMC($op);

if ($op != "") :

   $constExtDbInst = getExternalMetaDbInst();
   $dbMetaConn = getDbConnectionSpecial($constExtDbInst, $dbname, $dblogin, $dbpassword);

   // Check existence of the hash value
   if (existsEntry("meta_object.metaobject",array("mo_hash",$op), $dbMetaConn)) :

      $moId      = getFieldValueFromId("meta_object.metaobject", "mo_hash", $op, "mo_id", $dbMetaConn);
      $moIdRefDB = getFieldValueFromId("meta_object.metaobject", "mo_id", $moId, "mo_id_ref_db", $dbMetaConn);
      $objType   = getFieldValueFromId("meta_object.metaobject", "mo_id", $moId, "mo_obj_type", $dbMetaConn);
      $objId     = getFieldValueFromId("meta_object.metaobject", "mo_id", $moId, "mo_obj_id", $dbMetaConn);
      
      if ($moId != "" && $moId > "0") :

         // Get database instance
         $dbInst = getFieldValueFromId("meta_object.metaobject", "mo_id", $moIdRefDB, "mo_value", $dbMetaConn);

         if ($dbInst != "") :

            // Get database connection
            $db_conn = getDbConnectionSpecial($dbInst, $dbname, $dblogin, $dbpassword);

            // Get header and footer image global identifyer for target database
            // $imgIdentifyer = getFieldValueFromClause("parameter", "par_value", "par_key = 'MD_GLOBAL_SHORTNAME' AND hq_id = '0'", $db_conn);
            $imgIdentifyer = $db_conn->getOne("SELECT par_value FROM phoenix.parameter WHERE par_key = 'MD_GLOBAL_SHORTNAME' AND hq_id = '0'");

            if ($objType == "cs") :

                  $cmpId = $db_conn->getOne("SELECT cs.cmp_id FROM phoenix.customer AS cs WHERE cs.cs_id = '" . $objId . "' ");

                  if ($cmpId != "" && $cmpId > "0") :

                     $hqId = $db_conn->getOne("SELECT cs.hq_id FROM phoenix.customer AS cs WHERE cs.cs_id = '" . $objId . "' ");

                     // Change state of the newsletter abonnement
                     $result = $db_conn->query("UPDATE phoenix.company SET cmp_dsgvo = '1', cmp_newsletter = '1' WHERE cmp_id='" . $cmpId . "'");
                     $err = "";
                     if (DB::isError($result)) : $err = "ERR"; endif;

                     if ($err == "") :

                        // Write logdata into log database
                        writeToLogRemoteDB("145",$hqId,"","","","",$objId,"STATUS=OK|IP=" . $currentClientIP . "|MO_ID=" . $moId);

                        $out = getLngt("Wir bedanken uns herzlich für die Bestätigung!");
                     else :
                        // Update failed
                        writeToLogRemoteDB("145",$hqId,"","","","",$objId,"STATUS=UPDATE_ERR|IP=" . $currentClientIP . "|MO_ID=" . $moId);
                     endif;
                  else :
                     // Customer not found
                     writeToLogRemoteDB("145","","","","","","","STATUS=CS_NOT_FOUND|IP=" . $currentClientIP . "|MO_ID=" . $moId);
                  endif;

            elseif ($objType == "cr") :

                  $cmpId = $db_conn->getOne("SELECT cr.cmp_id FROM phoenix.courier AS cr WHERE cr.cr_id = '" . $objId . "' ");

                  if ($cmpId != "" && $cmpId > "0") :

                     $hqId = $db_conn->getOne("SELECT cr.hq_id FROM phoenix.courier AS cr WHERE cr.cr_id = '" . $objId . "' ");

                     // Change state of the newsletter abonnement
                     $result = $db_conn->query("UPDATE phoenix.company SET cmp_dsgvo = '1', cmp_newsletter = '1' WHERE cmp_id='" . $cmpId . "'");
                     $err = "";
                     if (DB::isError($result)) : $err = "ERR"; endif;

                     if ($err == "") :

                        // Write logdata into log database
                        writeToLogRemoteDB("145",$hqId,"","","","",$objId,"STATUS=OK|IP=" . $currentClientIP . "|MO_ID=" . $moId);

                        $out = getLngt("Wir bedanken uns herzlich für die Bestätigung!");
                     else :
                        // Update failed
                        writeToLogRemoteDB("145",$hqId,"","","","",$objId,"STATUS=UPDATE_ERR|IP=" . $currentClientIP . "|MO_ID=" . $moId);
                     endif;
                  else :
                     // Customer not found
                     writeToLogRemoteDB("145","","","","","","","STATUS=CS_NOT_FOUND|IP=" . $currentClientIP . "|MO_ID=" . $moId);
                  endif;

            elseif ($objType == "emp") :
                  // ...
            else :
                  // Write logdata into log database
                  writeToLogRemoteDB("145",$hqId,"","","","",$objId,"STATUS=OBJTYPE_NOT_FOUND|IP=" . $currentClientIP . "|MO_ID=" . $moId);
            
                  // OBJECTTYPE nicht gesetzt oder nicht bekannt
                  $out = $generalOutErrMsg;
            endif;

         else :
            // database not specified in metaobject row
            writeToLogRemoteDB("145","","","","","","","STATUS=DB_NOT_SPECIFIED|IP=" . $currentClientIP . "|MO_ID=" . $moId);
         endif;
      else :
         // HASH nicht gefunden um Zuordnung herzustellen
         $out = $generalOutErrMsg;
      endif;
   endif;
else :
   // Write logdata into log database
   writeToLogRemoteDB("145","","","","","","","STATUS=EMPTY_OPCODE|IP=" . $currentClientIP . "|MO_ID=" . $moId);
   
   $out = $generalOutErrMsg;
endif;

?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">

<script type="text/javascript">
<!--


-->
</script>

</head>


<body class="menu_bgcol">


<style type="text/css">


.mcTable { border:0px; width:650px; background:#CDD9FD; }

.mcTable2 { border:0px; width:630px; background:#CDD9FD; }

.mcTd { vertical-align:top; padding:5px; font-size:10pt; font-family:Helvetica,Arial; 
        font-style:normal; font-weight:normal; background:#CDD9FD; color:#000000; }

.spacer05 { height:5px; background:#CDD9FD;}
.spacer15 { height:15px; background:#CDD9FD;}

</style>


<center>
<table class="mcTable">
  <tr>
  	<td style="width:650px;" align="center">
  		<img src="../images/external/<?php echo strtolower($imgIdentifyer) ?>_nltr_header.jpg" border="0" height="167" width="640">
    </td>
  </tr>
</table>
<table class="mcTable"><tr><td class="spacer15"></td></tr></table>

<table class="mcTable">
  <tr>
    <td class="mcTd" align="center">
    	<?php echo getLngt("Sehr geehrte Damen und Herren,") ?>
    	<br><br>
    	<?php echo $out ?>
    </td>
  </tr>
</table>
<table class="mcTable"><tr><td class="spacer15"></td></tr></table>

<table class="mcTable">
  <tr>
  	<td style="width:650px;" align="center">
  		<img src="../images/external/<?php echo strtolower($imgIdentifyer) ?>_nltr_footer.jpg" border="0" height="69" width="640">
    </td>
  </tr>
</table>
</center>


</body>

</html>
