From 8e781421190e8fb265e7562860c8d696a4de039f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 8 Dec 2017 03:18:29 +0100 Subject: [PATCH] Added api: POST message/emergency (with db) --- build/api/module/message.php | 60 +++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/build/api/module/message.php b/build/api/module/message.php index 42fd1db..09a4c7e 100644 --- a/build/api/module/message.php +++ b/build/api/module/message.php @@ -26,23 +26,61 @@ class message public function POST_emergency($argv){ extract($argv); - $wsi = new wsinterop("localhost",9998); + /* (1) Get ID_USER if connected + ---------------------------------------------------------*/ + $id_user = null; + $name = null; + if( count($_SESSSION['ADMIN']) > 0 ) + $id_user = $_SESSION['ADMIN']['id']; + + elseif( count($_SESSSION['USER']) > 0 ) + $id_user = $_SESSION['USER']['id']; + + else + $name = $_SESSION['NAME']; + + + /* (2) Create emergenct + ---------------------------------------------------------*/ + /* (1) Try to create entry */ + $id_created = Repo::request('emergency', 'create', + $id_user, + $name, + time(), + $message, + 0, + $location[0], + is_null($URL_0) ? '' : $URL_0 + ); + + /* (2) If cannot create -> dispatch error */ + if( $id_created === false ) + return ['error' => new Error(Err::RepoError)]; + + + + /* (3) Send to WebSocket + ---------------------------------------------------------*/ + /* (1) Open socket */ + $wsi = new wsinterop('localhost', 9998); + + /* (2) Send data */ $wsi->send([ - "operation" => "PostMessage", - "message" => $message, - "username" => $_SESSION['NAME'], - "location" => $location, - //TODO implémenter la récupération d'id depuis la bdd - "id" => uniqid(), - "channelType" => "Emergency", - "channelName" => is_null($URL_0) ? "" : "$URL_0" + 'operation' => 'PostMessage', + 'message' => $message, + 'username' => $_SESSION['NAME'], + 'location' => $location, + 'id' => $id_created, + 'channelType' => 'Emergency', + 'channelName' => is_null($URL_0) ? '' : $URL_0 ]); + /* (3) Close socket */ $wsi->close(); - return ["sent" => true]; - //TODO: tout poster dans la BDD + + return ['sent' => true]; } public function DELETE_emergency($argv){