<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if($_SESSION['sml_si']!=session_id()) {
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$id_user = $_SESSION['id_user'];
$id_category = (int)$_POST['id_category'];
$array = array();

$settings = get_settings();
$user_info = get_user_info($id_user);
if(!isset($_SESSION['lang'])) {
    if(!empty($user_info['language'])) {
        $language = $user_info['language'];
    } else {
        $language = $settings['language'];
    }
} else {
    $language = $_SESSION['lang'];
}

$where = $where_f = "";
$role = get_user_role($id_user);
switch($role) {
    case 'administrator':
        $where = $where_f = "";
        break;
    case 'customer':
        $where = $where_f = " AND m.id_user=$id_user ";
        break;
    case 'editor':
        $where = $where_f = " AND m.id IN (0) ";
        $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'];
                if(!empty($ids)) {
                    $where = $where_f = " AND m.id IN ($ids) ";
                }
            }
        }
        break;
}

if($id_category!=0) {
    $where .= " AND ca.id_category = $id_category ";
}
$array_cat = array();
$query = "SELECT m.id,c.name as category_name,c.id as category_id FROM sml_maps as m
            LEFT JOIN sml_category_map_assoc as scva on scva.id_map=m.id
            LEFT JOIN sml_map_categories as c ON c.id=scva.id_category
            WHERE 1=1 $where_f
            GROUP BY m.id,scva.id_category,c.name,c.id;";
$result = $mysqli->query($query);
if($result) {
    if($result->num_rows>0) {
        while($row=$result->fetch_array(MYSQLI_ASSOC)) {
            if(!empty($row['category_id'])) {
                if(!in_array($row['category_id']."|".$row['category_name'],$array_cat)) {
                    $array_cat[] = $row['category_id']."|".$row['category_name'];
                }
            }
        }
    }
}

$query = "SELECT m.id,m.active,m.name,m.language,m.languages_enabled,m.date_created,u.username,GROUP_CONCAT(DISTINCT c.name SEPARATOR ', ') as category_name,GROUP_CONCAT(DISTINCT c.id) as category_id,m.map_style as style,m.background_image,(SELECT COUNT(id) FROM sml_markers WHERE id_map=m.id AND to_validate=0) as count_markers 
            FROM sml_maps as m 
            LEFT JOIN sml_users as u ON u.id=m.id_user
            LEFT JOIN sml_category_map_assoc as ca ON ca.id_map=m.id
            LEFT JOIN sml_map_categories as c ON c.id=ca.id_category
            WHERE 1=1 $where 
            GROUP BY m.id,m.active,m.name,m.date_created,u.username,m.map_style,m.background_image
            ORDER BY date_created DESC";
if($smt = $mysqli->prepare($query)) {
    $result = $smt->execute();
    if($result) {
        $result = get_result($smt);
        if(count($result)>0) {
            while($row = array_shift($result)) {
                if($user_info['role']=='editor') {
                    $editor_permissions = get_editor_permissions($id_user,$row['id']);
                    if($editor_permissions['edit_map']==1) {
                        $row['edit_permission']=true;
                    } else {
                        $row['edit_permission']=false;
                    }
                    if($editor_permissions['publish']==1) {
                        $row['publish_permission']=true;
                    } else {
                        $row['publish_permission']=false;
                    }
                } else {
                    $row['edit_permission']=true;
                    $row['publish_permission']=true;
                }
                $row['date_created'] = formatTime("dd MMM y",$language,strtotime($row['date_created']));
                if(empty($row['background_image'])) $row['background_image']='';
                if(empty($row['category_name'])) $row['category_name']='';
                if(!empty($row['languages_enabled'])) {
                    $row['languages_enabled']=json_decode($row['languages_enabled'],true);
                } else {
                    $row['languages_enabled']=array();
                }
                $default_language = $row['language'];
                if(empty($default_language)) {
                    $default_language = $settings['language'];
                }
                $row['languages_enabled'][$default_language]=0;
                $array_languages = array();
                foreach ($row['languages_enabled'] as $lang=>$enabled) {
                    if($enabled==1) {
                        array_push($array_languages,$lang);
                    }
                }
                $row['default_language']=$default_language;
                $row['languages']=$array_languages;
                $array[]=$row;
            }
        }
    }
}
ob_end_clean();
echo json_encode(array("map_list"=>$array,"categories"=>$array_cat));