From ddd0421592d1fb630c1ef9cf67eed052c830410b Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 27 Jan 2018 18:19:30 +0100 Subject: [PATCH] Comment content can be updated. --- RestControllers/commentsController.php | 47 ++++++++++++++++++++++++++ classes/components/comments.php | 23 +++++++++++++ 2 files changed, 70 insertions(+) diff --git a/RestControllers/commentsController.php b/RestControllers/commentsController.php index 004c062..9654046 100644 --- a/RestControllers/commentsController.php +++ b/RestControllers/commentsController.php @@ -7,6 +7,29 @@ class commentsController { + /** + * Edit a comment content + * + * @url POST /comments/edit + */ + public function edit_comment(){ + + user_login_required(); + + //Get comment ID + $commentID = $this->getPostCommentIDWithFullAccess("commentID"); + + //Get comment content$ + $new_content = $this->get_comment_content("content"); + + //Update comment content + if(!components()->comments->edit($commentID, $new_content)) + Rest_fatal_error(500, "Could not update comment content !"); + + //Success + return array("success" => "The comment has been updated !"); + } + /** * Delete a comment * @@ -47,4 +70,28 @@ class commentsController { //Return comment ID return $commentID; } + + /** + * Get a comment content from $_POST field + * + * @param string $name The name of post field containing the commment content + * @return string The comment content, if it passed security checks + */ + private function get_comment_content(string $name) : string { + + //Get comment content + if(!isset($_POST[$name])) + Rest_fatal_error(400, "Please specify the new content of the comment!"); + $comment_content = (string) $_POST[$name]; + + //Perform security check + if(!check_string_before_insert($comment_content)) + Rest_fatal_error(400, "Please check new comment content !"); + + //Make the comment secure before insertion + $comment_content = removeHTMLnodes($comment_content); + + //Return comment conent + return $comment_content; + } } \ No newline at end of file diff --git a/classes/components/comments.php b/classes/components/comments.php index f5e7176..45d936b 100644 --- a/classes/components/comments.php +++ b/classes/components/comments.php @@ -153,6 +153,29 @@ class Comments { } + /** + * Edit a comment content + * + * @param int $commentID The ID of the comment to update + * @param string $content The new content for the comment + * @return bool TRUE for a success / FALSE else + */ + public function edit(int $commentID, string $content) : bool { + + //Perform a request on the database + $newValues = array( + "commentaire" => $content + ); + + //Try to perform request + return CS::get()->db->updateDB( + $this::COMMENTS_TABLE, + "ID = ?", + $newValues, + array($commentID)); + + } + /** * Get the ID of the post associated to a comment *