mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 15:59:29 +00:00
API use ConversationMessage object
This commit is contained in:
parent
f39d162277
commit
829e21d65a
@ -327,6 +327,14 @@ class ConversationsController{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Process results
|
||||||
|
foreach($conversationsMessages as $name=>$values){
|
||||||
|
|
||||||
|
foreach($conversationsMessages[$name] as $num => $message)
|
||||||
|
$conversationsMessages[$name][$num] = ConversationsController::ConvMessageToAPI($message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//Return result
|
//Return result
|
||||||
return $conversationsMessages;
|
return $conversationsMessages;
|
||||||
}
|
}
|
||||||
@ -367,6 +375,10 @@ class ConversationsController{
|
|||||||
//Specify that user has seen last messages
|
//Specify that user has seen last messages
|
||||||
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
|
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
|
||||||
|
|
||||||
|
//Process the list of messages
|
||||||
|
foreach($messages as $num => $val)
|
||||||
|
$messages[$num] = ConversationsController::ConvMessageToAPI($val);
|
||||||
|
|
||||||
//Return the messges
|
//Return the messges
|
||||||
return $messages;
|
return $messages;
|
||||||
|
|
||||||
@ -475,7 +487,7 @@ class ConversationsController{
|
|||||||
$data["ID_user"] = $message->get_userID();
|
$data["ID_user"] = $message->get_userID();
|
||||||
$data["time_insert"] = $message->get_time_sent();
|
$data["time_insert"] = $message->get_time_sent();
|
||||||
$data["message"] = $message->has_message() ? $message->get_message() : "";
|
$data["message"] = $message->has_message() ? $message->get_message() : "";
|
||||||
$data["image_path"] = $message->has_image_path() ? $message->get_image_path() : null;
|
$data["image_path"] = $message->has_image_path() ? $message->get_image_url() : null;
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ class Conversations {
|
|||||||
public function sendMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{
|
public function sendMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{
|
||||||
|
|
||||||
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
|
//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; ...)
|
//But it manages all operations (insert message; save image; inform other users; ...)
|
||||||
|
|
||||||
//First, try to insert the message
|
//First, try to insert the message
|
||||||
if(!$this->insertMessage($userID, $conversationID, $message, $image_path))
|
if(!$this->insertMessage($userID, $conversationID, $message, $image_path))
|
||||||
@ -636,7 +636,7 @@ class Conversations {
|
|||||||
|
|
||||||
//Delete each message
|
//Delete each message
|
||||||
foreach($messages as $message){
|
foreach($messages as $message){
|
||||||
if(!$this->delete_message($message['ID'], $convID))
|
if(!$this->delete_message($message))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -672,7 +672,7 @@ class Conversations {
|
|||||||
|
|
||||||
//Delete each message
|
//Delete each message
|
||||||
foreach($messages as $message){
|
foreach($messages as $message){
|
||||||
if(!$this->delete_message($message['ID'], $convID))
|
if(!$this->delete_message($message))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -688,28 +688,16 @@ class Conversations {
|
|||||||
/**
|
/**
|
||||||
* Delete a single message of a conversation
|
* Delete a single message of a conversation
|
||||||
*
|
*
|
||||||
* @param int $messageID The ID of the message to delete
|
* @param ConversationMessage $message The message to delete
|
||||||
* @param int $convID The target conversation
|
|
||||||
* @param array $informations Optionnal, informations about the message
|
|
||||||
* @return bool True in case of success / false else
|
* @return bool True in case of success / false else
|
||||||
*/
|
*/
|
||||||
private function delete_message(int $messageID, int $convID, array $informations = null) : bool {
|
private function delete_message(ConversationMessage $message) : bool {
|
||||||
|
|
||||||
//Check if we have to fetch informations about the message
|
|
||||||
if(is_null($informations)){
|
|
||||||
$messages = $this->getMessages("WHERE ID_".$this->conversationsListTable." = ? AND ID = ?", array($convID, $messageID), false);
|
|
||||||
|
|
||||||
if(count($messages) == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
$informations = $messages[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
//Check if we have to delete an image
|
//Check if we have to delete an image
|
||||||
if(!is_null($informations["image_path"]) AND $informations["image_path"] !== ""){
|
if($message->has_image_path()){
|
||||||
|
|
||||||
//Get system path of the image
|
//Get system path of the image
|
||||||
$img_sys_path = path_user_data($informations["image_path"], true);
|
$img_sys_path = path_user_data($message->get_image_path(), true);
|
||||||
|
|
||||||
if(file_exists($img_sys_path)){
|
if(file_exists($img_sys_path)){
|
||||||
unlink($img_sys_path);
|
unlink($img_sys_path);
|
||||||
@ -719,7 +707,7 @@ class Conversations {
|
|||||||
|
|
||||||
//Delete message from the database
|
//Delete message from the database
|
||||||
$conditions = "ID = ?";
|
$conditions = "ID = ?";
|
||||||
$condValues = array($messageID);
|
$condValues = array($message->get_id());
|
||||||
return CS::get()->db->deleteEntry($this->conversationsMessagesTable, $conditions, $condValues);
|
return CS::get()->db->deleteEntry($this->conversationsMessagesTable, $conditions, $condValues);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -811,11 +799,10 @@ class Conversations {
|
|||||||
* Get a list of conversation messages based on specified conditions
|
* Get a list of conversation messages based on specified conditions
|
||||||
*
|
*
|
||||||
* @param string $conditions The conditions of the request
|
* @param string $conditions The conditions of the request
|
||||||
* @param Array $conditionsValues The values of the conditions (Optionnal)
|
* @param array $conditionsValues The values of the conditions (Optionnal)
|
||||||
* @param bool $transformPath Transform the path of the files into URLS (true by default)
|
* @return array The list of messages as ConversationMessage objects
|
||||||
* @return Array The list of messages
|
|
||||||
*/
|
*/
|
||||||
private function getMessages(string $conditions, array $conditionsValues = array(), bool $transformPath = true) : array{
|
private function getMessages(string $conditions, array $conditionsValues = array()) : array{
|
||||||
|
|
||||||
//Prepare database request
|
//Prepare database request
|
||||||
$tableName = $this->conversationsMessagesTable;
|
$tableName = $this->conversationsMessagesTable;
|
||||||
@ -833,16 +820,8 @@ class Conversations {
|
|||||||
$messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields);
|
$messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields);
|
||||||
|
|
||||||
//Process each message
|
//Process each message
|
||||||
if($transformPath){
|
foreach($messages as $num=>$message)
|
||||||
array_walk($messages, function(&$item){
|
$messages[$num] = self::dbToConvMessage($message);
|
||||||
|
|
||||||
//Check if the image of the message is not null
|
|
||||||
if($item["image_path"] !== null && $item["image_path"] != ""){
|
|
||||||
//Replace image name with full URL
|
|
||||||
$item["image_path"] = path_user_data($item["image_path"]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Return result
|
//Return result
|
||||||
return $messages;
|
return $messages;
|
||||||
@ -871,6 +850,28 @@ class Conversations {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a conversation message into ConversationMessage object
|
||||||
|
*
|
||||||
|
* @param array $entry Conversation entry in the database
|
||||||
|
* @return ConversationMessage Generated ConversationMessage object
|
||||||
|
*/
|
||||||
|
private function dbToConvMessage(array $entry) : ConversationMessage {
|
||||||
|
|
||||||
|
$message = new ConversationMessage();
|
||||||
|
|
||||||
|
$message->set_id($entry["ID"]);
|
||||||
|
$message->set_userID($entry["ID_user"]);
|
||||||
|
$message->set_time_sent($entry["time_insert"]);
|
||||||
|
if($entry["image_path"] != null)
|
||||||
|
$message->set_image_path($entry["image_path"]);
|
||||||
|
if($entry["message"] != null)
|
||||||
|
$message->set_message($entry["message"]);
|
||||||
|
|
||||||
|
return $message;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Register component
|
//Register component
|
||||||
|
@ -24,6 +24,10 @@ class ConversationMessage extends BaseUniqueObjectFromUser {
|
|||||||
return $this->image_path != null ? $this->image_path : "null";
|
return $this->image_path != null ? $this->image_path : "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_image_url() : string {
|
||||||
|
return path_user_data($this->get_image_path());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Set and get message
|
//Set and get message
|
||||||
public function set_message(string $message){
|
public function set_message(string $message){
|
||||||
|
Loading…
Reference in New Issue
Block a user