From 827fec68c70d11f550e9f4f7b078b5889bd1dd2a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 31 Aug 2018 09:56:46 +0200 Subject: [PATCH] Can delete groups. --- RestControllers/GroupsController.php | 19 ++++++++ RestControllers/PostsController.php | 6 --- classes/components/GroupsComponent.php | 32 ++++++++++++++ classes/components/notifications.php | 37 +++++++++++++++- classes/components/posts.php | 60 +++++++++++++++++++++++++- 5 files changed, 145 insertions(+), 9 deletions(-) diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index 5639563..607b768 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -706,6 +706,25 @@ class GroupsController { return array("success" => "Follow status has been successfully updated!"); } + /** + * Delete a group + * + * @url POST groups/delete + */ + public function delete(){ + + user_login_required(); + + //Get the group + $groupID = getPostGroupIdWithAccess("groupID", GroupInfo::ADMIN_ACCESS); + + //Delete the group + if(!components()->groups->delete_group($groupID)) + Rest_fatal_error(500, "Could not delete group!"); + + return array("success" => "The group has been successfully deleted!"); + } + /** * Parse a GroupInfo object into an array for the API * diff --git a/RestControllers/PostsController.php b/RestControllers/PostsController.php index 708c1b1..45cfb94 100644 --- a/RestControllers/PostsController.php +++ b/RestControllers/PostsController.php @@ -486,12 +486,6 @@ class PostsController { if(!CS::get()->components->posts->delete($postID)) Rest_fatal_error(500, "Couldn't delete post!"); - //Delete related notifications - $notification = new Notification(); - $notification->set_on_elem_type(Notification::POST); - $notification->set_on_elem_id($postID); - components()->notifications->delete($notification); - //Success return array("success" => "The post has been deleted!"); } diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 30b57f3..b43757c 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -684,6 +684,38 @@ class GroupsComponent { array($groupID, $userID)); } + /** + * Delete a group + * + * @param int $groupID Target group id + * @return bool TRUE for a success / FALSE else + */ + public function delete_group(int $groupID) : bool { + + //Delete group image + if(!$this->deleteLogo($groupID)) + return FALSE; + + //Delete all group posts + if(!components()->posts->deleteAllGroup($groupID)) + return FALSE; + + //Delete all group related notifications + if(!components()->notifications->deleteAllRelatedWithGroup($groupID)) + return FALSE; + + //Delete all group members + if(!db()->deleteEntry(self::GROUPS_MEMBERS_TABLE, "groups_id = ?", array($groupID))) + return FALSE; + + //Delete group information + if(!db()->deleteEntry(self::GROUPS_LIST_TABLE, "id = ?", array($groupID))) + return FALSE; + + //Success + return TRUE; + } + /** * Turn a database entry into a GroupInfo object * diff --git a/classes/components/notifications.php b/classes/components/notifications.php index a60b451..e3dc87e 100644 --- a/classes/components/notifications.php +++ b/classes/components/notifications.php @@ -93,7 +93,7 @@ class notificationComponent { //Determine the visibility level of the notification if($notification->get_on_elem_type() == Notification::POST){ - //Fetch post informations + //Fetch post information $info_post = components()->posts->get_single($notification->get_on_elem_id()); //Check for error @@ -402,6 +402,21 @@ class notificationComponent { } + /** + * Delete all the notifications related with a post + * + * @param int $postID The ID of the target post + * @return bool TRUE for a success / FALSE else + */ + public function deleteAllRelatedWithPost(int $postID) : bool { + + $notification = new Notification(); + $notification->set_on_elem_type(Notification::POST); + $notification->set_on_elem_id($postID); + return $this->delete($notification); + + } + /** * Delete all the notifications related with a user * @@ -421,6 +436,26 @@ class notificationComponent { } + /** + * Delete all the notifications related with a group + * + * @param int $groupID The ID of the target group + * @return bool TRUE for a success / FALSE else + */ + public function deleteAllRelatedWithGroup(int $groupID) : bool { + + //Groups membership notifications + $notification = new Notification(); + $notification->set_on_elem_type(Notification::GROUP_MEMBERSHIP); + $notification->set_on_elem_id($groupID); + if(!$this->delete($notification)) return FALSE; + + $notification->set_on_elem_type(Notification::GROUP_PAGE); + if(!$this->delete($notification)) return FALSE; + + return TRUE; + } + /** * Convert a notification object into database array * diff --git a/classes/components/posts.php b/classes/components/posts.php index fe9b23e..95b18e3 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -327,6 +327,34 @@ class Posts { } + /** + * Get the entire list of posts of a group, ignoring visibility levels + * + * @param int $groupID The ID of the target group + * @param bool $load_comments Specify whether the comments should be loaded or not + * @return array The list of posts + */ + public function getGroupEntirePostsList(int $groupID, bool $load_comments = FALSE) : array { + + //Security + if($groupID == 0) + return array(); + + //Prepare database request + $conditions = "WHERE group_id = ?"; + $dataConds = array($groupID); + + //Perform the request + $list = CS::get()->db->select( + $this::TABLE_NAME, + $conditions, + $dataConds + ); + + //Parse and return posts (do not load comments) + return $this->processGetMultiple($list, $load_comments); + } + /** * Get the entire list of posts that uses a movie * @@ -644,13 +672,17 @@ class Posts { */ public function delete(int $postID) : bool { - //Get informations about the post + //Get information about the post $post_info = $this->get_single($postID); - //Check if we didn't get informations about the post + //Check if we didn't get information about the post if(!$post_info->isValid()) return false; + //Delete all the notifications related to the post + if(!components()->notifications->deleteAllRelatedWithPost($postID)) + return FALSE; + //Delete the likes associated to the post if(!components()->likes->delete_all($postID, Likes::LIKE_POST)) return false; @@ -765,6 +797,30 @@ class Posts { return TRUE; } + /** + * Delete all the posts of a group + * + * @param int $groupID The ID of the target group + * @return bool TRUE for a success / FALSE else + */ + public function deleteAllGroup(int $groupID) : bool { + + //Get the list of posts of the group + $posts = $this->getGroupEntirePostsList($groupID); + + //Delete the list of posts + foreach($posts as $post){ + + //Delete the posts + if(!$this->delete($post->get_id())) + return FALSE; + + } + + //Success + return TRUE; + } + /** * Process processing of multiples posts entries in database *