<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ($_SERVER['REMOTE_ADDR']!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['sml_si']!=session_id())) {
    //DEMO CHECK
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$id_user = $_SESSION['id_user'];
$id_map = $_SESSION['id_map_sel'];
session_write_close();
$settings = get_settings();
$where = "";
switch(get_user_role($id_user)) {
    case 'customer':
        $where = " AND id_user=$id_user ";
        break;
    case 'editor':
        $where = " AND id IN () ";
        $query = "SELECT GROUP_CONCAT(id_map) as ids FROM sml_assign_maps WHERE id_user=$id_user;";
        $result = $mysqli->query($query);
        if($result) {
            if($result->num_rows==1) {
                $row=$result->fetch_array(MYSQLI_ASSOC);
                $ids = $row['ids'];
                $where = " AND id IN ($ids) ";
            }
        }
        break;
}
$code = "";
$query = "SELECT code FROM sml_maps WHERE id=$id_map $where LIMIT 1;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows==1) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $code = $row['code'];
    }
}
if(empty($code)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
    die();
}

$id = (int)$_POST['id'];
$table = strip_tags($_POST['table']);
$field = strip_tags($_POST['field']);
$value = $_POST['value'];
$lang = strip_tags($_POST['lang']);

switch($table) {
    case 'sml_maps_lang':
        $id_field = "id_map";
        break;
    case 'sml_categories_lang':
        $id_field = "id_category";
        break;
    case 'sml_markers_lang':
        $id_field = "id_marker";
        break;
    case 'sml_markers_connects_lang':
        $id_field = "id_marker_connect";
        break;
    case 'sml_story_lang':
        $id_field = "id_story";
        break;
    case 'sml_geodata_lang':
        $id_field = "id_geodata";
        break;
}

$query_c = "SELECT $id_field FROM $table WHERE $id_field=? AND language=? LIMIT 1;";
if($smt_c = $mysqli->prepare($query_c)) {
    $smt_c->bind_param('is',  $id,$lang);
    $result_c = $smt_c->execute();
    if ($result_c) {
        $result_c = get_result($smt_c);
        if (count($result_c) == 1) {
            $query = "UPDATE $table SET $field=? WHERE $id_field=? AND language=?;";
            if($smt = $mysqli->prepare($query)) {
                $smt->bind_param('sss', $value, $id,$lang);
            } else {
                ob_end_clean();
                echo json_encode(array("status"=>"error"));
            }
        } else {
            $query = "INSERT INTO $table($id_field,language,$field) VALUES(?,?,?);";
            if($smt = $mysqli->prepare($query)) {
                $smt->bind_param('iss', $id,$lang,$value);
            } else {
                ob_end_clean();
                echo json_encode(array("status"=>"error"));
            }
        }
        $result = $smt->execute();
        if($result) {
            ob_end_clean();
            echo json_encode(array("status"=>"ok"));
        } else {
            ob_end_clean();
            echo json_encode(array("status"=>"error"));
        }
    } else {
        ob_end_clean();
        echo json_encode(array("status"=>"error"));
    }
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error"));
}