<?php
	function fetch($structure, $field, $fallback = null, $escalation = 1)
	{
		if (array_key_exists($field, $structure))
		{
			return $structure[$field];
		}
		else
		{
			switch ($escalation)
			{
				case 0:
				{
					return $fallback;
					// break;
				}
				case 1:
				{
					return $fallback;
					// break;
				}
				case 2:
				{
					throw (sprintf("field '%s' missing in structure!", $field));
					return $fallback;
					// break;
				}
				default:
				{
					throw (sprintf("unhandled escalation level %u", $escalation));
					// break;
				}
			}
		}
	}
	
	function compose($names)
	{
		array_map
		(
			function ($name)
			{
				include("source/content/" . $name . ".html.php");
			},
			$names
		);
	}
	
	function mark($core, $classes)
	{
		return ('<span class="' . implode(" ", $classes) . '">' . $core . '</span>');
	}
	
	function format($string, $classes = ["word", "lang_fs"])
	{
		if ($string == NULL)
		{
			$string = "--";
		}
		else
		{
			$string = preg_replace("/\/([^\/]*)\//", mark("$1", ["ipa"]), $string);
			$string = preg_replace("/_([^_]*)_/", "<u>$1</u>", $string);
			$string = preg_replace("/\'([^\']*)\'/", mark("$1", $classes), $string);
		}
		return $string;
	}
	
	function _foo($fieldname)
	{
		return (
			function ($row) use (&$fieldname)
			{
				global $configuration;
				return ["original" => $row[$fieldname]["fs"], "translated" => $row[$fieldname][$configuration["target"]]];
			}
		);
	}
	
	function _baz($words, $type, $language)
	{
		return ((count($words) == 0) ? "?" : implode(", ", array_map(function ($word) use (&$type,&$language) {return mark($word, [$type, "lang_" . $language]);}, $words)));
	}
	
	function _bar($type = "word")
	{
		return (
			function ($value) use (&$type)
			{
				global $configuration;
				$output = "";
				$output .= _baz($value["original"], $type, "fs");
				$output .= " ~ ";
				$output .= _baz($value["translated"], $type, $configuration["target"]);
				return $output;
			}
		);
	}
	
	function proposal()
	{
 ?>
		<div class="note note_error">
			<span class="note_content">
				<p>This section is only a proposal yet; not part of the draft.</p>
			</span>
		</div>
<?php
	}
 ?>