<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
ob_start();
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
    die();
}
require_once("../../backend/functions.php");
require_once("../../db/connection.php");
session_write_close();
$id_map = (int)$_POST['id_map'];
$language = strip_tags($_POST['language']);
$settings = get_settings();
$categories_assoc = array();
$query = "SELECT DISTINCT id_geodata,id_category FROM sml_geodata_categories_assoc WHERE id_geodata IN (SELECT id FROM sml_geodata WHERE id_map=$id_map);";
$result = $mysqli->query($query);
if($result) {
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $categories_assoc[$row['id_geodata']].=$row['id_category'].",";
        }
    }
}
$array_geodata_lang = array();
$query = "SELECT languages_enabled,language as language_map FROM sml_maps WHERE id=$id_map LIMIT 1;";
$result = $mysqli->query($query);
if($result) {
    if ($result->num_rows == 1) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $count_languages_enabled = 0;
        if(empty($row['languages_enabled'])) {
            $row['languages_enabled']=array();
        } else {
            $row['languages_enabled']=json_decode($row['languages_enabled'],true);
        }
        foreach ($row['languages_enabled'] as $lang_enabled) {
            if($lang_enabled==1) {
                $count_languages_enabled++;
            }
        }
        $map_language = $row['language_map'];
        $default_language_settings = $settings['language'];
        if(empty($map_language)) $map_language=$default_language_settings;
        if($count_languages_enabled>=1) {
            if(!empty($language) && $language!=$map_language) {
                $query_lang = "SELECT * FROM sml_geodata_lang WHERE language='$language' AND id_geodata IN (SELECT id FROM sml_geodata WHERE id_map=$id_map);";
                $result_lang = $mysqli->query($query_lang);
                if($result_lang) {
                    if ($result_lang->num_rows > 0) {
                        while($row_lang = $result_lang->fetch_array(MYSQLI_ASSOC)) {
                            $id_geodata = $row_lang['id_geodata'];
                            unset($row_lang['id_geodata']);
                            unset($row_lang['language']);
                            $array_geodata_lang[$id_geodata]=$row_lang;
                        }
                    }
                }
            }
        }
    }
}
$array_geodata = array();
$query = "SELECT * FROM sml_geodata WHERE id_map=?";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('i',$id_map);
    $result = $smt->execute();
    if($result) {
        $result = get_result($smt);
        $count = count($result);
        if($count>0) {
            while($row = array_shift($result)) {
                $id_geodata = $row['id'];
                if(array_key_exists($id_geodata,$array_geodata_lang)) {
                    if (!empty($row['content']) && $row['content']!='<p><br></p>' && !empty($array_geodata_lang[$id_geodata]['content']) && $array_geodata_lang[$id_geodata]['content']!='<p><br></p>') {
                        $row['content'] = $array_geodata_lang[$id_geodata]['content'];
                    }
                }
                if (strpos($row['file'], 'http') !== 0) {
                    $row['file']='../viewer/geojson/'.$row['file'];
                }
                $row['id_categories'] = '';
                if(array_key_exists($row['id'],$categories_assoc)) {
                    $row['id_categories']=rtrim($categories_assoc[$row['id']],",");
                }
                $array_geodata[] = $row;
            }
        }
    }
}
ob_end_clean();
echo json_encode(array("geodata"=>$array_geodata));