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){