From 219e26b4c005225a2691926ec5666ae1c2f8e297 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 18 Jun 2017 10:41:43 +0200 Subject: [PATCH] Conversation name can be updated --- RestControllers/conversationsController.php | 21 ++++++++- classes/components/conversations.php | 51 ++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 9c71dd7..faef9a6 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -122,8 +122,27 @@ class conversationsController{ Rest_fatal_error(500, "Couldn't update user follow state !"); } + //Check if user asked to change moderation settings + if(isset($_POST['members']) OR isset($_POST['name'])){ + + //Check if user is allowed to change such settings + if(!CS::get()->components->conversations->userIsModerator(userID, $conversationID)) + Rest_fatal_error(401, "The user isn't a moderator, he can't updates such settings !"); + + //Update conversation name (if required) + if(isset($_POST["name"])){ + $conversationName = $_POST['name'] == "false" ? "" : $_POST['name']; + + //Update conversation name + if(!CS::get()->components->conversations->changeName($conversationID, $conversationName)) + Rest_fatal_error(500, "Couldn't update conversation name !"); + } + + + + } //Success - return array("success" => "User informations were successfully updated !"); + return array("success" => "Conversation informations were successfully updated !"); } } \ No newline at end of file diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 24781d0..5136e26 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -238,13 +238,62 @@ class conversations { return true; } + /** + * Change conversation name + * + * @param Integer $conversationID The ID of the conversation + * @param String $conversationName The name of the conversation + * @return Boolean True for a success + */ + public function changeName($conversationID, $conversationName){ + //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; + } + /** * Check if a user is a conversation moderator or not * * @param Integer $userID The ID of the user to check * @param Integer $conversationID The ID of the conversation to check - * @return Boolean True if the user is a conversation moderator or not + * @return Boolean True if the user is a conversation moderator / false else */ + public function userIsModerator($userID, $conversationID){ + //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; + } }