<?php
require_once 'Twig/autoload.php';
require_once 'config.php';
require_once 'functions.php';

use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader);

// --------------

if (!isset($_GET['id']))
	die(twig_error($twig, 400, "Keine ID angegeben."));

$id = (int)$_GET['id'];

$dbconn = pg_connect($dbconnstring)
	or die(twig_error($twig, 500, 'Datenbankfehler: ' . pg_last_error()));

$query = "SELECT data FROM tests WHERE id=" . $id;
$result = pg_query($dbconn, $query)
	or die(twig_error($twig, 'Datenbankfehler: ' . pg_last_error()));

$json = pg_fetch_result($result, 'data')
	or die(twig_error($twig, 404, 'Test nicht gefunden.'));

pg_close($dbconn);

echo $twig->render('test.html.twig', [ 'test' => $json ]);
?>

