2017-06-10 08:07:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Conversations component
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
class conversations {
|
|
|
|
|
2017-06-10 13:04:45 +00:00
|
|
|
/**
|
2017-06-16 17:08:58 +00:00
|
|
|
* @var String $conversationsListTable Name of the conversation list table
|
2017-06-10 13:04:45 +00:00
|
|
|
*/
|
2017-06-16 17:08:58 +00:00
|
|
|
private $conversationsListTable;
|
2017-06-10 13:04:45 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-16 17:08:58 +00:00
|
|
|
* @var String $conversationsUsersTable Name of the conversation users table
|
2017-06-10 13:04:45 +00:00
|
|
|
*/
|
2017-06-16 17:08:58 +00:00
|
|
|
private $conversationsUsersTable;
|
2017-06-10 13:04:45 +00:00
|
|
|
|
2017-06-24 08:05:16 +00:00
|
|
|
/**
|
|
|
|
* @var String $conversationMessagesTabel Name of the conversation messages table
|
|
|
|
*/
|
2017-06-25 16:09:18 +00:00
|
|
|
private $conversationsMessagesTable;
|
2017-06-24 08:05:16 +00:00
|
|
|
|
2017-06-10 13:04:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public constructor
|
|
|
|
*/
|
|
|
|
public function __construct(){
|
2017-06-16 17:08:58 +00:00
|
|
|
$this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list";
|
|
|
|
$this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users";
|
2017-06-25 16:09:18 +00:00
|
|
|
$this->conversationsMessagesTable = CS::get()->config->get("dbprefix")."conversations_messages";
|
2017-06-10 13:04:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 13:09:54 +00:00
|
|
|
/**
|
|
|
|
* Get the conversations list of a specified user
|
2017-06-16 17:08:58 +00:00
|
|
|
* or get informations of a specific conversation
|
2017-06-11 13:09:54 +00:00
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user to get the list
|
|
|
|
* @param int $conversationID Optionnal, the ID of conversation to get informatios from
|
2017-06-11 13:09:54 +00:00
|
|
|
* @return Mixed Array in case of result / False else
|
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function getList(int $userID, int $conversationID = 0){
|
2017-06-11 13:09:54 +00:00
|
|
|
|
|
|
|
//Prepare database request
|
2017-06-16 17:08:58 +00:00
|
|
|
$tablesName = $this->conversationsListTable.", ".$this->conversationsUsersTable;
|
2017-06-11 13:09:54 +00:00
|
|
|
|
|
|
|
//Prepare conditions
|
2017-06-16 17:08:58 +00:00
|
|
|
$tableJoinCondition = $this->conversationsListTable.".ID = ".$this->conversationsUsersTable.".ID_".$this->conversationsListTable."";
|
|
|
|
$userCondition = $this->conversationsUsersTable.".ID_utilisateurs = ?";
|
|
|
|
$orderResults = "ORDER BY ".$this->conversationsListTable.".last_active DESC";
|
2017-06-11 13:09:54 +00:00
|
|
|
|
2017-06-16 17:08:58 +00:00
|
|
|
//Specify conditions values
|
2017-06-11 13:09:54 +00:00
|
|
|
$conditionsValues = array($userID);
|
2017-06-16 17:08:58 +00:00
|
|
|
|
|
|
|
//Check if we have to get informations about just one conversation
|
|
|
|
if($conversationID != 0){
|
|
|
|
$specificConditions = "AND ".$this->conversationsListTable.".ID = ?";
|
|
|
|
$conditionsValues[] = $conversationID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$specificConditions = ""; //Nothing now
|
|
|
|
|
|
|
|
//Compile conditions
|
|
|
|
$conditions = "WHERE ".$tableJoinCondition." AND (".$userCondition.") ".$specificConditions." ".$orderResults;
|
2017-06-11 13:09:54 +00:00
|
|
|
|
|
|
|
//Fields list
|
|
|
|
$requiredFields = array(
|
2017-06-16 17:08:58 +00:00
|
|
|
$this->conversationsListTable.".ID",
|
|
|
|
$this->conversationsListTable.".last_active",
|
|
|
|
$this->conversationsListTable.".name",
|
|
|
|
$this->conversationsListTable.".ID_utilisateurs AS ID_owner",
|
|
|
|
$this->conversationsUsersTable.".following",
|
|
|
|
$this->conversationsUsersTable.".saw_last_message",
|
2017-06-11 13:09:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
//Perform database request
|
|
|
|
$results = CS::get()->db->select($tablesName, $conditions, $conditionsValues, $requiredFields);
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if($results === false)
|
|
|
|
return false; //An error occurred
|
|
|
|
|
|
|
|
//Process results
|
|
|
|
$conversationsList = array();
|
|
|
|
foreach($results as $processConversation){
|
|
|
|
$conversationsList[] = array(
|
|
|
|
"ID" => $processConversation["ID"],
|
|
|
|
"ID_owner" => $processConversation["ID_owner"],
|
|
|
|
"last_active" => $processConversation["last_active"],
|
|
|
|
"name" => ($processConversation["name"] == "" ? false : $processConversation["name"]),
|
|
|
|
"following" => $processConversation["following"],
|
2017-06-13 09:11:17 +00:00
|
|
|
"saw_last_message" => $processConversation["saw_last_message"],
|
|
|
|
|
|
|
|
//Get and add conversation members
|
2017-06-13 09:35:30 +00:00
|
|
|
"members" => $this->getConversationMembers($processConversation["ID"]),
|
2017-06-11 13:09:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return results
|
|
|
|
return $conversationsList;
|
|
|
|
}
|
|
|
|
|
2017-06-16 17:08:58 +00:00
|
|
|
|
2017-06-13 09:11:17 +00:00
|
|
|
/**
|
|
|
|
* Get a conversation members
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the conversation
|
|
|
|
* @return array A list of the conversation members (empty arary may means that an error occured)
|
2017-06-13 09:11:17 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function getConversationMembers(int $conversationID) : array {
|
2017-06-13 09:11:17 +00:00
|
|
|
|
|
|
|
//Perform a request on the database
|
2017-06-16 17:08:58 +00:00
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ?";
|
2017-06-13 09:11:17 +00:00
|
|
|
$conditionsValues = array($conversationID*1);
|
|
|
|
$getFields = array("ID_utilisateurs as userID");
|
|
|
|
|
|
|
|
//Perform the request
|
|
|
|
$results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields);
|
|
|
|
|
|
|
|
if($results === false)
|
|
|
|
return array(); //An error occured
|
|
|
|
|
|
|
|
//Process results
|
|
|
|
$membersList = array();
|
|
|
|
|
|
|
|
foreach($results as $processUser)
|
|
|
|
$membersList[] = $processUser["userID"];
|
|
|
|
|
|
|
|
//Return result
|
|
|
|
return $membersList;
|
|
|
|
}
|
|
|
|
|
2017-06-10 08:07:03 +00:00
|
|
|
/**
|
|
|
|
* Create a new conversation
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user creating the conversation
|
|
|
|
* @param bool $follow Defines if the user creating the conversation will follow it
|
|
|
|
* @param array $usersList The list of users following the conversation
|
2017-06-10 08:07:03 +00:00
|
|
|
* @param Mixed $name Optionnal, the name of the conversation
|
2017-12-29 17:01:53 +00:00
|
|
|
* @return int 0 for a fail else the ID of the newly created conversation
|
2017-06-10 08:07:03 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function create(int $userID, bool $follow, array $usersList, $name = "") : int{
|
2017-06-10 13:04:45 +00:00
|
|
|
|
|
|
|
$mainInformations = array(
|
|
|
|
"ID_utilisateurs" => $userID*1,
|
|
|
|
"name" => ($name ? $name : ""),
|
|
|
|
"last_active" => time(),
|
|
|
|
"creation_time" => time()
|
|
|
|
);
|
|
|
|
|
|
|
|
//First, insert the conversation in the main table
|
2017-06-16 17:08:58 +00:00
|
|
|
if(!CS::get()->db->addLine($this->conversationsListTable, $mainInformations))
|
2017-06-10 13:04:45 +00:00
|
|
|
return 0; //An error occured
|
|
|
|
|
|
|
|
//Get the last inserted ID
|
|
|
|
$conversationID = CS::get()->db->getLastInsertedID();
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if($conversationID == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
//Insert users registrattions
|
|
|
|
foreach($usersList as $processUser){
|
|
|
|
|
|
|
|
//Make user follow the conversation if required
|
2017-06-18 14:18:09 +00:00
|
|
|
$processUserFollowing = false;
|
2017-06-10 13:04:45 +00:00
|
|
|
if($userID == $processUser)
|
2017-06-18 14:18:09 +00:00
|
|
|
$processUserFollowing = $follow;
|
2017-06-10 13:04:45 +00:00
|
|
|
|
|
|
|
//Try to insert user in conversation
|
2017-06-18 14:18:09 +00:00
|
|
|
if(!$this->addMember($conversationID, $processUser, $processUserFollowing))
|
2017-06-10 13:04:45 +00:00
|
|
|
return 0; //Error
|
|
|
|
}
|
|
|
|
|
|
|
|
//Conversation creation is a success
|
|
|
|
return $conversationID;
|
|
|
|
}
|
2017-06-10 08:07:03 +00:00
|
|
|
|
2017-06-16 17:08:58 +00:00
|
|
|
/**
|
|
|
|
* Check if a user is a member of a conversation or not
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user to check
|
|
|
|
* @param int $conversationID The ID of the conversation to check
|
|
|
|
* @return bool True if the user belongs to the conversation
|
2017-06-16 17:08:58 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function userBelongsTo(int $userID, int $conversationID) : bool {
|
2017-06-16 17:08:58 +00:00
|
|
|
|
|
|
|
//Prepare a request on the database
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
|
|
|
|
$values = array(
|
|
|
|
$conversationID,
|
|
|
|
$userID
|
|
|
|
);
|
|
|
|
|
|
|
|
//Peform a request on the database
|
|
|
|
$result = CS::get()->db->count($tableName, $conditions, $values);
|
|
|
|
|
|
|
|
//Check if request failed
|
|
|
|
if($result === false)
|
|
|
|
return false; // An error occured
|
|
|
|
|
|
|
|
//Analyse result and return it
|
|
|
|
return $result != 0;
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:07:52 +00:00
|
|
|
/**
|
|
|
|
* Change the follow state of a user on conversation
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID to update
|
|
|
|
* @param int $conversationID The ID of the conversation
|
|
|
|
* @param bool $follow Specify if the conversation is followed or not
|
|
|
|
* @return bool True for a success
|
2017-06-18 08:07:52 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{
|
2017-06-18 08:07:52 +00:00
|
|
|
|
|
|
|
//Prepare the request on the database
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
|
|
|
|
$condVals = array(
|
|
|
|
$conversationID,
|
|
|
|
$userID
|
|
|
|
);
|
|
|
|
|
|
|
|
//Defines modifications
|
|
|
|
$modifs = array(
|
|
|
|
"following" => $follow ? 1 : 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
//Update the table
|
|
|
|
if(!CS::get()->db->updateDB($tableName, $conditions, $modifs, $condVals))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:41:43 +00:00
|
|
|
/**
|
|
|
|
* Change conversation name
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the conversation
|
|
|
|
* @param string $conversationName The name of the conversation
|
|
|
|
* @return bool True for a success
|
2017-06-18 08:41:43 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function changeName(int $conversationID, string $conversationName) : bool{
|
2017-06-18 08:41:43 +00:00
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsListTable;
|
|
|
|
$conditions = "ID = ?";
|
|
|
|
$condVals = array($conversationID);
|
|
|
|
|
|
|
|
//Changes
|
|
|
|
$changes = array(
|
|
|
|
"name" => $conversationName,
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform request
|
|
|
|
if(!CS::get()->db->updateDB($tableName, $conditions, $changes, $condVals))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-18 14:18:09 +00:00
|
|
|
/**
|
|
|
|
* Update conversation members list
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the conversation to update
|
|
|
|
* @param array $conversationMembers The new list of conversation members
|
|
|
|
* @return bool True for a success
|
2017-06-18 14:18:09 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function updateMembers(int $conversationID, array $conversationMembers) : bool{
|
2017-06-18 14:18:09 +00:00
|
|
|
|
|
|
|
//Get the current conversation list
|
|
|
|
$currentMembers = $this->getConversationMembers($conversationID);
|
|
|
|
|
|
|
|
//Determinate entries to add
|
|
|
|
$toAdd = array_diff($conversationMembers, $currentMembers);
|
|
|
|
|
|
|
|
//Determinate entries to remove
|
|
|
|
$toRemove = array_diff($currentMembers, $conversationMembers);
|
|
|
|
|
|
|
|
//Add new member
|
|
|
|
foreach($toAdd as $processInsert){
|
|
|
|
if(!$this->addMember($conversationID, $processInsert))
|
|
|
|
return false; //An error occured
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove old members
|
|
|
|
foreach($toRemove as $processDelete){
|
|
|
|
if(!$this->removeMember($conversationID, $processDelete))
|
|
|
|
return false; //An error occured
|
|
|
|
}
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a member to the list
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param int $userID The ID of the user to add to the conversation
|
|
|
|
* @param bool $follow Optionnal, specify if the user will follow or not the conversation
|
|
|
|
* @return bool True for a success
|
2017-06-18 14:18:09 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function addMember(int $conversationID, int $userID, bool $follow = false) : bool {
|
2017-06-18 14:18:09 +00:00
|
|
|
|
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$values = array(
|
|
|
|
"ID_".$this->conversationsListTable => $conversationID,
|
|
|
|
"ID_utilisateurs" => $userID,
|
|
|
|
"time_add" => time(),
|
|
|
|
"following" => $follow ? 1 : 0,
|
|
|
|
"saw_last_message" => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform request
|
|
|
|
return CS::get()->db->addLine($tableName, $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a member from the list
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param int $userID The ID of the user to remove from the conversation
|
|
|
|
* @return bool True for a success
|
2017-06-18 14:18:09 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function removeMember(int $conversationID, int $userID) : bool {
|
2017-06-18 14:18:09 +00:00
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
|
|
|
|
$values = array(
|
|
|
|
$conversationID,
|
|
|
|
$userID
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform request
|
|
|
|
return CS::get()->db->deleteEntry($tableName, $conditions, $values);
|
|
|
|
}
|
|
|
|
|
2017-06-18 08:07:52 +00:00
|
|
|
/**
|
|
|
|
* Check if a user is a conversation moderator or not
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user to check
|
|
|
|
* @param int $conversationID The ID of the conversation to check
|
|
|
|
* @return bool True if the user is a conversation moderator / false else
|
2017-06-18 08:07:52 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function userIsModerator(int $userID, int $conversationID) : bool {
|
2017-06-18 08:41:43 +00:00
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsListTable;
|
|
|
|
$conditions = "WHERE ID = ?";
|
|
|
|
$values = array($conversationID);
|
|
|
|
$requiredFields = array(
|
|
|
|
"ID_utilisateurs"
|
|
|
|
);
|
|
|
|
|
|
|
|
//Peform a request on the database
|
|
|
|
$results = CS::get()->db->select($tableName, $conditions, $values, $requiredFields);
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if($results === false)
|
|
|
|
return false; // An error occured
|
|
|
|
|
|
|
|
//Check if there was results
|
|
|
|
if(count($results) === 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check the first result only
|
|
|
|
return $results[0]["ID_utilisateurs"] == $userID;
|
2017-06-19 12:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a private conversation between two users
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $user1 The first user
|
|
|
|
* @param int $user2 The second user
|
|
|
|
* @return array The list of private conversations
|
2017-06-19 12:26:30 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function findPrivate(int $user1, int $user2) : array{
|
2017-06-19 12:26:30 +00:00
|
|
|
|
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsUsersTable." AS table1 JOIN ".
|
|
|
|
$this->conversationsUsersTable." AS table2";
|
|
|
|
|
|
|
|
//Prepare conditions
|
|
|
|
$joinCondition = "table1.ID_".$this->conversationsListTable." = table2.ID_".$this->conversationsListTable;
|
|
|
|
$whereConditions = "table1.ID_utilisateurs = ? OR table1.ID_utilisateurs = ?";
|
|
|
|
$groupCondition = "table1.ID_".$this->conversationsListTable." having count(*) = 4";
|
|
|
|
|
|
|
|
//Conditions values
|
|
|
|
$condValues = array($user1, $user2);
|
|
|
|
|
|
|
|
//Required fields
|
|
|
|
$requiredFields = array(
|
|
|
|
"table1.ID_".$this->conversationsListTable." as conversationID",
|
|
|
|
);
|
|
|
|
|
|
|
|
//Build conditions
|
|
|
|
$conditions = "ON ".$joinCondition." WHERE ".$whereConditions." GROUP BY ".$groupCondition;
|
|
|
|
|
|
|
|
//Try to perform request
|
|
|
|
$results = CS::get()->db->select($tableName, $conditions, $condValues, $requiredFields);
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if($results === false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Prepare return
|
|
|
|
$conversationsID = array();
|
|
|
|
foreach($results as $processConversation)
|
|
|
|
$conversationsID[] = $processConversation["conversationID"];
|
|
|
|
|
|
|
|
//Return result
|
|
|
|
return $conversationsID;
|
|
|
|
}
|
2017-06-18 08:07:52 +00:00
|
|
|
|
2017-06-24 08:05:16 +00:00
|
|
|
/**
|
|
|
|
* Insert a new message in the database
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user inserting the message
|
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param string $message The message to insert
|
2017-06-24 12:30:16 +00:00
|
|
|
* @param Mixed $image_path Optionnal, the path to an image associated with the message
|
2017-12-29 17:01:53 +00:00
|
|
|
* @return bool True for a success
|
2017-06-24 08:05:16 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function insertMessage(int $userID, int $conversationID, string $message, $image_path = false) : bool{
|
2017-06-24 08:05:16 +00:00
|
|
|
|
|
|
|
//Prepare values
|
2017-06-25 16:09:18 +00:00
|
|
|
$tableName = $this->conversationsMessagesTable;
|
2017-06-24 08:05:16 +00:00
|
|
|
$values = array(
|
|
|
|
"ID_".$this->conversationsListTable => $conversationID,
|
|
|
|
"ID_utilisateurs" => $userID,
|
|
|
|
"time_insert" => time(),
|
|
|
|
"message" => $message
|
|
|
|
);
|
|
|
|
|
2017-06-24 12:30:16 +00:00
|
|
|
//Add image path (if required)
|
|
|
|
if($image_path)
|
|
|
|
$values['image_path'] = $image_path;
|
|
|
|
|
2017-06-24 08:05:16 +00:00
|
|
|
//Try to insert new value in database
|
|
|
|
if(!CS::get()->db->addLine($tableName, $values))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the last time a conversation was active
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the conversation to update
|
|
|
|
* @param int $time The new time of last activity to set
|
|
|
|
* @return bool True for a success
|
2017-06-24 08:05:16 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function updateLastActive(int $conversationID, int $time) : bool{
|
2017-06-24 08:05:16 +00:00
|
|
|
|
|
|
|
//Perform a request on the database
|
|
|
|
$tableName = $this->conversationsListTable;
|
|
|
|
$conditions = "ID = ?";
|
|
|
|
$condVals = array($conversationID);
|
|
|
|
|
|
|
|
//Set new values
|
|
|
|
$newValues = array(
|
|
|
|
"last_active" => $time,
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform a request on the database
|
|
|
|
if(!CS::get()->db->updateDB($tableName, $conditions, $newValues, $condVals))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all the users of a conversation as "unread"
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the conversation to update
|
|
|
|
* @param array $exceptions Users that should not be marked as read
|
|
|
|
* @return bool True for a success
|
2017-06-24 08:05:16 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{
|
2017-06-24 08:05:16 +00:00
|
|
|
|
|
|
|
//Prepare request
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "ID_".$this->conversationsListTable." = ?";
|
|
|
|
$condVals = array($conversationID);
|
|
|
|
|
|
|
|
//Remove users exceptions
|
|
|
|
foreach($exceptions as $userID){
|
|
|
|
$conditions.= " AND ID_utilisateurs != ?";
|
|
|
|
$condVals[] = $userID;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Define new values
|
|
|
|
$newValues = array(
|
|
|
|
"saw_last_message" => 0
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform a request on the database
|
|
|
|
if(!CS::get()->db->updateDB($tableName, $conditions, $newValues, $condVals))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-26 08:38:58 +00:00
|
|
|
/**
|
|
|
|
* Mark the user of a conversation as "read" for a conversation
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user to update
|
|
|
|
* @param int $conversationID The ID of a conversation to update
|
|
|
|
* @return bool True for a success
|
2017-06-26 08:38:58 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function markUserAsRead(int $userID, int $conversationID) : bool {
|
2017-06-26 08:38:58 +00:00
|
|
|
|
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsUsersTable;
|
|
|
|
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
|
|
|
|
$condVals = array(
|
|
|
|
$conversationID,
|
|
|
|
$userID
|
|
|
|
);
|
|
|
|
|
|
|
|
//Define new values
|
|
|
|
$newValues = array(
|
|
|
|
"saw_last_message" => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
//Try to perform a request on the database
|
|
|
|
if(!CS::get()->db->updateDB($tableName, $conditions, $newValues, $condVals))
|
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:00:40 +00:00
|
|
|
/**
|
|
|
|
* Send a new message
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $userID The ID of the user sending the message
|
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param string $message The message
|
|
|
|
* @param Mixed $image_path Optionnal, define the path to an image associated with the message
|
|
|
|
* @return bool True for a success
|
2017-06-23 17:00:40 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function sendMessage(int $userID, int $conversationID, string $message, $image_path = false) : bool{
|
2017-06-23 17:00:40 +00:00
|
|
|
|
|
|
|
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
|
|
|
|
//But it manage all operations (insert message; save image; inform other users; ...)
|
|
|
|
|
2017-06-24 08:05:16 +00:00
|
|
|
//First, try to insert the message
|
2017-06-24 12:30:16 +00:00
|
|
|
if(!$this->insertMessage($userID, $conversationID, $message, $image_path))
|
2017-06-24 08:05:16 +00:00
|
|
|
return false; //An error occured
|
|
|
|
|
|
|
|
//Then, update the last activity of the conversation
|
|
|
|
if(!$this->updateLastActive($conversationID, time()))
|
|
|
|
return false; //An error occured (2)
|
|
|
|
|
|
|
|
//Then, set all the users of the conversation as unread
|
|
|
|
if(!$this->allUsersAsUnread($conversationID, array($userID)))
|
|
|
|
return false; //An error occured (3)
|
|
|
|
|
2017-06-23 17:00:40 +00:00
|
|
|
//Success
|
|
|
|
return true;
|
2017-06-24 08:05:16 +00:00
|
|
|
}
|
2017-06-23 17:00:40 +00:00
|
|
|
|
2017-06-25 16:09:18 +00:00
|
|
|
/**
|
|
|
|
* Get the last messages of a conversation
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param int $numberOfMessages The number of messages to return
|
|
|
|
* @return array The messages of the conversation
|
2017-06-25 16:09:18 +00:00
|
|
|
*/
|
2017-12-16 13:02:39 +00:00
|
|
|
public function getLastMessages(int $conversationID, int $numberOfMessages) : array {
|
2017-06-25 16:09:18 +00:00
|
|
|
|
|
|
|
//Define conditions
|
|
|
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1);
|
|
|
|
$condVals = array(
|
|
|
|
$conversationID
|
|
|
|
);
|
|
|
|
|
2017-06-26 08:27:38 +00:00
|
|
|
//Perform request
|
|
|
|
$messages = $this->getMessages($conditions, $condVals);
|
|
|
|
|
|
|
|
//Revert messages order
|
|
|
|
$messages = array_reverse($messages);
|
|
|
|
|
|
|
|
//Return messages
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the new messages of a conversation
|
|
|
|
*
|
2017-12-29 17:01:53 +00:00
|
|
|
* @param int $conversationID The ID of the target conversation
|
|
|
|
* @param int $lastMessageID The ID of the last know message
|
|
|
|
* @return array A list of messages
|
2017-06-26 08:27:38 +00:00
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
public function getNewMessages(int $conversationID, int $lastMessageID) : array {
|
2017-06-26 08:27:38 +00:00
|
|
|
|
|
|
|
//Define conditions
|
|
|
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID";
|
|
|
|
$condVals = array(
|
|
|
|
$conversationID,
|
|
|
|
$lastMessageID
|
|
|
|
);
|
|
|
|
|
|
|
|
//Perform request
|
|
|
|
$messages = $this->getMessages($conditions, $condVals);
|
|
|
|
|
|
|
|
//Return messages
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of conversation messages based on specified conditions
|
|
|
|
*
|
|
|
|
* @param String $conditions The conditions of the request
|
|
|
|
* @param Array $conditionsValues The values of the conditions (Optionnal)
|
|
|
|
* @return Array The list of messages
|
|
|
|
*/
|
2017-12-29 17:01:53 +00:00
|
|
|
private function getMessages(string $conditions, array $conditionsValues = array()) : array{
|
2017-06-26 08:27:38 +00:00
|
|
|
|
|
|
|
//Prepare database request
|
|
|
|
$tableName = $this->conversationsMessagesTable;
|
|
|
|
|
2017-06-25 17:11:00 +00:00
|
|
|
//Define required fields
|
|
|
|
$requiredFields = array(
|
|
|
|
"ID",
|
|
|
|
"ID_utilisateurs AS ID_user",
|
|
|
|
"image_path",
|
|
|
|
"message",
|
|
|
|
"time_insert"
|
|
|
|
);
|
|
|
|
|
2017-06-25 16:09:18 +00:00
|
|
|
//Try to perform request on the database
|
2017-06-26 08:27:38 +00:00
|
|
|
$messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields);
|
2017-06-25 17:11:00 +00:00
|
|
|
|
|
|
|
//Process each message
|
|
|
|
array_walk($messages, function(&$item){
|
2017-06-26 08:27:38 +00:00
|
|
|
|
|
|
|
//Check if the image of the message is not null
|
2017-06-25 17:11:00 +00:00
|
|
|
if($item["image_path"] !== null && $item["image_path"] != ""){
|
|
|
|
//Replace image name with full URL
|
|
|
|
$item["image_path"] = path_user_data($item["image_path"]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-26 08:27:38 +00:00
|
|
|
//Return result
|
2017-06-25 16:09:18 +00:00
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:03:41 +00:00
|
|
|
/**
|
|
|
|
* Check whether a conversation exists or not
|
|
|
|
*
|
|
|
|
* @param int $convID The ID of the conversation to check
|
|
|
|
* @return bool TRUE if it exists / false else
|
|
|
|
*/
|
|
|
|
public function exist(int $convID) : bool {
|
|
|
|
|
|
|
|
//Perform a request on the database
|
|
|
|
$tableName = $this->conversationsListTable;
|
|
|
|
|
|
|
|
return CS::get()->db->count($tableName, "WHERE ID = ?", array($convID)) > 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-10 08:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Register component
|
|
|
|
Components::register("conversations", new conversations());
|