[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: Implement AnnouncementsLoader

git at icinga.org git at icinga.org
Fri Nov 4 17:00:15 CET 2016


Module: icingaweb2
Branch: feature/announce-banner-11198
Commit: 5d70dce310d4457375fcf8f7740bb73fae4902e2
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=5d70dce310d4457375fcf8f7740bb73fae4902e2

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Fri Nov  4 16:59:57 2016 +0100

Implement AnnouncementsLoader

refs #11198

---

 application/layouts/scripts/body.phtml             |   11 +++
 library/Icinga/Web/AnnouncementsLoader.php         |   86 ++++++++++++++++++++
 .../Web/Controller/ModuleActionController.php      |   32 --------
 3 files changed, 97 insertions(+), 32 deletions(-)

diff --git a/application/layouts/scripts/body.phtml b/application/layouts/scripts/body.phtml
index 0423fc4..2c90f61 100644
--- a/application/layouts/scripts/body.phtml
+++ b/application/layouts/scripts/body.phtml
@@ -1,5 +1,6 @@
 <?php
 
+use Icinga\Web\AnnouncementsLoader;
 use Icinga\Web\Url;
 use Icinga\Web\Notification;
 use Icinga\Authentication\Auth;
@@ -18,6 +19,16 @@ if ($this->layout()->autorefreshInterval) {
 
 ?>
 <div id="header">
+<?php
+$announcements = AnnouncementsLoader::getInstance()->getAnnouncements();
+if (! empty($announcements)) {
+    echo '<ul role="alert" id="announcements">';
+    foreach ($announcements as $announcement) {
+        echo '<li>' . $this->escape($announcement->getMessage()) . '</li>';
+    }
+    echo '</ul>';
+}
+?>
   <div id="header-logo-container">
     <?= $this->qlink(
       '',
diff --git a/library/Icinga/Web/AnnouncementsLoader.php b/library/Icinga/Web/AnnouncementsLoader.php
new file mode 100644
index 0000000..d52df22
--- /dev/null
+++ b/library/Icinga/Web/AnnouncementsLoader.php
@@ -0,0 +1,86 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Web;
+
+use DateTime;
+use Icinga\Data\Filter\Filter;
+use Icinga\Data\Filter\FilterAnd;
+use Icinga\Exception\ProgrammingError;
+use Icinga\Repository\AnnouncementIniRepository;
+
+/**
+ * Load announcements for being displayed
+ */
+class AnnouncementsLoader
+{
+    /**
+     * Singleton instance
+     *
+     * @var AnnouncementsLoader
+     */
+    private static $instance;
+
+    /**
+     * The loaded announcements
+     *
+     * @var Announcement[]
+     */
+    private $announcements;
+
+    /**
+     * Get singleton instance
+     *
+     * @return AnnouncementsLoader
+     */
+    public static function getInstance()
+    {
+        if (self::$instance === null) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * AnnouncementsLoader constructor
+     */
+    final private function __construct()
+    {
+    }
+
+    /**
+     * Prevent an instance from being cloned
+     */
+    final private function __clone()
+    {
+        throw new ProgrammingError('Can not clone a singleton');
+    }
+
+    /**
+     * Load and get announcements
+     *
+     * @return Announcement[]
+     */
+    public function getAnnouncements()
+    {
+        if ($this->announcements === null) {
+            $repo = new AnnouncementIniRepository();
+            $now = new DateTime();
+            $query = $repo->select()
+                ->setFilter(new FilterAnd(array(
+                    Filter::expression('start', '<=', $now),
+                    Filter::expression('end', '>=', $now)
+                )))
+                ->order('start');
+
+            $announcements = array();
+            foreach ($query as $announcement) {
+                $announcements[] = new Announcement((array) $announcement);
+            }
+            $this->announcements = $announcements;
+        }
+
+        return $this->announcements;
+    }
+}
diff --git a/library/Icinga/Web/Controller/ModuleActionController.php b/library/Icinga/Web/Controller/ModuleActionController.php
index 3fb7db5..2cb5f62 100644
--- a/library/Icinga/Web/Controller/ModuleActionController.php
+++ b/library/Icinga/Web/Controller/ModuleActionController.php
@@ -3,15 +3,10 @@
 
 namespace Icinga\Web\Controller;
 
-use DateTime;
 use Icinga\Application\Config;
 use Icinga\Application\Icinga;
 use Icinga\Application\Modules\Manager;
 use Icinga\Application\Modules\Module;
-use Icinga\Data\Filter\Filter;
-use Icinga\Data\Filter\FilterAnd;
-use Icinga\Repository\AnnouncementIniRepository;
-use Icinga\Web\Notification;
 
 /**
  * Base class for module action controllers
@@ -35,33 +30,6 @@ class ModuleActionController extends ActionController
             && $this->getFrontController()->getDefaultModule() !== $this->getModuleName()) {
             $this->assertPermission(Manager::MODULE_PERMISSION_NS . $this->getModuleName());
         }
-        $this->showAnnouncements();
-    }
-
-    /**
-     * Show the user all announcements as notifications
-     *
-     * @return $this
-     */
-    protected function showAnnouncements()
-    {
-        if (! Icinga::app()->getRequest()->isXmlHttpRequest()) {
-            $repo = new AnnouncementIniRepository();
-            $now = new DateTime();
-            $query = $repo
-                ->select(array('message'))
-                ->applyFilter(new FilterAnd(array(
-                    Filter::expression('start', '<=', $now),
-                    Filter::expression('end', '>=', $now)
-                )))
-                ->order('start');
-
-            foreach ($query->fetchColumn() as $message) {
-                Notification::info($message);
-            }
-        }
-
-        return $this;
     }
 
     /**



More information about the icinga-checkins mailing list