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

174 lines
5.6 KiB
PHP

<?php
header('Cache-Control: no-store, no-cache, must-revalidate');
header("Expires: 0");
header('Pragma: no-cache');
@session_start();
require_once("../../monitoring/config.php");
function sendError($msg)
{
$obj = new StdClass();
$obj->errorCode = 1;
$obj->errorMessage = $msg;
$obj->data = array();
header('Content-Type: application/json');
header("Cache-Control: no-cache, must-revalidate");
echo json_encode($obj);
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';
$obj = new StdClass();
$obj->errorCode = 0;
$obj->errorMessage = '';
$obj->data = array();
$sql='select
id,
name
from
main.geofences_types gt
where
del=false
and gt.company_id = (select company_id from main._users where id='.$_SESSION['USER_ID'].')
and id in (select geofence_type_id from main.geofences g where g.del=false )
and (' . $object->geofence_type_id . ' is null or gt.id = ' . $object->geofence_type_id . ')
ORDER BY gt.name;';
try
{
$types = $db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
while ($type = $types->fetch(PDO::FETCH_ASSOC)) {
$type=(object)$type;
$type->geofences = array();
$sql = '
SELECT
g.id
,6 as icon_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 g.company_id = (select company_id from main._users where id='.$_SESSION['USER_ID'].')
and (' . $object->name . ' is null or g.name like ' . $object->name . ')
and g.geofence_type_id = '.$type->id.'
ORDER BY g.name;
';
try {
$geofences = $db->query($sql);
} catch (Exception $e) {
sendError($e->getMessage());
}
//Перебираю и последовательно отправляю не отправленные пломбы
while ($geofence = $geofences->fetch(PDO::FETCH_ASSOC)) {
$geofence = (object)$geofence;
array_push($type->geofences, $geofence);
}
array_push($obj->data, $type);
}
header('Content-Type: application/json');
header("Cache-Control: no-cache, must-revalidate");
echo json_encode($obj);
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());
}
$obj = new StdClass();
$obj->errorCode = 0;
$obj->errorMessage = '';
header('Content-Type: application/json');
header("Cache-Control: no-cache, must-revalidate");
echo json_encode($obj);
exit();
}else
{
sendError("ID is not set!");
}
}else
{
sendError("Fn is null!");
}