|
|
@ -27,59 +27,45 @@ class ConversationGateway{
|
|
|
|
///(with all the id of the messages and the users in the conversation)
|
|
|
|
///(with all the id of the messages and the users in the conversation)
|
|
|
|
/// Parameters : * $idUser (string): identifier of the user we want to get the conversations
|
|
|
|
/// Parameters : * $idUser (string): identifier of the user we want to get the conversations
|
|
|
|
public function getConversations(string $_idUser):?array{
|
|
|
|
public function getConversations(string $_idUser):?array{
|
|
|
|
$tabIdConversation=NULL;
|
|
|
|
// Declaration of arrays (NULL) and queries
|
|
|
|
$tabConversations=NULL;
|
|
|
|
$tabConversations=NULL;
|
|
|
|
$tabUsers=NULL;
|
|
|
|
$tabUsers=NULL;
|
|
|
|
$tabIdMessages=NULL;
|
|
|
|
|
|
|
|
$tabMessages=NULL;
|
|
|
|
$tabMessages=NULL;
|
|
|
|
|
|
|
|
$conversationQuery = "SELECT c.id, c.nom
|
|
|
|
$query1 = "SELECT idConversation FROM Use WHERE idUser=:idUser";
|
|
|
|
FROM T_E_CONVERSATION_COV c, T_J_DISCUTE_DIS d
|
|
|
|
$query2 = "SELECT idUser FROM Use WHERE idConversation=:idConv";
|
|
|
|
WHERE c.id=d.idConv
|
|
|
|
$query3 = "SELECT idMessage FROM Have WHERE idConversation=:idConv";
|
|
|
|
AND d.idUser=:idUser";
|
|
|
|
$query4 = "SELECT id, message, idSender FROM Message WHERE id=:id";
|
|
|
|
$messagesQuery = "SELECT m.id, m.message, m.idSender
|
|
|
|
$query5 = "SELECT id, nom FROM Conversation WHERE id=:idConv";
|
|
|
|
FROM T_R_MESSAGE_MSG m, T_J_DISCUTE_DIS d
|
|
|
|
|
|
|
|
WHERE m.id=h.idMessage
|
|
|
|
$arg1=array('idUser'=>array($_idUser, PDO::PARAM_STR));
|
|
|
|
AND h.idConv=:idConv";
|
|
|
|
|
|
|
|
$usersQuery = "SELECT d.idUser
|
|
|
|
$this->connection->execQuery($query1,$arg1);
|
|
|
|
FROM T_J_DISCUTE_DIS d
|
|
|
|
|
|
|
|
WHERE d.idConv = :idConv";
|
|
|
|
|
|
|
|
//Find all the conversations where the user belong
|
|
|
|
|
|
|
|
$argIdUser=array('idUser'=>array($_idUser, PDO::PARAM_STR));
|
|
|
|
|
|
|
|
$this->connection->execQuery($conversationQuery,$argIdUser);
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
foreach($res as $row){
|
|
|
|
|
|
|
|
$tabIdConversation[] = $row['idConversation'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach($tabIdConversation as $idConv){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$arg2 = array('idConv'=>array($idConv, PDO::PARAM_STR));
|
|
|
|
foreach($res as $row){
|
|
|
|
$this->connection->execQuery($query2,$arg2);
|
|
|
|
$argIdConv= array('idConv'=>array($row['idConversation'], PDO::PARAM_STR));
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
// Find all messages of the conversation
|
|
|
|
foreach($res as $row){
|
|
|
|
$this->connection->execQuery($messagesQuery,$argIdConv);
|
|
|
|
$tabUsers[] = $row['idUser'];
|
|
|
|
$resMessages=$this->connection->getRes();
|
|
|
|
}
|
|
|
|
foreach($resMessages as $rowMessages){
|
|
|
|
|
|
|
|
$tabUsers[] = new Message($rowMessages['id'],$rowMessages['message'],$rowMessages['idSender']);
|
|
|
|
$this->connection->execQuery($query3,$arg2);
|
|
|
|
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
|
|
|
|
foreach($res as $row){
|
|
|
|
|
|
|
|
$tabIdMessages[] = $row['idMessage'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach($tabIdMessages as $idMessage){
|
|
|
|
|
|
|
|
$arg3=array('id'=>array($idMessage,PDO::PARAM_STR));
|
|
|
|
|
|
|
|
$this->connection->execQuery($query4,$arg3);
|
|
|
|
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
|
|
|
|
foreach($res as $row){
|
|
|
|
|
|
|
|
$tabMessages[] = new Message($row['id'],$row['message'],$row['idSender']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all the users in the conversation
|
|
|
|
$this->connection->execQuery($query5,$arg2);
|
|
|
|
$this->connection->execQuery($usersQuery,$argIdConv);
|
|
|
|
$res=$this->connection->getRes();
|
|
|
|
$resUsers=$this->connection->getRes();
|
|
|
|
foreach($res as $row){
|
|
|
|
foreach($resUsers as $rowUsers){
|
|
|
|
$tabConversations[]= new Conversation($row['id'], $row['nom'],$tabMessages,$tabUsers);
|
|
|
|
$tabUsers[] = $rowUsers['idUser'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the conversation into the array
|
|
|
|
|
|
|
|
$tabConversations = new Conversation($row['id'],$row['nom'],$tabMessages,$tabUsers);
|
|
|
|
|
|
|
|
// Restore the arrays
|
|
|
|
$tabUsers=array();
|
|
|
|
$tabUsers=array();
|
|
|
|
$tabIdMessages=array();
|
|
|
|
$tabMessages=array();
|
|
|
|
$tabMessages=array();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tabConversations;
|
|
|
|
return $tabConversations;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -87,16 +73,17 @@ class ConversationGateway{
|
|
|
|
/// Brief : Adding a new conversation in database
|
|
|
|
/// Brief : Adding a new conversation in database
|
|
|
|
/// Parameters : * $c (Conversation): conversation we want to insert in database
|
|
|
|
/// Parameters : * $c (Conversation): conversation we want to insert in database
|
|
|
|
public function postConversation(Conversation $c): void{
|
|
|
|
public function postConversation(Conversation $c): void{
|
|
|
|
$query1 = "INSERT INTO Conversation VALUES(:idConv,:name)";
|
|
|
|
// Declare queries
|
|
|
|
$query2 = "INSERT INTO Use VALUES(:idUser,:idConv)";
|
|
|
|
$convCreationQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES(:idConv,:name)";
|
|
|
|
|
|
|
|
$addUserInConvQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)";
|
|
|
|
$arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR),
|
|
|
|
$argconvCreationQuery = array('idConv'=>array($c->id,PDO::PARAM_STR),
|
|
|
|
'name'=>array($c->name, PDO::PARAM_STR));
|
|
|
|
'name'=>array($c->name, PDO::PARAM_STR));
|
|
|
|
|
|
|
|
|
|
|
|
$this->connection->execQuery($query1,$arg1);
|
|
|
|
// Create a new conversation
|
|
|
|
|
|
|
|
$this->connection->execQuery($convCreationQuery,$argconvCreationQuery);
|
|
|
|
|
|
|
|
// Add users of the conversation in the conversation
|
|
|
|
foreach($c->listIdUsers as $idUsr){
|
|
|
|
foreach($c->listIdUsers as $idUsr){
|
|
|
|
$arg2 = array('idUser'=>array($idUsr, PDO::PARAM_STR),
|
|
|
|
$argUserInConvQuery = array('idUser'=>array($idUsr, PDO::PARAM_STR),
|
|
|
|
'idConv'=>array($c->id, PDO::PARAM_STR));
|
|
|
|
'idConv'=>array($c->id, PDO::PARAM_STR));
|
|
|
|
$this->connection->execQuery($query2,$arg2);
|
|
|
|
$this->connection->execQuery($query2,$arg2);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -105,64 +92,53 @@ class ConversationGateway{
|
|
|
|
/// Brief : Modifying an EXISTING match in database
|
|
|
|
/// Brief : Modifying an EXISTING match in database
|
|
|
|
/// Parameters : * $u (Matchs): match we want to update in database
|
|
|
|
/// Parameters : * $u (Matchs): match we want to update in database
|
|
|
|
public function putConversation(Conversation $c):void{
|
|
|
|
public function putConversation(Conversation $c):void{
|
|
|
|
$query7 = "SELECT idMessage FROM Have WHERE idConversation=:idConv";
|
|
|
|
// Declare the queries
|
|
|
|
$query8 = "DELETE FROM Message WHERE id = :id";
|
|
|
|
$conversationInsertionQuery = "INSERT INTO T_E_CONVERSATION_COV VALUES (:id,:nom)";
|
|
|
|
$query1 = "DELETE FROM Have WHERE idConversation = :idConv";
|
|
|
|
$messageInsertionQuery = "INSERT INTO T_R_MESSAGE_MSG VALUES(:id,:message,:idSender)";
|
|
|
|
$query2 = "DELETE FROM Use WHERE idConversation = :idConv";
|
|
|
|
$discuteInsertionQuery = "INSERT INTO T_J_DISCUTE_DIS VALUES(:idUser,:idConv)";
|
|
|
|
$query3 = "UPDATE Conversation SET nom=:nom WHERE id=:id";
|
|
|
|
$containInsertionQuery = "INSERT INTO T_J_CONTAIN_MESSAGE_CTN VALUES(:idConv,:idMessage)";
|
|
|
|
$query4 = "INSERT INTO Have VALUES (:idConv,:idMessage)";
|
|
|
|
$argConversationInsertion = array('id'=>array($c->id, PDO::PARAM_STR),
|
|
|
|
$query5 = "INSERT INTO Message VALUES(:id,:message,:idSender)";
|
|
|
|
'nom'=>array($c->name,PDO::PARAM_STR));
|
|
|
|
$query6 = "INSERT INTO Use VALUES(:idUsr,:idConv)";
|
|
|
|
// Delete current data from database
|
|
|
|
|
|
|
|
deleteConversation($c);
|
|
|
|
|
|
|
|
// Add conversation
|
|
|
|
$arg1 = array('idConv'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
$this->connection->execQuery($conversationInsertionQuery,$argConversationInsertion);
|
|
|
|
$arg2 = array('nom'=>array($c->name, PDO::PARAM_STR),
|
|
|
|
// Add messages to conversation
|
|
|
|
'id'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->connection->execQuery($query7,$arg1);
|
|
|
|
|
|
|
|
$res = $this->connection->getRes();
|
|
|
|
|
|
|
|
foreach($res as $idMsg){
|
|
|
|
|
|
|
|
$arg6 = array('id'=>array($idMsg['idMessage'],PDO::PARAM_STR));
|
|
|
|
|
|
|
|
$this->connection->execQuery($query8,$arg6);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->connection->execQuery($query1,$arg1);
|
|
|
|
|
|
|
|
$this->connection->execQuery($query2, $arg1);
|
|
|
|
|
|
|
|
$this->connection->execQuery($query3,$arg2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach($c->listMessages as $msg){
|
|
|
|
foreach($c->listMessages as $msg){
|
|
|
|
$arg3 = array('idConv'=>array($c->id,PDO::PARAM_STR),
|
|
|
|
$argContainInsertion = array('idConv'=>array($c->id,PDO::PARAM_STR),
|
|
|
|
'idMessage'=>array($msg->id,PDO::PARAM_STR));
|
|
|
|
'idMessage'=>array($msg->id,PDO::PARAM_STR));
|
|
|
|
$arg4 = array('id'=>array($msg->id,PDO::PARAM_STR),
|
|
|
|
$argMessageInsertion = array('id'=>array($msg->id,PDO::PARAM_STR),
|
|
|
|
'message'=>array($msg->message,PDO::PARAM_STR),
|
|
|
|
'message'=>array($msg->message,PDO::PARAM_STR),
|
|
|
|
'idSender'=>array($msg->idSender,PDO::PARAM_STR));
|
|
|
|
'idSender'=>array($msg->idSender,PDO::PARAM_STR));
|
|
|
|
$this->connection->execQuery($query4,$arg3);
|
|
|
|
$this->connection->execQuery($containInsertionQuery,$argContainInsertion);
|
|
|
|
$this->connection->execQuery($query5,$arg4);
|
|
|
|
$this->connection->execQuery($messageInsertionQuery,$argMessageInsertion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add user to conversation
|
|
|
|
foreach($c->listIdUsers as $idUsr){
|
|
|
|
foreach($c->listIdUsers as $idUsr){
|
|
|
|
$arg5 = array('idUsr'=>array($idUsr,PDO::PARAM_STR),
|
|
|
|
$argDiscuteInsertion = array('idUsr'=>array($idUsr,PDO::PARAM_STR),
|
|
|
|
'idConv'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
'idConv'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
$this->connection->execQuery($query6,$arg5);
|
|
|
|
$this->connection->execQuery($discuteInsertionQuery,$argDiscuteInsertion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Brief : Deleting a conversation and its messages from database
|
|
|
|
/// Brief : Deleting a conversation and its messages from database
|
|
|
|
/// Parameters : * $c (Conversation): conversation we want to delete from database
|
|
|
|
/// Parameters : * $c (Conversation): conversation we want to delete from database
|
|
|
|
|
|
|
|
// ----
|
|
|
|
|
|
|
|
// Ne pas oublier le on delete cascade dans la création des tables
|
|
|
|
|
|
|
|
// ----
|
|
|
|
public function deleteConversation(Conversation $c):void{
|
|
|
|
public function deleteConversation(Conversation $c):void{
|
|
|
|
$query1 = "DELETE FROM Message WHERE id=:id";
|
|
|
|
// Declare query and argument table
|
|
|
|
$query2 = "DELETE FROM Have WHERE idConversation = :idConv";
|
|
|
|
$deleteMessagesQuery = "DELETE FROM T_R_MESSAGE_MSG
|
|
|
|
$query3 = "DELETE FROM Use WHERE idConversation = :idConv";
|
|
|
|
WHERE id=(SELECT id
|
|
|
|
$query4 = "DELETE FROM Conversation WHERE id = :idConv";
|
|
|
|
FROM T_R_MESSAGE_MSG m, T_J_CONTAIN_MESSAGE_CTN c
|
|
|
|
|
|
|
|
WHERE m.id = c.idConversation
|
|
|
|
foreach($c->listMessages as $msg){
|
|
|
|
AND c.idConversation=:idConv";
|
|
|
|
$arg1 = array('id'=>array($msg->id,PDO::PARAM_STR));
|
|
|
|
$deleteConv = "DELETE FROM T_E_CONVERSATION_COV
|
|
|
|
$this->connection->execQuery($query1,$arg1);
|
|
|
|
WHERE id=:idConv"; // Suffisant grâce au on delete cascade (à ne pas oublier)
|
|
|
|
}
|
|
|
|
$argIdConv = array('idConv'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
$arg2 = array('idConv'=>array($c->id,PDO::PARAM_STR));
|
|
|
|
// Executing queries
|
|
|
|
$this->connection->execQuery($query2,$arg2);
|
|
|
|
$this->connection->execQuery($deleteMessagesQuery,$argIdConv);
|
|
|
|
$this->connection->execQuery($query3,$arg2);
|
|
|
|
$this->connection->execQuery($deleteConv,$argIdConv);
|
|
|
|
$this->connection->execQuery($query4,$arg2);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|