<?php
$uploadfile = "tmp/cont_" . $_SESSION['TID'] . "_" . PREFIX . ".csv";
$handle = fopen($uploadfile, 'r');
$dateiinhalt = fread($handle, filesize($uploadfile));

// Konvertiere Dateiinhalt
if (mb_check_encoding($dateiinhalt, 'UTF-8'))
{
	$dateiinhalt = mb_convert_encoding($dateiinhalt, 'ISO-8859-1', 'UTF-8');
}
elseif (mb_check_encoding($dateiinhalt, 'ISO-8859-1'))
{
}
// Ende Konvertierung

$array2prep = explode("\n", $dateiinhalt);
$number2prep = count($array2prep);
$j = 0;
$k = 0;
while ($j < $number2prep)
{
	if (1 == substr_count($array2prep[$j], '"') % 2)
	{
		$array[$k] = $array2prep[$j];
		$j++;
		while ((0 == substr_count($array2prep[$j], '"')) AND ($j < $number2prep))
		{
			$array[$k] .= "\n" . $array2prep[$j];
			$j++;
		}
		$array[$k] .= "\n" . $array2prep[$j];
		$j++;
	}
	else
	{
		$array[$k] = $array2prep[$j];
		$j++;
	}
	$k++;
}
$number_rows = count($array) - 1;
$error = 0;
$gut = 0;
echo "<h1>Ergebnis des Datenimports:</h1>\n";
for ($i = $_POST['Beginn']; $i < $number_rows; $i++)
{
	if ("," == $_POST['separator'])
	{
		$datensatz = explode(",", $array[$i]);
	}
	elseif (";" == $_POST['separator'])
	{
		$datensatz = explode(";", $array[$i]);
	}
	elseif ("Tabulator" == $_POST['separator'])
	{
		$datensatz = explode("\t", $array[$i]);
	}
	if ("" != $datensatz[$_POST['Datum']])
	{
		$nt = explode(".", $datensatz[$_POST['Datum']]);
		if (3 != sizeof($nt))
		{
			$nt = explode("-", $datensatz[$_POST['Datum']]);
			if (3 == sizeof($nt))
			{
				$Datum = "		Datum		= \"" . $nt['0'] . "-" . $nt['1'] . "-" . $nt['2'] . "\"";
			}
			else
			{
				$Datum = "		Datum		= CURDATE()";
			}
		}
		else
		{
			if (4 != strlen($nt['2']))
			{
				$Datum = "		Datum		= CURDATE()";
			}
			else
			{
				if (checkdate($nt['1'], $nt['0'], $nt['2']))
				{
					$Datum = "		Datum		= \"" . $nt['2'] . "-" . $nt['1'] . "-" . $nt['0'] . "\"";
				}
			}
		}
	}
	else
	{
		$Datum = "		Datum		= CURDATE()";
	}

	$number_cols = count($datensatz);
	if ($number_cols < 20)
	{
		for ($temp = $number_cols; $temp <= 20; $temp++)
		{
			$datensatz[$temp] = "";
		}
	}
	for ($temp = 0; $temp < 9; $temp++)
	{
		$datensatz[$temp] = str_replace('""', '\\', $datensatz[$temp]);
		$datensatz[$temp] = trim($datensatz[$temp]);
		$datensatz[$temp] = trim($datensatz[$temp], '"');
		$datensatz[$temp] = str_replace('\\', '"', $datensatz[$temp]);
	}
	$sql = "INSERT INTO " . PREFIX . "_Kontakte SET \n";
	if ("" == $_POST['ID'])
	{
		$sql .= "	ID		= NULL,\n";
	}
	else
	{
		$sql .= "	ID		= \"" . $datensatz[$_POST['ID']] . "\",";
	}
	$sql .= "	Firma		= \"" . $datensatz[$_POST['Firma']] . "\",
		Anrede 		= \"" . $datensatz[$_POST['Anrede']] . "\",
		Vorname 	= \"" . $datensatz[$_POST['Vorname']] . "\",
		Nachname	= \"" . $datensatz[$_POST['Nachname']] . "\",
		Strasse		= \"" . $datensatz[$_POST['Strasse']] . "\",
		Nummer		= \"" . $datensatz[$_POST['Nummer']] . "\",
		PLZ			= \"" . $datensatz[$_POST['PLZ']] . "\",
		Ort			= \"" . $datensatz[$_POST['Ort']] . "\",
		Land		= \"" . $datensatz[$_POST['Land']] . "\",
		Telefon		= \"" . $datensatz[$_POST['Telefon']] . "\",
		Fax			= \"" . $datensatz[$_POST['Fax']] . "\",
		Mobil		= \"" . $datensatz[$_POST['Mobil']] . "\",
		Email		= \"" . $datensatz[$_POST['Email']] . "\",
		Homepage	= \"" . $datensatz[$_POST['Homepage']] . "\",
		Gericht		= \"" . $datensatz[$_POST['Gericht']] . "\",
		SteuerNr	= \"" . $datensatz[$_POST['SteuerNr']] . "\",
		Kto_Inhaber	= \"" . $datensatz[$_POST['Kto_Inhaber']] . "\",
		Kto_Nummer	= \"" . $datensatz[$_POST['Kto_Nummer']] . "\",
		BLZ			= \"" . $datensatz[$_POST['BLZ']] . "\",
		Bank		= \"" . $datensatz[$_POST['Bank']] . "\",
		" . $Datum . ";";
	$stmt = $db->prepare($sql);
	if (!$stmt)
	{
		echo "<p>Datensatz konnte nicht angelegt werden: " . $db->error;
		$error++;
	}
	else
	{
		if (!$stmt->execute())
		{
			echo "<p>Datensatz konnte nicht angelegt werden: " . $stmt->error;
			$error++;
		}
		else
		{
			$gut++;
		}
	}
}
echo "<h2>" . $error . " fehlerhafte Datensätze wurden nicht eingefügt!<br />" . $gut . " Datensätze importiert</p>";
unlink($uploadfile);