From 57cff401ad1437e4f7bdc687bbd747fa9e630e4e Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 13 Jan 2018 19:38:38 +0100 Subject: [PATCH] Can delete post --- classes/components/posts.php | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/classes/components/posts.php b/classes/components/posts.php index 427019c..ef0f16e 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -380,6 +380,62 @@ class Posts { return CS::get()->db->updateDB($this::TABLE_NAME, $conditions, $new_values, $condValues); } + /** + * Delete a post + * + * @param int $postID The ID of the post to delete + * @return bool TRUE for a success / FALSE for a failure + */ + public function delete(int $postID) : bool { + + //Get informations about the post + $post_infos = $this->get_single($postID); + + //Check if we didn't get informations about the post + if(count($post_infos) == 0) + return false; + + //Delete the likes associated to the post + if(!components()->likes->delete_all($postID, Likes::LIKE_POST)) + return false; + + //Delete all the comments associated to the post + if(!components()->comments->delete_all($postID)) + return false; + + //Delete the associated image or PDF (if any) + if($post_infos['kind'] == $this::POST_KIND_IMAGE + || $post_infos['kind'] == $this::POST_KIND_PDF){ + + //Determine file path + $file_path = path_user_data($post_infos["file_path"], true); + + //Check if the file exists + if(file_exists($file_path)){ + + //Try to delete it + if(!unlink($file_path)) + return false; + } + } + + //Delete the associated survey (if any) + if($post_infos['kind'] == $this::POST_KIND_SURVEY){ + + //Check the post has an associated survey + if(components()->survey->exists($postID)){ + + //Try to delete survey + if(!components()->survey->delete($postID)) + return false; + } + + } + + //Delete the post + return CS::get()->db->deleteEntry($this::TABLE_NAME, "ID = ?", array($postID)); + } + /** * Fetch a single post from the database *