<?php

require("functions.php");

function parseToXML($htmlStr) {
    $xmlStr = str_replace('<', '&lt;', $htmlStr);
    $xmlStr = str_replace('>', '&gt;', $xmlStr);
    $xmlStr = str_replace('"', '&quot;', $xmlStr);
    $xmlStr = str_replace("'", '&#39;', $xmlStr);
    $xmlStr = str_replace("&", '&amp;', $xmlStr);
    $xmlStr = str_replace("Á", '&#193;', $xmlStr);
    $xmlStr = str_replace("á", '&#225;', $xmlStr);
    $xmlStr = str_replace("À", '&#192;', $xmlStr);
    $xmlStr = str_replace("á", '&#224;', $xmlStr);
    $xmlStr = str_replace("Ì", '&#204;', $xmlStr);
    $xmlStr = str_replace("ì", '&#236;', $xmlStr);
    $xmlStr = str_replace("Ù", '&#217;', $xmlStr);
    $xmlStr = str_replace("ù", '&#249;', $xmlStr);
    $xmlStr = str_replace("É", '&#201;', $xmlStr);
    $xmlStr = str_replace("é", '&#233;', $xmlStr);
    $xmlStr = str_replace("È", '&#200;', $xmlStr);
    $xmlStr = str_replace("è", '&#232;', $xmlStr);
    $xmlStr = str_replace("Ó", '&#211;', $xmlStr);
    $xmlStr = str_replace("ó", '&#243;', $xmlStr);
    $xmlStr = str_replace("Ò", '&#210;', $xmlStr);
    $xmlStr = str_replace("ò", '&#242;', $xmlStr);

    return $xmlStr;
}

// Query for Fragerunde 5, Frage 6: pantofole
$query = "SELECT DISTINCT(ort.plz) AS plz, 
    ort.ortsname AS ortsname, 
    ort.lat AS lat, 
    ort.lng AS lng, 
    antwort.antwort_informant AS response, 
    informant.geschlecht AS geschlecht, 
    informant.informant_alter AS inf_alter
    FROM antwort
    INNER JOIN informant
    ON antwort.informantid = informant.informantid
    INNER JOIN ort
    ON informant.ortid = ort.ortid
    WHERE antwort.fragerunde = 5
    AND antwort.fragenr = 6
    AND antwort.antwort_informant <> ''
    ORDER BY antwort.antwort_informant;";


$result = mysqli_query($db_server, $query);
if (!$result) {
    die('Invalid query: ' . mysqli_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)) {
    $response = parseToXML($row['response']);
    // Add to the XML Document Node
    echo '<marker ';
    echo 'loc="' . parseToXML($row['ortsname']) . '" ';
    echo 'ortsname="' . parseToXML($row['ortsname']) . '" ';
    echo 'response="' . $response . '" ';
    echo 'lat="' . $row['lat'] . '" ';
    echo 'lng="' . $row['lng'] . '" ';
    echo 'sex="' . parseToXML($row['geschlecht']) . '" ';
    echo 'age="' . $row['inf_alter'] . '" ';
    if (preg_match("/^pan/i", $response)) {
        echo 'type="pantofole" ';
    } elseif (preg_match("/^cia/i", $response)) {
        echo 'type="ciabatte" ';
    } elseif (preg_match("/^ba/i", $response)) {
        echo 'type="babbucce" ';
    } elseif (preg_match("/^pat/i", $response)) {
        echo 'type="pattine" ';
    } elseif (preg_match("/^[sz]av/i", $response)) {
        echo 'type="savate" ';
    } else {
        echo 'type="altro" ';
    }
    echo 'checked="true" ';
    echo '/>';
}

// End of XML file
echo '</markers>';
?>
