From 6d96091c3fe639998e50c8adfb052ba3d40b0d73 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 20 Feb 2018 14:40:42 +0100 Subject: [PATCH] Added a method to delete notifications. --- RestControllers/notificationsController.php | 72 +++++++++++++++++++++ classes/components/notifications.php | 24 +++++++ 2 files changed, 96 insertions(+) diff --git a/RestControllers/notificationsController.php b/RestControllers/notificationsController.php index cf438ff..cf2f935 100644 --- a/RestControllers/notificationsController.php +++ b/RestControllers/notificationsController.php @@ -50,6 +50,78 @@ class notificationsController { } + /** + * Mark a notification as seen + * + * @url POST notifications/mark_seen + */ + public function mark_seen(){ + + user_login_required(); + + //Get notification ID + $notifID = $this->getPostNotifID("notifID"); + + //Check the kind of deletion to do + $delete_similar = false; + if(isset($_POST['delete_similar'])) + $delete_similar = ($_POST['delete_similar'] === "true"); + + //Get informations about the notification + $infos_notif = components()->notifications->get_single($notifID); + + //Check for error + if(!$infos_notif->has_id()) + Rest_fatal_error(500, "An error occured while trying to process the notification !"); + + //Perform the required kind of deletion required + $notification = new Notification(); + + if(!$delete_similar){ + $notification->set_id($notifID); + } + + else { + $notification->set_on_elem_type($infos_notif->get_on_elem_type()); + $notification->set_on_elem_id($infos_notif->get_on_elem_id()); + $notification->set_dest_user_id($infos_notif->get_dest_user_id()); + } + + //Delete the notification + if(!components()->notifications->delete($notification)) + Rest_fatal_error(500, "Could not delete the notification !"); + + //Success + return array("success" => "The notification was deleted."); + + } + + + /** + * Get a valid notification ID in a POST request + * + * @param string $name The name of the post field containing the notification ID + * @return int The ID of the notification + */ + private function getPostNotifID(string $name) : int { + + user_login_required(); + + //Check the POST request + if(!isset($_POST[$name])) + Rest_fatal_error(400, "Please specify the ID of the notification in '".$name."' !"); + + $notifID = (int) $_POST[$name]; + + //Check if the notification exists and if the user is allowed to use it + $notif = new Notification(); + $notif->set_dest_user_id(userID); + $notif->set_id($notifID); + if(!components()->notifications->similar_exists($notif)) + Rest_fatal_error(404, "The specified notification was not found !"); + + return $notifID; + } /** * Turn a notification entry into a standard API notification array diff --git a/classes/components/notifications.php b/classes/components/notifications.php index 325dd50..8998256 100644 --- a/classes/components/notifications.php +++ b/classes/components/notifications.php @@ -54,6 +54,30 @@ class notificationComponent { return $list; } + /** + * Get the informations about a single notification + * + * @param int $notifID The target notification ID + * @return Notification The target notification + */ + public function get_single(int $notifID) : Notification { + + //Perform database request + $conditions = "WHERE id = ?"; + $values = array($notifID); + + //Perform the request + $result = CS::get()->db->select(self::NOTIFICATIONS_TABLE, $conditions, $values); + + //Check for error + if(count($result) == 0) + return new Notification(); //Return empty notification + + //Parse and return notification + return $this->dbToNotification($result[0]); + + } + /** * Push a new notification *