From e793504a55ed49e31e9b78ca02f3de85d085ea51 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Sat, 26 Oct 2013 23:15:44 +0000
Subject: [PATCH] refs #4179 handle persistent notifications

---
 core/Notification/Manager.php                |  4 ++-
 plugins/CoreHome/Controller.php              |  7 +++++
 plugins/CoreHome/javascripts/notification.js | 27 +++++++++++++++++---
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/core/Notification/Manager.php b/core/Notification/Manager.php
index 62b245a389..dbde9bd7fd 100644
--- a/core/Notification/Manager.php
+++ b/core/Notification/Manager.php
@@ -33,7 +33,9 @@ class Manager
         $session = static::getSession();
         $session->$id = $notification;
 
-        $session->setExpirationHops(1, $id);
+        if (Notification::TYPE_PERSISTENT != $notification->type) {
+            $session->setExpirationHops(1, $id);
+        }
     }
 
     public static function getAllNotificationsToDisplay()
diff --git a/plugins/CoreHome/Controller.php b/plugins/CoreHome/Controller.php
index 15b0ba1ef1..27776f3512 100644
--- a/plugins/CoreHome/Controller.php
+++ b/plugins/CoreHome/Controller.php
@@ -25,6 +25,7 @@ use Piwik\Site;
 use Piwik\UpdateCheck;
 use Piwik\Url;
 use Piwik\View;
+use Piwik\Notification\Manager as NotificationManager;
 
 /**
  *
@@ -69,6 +70,12 @@ class Controller extends \Piwik\Plugin\Controller
         echo $view->render();
     }
 
+    public function markNotificationAsRead()
+    {
+        $notificationId = Common::getRequestVar('notificationId');
+        NotificationManager::cancel($notificationId);
+    }
+
     protected function getDefaultIndexView()
     {
         $view = new View('@CoreHome/getDefaultIndexView');
diff --git a/plugins/CoreHome/javascripts/notification.js b/plugins/CoreHome/javascripts/notification.js
index b982c72ca3..9f0423475a 100644
--- a/plugins/CoreHome/javascripts/notification.js
+++ b/plugins/CoreHome/javascripts/notification.js
@@ -22,6 +22,11 @@
             options = {};
         }
 
+        if ('persistent' == options.type && options.noclear) {
+            // otherwise it is never possible to dismiss the notification
+            options.noclear = false;
+        }
+
         var template = '<div class="notification';
 
         if (options.context) {
@@ -37,7 +42,6 @@
         template += '>';
 
         if (!options.noclear) {
-
             template += '<button type="button" class="close" data-dismiss="alert">&times;</button>';
         }
 
@@ -50,13 +54,17 @@
 
         var notificationNode = $(template).appendTo('#notificationContainer');
 
-        if (!options.noclear) {
-            addCloseEvent(notificationNode);
+        if ('persistent' == options.type) {
+            addPersistentEvent(notificationNode);
         }
 
         if ('toast' == options.type) {
             addToastEvent(notificationNode);
         }
+
+        if (!options.noclear) {
+            addCloseEvent(notificationNode);
+        }
     };
 
     exports.Notification = Notification;
@@ -66,6 +74,7 @@
         setTimeout(function () {
             notificationNode.fadeOut( 'slow', function() {
                 notificationNode.remove();
+                notificationNode = null;
             });
         }, 15 * 1000);
     }
@@ -78,4 +87,16 @@
         });
     };
 
+    function addPersistentEvent(notificationNode) {
+        $(notificationNode).on('click', '.close', function (event) {
+            var ajaxHandler = new ajaxHelper();
+            ajaxHandler.addParams({
+                module: 'CoreHome',
+                action: 'markNotificationAsRead'
+            }, 'GET');
+            ajaxHandler.addParams({notificationId: $(notificationNode).data('id')}, 'POST');
+            ajaxHandler.send(true);
+        });
+    };
+
 })(jQuery, require);
\ No newline at end of file
-- 
GitLab