From 0c7d8ab48164a90ce55940f497eed43d8cc17ab4 Mon Sep 17 00:00:00 2001
From: Matthias Jentsch <matthias.jentsch@netways.de>
Date: Mon, 4 May 2015 14:59:39 +0200
Subject: [PATCH] Add controlle for handling multiple comments

refs #8903
---
 .../controllers/CommentsController.php        | 86 +++++++++++++++++++
 .../views/scripts/comment/remove.phtml        | 11 +++
 .../views/scripts/comments/remove-all.phtml   | 12 +++
 .../views/scripts/comments/show.phtml         | 32 +++++++
 .../partials/comment/comment-header.phtml     |  2 +-
 .../partials/comment/comments-header.phtml    | 30 +++++++
 6 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 modules/monitoring/application/controllers/CommentsController.php
 create mode 100644 modules/monitoring/application/views/scripts/comment/remove.phtml
 create mode 100644 modules/monitoring/application/views/scripts/comments/remove-all.phtml
 create mode 100644 modules/monitoring/application/views/scripts/comments/show.phtml
 create mode 100644 modules/monitoring/application/views/scripts/partials/comment/comments-header.phtml

diff --git a/modules/monitoring/application/controllers/CommentsController.php b/modules/monitoring/application/controllers/CommentsController.php
new file mode 100644
index 000000000..9e477d3bb
--- /dev/null
+++ b/modules/monitoring/application/controllers/CommentsController.php
@@ -0,0 +1,86 @@
+<?php
+/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
+
+use Icinga\Module\Monitoring\Controller;
+use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
+use Icinga\Web\Url;
+use Icinga\Web\Widget\Tabextension\DashboardAction;
+use Icinga\Data\Filter\Filter;
+
+/**
+ * Display detailed information about a comment
+ */
+class Monitoring_CommentsController extends Controller
+{
+    protected $comments;
+
+    /**
+     * Add tabs
+     */
+    public function init()
+    {
+        $this->filter = Filter::fromQueryString(str_replace(
+                'comment_id',
+                'comment_internal_id',
+                (string)$this->params
+        ));
+        $this->comments = $this->backend->select()->from('comment', array(
+            'id'         => 'comment_internal_id',
+            'objecttype' => 'comment_objecttype',
+            'comment'    => 'comment_data',
+            'author'     => 'comment_author_name',
+            'timestamp'  => 'comment_timestamp',
+            'type'       => 'comment_type',
+            'persistent' => 'comment_is_persistent',
+            'expiration' => 'comment_expiration',
+            'host_name',
+            'service_description',
+            'host_display_name',
+            'service_display_name'
+        ))->addFilter($this->filter)->getQuery()->fetchAll();
+        
+        if (false === $this->comments) {
+            throw new Zend_Controller_Action_Exception($this->translate('Comment not found'));
+        }
+         
+        $this->getTabs()
+            ->add(
+                'comments',
+                array(
+                    'title' => $this->translate(
+                        'Display detailed information about multiple comments.'
+                    ),
+                    'icon' => 'comment',
+                    'label' => $this->translate('Comments'),
+                    'url'   =>'monitoring/comments/show'
+                )
+        )->activate('comments')->extend(new DashboardAction());
+    }
+    
+    public function showAction()
+    {
+        $this->view->comments = $this->comments;
+        $this->view->listAllLink = Url::fromPath('monitoring/list/comments')
+                ->setQueryString($this->filter->toQueryString());
+        $this->view->removeAllLink = Url::fromPath('monitoring/comments/remove-all')
+                ->setParams($this->params);
+    }
+
+    public function removeAllAction()
+    {
+        $this->assertPermission('monitoring/command/comment/delete');
+        $this->view->comments = $this->comments;
+        $this->view->listAllLink = Url::fromPath('monitoring/list/comments')
+                ->setQueryString($this->filter->toQueryString());
+        $delCommentForm = new DeleteCommentCommandForm();
+        $delCommentForm->setTitle($this->view->translate('Remove all Comments'));
+        $delCommentForm->addDescription(sprintf(
+            $this->translate('Confirm removal of %d comments.'),
+            count($this->comments)
+        ));
+        $delCommentForm->setObjects($this->comments)
+                ->setRedirectUrl(Url::fromPath('monitoring/list/downtimes'))
+                ->handleRequest();
+        $this->view->delCommentForm = $delCommentForm;
+    }
+}
diff --git a/modules/monitoring/application/views/scripts/comment/remove.phtml b/modules/monitoring/application/views/scripts/comment/remove.phtml
new file mode 100644
index 000000000..73f8c68a8
--- /dev/null
+++ b/modules/monitoring/application/views/scripts/comment/remove.phtml
@@ -0,0 +1,11 @@
+<div class="controls">
+       
+  <?php if (! $this->compact): ?>
+    <?= $this->tabs; ?>
+  <?php endif ?>
+
+  <?= $this->render('partials/downtime/downtime-header.phtml'); ?>
+</div>
+<div class="content object-command">
+    <?= $delDowntimeForm; ?>
+</div>
diff --git a/modules/monitoring/application/views/scripts/comments/remove-all.phtml b/modules/monitoring/application/views/scripts/comments/remove-all.phtml
new file mode 100644
index 000000000..698c4eea7
--- /dev/null
+++ b/modules/monitoring/application/views/scripts/comments/remove-all.phtml
@@ -0,0 +1,12 @@
+<div class="controls">
+    
+  <?php if (! $this->compact): ?>
+    <?= $this->tabs; ?>
+  <?php endif ?>
+
+  <?= $this->render('partials/comment/comments-header.phtml'); ?>
+</div>
+
+<div class="content object-command">
+    <?= $delCommentForm ?>
+</div>
diff --git a/modules/monitoring/application/views/scripts/comments/show.phtml b/modules/monitoring/application/views/scripts/comments/show.phtml
new file mode 100644
index 000000000..f54f3904a
--- /dev/null
+++ b/modules/monitoring/application/views/scripts/comments/show.phtml
@@ -0,0 +1,32 @@
+<div class="controls">
+    <?php if (! $this->compact): ?>
+        <?= $this->tabs; ?>
+    <?php endif ?>
+    
+    <?= $this->render('partials/comment/comments-header.phtml'); ?>
+</div>
+
+<div class="content">
+  <h3><?= $this->icon('reschedule') ?> <?= $this->translate('Commands') ?> </h3>
+  <p>
+    <?= sprintf(
+            $this->translate('Issue commands to all %s selected comments.'),
+            '<b>' . count($comments) . '</b>'
+        )
+    ?>
+    <div>
+    <?= $this->qlink(
+        sprintf(
+            $this->translate('Remove all %d scheduled comments'),
+            count($comments)
+        ),
+        $removeAllLink,
+        null,
+        array(
+            'icon' => 'trash',
+            'title' => $this->translate('Remove all selected comments.')
+        )
+    ) ?>
+    </div>
+  </p>
+</div>
diff --git a/modules/monitoring/application/views/scripts/partials/comment/comment-header.phtml b/modules/monitoring/application/views/scripts/partials/comment/comment-header.phtml
index ce0eabe03..c3e0fadda 100644
--- a/modules/monitoring/application/views/scripts/partials/comment/comment-header.phtml
+++ b/modules/monitoring/application/views/scripts/partials/comment/comment-header.phtml
@@ -1,6 +1,6 @@
 <table class="action">
   <tr class="state invalid">
-    <td class="state">    
+    <td class="state" style="width: 12em;">
         <?= $this->render('partials/comment/comment-description.phtml'); ?>
     </td>
     <td>
diff --git a/modules/monitoring/application/views/scripts/partials/comment/comments-header.phtml b/modules/monitoring/application/views/scripts/partials/comment/comments-header.phtml
new file mode 100644
index 000000000..4ec725549
--- /dev/null
+++ b/modules/monitoring/application/views/scripts/partials/comment/comments-header.phtml
@@ -0,0 +1,30 @@
+<table class="action">
+    <?php $i = 0; foreach ($comments as $comment):
+        if (++ $i > 5) {
+            continue;
+        }
+        $this->comment = $comment;
+    ?>
+
+        <tr class="state invalid">
+          <td class="state" style="width: 12em;">
+            <?= $this->render('partials/comment/comment-description.phtml'); ?>
+          </td>
+          <td>
+            <?= $this->render('partials/comment/comment-detail.phtml'); ?>
+          </td>
+        </tr>
+    <?php endforeach; ?>
+</table>
+
+<p>
+    <?= $this->qlink(
+        sprintf($this->translate('List all %d comments …'), count($comments)),
+        $listAllLink,
+        null,
+        array(
+            'title' => $this->translate('List all'),
+            'data-base-target' => "_next"
+        )
+    ) ?>
+</p>