From 3954a0b64cf88519d683e88aa2b0d9f7eef005c3 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 18 Feb 2018 14:40:54 +0100 Subject: [PATCH] First notification --- RestControllers/commentsController.php | 2 +- classes/components/notifications.php | 41 ++++++++++++++++++-------- classes/models/Notification.php | 16 +++++----- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/RestControllers/commentsController.php b/RestControllers/commentsController.php index 3ef01d7..afccc2e 100644 --- a/RestControllers/commentsController.php +++ b/RestControllers/commentsController.php @@ -48,7 +48,7 @@ class commentsController { //Create a notification $notification = new Notification(); - $notification->set_time(time()); + $notification->set_time_create(time()); $notification->set_from_user_id(userID); $notification->set_on_elem_id($postID); $notification->set_on_elem_type(Notification::POST); diff --git a/classes/components/notifications.php b/classes/components/notifications.php index b35352f..d39455f 100644 --- a/classes/components/notifications.php +++ b/classes/components/notifications.php @@ -59,7 +59,7 @@ class notificationComponent { } //Check if target user and page owner are friends - if(!components()->friends->are_friend($friend->getFriendID(), $notification->from_container_id())) + if(!components()->friends->are_friend($friend->getFriendID(), $notification->get_from_container_id())) continue; //Add the user to the list @@ -90,6 +90,9 @@ class notificationComponent { */ private function push_public(Notification $notification, array $usersID) : bool { + //Mark the notification as public + $notification->set_event_visibility(Notification::EVENT_PUBLIC); + //Process the list of users foreach($usersID as $current_user){ @@ -120,6 +123,9 @@ class notificationComponent { */ private function push_private(Notification $notification) : bool { + //Mark the notification as private + $notification->set_event_visibility(Notification::EVENT_PRIVATE); + //Check if a similar notification already exists or not if($this->similar_exists($notification)) return false; //A similar notification already exists @@ -140,15 +146,7 @@ class notificationComponent { //Prepare the request $tableName = self::NOTIFICATIONS_TABLE; - $conditions = array( - - "seen" => $notification->is_seen() ? 1 : 0, - "dest_user_id" => $notification->get_dest_user_id(), - "on_elem_id" => $notification->get_on_elem_id(), - "on_elem_type" => $notification->get_on_elem_type(), - "type" => $notification->get_type() - - ); + $conditions = $this->noticationToDB($notification, false); //Make conditions $process_conditions = CS::get()->db->splitConditionsArray($conditions); @@ -169,21 +167,38 @@ class notificationComponent { private function create(Notification $notification) : bool { //Generate notification datas - $values = $this->noticationToDB($notification); + $values = $this->noticationToDB($notification, TRUE); + //Save the notification in the database + return CS::get()->db->addLine(self::NOTIFICATIONS_TABLE, $values); } /** * Convert a notification object into database array * * @param Notification $notification The notification to convert + * @param bool $full_entry TRUE to process the entire notification, else only + * the main informations will be processed * @return array The converted array */ - private function noticationToDB(Notification $notification) : array { + private function noticationToDB(Notification $notification, bool $full_entry = TRUE) : array { $data = array(); - + $data['seen'] = $notification->is_seen() ? 1 : 0; + $data['from_user_id'] = $notification->get_from_user_id(); + $data['dest_user_id'] = $notification->get_dest_user_id(); + $data['type'] = $notification->get_type(); + $data['on_elem_id'] = $notification->get_on_elem_id(); + $data['on_elem_type'] = $notification->get_on_elem_type(); + + //Specify complementary fields only if required + if($full_entry){ + $data['from_container_id'] = $notification->get_from_container_id(); + $data['from_container_type'] = $notification->get_from_container_type(); + $data['time_create'] = $notification->get_time_create(); + $data['visibility'] = $notification->get_event_visibility(); + } return $data; } diff --git a/classes/models/Notification.php b/classes/models/Notification.php index e7b1ce2..121772f 100644 --- a/classes/models/Notification.php +++ b/classes/models/Notification.php @@ -40,7 +40,7 @@ class Notification { //Private fields private $id; - private $time; + private $time_create; private $seen; private $from_user_id; private $dest_user_id; @@ -86,19 +86,19 @@ class Notification { /** * Set notification creation time * - * @param int $time The creation time + * @param int $time_create The creation time */ - public function set_time(int $time){ - $this->time = $time; + public function set_time_create(int $time_create){ + $this->time_create = $time_create; } /** - * Get notification time + * Get notification creation time * - * @return int The time of the notification + * @return int The creation time of the notification */ - public function get_time() : int { - return $this->time; + public function get_time_create() : int { + return $this->time_create; } /**