<?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("../../db/connection.php");
require_once("../../backend/functions.php");
$id_map = (int)$_POST['id_map'];
$id_marker = (int)$_POST['id_marker'];
$auth_settings = sml_get_viewer_auth_settings($id_map);
$id_user = ((int)$auth_settings['viewer_auth_mode']>0) ? sml_get_viewer_user_id() : null;
session_write_close();
if(empty($id_user)) {
    ob_end_clean();
    echo json_encode(array("status"=>"auth_required"));
    exit;
}
$query = "SELECT * FROM sml_markers WHERE id=? AND id_map=? AND id_user=? AND to_validate=1 LIMIT 1;";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('iii',$id_marker,$id_map,$id_user);
    $smt->execute();
    $result = get_result($smt);
    if(count($result)==1) {
        $marker = array_shift($result);
        $marker['categories'] = array();
        $query_c = "SELECT id_category FROM sml_markers_categories_assoc WHERE id_marker=?;";
        if($smt_c = $mysqli->prepare($query_c)) {
            $smt_c->bind_param('i',$id_marker);
            $smt_c->execute();
            $result_c = get_result($smt_c);
            foreach($result_c as $row_c) {
                $marker['categories'][] = (int)$row_c['id_category'];
            }
        }
        $marker['total_images'] = 0;
        $query_i = "SELECT COUNT(id) as total_images FROM sml_images WHERE id_marker=?;";
        if($smt_i = $mysqli->prepare($query_i)) {
            $smt_i->bind_param('i',$id_marker);
            $smt_i->execute();
            $result_i = get_result($smt_i);
            if(count($result_i)==1) {
                $row_i = array_shift($result_i);
                $marker['total_images'] = (int)$row_i['total_images'];
            }
        }
        unset($marker['id_user']);
        ob_end_clean();
        echo json_encode(array("status"=>"ok","marker"=>$marker));
        exit;
    }
}
ob_end_clean();
echo json_encode(array("status"=>"error"));
