<?php

/*
* Project: FI-WARE
* Copyright (c) 2014 Center for Internet Excellence, University of Oulu, All Rights Reserved
* For conditions of distribution and use, see copyright notice in LICENSE
*/
require_once 'db.php';
require_once 'data_manager.php';
require_once 'util.php';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
    if (isset($_GET['poi_id'])) {
        $uuid = pg_escape_string($_GET['poi_id']);
        $db_opts = get_db_options();
        $pgcon = connectPostgreSQL($db_opts["sql_db_name"]);
        $fw_core_tbl = $db_opts['fw_core_table_name'];
        $del_stmt = "DELETE FROM {$fw_core_tbl} WHERE uuid='{$uuid}'";
        $del_result = pg_query($del_stmt);
        if (!$del_result) {
            header("HTTP/1.0 500 Internal Server Error");
            $error = pg_last_error();
            die($error);
        }
        $rows_deleted = pg_affected_rows($del_result);
        if ($rows_deleted != 1) {
            header("HTTP/1.0 400 Bad Request");
            die("The specified UUID was not found from the database!");
        }
        $components = get_supported_components();
        $m_db = connectMongoDB($db_opts['mongo_db_name']);
        foreach ($components as $component) {
 }
 if ($lon < -180 or $lon > 180 or $lat < -90 or $lat > 90) {
     header("HTTP/1.0 400 Bad Request");
     die("Coordinate values are out of range [-180 -90, 180 90]");
 }
 if (isset($_GET['radius'])) {
     $radius = $_GET['radius'];
     if (!is_numeric($radius)) {
         header("HTTP/1.0 400 Bad Request");
         echo "'radius' must be a numeric value!";
         return;
     }
 }
 $common_params = handle_common_search_params();
 $db_opts = get_db_options();
 $pgcon = connectPostgreSQL($db_opts['sql_db_name']);
 $fw_core_tbl = $db_opts['fw_core_table_name'];
 if (isset($common_params['categories'])) {
     $query = "SELECT uuid, array_to_string(categories, ',') as categories, thumbnail, st_x(location::geometry) as lon, st_y(location::geometry) as lat, st_astext(geometry) as geometry, timestamp, " . "source_name, source_website, source_id, source_license " . "FROM {$fw_core_tbl} WHERE ST_DWithin(location, ST_GeogFromText('POINT({$lon} {$lat})'), {$radius}) AND categories && '{" . $common_params['categories'] . "}' LIMIT " . $common_params['max_results'];
 } else {
     $query = "SELECT uuid, array_to_string(categories, ',') as categories, " . "thumbnail, st_x(location::geometry) as lon, st_y(location::geometry) " . "as lat, st_astext(geometry) as geometry, timestamp, userid, " . "source_name, source_website, source_id, source_license " . "FROM {$fw_core_tbl} WHERE ST_DWithin(location, ST_GeogFromText(" . "'POINT({$lon} {$lat})'), {$radius}) LIMIT " . $common_params['max_results'];
 }
 //     echo "<br>" . $query;
 $core_result = pg_query($query);
 if (!$core_result) {
     header("HTTP/1.0 500 Internal Server Error");
     $error = pg_last_error();
     die($error);
 }
 $incl_fw_core = FALSE;
 if (in_array("fw_core", $common_params['components'])) {