From d9101da79e839984e0e782b48113dc61157d6688 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Thu, 26 May 2016 20:50:50 -0400 Subject: Mark notification as read when clicking on it --- ChangeLog | 1 + app/Controller/WebNotification.php | 50 --------------- app/Controller/WebNotificationController.php | 79 ++++++++++++++++++++++++ app/Model/UserUnreadNotification.php | 26 +++++++- app/Template/dashboard/notifications.php | 14 ++--- tests/units/Model/UserUnreadNotificationTest.php | 20 ++++++ 6 files changed, 129 insertions(+), 61 deletions(-) delete mode 100644 app/Controller/WebNotification.php create mode 100644 app/Controller/WebNotificationController.php diff --git a/ChangeLog b/ChangeLog index 3ebb09ba..1342c159 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ New features: Improvements: +* Mark web notification as read when clicking on it * Support strtotime strings for date search * Reset failed login counter and unlock user when changing password * Task do not open anymore in a new window on the Gantt chart diff --git a/app/Controller/WebNotification.php b/app/Controller/WebNotification.php deleted file mode 100644 index a62da0e3..00000000 --- a/app/Controller/WebNotification.php +++ /dev/null @@ -1,50 +0,0 @@ -getUserId(); - - $this->userUnreadNotification->markAllAsRead($user_id); - $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id))); - } - - /** - * Mark a notification as read - * - * @access public - */ - public function remove() - { - $user_id = $this->getUserId(); - $notification_id = $this->request->getIntegerParam('notification_id'); - - $this->userUnreadNotification->markAsRead($user_id, $notification_id); - $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id))); - } - - private function getUserId() - { - $user_id = $this->request->getIntegerParam('user_id'); - - if (! $this->userSession->isAdmin() && $user_id != $this->userSession->getId()) { - $user_id = $this->userSession->getId(); - } - - return $user_id; - } -} diff --git a/app/Controller/WebNotificationController.php b/app/Controller/WebNotificationController.php new file mode 100644 index 00000000..c7b9c8ff --- /dev/null +++ b/app/Controller/WebNotificationController.php @@ -0,0 +1,79 @@ +getUserId(); + + $this->userUnreadNotification->markAllAsRead($user_id); + $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id))); + } + + /** + * Mark a notification as read + * + * @access public + */ + public function remove() + { + $user_id = $this->getUserId(); + $notification_id = $this->request->getIntegerParam('notification_id'); + + $this->userUnreadNotification->markAsRead($user_id, $notification_id); + $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id))); + } + + /** + * Redirect to the task and mark notification as read + */ + public function redirect() + { + $user_id = $this->getUserId(); + $notification_id = $this->request->getIntegerParam('notification_id'); + + $notification = $this->userUnreadNotification->getById($notification_id); + $this->userUnreadNotification->markAsRead($user_id, $notification_id); + + if (empty($notification)) { + $this->response->redirect($this->helper->url->to('DashboardController', 'notifications', array('user_id' => $user_id))); + } elseif ($this->helper->text->contains($notification['event_name'], 'comment')) { + $this->response->redirect($this->helper->url->to( + 'task', + 'show', + array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id']), + 'comment-'.$notification['event_data']['comment']['id'] + )); + } else { + $this->response->redirect($this->helper->url->to( + 'task', + 'show', + array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id']) + )); + } + } + + private function getUserId() + { + $user_id = $this->request->getIntegerParam('user_id'); + + if (! $this->userSession->isAdmin() && $user_id != $this->userSession->getId()) { + $user_id = $this->userSession->getId(); + } + + return $user_id; + } +} diff --git a/app/Model/UserUnreadNotification.php b/app/Model/UserUnreadNotification.php index cc0f326a..42893666 100644 --- a/app/Model/UserUnreadNotification.php +++ b/app/Model/UserUnreadNotification.php @@ -35,6 +35,23 @@ class UserUnreadNotification extends Base )); } + /** + * Get one notification + * + * @param integer $notification_id + * @return array|null + */ + public function getById($notification_id) + { + $notification = $this->db->table(self::TABLE)->eq('id', $notification_id)->findOne(); + + if (! empty($notification)) { + $this->unserialize($notification); + } + + return $notification; + } + /** * Get all notifications for a user * @@ -47,8 +64,7 @@ class UserUnreadNotification extends Base $events = $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('date_creation')->findAll(); foreach ($events as &$event) { - $event['event_data'] = json_decode($event['event_data'], true); - $event['title'] = $this->notification->getTitleWithoutAuthor($event['event_name'], $event['event_data']); + $this->unserialize($event); } return $events; @@ -90,4 +106,10 @@ class UserUnreadNotification extends Base { return $this->db->table(self::TABLE)->eq('user_id', $user_id)->exists(); } + + private function unserialize(&$event) + { + $event['event_data'] = json_decode($event['event_data'], true); + $event['title'] = $this->notification->getTitleWithoutAuthor($event['event_name'], $event['event_data']); + } } diff --git a/app/Template/dashboard/notifications.php b/app/Template/dashboard/notifications.php index b64eb0b7..e0e9b878 100644 --- a/app/Template/dashboard/notifications.php +++ b/app/Template/dashboard/notifications.php @@ -8,7 +8,7 @@ @@ -36,16 +36,12 @@ - text->contains($notification['event_name'], 'comment')): ?> - url->link($notification['title'], 'task', 'show', array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id']), false, '', '', false, 'comment-'.$notification['event_data']['comment']['id']) ?> - text->contains($notification['event_name'], 'task.overdue')): ?> + text->contains($notification['event_name'], 'task.overdue')): ?> 1): ?> - - url->link($notification['title'], 'task', 'show', array('task_id' => $notification['event_data']['tasks'][0]['id'], 'project_id' => $notification['event_data']['tasks'][0]['project_id'])) ?> - url->link($notification['title'], 'task', 'show', array('task_id' => $notification['event_data']['task']['id'], 'project_id' => $notification['event_data']['task']['project_id'])) ?> + url->link($notification['title'], 'WebNotificationController', 'redirect', array('notification_id' => $notification['id'], 'user_id' => $user['id'])) ?> @@ -53,9 +49,9 @@ - url->link(t('Mark as read'), 'webNotification', 'remove', array('user_id' => $user['id'], 'notification_id' => $notification['id'])) ?> + url->link(t('Mark as read'), 'WebNotificationController', 'remove', array('user_id' => $user['id'], 'notification_id' => $notification['id'])) ?> - \ No newline at end of file + diff --git a/tests/units/Model/UserUnreadNotificationTest.php b/tests/units/Model/UserUnreadNotificationTest.php index 843b7860..eb162a40 100644 --- a/tests/units/Model/UserUnreadNotificationTest.php +++ b/tests/units/Model/UserUnreadNotificationTest.php @@ -87,4 +87,24 @@ class UserUnreadNotificationTest extends Base $this->assertArrayHasKey('title', $notifications[0]); $this->assertTrue(is_array($notifications[0]['event_data'])); } + + public function testGetOne() + { + $wn = new UserUnreadNotification($this->container); + $p = new Project($this->container); + $tf = new TaskFinder($this->container); + $tc = new TaskCreation($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); + + $wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1))); + $wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1))); + + $this->assertEmpty($wn->getAll(2)); + + $notification = $wn->getById(1); + $this->assertArrayHasKey('title', $notification); + $this->assertTrue(is_array($notification['event_data'])); + } } -- cgit v1.2.3