<?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 1, Frage 9: immondizia
$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 sex from antwort, informant, ort where fragenr = 1 and antwort.fragerunde = 1 and antwort.antwort_informant <> '' and antwort.informantid = informant.informantid and informant.ortid = ort.ortid and (informant.geschlecht like '%' and informant.informant_alter > 0) 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['sex']) . '" ';
    if (preg_match("/^mar/i", $response)) {
        echo 'type="marinare" ';
    } elseif (preg_match("/^fare? se/i", $response)) {
        echo 'type="far_sega" ';
    } elseif (preg_match("/^far l/i", $response)) {
        echo 'type="far_lippe" ';
    } elseif (preg_match("/^far fil/i", $response)) {
        echo 'type="far_filone" ';
    } elseif (preg_match("/^fare? cu/i", $response)) {
        echo 'type="far_cuppo" ';
    } elseif (preg_match("/^fare? fug/i", $response)) {
        echo 'type="far_fughino" ';
    } elseif (preg_match("/^fug/i", $response)) {
	echo 'type="far_fughino" ';
    } elseif (preg_match("/^far be/i", $response)) {
        echo 'type="far_berna" ';
    } elseif (preg_match("/^far v/i", $response)) {
        echo 'type="far_vela" ';
    } elseif (preg_match("/^far m/i", $response)) {
        echo 'type="far_manca" ';
    } elseif (preg_match("/^far sali/i", $response)) {
        echo 'type="far_salina" ';
    } elseif (preg_match("/^far for/i", $response)) {
        echo 'type="far_forca" ';
    } elseif (preg_match("/^tag/i", $response)) {
        echo 'type="tagliare" ';
    } elseif (preg_match("/^bru/i", $response)) {
        echo 'type="bruciare" ';
    } elseif (preg_match("/^cal/i", $response)) {
        echo 'type="caliarsela" ';
    } elseif (preg_match("/^buc/i", $response)) {
        echo 'type="bucare" ';
    } elseif (preg_match("/^big/i", $response)) {
        echo 'type="bigiare" ';
    } elseif (preg_match("/^but/i", $response)) {
        echo 'type="buttarsela" ';
    } elseif (preg_match("/^nar/i", $response)) {
	echo 'type="nargiare" ';
    } elseif (preg_match("/ferie/i", $response)) {
        echo 'type="ferie" ';
    } elseif (preg_match("/^bo/i", $response)) {
	echo 'type="bossare" ';
    } elseif (preg_match("/^imp/i", $response)) {
	echo 'type="impiccare" ';
    } elseif (preg_match("/scapo/i", $response)) {
	echo 'type="far_scapola" ';
    } else {
        echo 'type="altro" ';
    }
    echo 'checked="true" ';
    echo '/>';
}

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