<?php     

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

// Execution-Time for script
set_time_limit(30000);


// ********************************************
// * Trigger for form "Transporteur 2. Seite" *
// ********************************************
// Preconditions:
// hq_id (default "0" wg. mandantenübergreifender Nutzung)
// mtfc_id  (Kategorie, "3" <=> "2. Transporteursseite")

$tmpCategory = "3";
$tmpHqId = $hq_id;
$gHqId = true;
$constFormSingleHQ = getParameterValue("0", "SYSTEM_FORM_SINGLE_HQ_" . $category, "0");
if ($constFormSingleHQ != "" && $constFormSingleHQ != "0") :
   $gHqId = false;
else :
   $constFormSingleHQ = getParameterValue("0", "SYSTEM_FORM_SINGLE_HQ", "0");
   if ($constFormSingleHQ != "" && $constFormSingleHQ != "0") :
      $gHqId = false;
   endif;
endif;
if ($gHqId == true) :
   $tmpHqId = "0";
endif;

$currentDate = getDateTime("1");
$currentDateTime = getDateTime("6");

$sqlquery = "SELECT usr.usr_firstname, usr.usr_name, mtfk.mtfk_name, mtfv.mtfv_value"
          . " FROM metafieldcategory AS mtfc, metafieldkey AS mtfk, metafieldcategorykey AS mtfck,"
          . "      metafieldvalue AS mtfv, courier AS cr, user AS usr"
          . " WHERE mtfk.mtfk_type IN ('triggerdate', 'triggerdatetime') AND"
          . "       mtfck.mtfc_id = '" . $tmpCategory . "' AND"
          . "       mtfck.mtfc_id = mtfc.mtfc_id AND"
          . "       mtfck.hq_id = '" . $tmpHqId . "' AND"
          . "       mtfck.mtfk_id = mtfk.mtfk_id AND"
          . "       mtfv.mtfck_id = mtfck.mtfck_id AND"
          . "       mtfv.mtfv_id = cr.cr_id AND"
          . "       cr.usr_id = usr.usr_id AND"
          . "       mtfv.mtfv_value != '' AND"
          . "       " . $currentDate . " >= CONCAT(SUBSTRING(mtfv.mtfv_value,7,4),SUBSTRING(mtfv.mtfv_value,4,2),SUBSTRING(mtfv.mtfv_value,1,2))"
          . " ORDER BY mtfk.mtfk_name, usr.usr_name, usr.usr_firstname";
$result = $db->query($sqlquery);
$triggerArray = array();
if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
while ($row = $result->fetch_assoc()):
      $triggerArray[] = array($row["usr_firstname"], $row["usr_name"], $row["mtfk_name"], $row["mtfv_value"]);
endwhile;
$result->free();

$triggerArrayLen = count($triggerArray);
$triggerOut = "";
if ($triggerArrayLen > 0) :
   $triggerOut .= "<table>\n";
   for ($i = 0; $i < $triggerArrayLen; $i++) :
       $tmpArray = $triggerArray[$i];
       $tmpArrayLen = count($tmpArray);
       $triggerOut .= "<tr>\n";
       for ($j = 0; $j < $tmpArrayLen; $j++) :
           $triggerOut .= "<td>" . $tmpArray[$j] . "&nbsp;&nbsp;&nbsp;</td>\n";
       endfor;
       $triggerOut .= "</tr>\n";
   endfor;
   $triggerOut .= "</table>\n";
endif;
echo $triggerOut;
?>