Files
GEOVizor_PHP/observation/pscripts/geofences.php
2023-11-07 19:51:49 +06:00

146 lines
4.7 KiB
PHP

<?php
//Выбираю объезды в GeoJSON и отправляю клиенту
require_once("../../monitoring/config.php");
function sendError($msg)
{
$json='{"errorCode":1,"errorMessage":'.json_encode($msg,JSON_UNESCAPED_UNICODE).'}';
header('Content-Type: application/json');
echo $json;
exit;
}
$db = connectToDB();
//$db->exec('SET NAMES utf8');
//$db->exec("SET time_zone = '+00:00'");
$fn=0;
if(isset($_GET['fn'])) { $fn=$_GET['fn']; }
if($fn=='1') //Вернуть список
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if(!property_exists($object,'name') or $object->name=='') $object->name='null'; else $object->name='\'%'.$object->name.'%\'';
//if(!property_exists($object,'geofence_group_id') or $object->geofence_group_id=='') $object->geofence_group_id='null';
if(!property_exists($object,'geofence_type_id') or $object->geofence_type_id=='') $object->geofence_type_id='null';
$sql = '
SELECT
g.id
,g.name
,coalesce(ST_NPoints(geom),0) as count
,ST_X(ST_Centroid(geom)) lon
,ST_Y(ST_Centroid(geom)) lat
,gt.name as geofence_type_name
FROM
main.geofences g
left join main.geofences_types gt on gt.id=g.geofence_type_id
where
g.del=false
and ('.$object->name.' is null or g.name like '.$object->name.')
and ('.$object->geofence_type_id.' is null or g.geofence_type_id = '.$object->geofence_type_id.')
ORDER BY g.name;
';
try
{
$res = $db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
//Массив объектов
$json ='[';
//Перебираю и последовательно отправляю не отправленные пломбы
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$json .="\n";
$json .='{';
$json .='"id":'.json_encode($row['id']).",\n";
$json .='"name":'.json_encode($row['name'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"geofence_type_name":'.json_encode($row['geofence_type_name'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"lat":'.json_encode($row['lat']).",\n";
$json .='"lon":'.json_encode($row['lon']).",\n";
$json .='"count":'.json_encode($row['count'])."\n";
$json .='},';
}
if($json[strlen($json) - 1]==','){
$json=substr ( $json , 0, strlen($json)-1 );
}
$json .=']';
header('Content-Type: application/json');
echo $json;
exit;
}else if($fn=='2') //Вернуть GEOJSON по ID записи
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if($object->id=='') $object->id='null';
/*if($object->cnumber=='') $object->cnumber='null'; else $object->cnumber='"%'.$object->cnumber.'%"'; //Гос. номер
if($object->tnumber=='') $object->tnumber='null'; else $object->tnumber='"%'.$object->tnumber.'%"'; //Номер ТД
if($object->country_seal_begin=='') $object->country_seal_begin='null'; //Страна пломб.
if($object->country_seal_end=='') $object->country_seal_end='null'; //Страна распломб.*/
//Выбираю геометрию
$sql="select ST_AsGeoJSON(geom,6,0) as geom from main.geofences where id=".$object->id.";";
try
{
$res = $db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
$json="{}";
if($res!=NULL && $res->rowCount()>0)
{
while ($row = $res->fetch(PDO::FETCH_NUM))
{
$json=$row[0];
}
}
header('Content-Type: application/json');
echo $json;
exit;
}else if($fn=='3') //Удалить запись
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if($object==null) $object = (object)[];
if(!property_exists($object,'id') or $object->id=='') $object->id=null; //Поле заполнено если редактирвание записи
if($object->id!=null)
{
try
{
$sql="update main.geofences set del=true where id=".$object->id;
$db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
$json='{}';
header('Content-Type: application/json');
echo $json;
exit;
}else
{
sendError("ID is not set!");
}
}else
{
sendError("Fn is null!");
}