<?php
/*=======================================================================
 *
 * import_ITCS.php
 *
 =======================================================================*/

include_once ("../import/import.php");

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "statusMessage", "importFile", "executeImportProcess", "deactivateMenu", "objType", "objId"));

/*
$HTTP_POST_VARS = !empty($HTTP_POST_VARS) ? $HTTP_POST_VARS : $_POST;
$HTTP_GET_VARS = !empty($HTTP_GET_VARS) ? $HTTP_GET_VARS : $_GET;
$HTTP_COOKIE_VARS = !empty($HTTP_COOKIE_VARS) ? $HTTP_COOKIE_VARS : $_COOKIE;
$HTTP_SERVER_VARS = !empty($HTTP_SERVER_VARS) ? $HTTP_SERVER_VARS : $_SERVER;
$HTTP_ENV_VARS = !empty($HTTP_ENV_VARS) ? $HTTP_ENV_VARS : $_ENV;
$HTTP_POST_FILES = !empty($HTTP_POST_FILES) ? $HTTP_POST_FILES : $_FILES;

if (!isset($PHP_SELF))
  $PHP_SELF = $_SERVER["PHP_SELF"];

if (substr(phpversion(), 0, 3) >= "5.1") :
   date_default_timezone_set('Europe/Berlin');
endif;

// Check HTTP-Parameters
// getSecHttpVars("1",array("f_act", "statusMessage", "importFile", "executeImportProcess", "deactivateMenu", "objType", "objId"));
$f_act = $HTTP_POST_VARS["f_act"]; if ($f_act) == "" : $f_act = $HTTP_GET_VARS["f_act"]; endif;
$statusMessage = $HTTP_POST_VARS["statusMessage"]; if ($statusMessage) == "" : $statusMessage = $HTTP_GET_VARS["statusMessage"]; endif;
$importFile =  $HTTP_POST_VARS["importFile"]; if ($importFile) == "" : $importFile = $HTTP_GET_VARS["importFile"]; endif;
$executeImportProcess = $HTTP_POST_VARS["executeImportProcess"]; if ($executeImportProcess) == "" : $executeImportProcess = $HTTP_GET_VARS["executeImportProcess"]; endif;
*/


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

function get_url ($request_url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

function create_url ($req) {
    global $rfcProt,$rfcUrl;
    $request_url = $rfcProt . $rfcUrl ."?operation=".urlencode($req);
    return $request_url;
}



getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("IMPORT ITCS");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access
// $usrAccessArray["hq"] = "1";
// authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
// if (!(authCheckEmployeeRights($emp_id, "22"))) : gotoReferer("1"); endif;

// $empHasAdminRights = false;
// $empIdRootAdmin = getEmpIdOfRootAdmin($userTypeName);
// if ($empIdRootAdmin != "" && $emp_id == $empIdRootAdmin) : 
   $empHasAdminRights = true;
// endif;

$debug = false;
// if ($empIdRootAdmin == $emp_id) :
//    $debug = true;
// endif;

if ($f_act != "executeImport" || $executeImportProcess != "1") : $executeImportProcess = ""; endif;    // Execute import after check run
$showExecutionProcessButton = true;
$outText = "";
$closeWindow = "0";
$delimiter = ";";
$fire = true;

// Customer and headquarters references have to exist
/*
$hqId = "";
$csEid = "";
$objType = trim($objType);
$objId = trim($objId);
if ($objType == "CS" && is_numeric($objId)) :
   $csId = getFieldValueFromId("phoenix.customer", "cs_id", $objId, "cs_id");
endif;
if ($csId != "") :
   $hqId = getFieldValueFromId("phoenix.customer", "cs_id", $csId, "hq_id");
   $csEid = getFieldValueFromId("phoenix.customer", "cs_id", $csId, "cs_eid");
endif;
if ($debug) :
   echo "f_act : " . $f_act . "<br>";
   echo "objType : " . $objType . "<br>";
   echo "objId : " . $objId . "<br>";
   echo "hqId : " . $hqId . "<br>";
   echo "csId : " . $csId . "<br>";
   echo "<br>";
endif;
*/

// Current file to be imported
$importFile = urldecode($importFile);
$posLastSlash = strripos($importFile, "/");
$posLastSlash++;
$pathname = substr($importFile, 0, $posLastSlash);
$filename = substr($importFile, $posLastSlash);
$outText = "<span class=\"f10bp1_blue\">" . getLngt("IMPORTDATEI:") . "</span>&nbsp;" . $filename . "<br><br>";


if ($importFile != "") :

   // Create internal representation
   $srvpArray = array();
   $zoneArray = array();

   if (file_exists($importFile)) :

      $currentTime = getDateTime("0");

      // ********************
      // * IMPORT FILE xxxx *
      // ********************
      if (!(strpos($filename, "import_ITCS_") === false)) :

         if ($executeImportProcess) :

            // Init
            $rfcProt = "https://";
            $rfcUrl = "app.assecutor.de/cgi-bin/vudi_itc.test/";

            $op = "import";
            $auth = "local";
            $type = "csv";

            $data = importDataset($importFile);
            $dataLen = count($data);

            for ($j = 0; $j < $dataLen; $j++) {
                $xml_ = "<XML><operation op=\"" . $op . "\" auth=\"" . $auth . "\" type=\"" . $type . "\"><data>" . $data[$j] . "</data></operation></XML>";
                $request_url = create_url($xml_);
                $response = get_url($request_url);
                echo $response;
                echo "<br />";
                $response = json_decode($response);
            }
         endif;
      endif;
   else :
      $statusMessage .= getLngt("Die angegebene Datei existiert nicht!");
   endif;
else :
   $statusMessage .= getLngt("Es wurde keine Datei spezifiziert!");
endif;
?>

<html>
<head>
<title><?php echo $pageTitel ?></title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

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

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

function finishPageExecuteImportProcess(f_act) {  
  document.forms[0].f_act.value=f_act;
  document.forms[0].executeImportProcess.value = '1';
  document.forms[0].submit();
};

function onBodyLoad() {
	displayStatusMessage();
	var closeWindow = '<?php echo $closeWindow ?>';
	if (closeWindow == '1') {
     opener.document.forms[0].submit();
     window.close();
  };
};
-->
</script>

</head>

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

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

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

  <div>
    <form name="import_form" action="../import/import_ITCS.php" method="post">
      <input type="hidden" name="f_act" value="">
      <?php echo $phpCurrentNavigationInputHidden ?>
      <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
      <input type="hidden" name="importFile" value="<?php echo $importFile; ?>">
      <input type="hidden" name="executeImportProcess" value="<?php echo $executeImportProcess; ?>">
      <input type="hidden" name="objType" value="<?php echo $objType; ?>">
      <input type="hidden" name="objId" value="<?php echo $objId; ?>">

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

      <?php
        if ($showExecutionProcessButton && $executeImportProcess != "1") :
           echo defineButtonType10(getLngt("Import starten"), "action_import", "finishPageExecuteImportProcess('executeImport');", "200");
           echo htmlDivLineSpacer("5px");
           echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "200");
           echo htmlDivLineSpacer("5px");
        endif;
      ?>

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

      <div>
        <table border="1" margin="10" padding="10">
        	<tr>
        		<td style="vertical-align:top;">
              <?php echo $outText; ?>
            </td>
          </tr>
        </table>    
      </div>
    </form>
  </div>

</div>

</body>

</html>