diff --git a/RestControllers/postsController.php b/RestControllers/postsController.php index 5609d59..15753f7 100644 --- a/RestControllers/postsController.php +++ b/RestControllers/postsController.php @@ -327,7 +327,32 @@ class postsController { } - + /** + * Change the visibility level of a post + * + * @url POST /posts/set_visibility_level + */ + public function set_visibility_level(){ + + user_login_required(); + + //Get the post ID + $postID = getPostPostID("postID"); + + //Get the visibility level + $new_visibility = $this->getPostVisibilityLevel("new_level"); + + //Check if the user is allowed to change the visibility level of the post + if(CS::get()->components->posts->access_level($postID, userID) != Posts::FULL_ACCESS) + Rest_fatal_error(401, "You are not allowed to change the visibility level of this post !"); + + //Try to update visibility level + if(!CS::get()->components->posts->update_level($postID, $new_visibility)) + Rest_fatal_error(500, "Couldn't update visibility level !"); + + //Success + return array("success" => "The visibility level has been updated !"); + } /** diff --git a/classes/components/posts.php b/classes/components/posts.php index a22b127..427019c 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -358,6 +358,28 @@ class Posts { return $postID; } + /** + * Update the visibility level of a post + * + * @param int $postID The ID of the post to update + * @param int $level The new level for the post + * @return bool TRUE in case of success / FALSE in case of failure + */ + public function update_level(int $postID, int $level) : bool { + + //Set the new values + $new_values = array( + "niveau_visibilite" => $level + ); + + //Set the conditions + $conditions = "ID = ?"; + $condValues = array($postID); + + //Perform the request + return CS::get()->db->updateDB($this::TABLE_NAME, $conditions, $new_values, $condValues); + } + /** * Fetch a single post from the database *