<?php

require_once("config/connect.php");
require_once("global_functions.php");
set_time_limit(600);
$synchronizeTables = array("products", "categories");

function get_table_from_host($url) {
	global $incr;
	if ($incr === TRUE) $CONDITION="&incr=TRUE";
	else $CONDITION="&incr=FALSE";
	//echo $url;
    $xml = simplexml_load_file($url . $CONDITION);

	if (!$xml) {
		echo "Fehler beim Laden der XML vom entfernten Rechner: $url$CONDITION\n";
		foreach(libxml_get_errors() as $error) {
			echo "\t", $error->message . "\n";
		}
	}
	return($xml);
}

function does_a_record_exist($query) {
	$sqlP=mysql_query($query);
	if (! $sqlP) {
		die('ERROR: ' . mysql_error() );
	}
	if (mysql_num_rows($sqlP)>=1) return (TRUE);
	else return (FALSE);
}

function run_sql_insert($query) {
	$sqlP=mysql_query($query);
	if (! $sqlP) {
		die('ERROR: ' . mysql_error() . ' Kommando: ' . $query );
	}
	else return (TRUE);
}

function get_fields($table) {
	$query = mysql_query("DESCRIBE $table;");
	$result = array();
	if($query) {
		while($row = mysql_fetch_assoc($query)) {
			$result[] = $row['Field'];
		}
	}
	return $result;
}

function transfer_table($url, $table_name, $table_key, $kassa_id) {
	global $incr;
	ini_set('memory_limit', '-1');
	$xml=get_table_from_host($url . "/export_xml.php?TABELLE=".$table_name."&KASSA=".$kassa_id);
	$fields = get_fields($table_name);
	$rows = 0;
	mysql_query("Start transaction");
    foreach ($xml->row as $reihe) {
		set_time_limit(60);
        $table_id=urldecode($reihe->$table_key);

        $sql_string_update="";
        $sql_string_insert1="";
        $sql_string_insert2="";

        $array=get_object_vars($reihe);

		foreach($array as $key=>$value) {
			if (in_array($key, $fields)) {
            	$value=urldecode($value);
            	$value=addslashes($value);

            	if (isset($sql_string_update) && !empty($sql_string_update)) $sql_string_update=$sql_string_update . ", ". $key . "='".$value."'";
				else $sql_string_update=" ". $key . "='".$value."'";
				
				if (isset($sql_string_insert1) && !empty($sql_string_insert1)) $sql_string_insert1=$sql_string_insert1 . ", ". $key;
				else $sql_string_insert1=" (". $key;
				
				if (isset($sql_string_insert2) && !empty($sql_string_insert2)) $sql_string_insert2=$sql_string_insert2 . ", '". $value . "'";
				else $sql_string_insert2=" ('". $value . "'";
			}
        }
        
        $sql_string_update="UPDATE $table_name SET " . $sql_string_update . " WHERE $table_key='".$table_id."'";
        $sql_string_insert="INSERT INTO $table_name " . $sql_string_insert1 . ") VALUES " . $sql_string_insert2 . ")";

        if (does_a_record_exist("SELECT $table_key FROM $table_name WHERE $table_key='".$table_id."'")) {
			run_sql_insert($sql_string_update); 
			$rows++;
		} else {
			run_sql_insert($sql_string_insert);
			$rows++;
		}
		unset($xml);
	}
	mysql_query("commit");
	echo "Datensaetze in Tabelle $table_name eingefuegt/geaendert: " . $rows . "\n";
	return;
}

function run_import() {
	$time = -microtime(true);
    $url = FILIALSYSTEM_URL_ZENTRALSYSTEM;
	$url=$url."/synchronize";

	$kassa_id=FILIALSYSTEM_KASSA_NAME;
	//ini_set('memory_limit', '555555M');
	transfer_table($url, 'products', 'ID', $kassa_id);
	transfer_table($url, 'categories', 'ID', $kassa_id);
	//transfer($url, 'products', 'ID', $kassa_id);
	$time += microtime(true);
	echo "Dauer: " . $time . "\n";
	return;
}

$incr=FALSE;

if (isset($_POST['submit']) || isset($argv[1])) {
	if ($argv[1] === "true") {
		$incr = TRUE;
	}
	run_import();
}
?>