diff --git a/application/Espo/Controllers/Notification.php b/application/Espo/Controllers/Notification.php index 3c2f82379b..03811feadf 100644 --- a/application/Espo/Controllers/Notification.php +++ b/application/Espo/Controllers/Notification.php @@ -54,7 +54,13 @@ class Notification extends \Espo\Core\Controllers\Base public function actionNotReadCount() { $userId = $this->getUser()->id; - return $this->getService('Notification')->getNotReadCount($userId); + return $this->getService('Notification')->getNotReadCount($userId); + } + + public function actionMarkAllRead($params, $data, $request) + { + $userId = $this->getUser()->id; + return $this->getService('Notification')->markAllRead($userId); } } diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index 844cce3b28..c00b12156f 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -114,7 +114,8 @@ "Administration": "Administration", "Run Import": "Run Import", "Duplicate": "Duplicate", - "Notifications": "Notifications" + "Notifications": "Notifications", + "Mark all read": "Mark all read" }, "messages": { "notModified": "You have not modified the record", diff --git a/application/Espo/Services/Notification.php b/application/Espo/Services/Notification.php index 842616d114..fb282af174 100644 --- a/application/Espo/Services/Notification.php +++ b/application/Espo/Services/Notification.php @@ -84,6 +84,14 @@ class Notification extends \Espo\Core\Services\Base ))->count(); } + public function markAllRead($userId) + { + $pdo = $this->getEntityManager()->getPDO(); + $sql = "UPDATE notification SET `read` = 1 WHERE user_id = ".$pdo->quote($userId)." AND `read` = 0"; + $pdo->prepare($sql)->execute(); + return true; + } + public function getList($userId, array $params = array()) { $searchParams = array(); diff --git a/frontend/client/res/templates/notifications/panel.tpl b/frontend/client/res/templates/notifications/panel.tpl index e8907a370f..3e8c537a30 100644 --- a/frontend/client/res/templates/notifications/panel.tpl +++ b/frontend/client/res/templates/notifications/panel.tpl @@ -1,5 +1,5 @@
-
{{translate 'Notifications'}}
+
{{translate 'Mark all read'}}{{translate 'Notifications'}}
{{translate 'Loading...'}} diff --git a/frontend/client/src/views/notifications/badge.js b/frontend/client/src/views/notifications/badge.js index 0e50a756c8..01ef51a690 100644 --- a/frontend/client/src/views/notifications/badge.js +++ b/frontend/client/src/views/notifications/badge.js @@ -49,14 +49,22 @@ Espo.define('Views.Notifications.Badge', 'View', function (Dep) { this.checkUpdates(); }, + showNotRead: function (count) { + this.$icon.addClass('warning'); + this.$badge.attr('title', this.translate('New notifications') + ': ' + count); + }, + + hideNotRead: function () { + this.$icon.removeClass('warning'); + this.$badge.attr('title', ''); + }, + checkUpdates: function () { $.ajax('Notification/action/notReadCount').done(function (count) { if (count) { - this.$icon.addClass('warning'); - this.$badge.attr('title', this.translate('New notifications') + ': ' + count); + this.showNotRead(count); } else { - this.$icon.removeClass('warning'); - this.$badge.attr('title', ''); + this.hideNotRead(); } }.bind(this)); @@ -82,6 +90,9 @@ Espo.define('Views.Notifications.Badge', 'View', function (Dep) { el: '#notifications-panel', }, function (view) { view.render(); + this.listenTo(view, 'all-read', function () { + this.hideNotRead(); + }, this); }.bind(this)); $document = $(document); diff --git a/frontend/client/src/views/notifications/panel.js b/frontend/client/src/views/notifications/panel.js index 86a8a7ecdc..400fb440c1 100644 --- a/frontend/client/src/views/notifications/panel.js +++ b/frontend/client/src/views/notifications/panel.js @@ -25,6 +25,17 @@ Espo.define('Views.Notifications.Panel', 'View', function (Dep) { template: 'notifications.panel', + events: { + 'click [data-action="markAllNotificationsRead"]': function () { + $.ajax({ + url: 'Notification/action/markAllRead', + type: 'POST' + }).done(function (count) { + this.trigger('all-read'); + }.bind(this)); + }, + }, + setup: function () { this.wait(true); this.getCollectionFactory().create('Notification', function (collection) {