<?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 2, Frage 8: 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 = 8 and antwort.fragerunde = 2 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;";

// (antwort_informant REGEXP " . $query_string_1 . " or antwort_informant REGEXP " . $query_string_2 . " or antwort_informant REGEXP " . $query_string_3 . " or antwort_informant REGEXP " . $query_string_4 . ") and


$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("/^pise/i", $response)) {
        echo 'type="pisellino" ';
    } elseif (preg_match("/^pis[tp]/i", $response)) {
        echo 'type="pistolino" ';
    } elseif (preg_match("/^piscia/i", $response)) {
        echo 'type="pisciarella" ';
    } elseif (preg_match("/^p[ie]s/i", $response)) {
        echo 'type="pesciolino" ';
    } elseif (preg_match("/^[uo]/i", $response)) {
        echo 'type="uccellino" ';
    } elseif (preg_match("/^a/i", $response)) {
        echo 'type="acciduzzo" ';
    } elseif (preg_match("/^pip.n/i", $response)) {
        echo 'type="pipino" ';
    } elseif (preg_match("/^m/i", $response)) {
        echo 'type="minchiella" ';
    } elseif (preg_match("/^pici/i", $response)) {
        echo 'type = "piciu" ';
    } elseif (preg_match("/^pic/i", $response)) {
        echo 'type="picarella" ';
    } elseif (preg_match("/^ci/i", $response)) {
        echo 'type = "ciccio" ';
    } elseif (preg_match("/^p[^e]/i", $response)) {
        echo 'type="pipi" ';
    } else {
        echo 'type="altro" ';
    }
    echo 'checked="false" ';
    echo '/>';
}

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