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.'%\''; $sql = ' SELECT id ,name ,date_start ,date_end ,coalesce(ST_NPoints(geom),0) as count ,ST_X(ST_Centroid(geom)) lon ,ST_Y(ST_Centroid(geom)) lat FROM main.detours where del=false and ('.$object->name.' is null or name like '.$object->name.') ORDER BY 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 .='"date_start":'.json_encode($row['date_start'],JSON_UNESCAPED_UNICODE).",\n"; $json .='"date_end":'.json_encode($row['date_end'],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.detours 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.detours 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!"); }