<?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();
}
$id_map = $_SESSION['id_map_sel'];
require_once("../../db/connection.php");
require_once("../functions.php");
$id_marker = $_POST['id'];
$name = strip_tags($_POST['name']);
if(isset($_POST['id_categories'])) {
    $id_categories = $_POST['id_categories'];
} else {
    $id_categories = [];
}
$street = strip_tags($_POST['street']);
$city = strip_tags($_POST['city']);
$postal_code = strip_tags($_POST['postal_code']);
$country = strip_tags($_POST['country']);
$county = strip_tags($_POST['county']);
$state = strip_tags($_POST['state']);
$website = strip_tags($_POST['website']);
$website_caption = strip_tags($_POST['website_caption']);
$phone = strip_tags($_POST['phone']);
$whatsapp = strip_tags($_POST['whatsapp']);
$email = strip_tags($_POST['email']);
$hours = $_POST['h_desc'];
$description = $_POST['description'];
$lat = str_replace(",",".",$_POST['lat']);
$lon = str_replace(",",".",$_POST['lon']);
if(empty($lat)) $lat=NULL;
if(empty($lon)) $lon=NULL;
$array_images = json_decode($_POST['array_images'],true);
$active = (int)$_POST['active'];
$open_sheet = (int)$_POST['open_sheet'];
$view_popup = (int)$_POST['view_popup'];
$to_validate = (int)$_POST['to_validate'];
$centered = (int)$_POST['centered'];
$featured = (int)$_POST['featured'];
$view_directions = (int)$_POST['view_directions'];
$view_street_view = (int)$_POST['view_street_view'];
$view_review = (int)$_POST['view_review'];
$view_add_photo = (int)$_POST['view_add_photo'];
$view_share = (int)$_POST['view_share'];
$color_hex = strip_tags($_POST['color_hex']);
$icon_color_hex = strip_tags($_POST['icon_color_hex']);
if(empty($icon_color_hex)) $icon_color_hex="#ffffff";
$color_hex_hover = strip_tags($_POST['color_hex_hover']);
$icon_color_hex_hover = strip_tags($_POST['icon_color_hex_hover']);
$marker_size = $_POST['marker_size'];
$id_icon_library = $_POST['id_icon_library'];
$icon = strip_tags($_POST['icon']);
$icon_label = strip_tags($_POST['icon_label']);
$icon_label_color = strip_tags($_POST['icon_label_color']);
$icon_label_size = (int) $_POST['icon_label_size'];
if(empty($icon_label_size)) $icon_label_size=12;
$icon_e1 = $_POST['icon_e1'];
$field_e1 = htmlspecialchars_decode($_POST['field_e1']);
$icon_e2 = $_POST['icon_e2'];
$field_e2 = htmlspecialchars_decode($_POST['field_e2']);
$icon_e3 = $_POST['icon_e3'];
$field_e3 = htmlspecialchars_decode($_POST['field_e3']);
$icon_e4 = $_POST['icon_e4'];
$field_e4 = htmlspecialchars_decode($_POST['field_e4']);
$icon_e5 = $_POST['icon_e5'];
$field_e5 = htmlspecialchars_decode($_POST['field_e5']);
$icon_e6 = $_POST['icon_e6'];
$field_e6 = htmlspecialchars_decode($_POST['field_e6']);
$icon_e7 = $_POST['icon_e7'];
$field_e7 = htmlspecialchars_decode($_POST['field_e7']);
$icon_e8 = $_POST['icon_e8'];
$field_e8 = htmlspecialchars_decode($_POST['field_e8']);
$icon_e9 = $_POST['icon_e9'];
$field_e9 = htmlspecialchars_decode($_POST['field_e9']);
$icon_e10 = $_POST['icon_e10'];
$field_e10 = htmlspecialchars_decode($_POST['field_e10']);
$icon_e11 = $_POST['icon_e11'];
$field_e11 = htmlspecialchars_decode($_POST['field_e11']);
$icon_e12 = $_POST['icon_e12'];
$field_e12 = htmlspecialchars_decode($_POST['field_e12']);
$icon_e13 = $_POST['icon_e13'];
$field_e13 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e13']));
$icon_e14 = $_POST['icon_e14'];
$field_e14 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e14']));
$icon_e15 = $_POST['icon_e15'];
$field_e15 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e15']));
$icon_e16 = $_POST['icon_e16'];
$field_e16 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e16']));
$icon_e17 = $_POST['icon_e17'];
$field_e17 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e17']));
$icon_e18 = $_POST['icon_e18'];
$field_e18 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e18']));
$icon_e19 = $_POST['icon_e19'];
$field_e19 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e19']));
$icon_e20 = $_POST['icon_e20'];
$field_e20 = str_replace("'","\'",htmlspecialchars_decode($_POST['field_e20']));
$icon_b1 = $_POST['icon_b1'];
$title_b1 = str_replace("'","\'",$_POST['title_b1']);
$value_b1 = str_replace("'","\'",htmlspecialchars_decode($_POST['value_b1']));
$name_e1 = strip_tags($_POST['name_e1']);
$name_e2 = strip_tags($_POST['name_e2']);
$name_e3 = strip_tags($_POST['name_e3']);
$name_e4 = strip_tags($_POST['name_e4']);
$name_e5 = strip_tags($_POST['name_e5']);
$name_e6 = strip_tags($_POST['name_e6']);
$name_e7 = strip_tags($_POST['name_e7']);
$name_e8 = strip_tags($_POST['name_e8']);
$name_e9 = strip_tags($_POST['name_e9']);
$name_e10 = strip_tags($_POST['name_e10']);
$name_e11 = strip_tags($_POST['name_e11']);
$name_e12 = strip_tags($_POST['name_e12']);
$name_e13 = strip_tags($_POST['name_e13']);
$name_e14 = strip_tags($_POST['name_e14']);
$name_e15 = strip_tags($_POST['name_e15']);
$name_e16 = strip_tags($_POST['name_e16']);
$name_e17 = strip_tags($_POST['name_e17']);
$name_e18 = strip_tags($_POST['name_e18']);
$name_e19 = strip_tags($_POST['name_e19']);
$name_e20 = strip_tags($_POST['name_e20']);
$visible_e1 = (int)$_POST['visible_e1'];
$visible_e2 = (int)$_POST['visible_e2'];
$visible_e3 = (int)$_POST['visible_e3'];
$visible_e4 = (int)$_POST['visible_e4'];
$visible_e5 = (int)$_POST['visible_e5'];
$visible_e6 = (int)$_POST['visible_e6'];
$visible_e7 = (int)$_POST['visible_e7'];
$visible_e8 = (int)$_POST['visible_e8'];
$visible_e9 = (int)$_POST['visible_e9'];
$visible_e10 = (int)$_POST['visible_e10'];
$visible_e11 = (int)$_POST['visible_e11'];
$visible_e12 = (int)$_POST['visible_e12'];
$visible_e13 = (int)$_POST['visible_e13'];
$visible_e14 = (int)$_POST['visible_e14'];
$visible_e15 = (int)$_POST['visible_e15'];
$visible_e16 = (int)$_POST['visible_e16'];
$visible_e17 = (int)$_POST['visible_e17'];
$visible_e18 = (int)$_POST['visible_e18'];
$visible_e19 = (int)$_POST['visible_e19'];
$visible_e20 = (int)$_POST['visible_e20'];
$min_zoom_level = $_POST['min_zoom_level'];
$geofence_radius = $_POST['geofence_radius'];
$geofence_color = $_POST['geofence_color'];
$open_extra_content = (int)$_POST['open_extra_content'];
if(empty($geofence_radius)) $geofence_radius=0;
if($geofence_radius<0) $geofence_radius=0;
if(empty($geofence_color)) $geofence_color="rgba(0,0,255,0.1)";
$popup_image_height = $_POST['popup_image_height'];
$popup_background = $_POST['popup_background'];
if(empty($popup_background)) $popup_background='#ffffff';
$popup_color = $_POST['popup_color'];
if(empty($popup_color)) $popup_color='#000000';
$schedule = $_POST['schedule'];
$schedule_box = $_POST['schedule_box'];
$box_warning = strip_tags($_POST['box_warning']);
$type = strip_tags($_POST['type']);
$link = strip_tags($_POST['link']);
$visible_map = (int)$_POST['visible_map'];
$visible_search = (int)$_POST['visible_search'];
$song = strip_tags($_POST['song']);
$song_link = strip_tags($_POST['song_link']);
$song_bg_volume = (float)$_POST['song_bg_volume'];
$song_volume = (float)$_POST['song_volume'];
$song_play_bg = (int)$_POST['song_play_bg'];
$selected_zoom = (int)$_POST['selected_zoom'];
$duration_zoom = (int)$_POST['duration_zoom'];
$view_last_update = (int)$_POST['view_last_update'];
$tripadvisor_location_id = strip_tags($_POST['tripadvisor_location_id']);
$array_lang = json_decode($_POST['array_lang'],true);
$map = get_map($id_map,$_SESSION['id_user']);
if($marker_size==$map['markers_size']) {
    $marker_size = 0;
}
$status = false;
$current_tripadvisor_location_id = "";
$query_t = "SELECT * FROM sml_markers WHERE id=$id_marker LIMIT 1;";
$result_t = $mysqli->query($query_t);
$curr_data = [];
if($result_t) {
    $curr_data = $result_t->fetch_array(MYSQLI_ASSOC);
    $current_tripadvisor_location_id = $curr_data['tripadvisor_location_id'] ?? '';
}
$fields_to_check = [
    'lat' => $lat, 'lon' => $lon, 'name' => $name, 'type' => $type, 'link' => $link,
    'street' => $street, 'city' => $city, 'county' => $county, 'state' => $state,
    'postal_code' => $postal_code, 'country' => $country, 'website' => $website,
    'website_caption' => $website_caption, 'email' => $email, 'phone' => $phone,
    'whatsapp' => $whatsapp, 'hours' => $hours, 'description' => $description,
    'extra_field_name_1' => $name_e1, 'extra_field_value_1' => $field_e1,
    'extra_field_name_2' => $name_e2, 'extra_field_value_2' => $field_e2,
    'extra_field_name_3' => $name_e3, 'extra_field_value_3' => $field_e3,
    'extra_field_name_4' => $name_e4, 'extra_field_value_4' => $field_e4,
    'extra_field_name_5' => $name_e5, 'extra_field_value_5' => $field_e5,
    'extra_field_name_6' => $name_e6, 'extra_field_value_6' => $field_e6,
    'extra_field_name_7' => $name_e7, 'extra_field_value_7' => $field_e7,
    'extra_field_name_8' => $name_e8, 'extra_field_value_8' => $field_e8,
    'extra_field_name_9' => $name_e9, 'extra_field_value_9' => $field_e9,
    'extra_field_name_10' => $name_e10, 'extra_field_value_10' => $field_e10,
    'extra_field_name_11' => $name_e11, 'extra_field_value_11' => $field_e11,
    'extra_field_name_12' => $name_e12, 'extra_field_value_12' => $field_e12,
    'extra_field_name_13' => $name_e13, 'extra_field_value_13' => $field_e13,
    'extra_field_name_14' => $name_e14, 'extra_field_value_14' => $field_e14,
    'extra_field_name_15' => $name_e15, 'extra_field_value_15' => $field_e15,
    'extra_field_name_16' => $name_e16, 'extra_field_value_16' => $field_e16,
    'extra_field_name_17' => $name_e17, 'extra_field_value_17' => $field_e17,
    'extra_field_name_18' => $name_e18, 'extra_field_value_18' => $field_e18,
    'extra_field_name_19' => $name_e19, 'extra_field_value_19' => $field_e19,
    'extra_field_name_20' => $name_e20, 'extra_field_value_20' => $field_e20,
    'extra_button_value_1' => $value_b1, 'extra_button_title_1' => $title_b1,
    'box_warning' => $box_warning, 'song' => $song, 'song_link' => $song_link
];
$needs_update_date = false;
foreach($fields_to_check as $db_col => $new_val) {
    $old_val = isset($curr_data[$db_col]) ? $curr_data[$db_col] : '';
    $old_str = trim((string)$old_val);
    $new_str = trim((string)$new_val);
    if ($old_str === $new_str) continue;
    $old_str = stripslashes($old_str);
    $new_str = stripslashes($new_str);
    if ($old_str === $new_str) continue;
    if (is_numeric($old_str) && is_numeric($new_str)) {
        if ((float)$old_str == (float)$new_str) continue;
    }
    preg_match_all('/(?:href|src)=["\']([^"\']+)["\']/i', $old_str, $old_links);
    preg_match_all('/(?:href|src)=["\']([^"\']+)["\']/i', $new_str, $new_links);
    $old_plain = html_entity_decode(strip_tags($old_str), ENT_QUOTES);
    $new_plain = html_entity_decode(strip_tags($new_str), ENT_QUOTES);
    if (!empty($old_links[1])) $old_plain .= ' ' . implode(' ', $old_links[1]);
    if (!empty($new_links[1])) $new_plain .= ' ' . implode(' ', $new_links[1]);
    $old_plain = trim(preg_replace('/\s+/', ' ', $old_plain));
    $new_plain = trim(preg_replace('/\s+/', ' ', $new_plain));
    if ($old_plain === $new_plain || (empty($old_plain) && empty($new_plain))) {
        continue;
    }
    $needs_update_date = true;
    break;
}
if (!$needs_update_date) {
    $existing_categories = [];
    $query_c = "SELECT id_category FROM sml_markers_categories_assoc WHERE id_marker=$id_marker;";
    $result_c = $mysqli->query($query_c);
    if($result_c) {
        while($row_c = $result_c->fetch_array(MYSQLI_ASSOC)) {
            $existing_categories[] = (int)$row_c['id_category'];
        }
    }
    $incoming_categories = array_map('intval', $id_categories);
    sort($existing_categories);
    sort($incoming_categories);
    if ($existing_categories !== $incoming_categories) {
        $needs_update_date = true;
    }
}
if (!$needs_update_date && !empty($array_images) && is_array($array_images)) {
    foreach ($array_images as $img) {
        if (isset($img['action']) && in_array($img['action'], ['insert', 'delete'])) {
            $needs_update_date = true;
            break;
        }
    }
}
$query = "UPDATE sml_markers SET name=?,type=?,link=?,street=?,city=?,postal_code=?,country=?,county=?,state=?,website=?,website_caption=?,phone=?,whatsapp=?,email=?,hours=?,description=?,lat=?,lon=?,icon=?,icon_label=?,icon_label_color=?,icon_label_size=?,active=?,to_validate=?,centered=?,featured=?,view_directions=?,view_street_view=?,view_review=?,view_share=?,color_hex=?,icon_color_hex=?,marker_size=?,id_icon_library=?,extra_field_icon_1=?,extra_field_icon_2=?,extra_field_icon_3=?,extra_field_icon_4=?,extra_field_icon_5=?,extra_field_icon_6=?,extra_field_icon_7=?,extra_field_icon_8=?,extra_field_icon_9=?,extra_field_icon_10=?,extra_field_icon_11=?,extra_field_icon_12=?,extra_field_icon_13=?,extra_field_icon_14=?,extra_field_icon_15=?,extra_field_icon_16=?,extra_field_icon_17=?,extra_field_icon_18=?,extra_field_icon_19=?,extra_field_icon_20=?,extra_field_value_1=?,extra_field_value_2=?,extra_field_value_3=?,extra_field_value_4=?,extra_field_value_5=?,extra_field_value_6=?,extra_field_value_7=?,extra_field_value_8=?,extra_field_value_9=?,extra_field_value_10=?,extra_field_value_11=?,extra_field_value_12=?,extra_field_value_13=?,extra_field_value_14=?,extra_field_value_15=?,extra_field_value_16=?,extra_field_value_17=?,extra_field_value_18=?,extra_field_value_19=?,extra_field_value_20=?,extra_button_icon_1=?,extra_button_title_1=?,extra_button_value_1=?,min_zoom_level=?,geofence_radius=?,geofence_color=?,open_sheet=?,view_popup=?,popup_image_height=?,popup_background=?,popup_color=?,color_hex_hover=?,icon_color_hex_hover=?,open_extra_content=?,schedule=?,visible_map=?,visible_search=?,song=?,song_bg_volume=?,song_volume=?,extra_field_name_1=?,extra_field_name_2=?,extra_field_name_3=?,extra_field_name_4=?,extra_field_name_5=?,extra_field_name_6=?,extra_field_name_7=?,extra_field_name_8=?,extra_field_name_9=?,extra_field_name_10=?,extra_field_name_11=?,extra_field_name_12=?,extra_field_name_13=?,extra_field_name_14=?,extra_field_name_15=?,extra_field_name_16=?,extra_field_name_17=?,extra_field_name_18=?,extra_field_name_19=?,extra_field_name_20=?,selected_zoom=?,tripadvisor_location_id=?,duration_zoom=?,schedule_box_warning=?,box_warning=?,song_link=?,extra_field_visible_1=?,extra_field_visible_2=?,extra_field_visible_3=?,extra_field_visible_4=?,extra_field_visible_5=?,extra_field_visible_6=?,extra_field_visible_7=?,extra_field_visible_8=?,extra_field_visible_9=?,extra_field_visible_10=?,extra_field_visible_11=?,extra_field_visible_12=?,extra_field_visible_13=?,extra_field_visible_14=?,extra_field_visible_15=?,extra_field_visible_16=?,extra_field_visible_17=?,extra_field_visible_18=?,extra_field_visible_19=?,extra_field_visible_20=?,song_play_bg=?,view_last_update=?,view_add_photo=? WHERE id=?;";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('sssssssssssssssssssssiiiiiiiiissdisssssssssssssssssssssssssssssssssssssssssssiisiiissssisiisddssssssssssssssssssssisisssiiiiiiiiiiiiiiiiiiiiiiii',$name,$type,$link,$street,$city,$postal_code,$country,$county,$state,$website,$website_caption,$phone,$whatsapp,$email,$hours,$description,$lat,$lon,$icon,$icon_label,$icon_label_color,$icon_label_size,$active,$to_validate,$centered,$featured,$view_directions,$view_street_view,$view_review,$view_share,$color_hex,$icon_color_hex,$marker_size,$id_icon_library,$icon_e1,$icon_e2,$icon_e3,$icon_e4,$icon_e5,$icon_e6,$icon_e7,$icon_e8,$icon_e9,$icon_e10,$icon_e11,$icon_e12,$icon_e13,$icon_e14,$icon_e15,$icon_e16,$icon_e17,$icon_e18,$icon_e19,$icon_e20,$field_e1,$field_e2,$field_e3,$field_e4,$field_e5,$field_e6,$field_e7,$field_e8,$field_e9,$field_e10,$field_e11,$field_e12,$field_e13,$field_e14,$field_e15,$field_e16,$field_e17,$field_e18,$field_e19,$field_e20,$icon_b1,$title_b1,$value_b1,$min_zoom_level,$geofence_radius,$geofence_color,$open_sheet,$view_popup,$popup_image_height,$popup_background,$popup_color,$color_hex_hover,$icon_color_hex_hover,$open_extra_content,$schedule,$visible_map,$visible_search,$song,$song_bg_volume,$song_volume,$name_e1,$name_e2,$name_e3,$name_e4,$name_e5,$name_e6,$name_e7,$name_e8,$name_e9,$name_e10,$name_e11,$name_e12,$name_e13,$name_e14,$name_e15,$name_e16,$name_e17,$name_e18,$name_e19,$name_e20,$selected_zoom,$tripadvisor_location_id,$duration_zoom,$schedule_box,$box_warning,$song_link,$visible_e1,$visible_e2,$visible_e3,$visible_e4,$visible_e5,$visible_e6,$visible_e7,$visible_e8,$visible_e9,$visible_e10,$visible_e11,$visible_e12,$visible_e13,$visible_e14,$visible_e15,$visible_e16,$visible_e17,$visible_e18,$visible_e19,$visible_e20,$song_play_bg,$view_last_update,$view_add_photo,$id_marker);
    $result = $smt->execute();
    if($result) {
        save_input_langs($array_lang,'sml_markers_lang','id_marker',$id_marker);
        $status = true;
    }
}
if($current_tripadvisor_location_id!=$tripadvisor_location_id) {
    $mysqli->query("UPDATE sml_markers SET tripadvisor_link=NULL,tripadvisor_reviews=NULL,tripadvisor_rating=NULL WHERE id=$id_marker;");
}
if($status) {
    if ($needs_update_date) {
        $now = date('Y-m-d H:i:s');
        $mysqli->query("UPDATE sml_markers SET update_date='$now' WHERE id=$id_marker;");
    }
    $query = "DELETE FROM sml_markers_categories_assoc WHERE id_marker=?;";
    if($smt = $mysqli->prepare($query)) {
        $smt->bind_param('i',$id_marker);
        $smt->execute();
    }
    if($centered==1) {
        $query = "UPDATE sml_markers SET centered=0 WHERE id!=? AND id_map=?;";
        if($smt = $mysqli->prepare($query)) {
            $smt->bind_param('ii',$id_marker,$id_map);
            $smt->execute();
        }
    }
    foreach ($id_categories as $id_category) {
        $query = "INSERT INTO sml_markers_categories_assoc(id_marker,id_category) VALUES(?,?);";
        if($smt = $mysqli->prepare($query)) {
            $smt->bind_param('ii',$id_marker,$id_category);
            $smt->execute();
        }
    }
    $priority = 0;
    foreach ($array_images as $image) {
        $id = $image['id'];
        $action = $image['action'];
        $main = $image['main'];
        $esclude = $image['exclude'];
        $foto = $image['image'];
        switch($action) {
            case 'insert':
                $query = "INSERT INTO sml_images(id_marker,main,image,priority,exclude) VALUES(?,?,?,?,?);";
                if($smt = $mysqli->prepare($query)) {
                    $smt->bind_param('iisii',$id_marker,$main,$foto,$priority,$esclude);
                    $smt->execute();
                }
                break;
            case 'update':
                $query = "UPDATE sml_images SET main=?,priority=?,exclude=?,image=? WHERE id=?;";
                if($smt = $mysqli->prepare($query)) {
                    $smt->bind_param('iiisi',$main,$priority,$esclude,$foto,$id);
                    $smt->execute();
                }
                break;
            case 'delete':
                $query = "DELETE FROM sml_images WHERE id=?;";
                if($smt = $mysqli->prepare($query)) {
                    $smt->bind_param('i',$id);
                    $smt->execute();
                }
                break;
        }
        $priority++;
    }
    require_once("../../services/clean_images.php");
    ob_end_clean();
    echo json_encode(array("status"=>"ok"));
    exit;
} else {
    ob_end_clean();
    echo json_encode(array("status"=>"error","error"=>$mysqli->error));
    exit;
}