2017-12-07 19:24:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: lucas
|
|
|
|
* Date: 07/12/17
|
|
|
|
* Time: 19:48
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace api\module;
|
|
|
|
|
|
|
|
|
2017-12-08 02:39:29 +00:00
|
|
|
use \database\core\Repo;
|
|
|
|
use \error\core\Error;
|
|
|
|
use \error\core\Err;
|
2017-12-07 19:24:59 +00:00
|
|
|
use kwebsocket\core\wsinterop;
|
|
|
|
|
|
|
|
class message
|
|
|
|
{
|
|
|
|
|
2017-12-08 00:56:21 +00:00
|
|
|
public function GET_emergency($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$messages = [];
|
|
|
|
|
|
|
|
return ["sent" => true, "messages" => $messages];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:59 +00:00
|
|
|
public function POST_emergency($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
2017-12-08 02:18:29 +00:00
|
|
|
/* (1) Get ID_USER if connected
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$id_user = null;
|
|
|
|
$name = null;
|
|
|
|
|
2017-12-08 02:39:29 +00:00
|
|
|
if( count($_SESSION['ADMIN']) > 0 )
|
2017-12-08 02:18:29 +00:00
|
|
|
$id_user = $_SESSION['ADMIN']['id'];
|
|
|
|
|
2017-12-08 02:39:29 +00:00
|
|
|
elseif( count($_SESSION['USER']) > 0 )
|
2017-12-08 02:18:29 +00:00
|
|
|
$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,
|
|
|
|
$message,
|
|
|
|
0,
|
|
|
|
$location[0],
|
2017-12-08 02:39:29 +00:00
|
|
|
$location[1],
|
|
|
|
$URL_0
|
2017-12-08 02:18:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* (2) If cannot create -> dispatch error */
|
|
|
|
if( $id_created === false )
|
|
|
|
return ['error' => new Error(Err::RepoError)];
|
|
|
|
|
|
|
|
|
2017-12-07 19:24:59 +00:00
|
|
|
|
2017-12-08 02:18:29 +00:00
|
|
|
/* (3) Send to WebSocket
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Open socket */
|
|
|
|
$wsi = new wsinterop('localhost', 9998);
|
|
|
|
|
|
|
|
/* (2) Send data */
|
2017-12-07 19:24:59 +00:00
|
|
|
$wsi->send([
|
2017-12-08 02:18:29 +00:00
|
|
|
'operation' => 'PostMessage',
|
|
|
|
'message' => $message,
|
|
|
|
'username' => $_SESSION['NAME'],
|
|
|
|
'location' => $location,
|
|
|
|
'id' => $id_created,
|
|
|
|
'channelType' => 'Emergency',
|
2017-12-08 02:46:42 +00:00
|
|
|
'channelName' => "$URL_0"
|
2017-12-07 22:40:05 +00:00
|
|
|
]);
|
|
|
|
|
2017-12-08 02:18:29 +00:00
|
|
|
/* (3) Close socket */
|
2017-12-07 22:40:05 +00:00
|
|
|
$wsi->close();
|
|
|
|
|
2017-12-08 02:18:29 +00:00
|
|
|
|
2017-12-08 02:39:29 +00:00
|
|
|
return ['sent' => intval($id_created) ];
|
2017-12-07 22:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function DELETE_emergency($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$wsi = new wsinterop("localhost",9998);
|
|
|
|
|
|
|
|
$wsi->send([
|
|
|
|
"operation" => "DelMessage",
|
|
|
|
"id" => $id,
|
2017-12-07 19:24:59 +00:00
|
|
|
"channelType" => "Emergency",
|
2017-12-07 20:06:09 +00:00
|
|
|
"channelName" => is_null($URL_0) ? "" : "$URL_0"
|
2017-12-07 19:24:59 +00:00
|
|
|
]);
|
|
|
|
|
2017-12-07 20:06:09 +00:00
|
|
|
$wsi->close();
|
|
|
|
|
2017-12-07 19:24:59 +00:00
|
|
|
return ["sent" => true];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
2017-12-07 23:43:58 +00:00
|
|
|
public function PUT_emergency($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$wsi = new wsinterop("localhost",9998);
|
|
|
|
|
|
|
|
$wsi->send([
|
|
|
|
"operation" => "UpdMessage",
|
|
|
|
"id" => $id,
|
|
|
|
"message" => $message,
|
|
|
|
"channelType" => "Emergency",
|
|
|
|
"channelName" => is_null($URL_0) ? "" : "$URL_0"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$wsi->close();
|
|
|
|
|
|
|
|
return ["sent" => true];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
2017-12-08 00:56:21 +00:00
|
|
|
public function POST_event($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$wsi = new wsinterop("localhost",9998);
|
|
|
|
|
|
|
|
$wsi->send([
|
|
|
|
"operation" => "PostMessage",
|
|
|
|
"message" => $message,
|
2017-12-08 01:54:21 +00:00
|
|
|
"username" => $_SESSION['NAME'],
|
2017-12-08 00:56:21 +00:00
|
|
|
"location" => $location,
|
|
|
|
"type" => $type,
|
|
|
|
//TODO implémenter la récupération d'id depuis la bdd
|
|
|
|
"id" => uniqid(),
|
|
|
|
"channelType" => "Event",
|
|
|
|
"channelName" => is_null($URL_0) ? "" : "$URL_0"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$wsi->close();
|
|
|
|
|
|
|
|
return ["sent" => true];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
|
|
|
public function DELETE_event($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$wsi = new wsinterop("localhost",9998);
|
|
|
|
|
|
|
|
$wsi->send([
|
|
|
|
"operation" => "DelMessage",
|
|
|
|
"id" => $id,
|
|
|
|
"channelType" => "Event",
|
|
|
|
"channelName" => is_null($URL_0) ? "" : "$URL_0"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$wsi->close();
|
|
|
|
|
|
|
|
return ["sent" => true];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
|
|
|
public function PUT_event($argv){
|
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
$wsi = new wsinterop("localhost",9998);
|
|
|
|
|
|
|
|
$wsi->send([
|
|
|
|
"operation" => "UpdMessage",
|
|
|
|
"id" => $id,
|
|
|
|
"message" => $message,
|
|
|
|
"channelType" => "Event",
|
|
|
|
"channelName" => is_null($URL_0) ? "" : "$URL_0"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$wsi->close();
|
|
|
|
|
|
|
|
return ["sent" => true];
|
|
|
|
//TODO: tout poster dans la BDD
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:24:59 +00:00
|
|
|
}
|