<?php
	function erster_name($nomina) {
		// Gibt den ersten Namen zurück.
		$nomen = trim($nomina);
		$pos = strpos($nomina, "=");
		if ($pos != false) {
			$nomen = substr($nomina, 0, $pos-1);
		}
		return  $nomen;
	}
	function vorname_nachname($nomen) {
		// Kehrt Reihenfolge "Nachname, Vorname" um.
		$nomen = trim($nomen);
		$pos = strpos($nomen, "=");
		if ($pos != false) {
			// nomen primum:
			$nomen_pp = substr($nomen, 0, $pos-1);
			// nomina sequenta:
			$nomen_ps = substr($nomen, $pos+2);
			$nomen = $nomen_pp;
		}
		$nomina = explode ("/", $nomen);
		$num = count($nomina);
		$novus = array();
		$numeratrum = 0;
		while ($numeratrum < $num) {
			$n=$nomina[$numeratrum];
			$pos = strpos($n, ",");
			if ($pos != false) {
				$cognomen = substr($n, 0, $pos);
				$praenomen = substr($n, $pos+2);
				$n = trim($praenomen) . " " . trim($cognomen);
			}
			$novus[] = $n;
			$numeratrum++;
		}
		$novus = implode(" / ", $novus);
		if (isset($nomen_ps)) {$novus = $novus . " = " . $nomen_ps;}
		return $novus;
	}
	function vorname_nachname_arr (&$value, $key) {
		// callback-Funktion, siehe: alle_namen();
		// kehrt Reihenfolge "Nachname, Vorname" um.
		$pos = strpos($value, ",");
		if ($pos != false) {
			$nachname = substr($value, 0, $pos);
			$vorname = substr($value, $pos+2);
			$value = $vorname . " " . $nachname;
		}
	}
	function alle_namen ($erster_name, $mysql_row) {
		// Kehrt die Reihenfolge "Nachname, Vorname" aller Namen um,
		// sortiert diese, fügt "=" zwischen den Namen ein und gibt alle Namen wieder zurück.
		if (strpos($mysql_row, "=")) {
			$alle_namen = $mysql_row;
			$alle_namen = explode(" = ", $alle_namen);
			array_walk($alle_namen,"vorname_nachname_arr");
			sort($alle_namen);
			$alle_namen = implode(" = ", $alle_namen);
			$r = $alle_namen;
		} else {
			$r = $erster_name;
		}
		return $r;
	}
	function namen_ausgeben ($name, $orig_name, $link, $nil) {
		// gebraucht von symbolae.php, epistulae.php, versiones.php
		$name = trim($name);
		$link = trim($link);
		if ($name != "") {
			$x = substr_count($name, "=");
			if ($orig_name != "") {
				if ($link != "") {$orig_name = " [*<a class='oname' target= '_blank' href=$link>" . $orig_name . "</a>&thinsp;&hellip;]";} else {$orig_name = " [*" . $orig_name . "]";}
			}
			if ($x == 0) {
				$res = $name . "<span style='color:gray;'>$orig_name</span>";
				$exit_arr = array("N. N.", "Anonymus", "Auctores varii");
				if (in_array($name, $exit_arr)) {
					$res = $name;
				}
			}
			if ($x == 1) {
				$beide_namen_arr = explode(" = ", $name);
				$res = $beide_namen_arr['0'] . "<span style='color:gray;'> $orig_name = " . $beide_namen_arr['1'] . "</span>";
			}
			if ($x >= 2) {
				$alle_namen_arr = explode(" = ", $name);
				$erster_name = array_shift($alle_namen_arr);
				sort($alle_namen_arr);
				$alle_namen = implode(" = ", $alle_namen_arr);
				$res = $erster_name . "<span style='color:gray;'> $orig_name = " . $alle_namen . "</span>";
			}
		} else {
			$res = $nil;
		}
		return $res;
	}
?>
