<?php
	include("ini.inc.php");
	include("namen.inc.php");
	require_once $_SERVER['DOCUMENT_ROOT'] . '/db_connection.php';
	$arr_iid = (isset($_POST["arr_iid"])) ? $_POST ["arr_iid"] : "";
	$arr_zeilen=array();
	$arr_sorter=array();
	foreach($arr_iid as $i) {
		if(($i > 200000) AND ($i < 300000)) {
			$sql_select = "SELECT * FROM palaestralatina WHERE IID = " .  $i ;
			$sql_result = $db->query($sql_select);
			while ($row = $sql_result->fetch_object()) {
				$ausgabe = "";
				$bis="";
				$iid = $row->IID;
				$autor = $row->Autor;
				$autor = erster_name($autor);
				$titel = $row->Titel;
				$PDFseite = $row->PDFseite;
				$epistula = " – Epistula";
				if ($titel != "" && $autor != "")  { $titel = " &ndash; " . $titel; }
				if ($autor == "") { $epistula = "Epistula"; }
				$p = " p. ". $row->Seite;
				if ($row->Bis  != "") { $bis =  "-" . $row->Bis; $p = " pp. ". $row->Seite; }
				$fasc = $row->Faszikel;
				if (substr($fasc,-2) == ".0") {$fasc = substr($fasc, 0, -2);}
				// Fundort
				$bandteil = $row->Bandteil;
				$bdbt = "";
				$fascc = $fasc;
				if($fasc == 33) {$fascc = "33/4";$bandteil = "6-7";}
				if($fasc == 63) {$fascc = "63/4";$bandteil = "";}
				if($fasc == 77) {$fascc = "77/8";$bandteil = "1-2";}
				if($fasc == 175) {$fascc = "175/6";$bandteil = "3-4";}
				$bdbt = "$row->Monat $row->Jahr; a. " . $row->Band . ", fasc. " . $bandteil;
				if ($row->Bandteil == "0") {$bdbt = $row->Monat . " " . $row->Jahr . "; a. " . $row->Band;}
				if ($row->Band == "0") {$bdbt = $row->Monat . " " . $row->Jahr;}
				$In = " &ndash; <u>In</u>: Palaestra Latina, n. " . $fascc . " (" . $bdbt . "), " . $p . $bis;
				//
				if($row->Textgattung == "Epistula") {
					$ort = $row->Ort;
					$datum = $row->Briefdatum;
					$briefempfaenger = $row->Briefempfaenger;
					if ($ort != "") { $ort = ", " . erster_name($ort); } else { $ort=erster_name($ort); }
					if ($datum != "")  { $datum = ", " . $datum; }
					if ($briefempfaenger != "") { $briefempfaenger = " → " . erster_name($briefempfaenger); }
					$ausgabe = $autor . $epistula . $ort . $datum . $briefempfaenger . $In;
				} else {
					$ausgabe = $autor . $titel . $In;
				}
			// Ausgabe-String in erstes Wort und alles Folgende zerlegen
				$ew = substr($ausgabe, 0,  strpos($ausgabe, " "));
				$af = substr($ausgabe, strpos($ausgabe, " "));
			// erstes Wort groß und sperren
				$ew_gs = mb_strtoupper($ew, "UTF-8");
				$ew_gs = "<span style='letter-spacing:1px;'>$ew_gs</span>";
			// neuer Ausgabe-String in Zeilen-Array
					$arr_sorter[] = $ew_gs . $af;
					$fascc = $fasc;
					if($fascc == "53.1"){$fascc = "53a";}
					if($fascc == "53.2"){$fascc = "53b";}
					if($fascc == "54.1"){$fascc = "54a";}
					if($fascc == "54.2"){$fascc = "54b";}
					$pal_urll = $pal_url;
					if($fasc == 175) {$fascc = "175-6";}
					if(($fasc >= 53) AND ($fasc <= 157) OR ($fasc == 200) OR ($fasc == 222) OR ($fasc == 223)) {
						$pal_urll = rtrim($pal_url, "-"); 
						$pal_urll = $pal_urll . "_";
					}
					if(($fasc >= 1) AND ($fasc <= 9)) {
						$fascc = "0".$fasc;
					}
					if($fasc == "33"){$fascc = "33 et 34";}
					if($fasc == "63") {$fascc = "63_64";}
					if($fasc == "77"){$fascc = "77_78";}
					if($PDFseite > 0) {
						$ausgabe = "<a target='_blank' href='" . $pal_urll. $fascc . ".pdf#page=" . $PDFseite ."'>" . $ew_gs . $af . "</a>" ."@" . $iid . "|";
					}
					else {
						$ausgabe = $ew_gs . $af ."@" . $iid . "|";
					}
				$arr_zeilen[] = $ausgabe;
			}
		}
	}
	$db->close();
	array_multisort($arr_sorter, $arr_zeilen);
	echo json_encode($arr_zeilen);
?>
