From 3729b56ff46ff3f2ec0453c441c763a2e3d3531a Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 11 Jun 2017 15:09:54 +0200 Subject: [PATCH] Created get conversations list method --- RestControllers/conversationsController.php | 19 +++++++ classes/components/conversations.php | 57 ++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 2acb0cf..e589ed9 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -7,6 +7,25 @@ class conversationsController{ + /** + * Get the conversations list + * + * @url POST /conversations/getList + */ + public function getConversationsList(){ + user_login_required(); + + //Try to get the list + $conversationsList = CS::get()->components->conversations->getList(userID); + + //Check for errors + if($conversationsList === false) + Rest_fatal_error(500, "Couldn't get conversations list !"); + + //Return results + return $conversationsList; + } + /** * Create a new conversation * diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 78fbae4..b997e6a 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -26,6 +26,60 @@ class conversations { $this->conversationUsersTable = CS::get()->config->get("dbprefix")."conversations_users"; } + /** + * Get the conversations list of a specified user + * + * @param Integer $userID The ID of the user to get the list + * @return Mixed Array in case of result / False else + */ + public function getList($userID){ + + //Prepare database request + $tablesName = $this->conversationListTable.", ".$this->conversationUsersTable; + + //Prepare conditions + $tableJoinCondition = $this->conversationListTable.".ID = ".$this->conversationUsersTable.".ID_".$this->conversationListTable.""; + $userCondition = $this->conversationUsersTable.".ID_utilisateurs = ?"; + $orderResults = "ORDER BY ".$this->conversationListTable.".last_active DESC"; + + //Compile conditions + $conditions = "WHERE ".$tableJoinCondition." AND (".$userCondition.") ".$orderResults; + $conditionsValues = array($userID); + + //Fields list + $requiredFields = array( + $this->conversationListTable.".ID", + $this->conversationListTable.".last_active", + $this->conversationListTable.".name", + $this->conversationListTable.".ID_utilisateurs AS ID_owner", + $this->conversationUsersTable.".following", + $this->conversationUsersTable.".saw_last_message", + ); + + //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"], + "saw_last_message" => $processConversation["saw_last_message"] + ); + } + + //Return results + return $conversationsList; + } + /** * Create a new conversation * @@ -62,7 +116,8 @@ class conversations { $userInformations = array( "ID_".$this->conversationListTable => $conversationID, "time_add" => time(), - "saw_last_message" => 1 + "saw_last_message" => 1, + "ID_utilisateurs" => $processUser, ); //Make user follow the conversation if required