From 296d8c79695264d3bde368b8ad690a0ff7d771ef Mon Sep 17 00:00:00 2001
From: Thomas Gelf <thomas@gelf.net>
Date: Fri, 7 Mar 2014 23:15:51 +0000
Subject: [PATCH] Send notifications on site reload and via XHR

---
 application/layouts/scripts/body.phtml             | 14 ++++++++++++--
 library/Icinga/Web/Controller/ActionController.php |  9 +++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/application/layouts/scripts/body.phtml b/application/layouts/scripts/body.phtml
index d41dd76b4..4ef0f99ae 100644
--- a/application/layouts/scripts/body.phtml
+++ b/application/layouts/scripts/body.phtml
@@ -1,10 +1,20 @@
 <?php
 
 use Icinga\Web\Url;
+use Icinga\Web\Notification;
+use Icinga\Authentication\Manager as Auth;
 
-if (Icinga\Authentication\Manager::getInstance()->isAuthenticated()): ?>
+if (Auth::getInstance()->isAuthenticated()): ?>
  <div id="header">
-   <ul id="notifications"></ul>
+   <ul id="notifications"><?php
+
+$notifications = Notification::getInstance();
+if ($notifications->hasMessages()) {
+    foreach ($notifications->getMessages() as $m) {
+        echo '<li class="' . $m->type . '">' . $this->escape($m->message) . '</li>';
+    }
+}
+   ?></ul>
    <div id="logo"><a href="<?= $this->href('/dashboard') ?>"><img src="<?= $this->href('img/logo_icinga-inv.png') ?>" class="logo" /></a>
  </div>
  </div>
diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php
index 841bf0ccb..973086c18 100644
--- a/library/Icinga/Web/Controller/ActionController.php
+++ b/library/Icinga/Web/Controller/ActionController.php
@@ -40,6 +40,7 @@ use Icinga\Application\Config;
 use Icinga\Util\Translator;
 use Icinga\Web\Widget\Tabs;
 use Icinga\Web\Url;
+use Icinga\Web\Notification;
 use Icinga\Logger\Logger;
 use Icinga\Web\Request;
 use Icinga\File\Pdf;
@@ -299,6 +300,14 @@ class ActionController extends Zend_Controller_Action
         if ($this->_request->isXmlHttpRequest() || $this->getParam('view') === 'compact') {
             $this->_helper->layout()->setLayout('inline');
         }
+
+        $notifications = Notification::getInstance();
+        if ($this->_request->isXmlHttpRequest() && $notifications->hasMessages()) {
+            foreach ($notifications->getMessages() as $m) {
+                header('X-Icinga-Notification: ' . $m->type . ' ' . $m->message);
+            }
+        }
+
         if ($this->view->title) {
             if (preg_match('~[\r\n]~', $this->view->title)) {
                 // TODO: Innocent exception and error log for hack attempts