<?php
$uploadfile = "tmp/" . $_SESSION['TID'] . "_" . PREFIX . "_cont.csv";
if ("1" != $_POST['step'])
{
	move_uploaded_file($_FILES['Datei']['tmp_name'], $uploadfile);
}
$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_datensaetze = count($array) - 1;

if ((!isset($_POST['prev'])) AND (!isset($_POST['next'])))
{
	if (isset($_POST['Datensatz']))
	{
		$i = $_POST['Datensatz'];
	}
	else
	{
		$i = 0;
	}
}
else
{
	if (isset($_POST['prev']))
	{
		$i = $_POST['prev'];
	}
	if (isset($_POST['next']))
	{
		$i = $_POST['next'];
	}
}

if ((";" == $_POST['separator']) OR (!isset($_POST['separator'])))
{
	$datensatz = explode(";", $array[$i]);
}
elseif ("," == $_POST['separator'])
{
	$datensatz = explode(",", $array[$i]);
}
elseif ("Tabulator" == $_POST['separator'])
{
	$datensatz = explode("\t", $array[$i]);
}

$number_cols = count($datensatz);
if ($number_cols < 21)
{
	for ($temp = $number_cols; $temp <= 21; $temp++)
	{
		$datensatz[$temp] = "";
	}
}
$number_cols = count($datensatz);

for ($temp = 0; $temp < 21; $temp++)
{
	$datensatz[$temp] = str_replace('""', '\\', $datensatz[$temp]);
	$datensatz[$temp] = trim($datensatz[$temp]);
	$datensatz[$temp] = trim($datensatz[$temp], '"');
	$datensatz[$temp] = str_replace('\\', '"', $datensatz[$temp]);
}

echo "<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\" >\n";
echo "	<input type=\"hidden\" name=\"step\" value=\"1\" />\n";
echo "	<input type=\"hidden\" name=\"Typ\" value=\"" . $_POST['Typ'] . "\" />\n";
echo "	<input type=\"hidden\" name=\"Datensatz\" value=\"" . $i . "\" />\n";
echo "	<input type=\"hidden\" name=\"formaction\" value=\"upload\" />\n";
--$i;
echo "	<table style=\"border: none; width: 300px; margin: auto; text-align: center;\">\n";
echo "		<colgroup>\n";
echo "			<col width=\"48px;\" />\n";
echo "			<col width=\"200px;\" />\n";
echo "			<col width=\"48px;\" />\n";
echo "		</colgroup>\n";
echo "		<tr>\n";
echo "			<td style=\"border:none;\">\n";
if (0 <= (int)$i)
{
	echo "				<input type=\"submit\" class=\"button_small_prev\" name=\"prev\" value=\"" . $i . "\" title=\"vorigen Datensatz anzeigen\" />\n";
}
echo "			</td>\n";
++$i;
echo "			<td style=\"border:none; font-size: 1.5em; color: #000000; font-weight: bold;\">" . ($i + 1) . " (" . $number_datensaetze . ")</td>\n";
++$i;
echo "			<td style=\"border:none;\">\n";
if (isset($array[($i + 1)]))
{
	echo "				<input type=\"submit\" class=\"button_small_next\" name=\"next\" value=\"" . $i . "\" title=\"nächsten Datensatz anzeigen\" />\n";
}
echo "			</td>\n";
echo "		</tr>\n";
echo "	</table>\n";
echo "	<br />\n";
echo "<div id=\"imp_left\">\n";
echo "	<fieldset>\n";
echo "		<legend>Zuordnung</legend>\n";
echo "		<h2>" . $i . ". Datensatz der Datei</h2>\n";
echo "		<label class=\"data\">Trennzeichen</label>\n";
echo "		<select name=\"separator\">\n";
echo "			<option value=\";\"";
if ((";" == $_POST['separator']) OR (!isset($_POST['separator'])))
{
	echo " selected=\"selected\"";
}
echo ">Semicolon</option>\n";
echo "			<option value=\",\"";
if ("," == $_POST['separator'])
{
	echo " selected=\"selected\"";
}
echo ">Komma</option>\n";
echo "			<option value=\"Tabulator\"";
if ("Tabulator" == $_POST['separator'])
{
	echo " selected=\"selected\"";
}
echo ">Tabulator</option>\n";
echo "		</select>\n";
echo "		<input type=\"submit\" style=\"float:right;\" class=\"button_small_ok\" name=\"vorschau\" value=\"vorschau\" title=\"Trennzeichen festlegen\" />\n";
echo "		<br />&nbsp;<br />\n";
for ($temp = 0; $temp < $number_cols; $temp++)
{
	echo "		<label class=\"data\">" . nl2br($datensatz[$temp]) . "</label>\n";
	echo "		<select name=\"Daten[" . $temp . "]\">\n";
	echo "			<option value=\"\"> </option>\n";
	echo "			<option value=\"ID\" ";
	if ("ID" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Kundennummer</option>\n";
	echo "			<option value=\"Firma\" ";
	if ("Firma" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Firma</option>\n";
	echo "			<option value=\"Anrede\" ";
	if ("Anrede" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Anrede</option>\n";
	echo "			<option value=\"Vorname\" ";
	if ("Vorname" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Vorname</option>\n";
	echo "			<option value=\"Nachname\" ";
	if ("Nachname" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Nachname</option>\n";
	echo "			<option value=\"Strasse\" ";
	if ("Strasse" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Strasse</option>\n";
	echo "			<option value=\"Nummer\" ";
	if ("Nummer" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Hausnummer</option>\n";
	echo "			<option value=\"PLZ\" ";
	if ("PLZ" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">PLZ</option>\n";
	echo "			<option value=\"Ort\" ";
	if ("Ort" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Ort</option>\n";
	echo "			<option value=\"Land\" ";
	if ("Land" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Land</option>\n";
	echo "			<option value=\"Telefon\" ";
	if ("Telefon" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Telefon</option>\n";
	echo "			<option value=\"Fax\" ";
	if ("Fax" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Fax</option>\n";
	echo "			<option value=\"Mobil\" ";
	if ("Mobil" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Fax</option>\n";
	echo "			<option value=\"Email\" ";
	if ("Email" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Email</option>\n";
	echo "			<option value=\"Homepage\" ";
	if ("Homepage" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Homepage</option>\n";
	echo "			<option value=\"Gericht\" ";
	if ("Gericht" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Gericht</option>\n";
	echo "			<option value=\"SteuerNr\" ";
	if ("SteuerNr" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Steuer-Nummer</option>\n";
	echo "			<option value=\"Kto_Inhaber\" ";
	if ("Kto_Inhaber" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Konto Inhaber</option>\n";
	echo "			<option value=\"Kto_Nummer\" ";
	if ("IBAN" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">IBAN</option>\n";
	echo "			<option value=\"BLZ\" ";
	if ("BIC" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">BIC</option>\n";
	echo "			<option value=\"Bank\" ";
	if ("Bank" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Bank</option>\n";
	echo "			<option value=\"Datum\" ";
	if ("Datum" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Kunde seit</option>\n";
	echo "		</select>\n";
	echo "		<br />\n";
}
echo "		<br />\n";
echo "		<hr />\n";
echo "		<br />\n";
echo "		<input type=\"submit\" class=\"button_view\" name=\"vorschau\" value=\"vorschau\" title=\"Vorschau anzeigen\" />\n";
echo "		<br />\n";
echo "	</fieldset>\n";
echo "</form>\n";
echo "</div>\n";

foreach ($_POST['Daten'] AS $key => $value)
{
	$zuordnung[$value] = $key;
}


echo "<div id=\"imp_right\">\n";
echo "	<fieldset>\n";
echo "		<legend>Vorschau</legend>\n";
echo "		<h2>Kundendaten</h2>\n";
echo "		<label>Kundennummer</label>\n";
echo "		<label class=\"data\">" . $datensatz[$zuordnung['ID']] . "</label>\n";
if ("0" == $zuordnung['ID'])
{
	echo "		<img src=\"img/small_info.png\" alt=\"Ein Datensatz wird nicht angelegt, falls ein Kunde mit der gleichen Kundennummer vorhanden ist!\" title=\"Ein Datensatz wird nicht angelegt, falls ein Kunde mit der gleichen Kundennummer vorhanden ist!\" />\n";
}
else
{
	echo "		<img src=\"img/small_info.png\" alt=\"Die Kundennummern werden automatisch erstellt!\" title=\"Die Kundennummern werden automatisch erstellt!\" />\n";
}
echo "		<br />\n";
echo "		<label>Firma</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Firma']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Anrede</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Anrede']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Vorname</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Vorname']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Nachname</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Nachname']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Strasse</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Strasse']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Hausnummer</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Nummer']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>PLZ</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['PLZ']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Ort</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Ort']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Land</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Land']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Telefon</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Telefon']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Fax</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Fax']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Mobil</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Mobil']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Email</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Email']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Homepage</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Homepage']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Registergericht</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Gericht']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>USt-ID / SteuerNr.</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['SteuerNr']]) . "</label>\n";
echo "		<br />\n";
echo "		<h2>Bankdaten</h2>\n";
echo "		<label>Inhaber</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Kto_Inhaber']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Konto-Nr</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Kto_Nummer']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>BLZ</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['BLZ']]) . "</label>\n";
echo "		<br />\n";
echo "		<label>Bank</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Bank']]) . "</label>\n";
echo "		<br />\n";
echo "		<h2>Kunde seit</h2>\n";
echo "		<label>Datum</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Datum']]) . "</label>\n";
echo "		<br />\n";
echo "		<hr />\n";
echo "		<br />\n";
echo "		<form action=\"index.php?" . $_SERVER['QUERY_STRING'] . "\" method=\"post\" />\n";
echo "			<input type=\"hidden\" name=\"separator\" value=\"" . $_POST['separator'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"ID\" value=\"" . $zuordnung['ID'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Firma\" value=\"" . $zuordnung['Firma'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Anrede\" value=\"" . $zuordnung['Anrede'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Vorname\" value=\"" . $zuordnung['Vorname'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Nachname\" value=\"" . $zuordnung['Nachname'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Strasse\" value=\"" . $zuordnung['Strasse'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Nummer\" value=\"" . $zuordnung['Nummer'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"PLZ\" value=\"" . $zuordnung['PLZ'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Ort\" value=\"" . $zuordnung['Ort'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Land\" value=\"" . $zuordnung['Land'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Telefon\" value=\"" . $zuordnung['Telefon'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Fax\" value=\"" . $zuordnung['Fax'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Mobil\" value=\"" . $zuordnung['Mobil'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Email\" value=\"" . $zuordnung['Email'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Homepage\" value=\"" . $zuordnung['Homepage'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Gericht\" value=\"" . $zuordnung['Gericht'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"SteuerNr\" value=\"" . $zuordnung['SteuerNr'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Kto_Inhaber\" value=\"" . $zuordnung['Kto_Inhaber'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Kto_Nummer\" value=\"" . $zuordnung['Kto_Nummer'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"BLZ\" value=\"" . $zuordnung['BLZ'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Bank\" value=\"" . $zuordnung['Bank'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Datum\" value=\"" . $zuordnung['Datum'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Beginn\" value=\"0\" />\n";
echo "			<label style=\"width: 400px;\" >Erster Datensatz enthält Feldnamen und soll ignoriert werden:</label>\n";
echo "			<input type=\"checkbox\" name=\"Beginn\" value=\"1\" />\n";
echo "			<br /><br />\n";
echo "			<input type=\"submit\" class=\"button_save\" name=\"formaction\" value=\"cont_save\" title=\"Kunden importieren\" />\n";
echo "		</form>\n";
echo "	</fieldset>\n";
echo "</div>\n";