<?php
$uploadfile = "tmp/" . $_SESSION['TID'] . "_" . PREFIX . "_art.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 < 9)
{
	for ($temp = $number_cols; $temp < 9; $temp++)
	{
		$datensatz[$temp] = "";
	}
}
$number_cols = count($datensatz);

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]);
}

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 ">Artikelnummer</option>\n";
	echo "			<option value=\"Bezeichnung\" ";
	if ("Bezeichnung" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Bezeichnung</option>\n";
	if ("" != $_SESSION['Firma']['Feld1_Name'])
	{
		echo "			<option value=\"Feld1\" ";
		if ("Feld1" == $_POST['Daten'][$temp])
		{
			echo "selected=\"selected\"";
		}
		echo ">" . $_SESSION['Firma']['Feld1_Name'] . "</option>\n";
	}
	if ("" != $_SESSION['Firma']['Feld2_Name'])
	{
		echo "			<option value=\"Feld2\" ";
		if ("Feld2" == $_POST['Daten'][$temp])
		{
			echo "selected=\"selected\"";
		}
		echo ">" . $_SESSION['Firma']['Feld2_Name'] . "</option>\n";
	}
	if ("" != $_SESSION['Firma']['Feld3_Name'])
	{
		echo "			<option value=\"Feld3\" ";
		if ("Feld3" == $_POST['Daten'][$temp])
		{
			echo "selected=\"selected\"";
		}
		echo ">" . $_SESSION['Firma']['Feld3_Name'] . "</option>\n";
	}
	if ("" != $_SESSION['Firma']['Feld4_Name'])
	{
		echo "			<option value=\"Feld4\" ";
		if ("Feld4" == $_POST['Daten'][$temp])
		{
			echo "selected=\"selected\"";
		}
		echo ">" . $_SESSION['Firma']['Feld4_Name'] . "</option>\n";
	}
	if ("" != $_SESSION['Firma']['Feld5_Name'])
	{
		echo "			<option value=\"Feld5\" ";
		if ("Feld5" == $_POST['Daten'][$temp])
		{
			echo "selected=\"selected\"";
		}
		echo ">" . $_SESSION['Firma']['Feld5_Name'] . "</option>\n";
	}
	echo "			<option value=\"Preis\" ";
	if ("Preis" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">Preis</option>\n";
	echo "			<option value=\"Datum\" ";
	if ("Datum" == $_POST['Daten'][$temp])
	{
		echo "selected=\"selected\"";
	}
	echo ">In die Datenbank aufgenommen:</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>Artikeldaten</h2>\n";
echo "		<label>Artikelnummer</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['ID']]) . "</label>\n";
if ("0" == $zuordnung['ID'])
{
	echo "		<img src=\"img/small_info.png\" alt=\"Ein Datensatz wird nicht angelegt, falls ein Artikel mit der gleichen Artikelnummer vorhanden ist!\" title=\"Ein Datensatz wird nicht angelegt, falls ein Artikel mit der gleichen Artikelnummer vorhanden ist!\" />\n";
}
else
{
	echo "		<img src=\"img/small_info.png\" alt=\"Die Artikelnummern werden automatisch erstellt!\" title=\"Die Artikelnummern werden automatisch erstellt!\" />\n";
}
echo "		<br />\n";
echo "		<label>Bezeichnung</label>\n";
echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Bezeichnung']]) . "</label>\n";
echo "		<br />\n";
if ("" != $_SESSION['Firma']['Feld1_Name'])
{
	echo "		<label>" . $_SESSION['Firma']['Feld1_Name'] . "</label>\n";
	echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Feld1']]) . "</label>\n";
	echo "		<br />\n";
}
if ("" != $_SESSION['Firma']['Feld2_Name'])
{
	echo "		<label>" . $_SESSION['Firma']['Feld2_Name'] . "</label>\n";
	echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Feld2']]) . "</label>\n";
	echo "		<br />\n";
}
if ("" != $_SESSION['Firma']['Feld3_Name'])
{
	echo "		<label>" . $_SESSION['Firma']['Feld3_Name'] . "</label>\n";
	echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Feld3']]) . "</label>\n";
	echo "		<br />\n";
}
if ("" != $_SESSION['Firma']['Feld4_Name'])
{
	echo "		<label>" . $_SESSION['Firma']['Feld4_Name'] . "</label>\n";
	echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Feld4']]) . "</label>\n";
	echo "		<br />\n";
}
if ("" != $_SESSION['Firma']['Feld5_Name'])
{
	echo "		<label>" . $_SESSION['Firma']['Feld5_Name'] . "</label>\n";
	echo "		<label class=\"data\">" . nl2br($datensatz[$zuordnung['Feld5']]) . "</label>\n";
	echo "		<br />\n";
}
echo "		<label>Preis</label>\n";
echo "		<label class=\"data\">" . $datensatz[$zuordnung['Preis']] . "</label>\n";
echo "		<br />\n";
echo "		<label>in der Datenbank seit</label>\n";
echo "		<label class=\"data\">" . $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=\"Bezeichnung\" value=\"" . $zuordnung['Bezeichnung'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Feld1\" value=\"" . $zuordnung['Feld1'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Feld2\" value=\"" . $zuordnung['Feld2'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Feld3\" value=\"" . $zuordnung['Feld3'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Feld4\" value=\"" . $zuordnung['Feld4'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Feld5\" value=\"" . $zuordnung['Feld5'] . "\" />\n";
echo "			<input type=\"hidden\" name=\"Preis\" value=\"" . $zuordnung['Preis'] . "\" />\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=\"art_save\" title=\"Artikel importieren\" />\n";
echo "		</form>\n";
echo "	</fieldset>\n";
echo "</div>\n";