From 7b6641dc848b6fd912b32de960760854715a35eb Mon Sep 17 00:00:00 2001
From: Matthias Jentsch <matthias.jentsch@netways.de>
Date: Tue, 21 Apr 2015 14:49:27 +0200
Subject: [PATCH] Add basic downtime controller and views

refs #8902
---
 .../controllers/DowntimeController.php        |  79 ++++++++
 .../views/scripts/downtime/show.phtml         | 179 ++++++++++++++++++
 2 files changed, 258 insertions(+)
 create mode 100644 modules/monitoring/application/controllers/DowntimeController.php
 create mode 100644 modules/monitoring/application/views/scripts/downtime/show.phtml

diff --git a/modules/monitoring/application/controllers/DowntimeController.php b/modules/monitoring/application/controllers/DowntimeController.php
new file mode 100644
index 000000000..13e7f9e14
--- /dev/null
+++ b/modules/monitoring/application/controllers/DowntimeController.php
@@ -0,0 +1,79 @@
+<?php
+/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
+
+use Icinga\Module\Monitoring\Controller;
+use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm;
+use Icinga\Web\Url;
+use Icinga\Web\Widget\Tabextension\DashboardAction;
+
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/**
+ * Display detailed information about a downtime
+ */
+class Monitoring_DowntimeController extends Controller
+{
+    protected $downtime;
+    
+    /**
+     * Add tabs
+     */
+    public function init()
+    {
+        $downtimeId = $this->params->get('downtime_id');
+        
+        $this->downtime = $this->backend->select()->from('downtime', array(
+            'id'              => 'downtime_internal_id',
+            'objecttype'      => 'downtime_objecttype',
+            'comment'         => 'downtime_comment',
+            'author_name'     => 'downtime_author_name',
+            'start'           => 'downtime_start',
+            'scheduled_start' => 'downtime_scheduled_start',
+            'scheduled_end'   => 'downtime_scheduled_end',
+            'end'             => 'downtime_end',
+            'duration'        => 'downtime_duration',
+            'is_flexible'     => 'downtime_is_flexible',
+            'is_fixed'        => 'downtime_is_fixed',
+            'is_in_effect'    => 'downtime_is_in_effect',
+            'entry_time'      => 'downtime_entry_time',
+            'host_state'      => 'downtime_host_state',
+            'service_state'   => 'downtime_service_state',
+            'host_name',
+            'host',
+            'service',
+            'service_description',
+            'host_display_name',
+            'service_display_name'
+        ))->where('downtime_internal_id', $downtimeId)->getQuery()->fetchRow();
+        
+        $this->getTabs()
+            ->add(
+                'downtime',
+                array(
+                    'title' => $this->translate(
+                        'Display detailed information about a downtime.'
+                    ),
+                    'icon' => 'plug',
+                    'label' => $this->translate('Downtime'),
+                    'url'   =>'monitoring/downtimes/show'
+                )
+        )->activate('downtime')->extend(new DashboardAction());
+    }
+    
+    public function showAction()
+    {
+        $this->view->downtime = $this->downtime;
+        $this->view->delDowntimeForm = new DeleteDowntimeCommandForm();
+        $this->view->delDowntimeForm->setObjects($this->downtime);
+        $this->view->listAllLink = Url::fromPath('monitoring/list/downtimes');
+        $this->view->showHostLink = Url::fromPath('monitoring/host/show')
+                ->setParam('host', $this->downtime->host);
+        $this->view->showServiceLink = Url::fromPath('monitoring/service/show')
+                ->setParam('host', $this->downtime->host)
+                ->setParam('service', $this->downtime->service_description);
+    }
+}
diff --git a/modules/monitoring/application/views/scripts/downtime/show.phtml b/modules/monitoring/application/views/scripts/downtime/show.phtml
new file mode 100644
index 000000000..806897e90
--- /dev/null
+++ b/modules/monitoring/application/views/scripts/downtime/show.phtml
@@ -0,0 +1,179 @@
+<?php
+use Icinga\Module\Monitoring\Object\Service;
+use Icinga\Module\Monitoring\Object\Host;
+
+if (isset($downtime->service_description)) {
+    $isService = true;
+    $stateName = Service::getStateText($downtime->service_state);
+} else {
+    $isService = false;
+    $stateName = Host::getStateText($downtime->host_state);
+}
+?>
+
+<div class="controls">
+  <?php if (! $this->compact): ?>
+    <?= $this->tabs; ?>
+  <?php endif ?>
+  <h1>
+    <?php if ($isService): ?>
+       <?php 
+          $link = $this->link()->service(
+                $downtime->service_description, 
+                $downtime->service_display_name,
+                $downtime->host_name,
+                $downtime->host_display_name
+          );
+          $icon = $this->icon('service', $this->translate('Service')); 
+        ?>
+    <?php else: ?>
+      <?php 
+        $icon = $this->icon('host', $this->translate('Host'));
+        $link = $this->link()->host($downtime->host_name, $downtime->host_display_name)
+      ?>
+    <?php endif ?>
+    <?= sprintf(
+            $this->translate(
+                'Downtime for %s',
+                'Downtime for host or service.'
+            ),
+            $link
+        ); ?>
+    <?= $icon ?>
+  </h1>
+</div>
+
+<div class="content" data-base-target="_next">
+<small>
+  <?php if ($downtime->is_flexible): ?>
+    <?php if ($downtime->is_in_effect): ?>
+      <?= sprintf(
+        $isService
+          ? $this->translate('This flexible service downtime was started on %s at %s and lasts for %s until %s at %s.')
+          : $this->translate('This flexible host downtime was started on %s at %s and lasts for %s until %s at %s.'),
+        date('d.m.y', $downtime->start),
+        date('H:i', $downtime->start),
+        $this->format()->duration($downtime->duration),
+        date('d.m.y', $downtime->end),
+        date('H:i', $downtime->end)
+      ); ?>
+    <?php else: ?>
+      <?= sprintf(
+        $isService
+          ? $this->translate('This flexible service downtime has been scheduled to start between %s - %s and to last for %s.')
+          : $this->translate('This flexible host downtime has been scheduled to start between %s - %s and to last for %s.'),
+        date('d.m.y H:i', $downtime->scheduled_start),
+        date('d.m.y H:i', $downtime->scheduled_end),
+        $this->format()->duration($downtime->duration)
+      ); ?>
+    <?php endif ?>
+  <?php else: ?>
+    <?php if ($downtime->is_in_effect): ?>
+      <?= sprintf(
+        $isService
+          ? $this->translate('This fixed service downtime was started on %s at %s and expires on %s at %s.')
+          : $this->translate('This fixed host downtime was started on %s at %s and expires on %s at %s.'),
+        date('d.m.y', $downtime->start),
+        date('H:i', $downtime->start),
+        date('d.m.y', $downtime->end),
+        date('H:i', $downtime->end)
+      ); ?>
+    <?php else: ?>
+      <?= sprintf(
+        $isService
+          ? $this->translate('This fixed service downtime has been scheduled to start on %s at %s and to end on %s at %s.')
+          : $this->translate('This fixed host downtime has been scheduled to start on %s at %s and to end on %s at %s.'),
+        date('d.m.y', $downtime->scheduled_start),
+        date('H:i', $downtime->scheduled_start),
+        date('d.m.y', $downtime->scheduled_end),
+        date('H:i', $downtime->scheduled_end)
+      ); ?>
+    <?php endif ?>
+  <?php endif ?>
+</small>
+<br>
+<br>
+    
+<table class="avp newsection"> 
+  <tbody>
+    <tr>
+      <th><?= $this->translate('Author') ?></th>
+      <td><?= $this->escape($this->downtime->author_name) ?></td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('Comments') ?></th>
+      <td><?= $this->escape($this->downtime->comment) ?></td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('Scheduled start') ?></th>
+      <td><?= date('d.m.y H:i', $this->downtime->scheduled_start) ?></td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('Scheduled end') ?></th>
+      <td><?= date('d.m.y H:i', $this->downtime->scheduled_end) ?></td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('Is Flexible') ?></th>
+      <td>
+      <?= $this->escape(
+          $this->downtime->is_flexible ? 
+          $this->translate('Yes') : $this->translate('No')
+        ); 
+      ?>
+      </td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('In Effect') ?></th>
+      <td>
+      <?= $this->escape(
+          $this->downtime->is_in_effect ? 
+          $this->translate('Yes') : $this->translate('No')
+        ); 
+      ?>
+      </td>
+    </tr>
+
+    <?php if ($this->downtime->is_flexible && $this->downtime->is_in_effect): ?>
+    <tr>
+      <th><?= $this->translate('Start') ?></th>
+      <td><?= date('d.m.y H:i', $downtime->start); ?></td>
+    </tr>
+    <tr>
+      <th><?= $this->translate('End') ?></th>
+      <td><?= date('d.m.y H:i', $downtime->end); ?></td>
+    </tr>
+    <?php endif; ?>
+    <tr>
+      <th><?= $this->translate('Duration') ?></th>
+      <td><?= $this->format()->duration($this->escape($this->downtime->duration)); ?></td>
+    </tr>
+    
+    <?php if (isset($delDowntimeForm)): // Form is unset if the current user lacks the respective permission ?>
+    <tr class="newsection">
+      <th><?= $this->translate('Commands') ?></th>
+      <td>
+        <?php
+        $delDowntimeForm->populate(array('downtime_id' => $downtime->id, 'redirect' => $this->listAllLink));
+        if (! $isService) {
+          $delDowntimeForm->setAction(
+              $this->url('monitoring/host/delete-downtime',
+              array('host' => $downtime->host_name))
+          );
+        } else {
+          $delDowntimeForm->setAction(
+              $this->url('monitoring/service/delete-downtime', array(
+                  'host'      => $downtime->host_name,
+                  'service'   => $downtime->service_description
+              ))
+          );
+        }
+        echo $delDowntimeForm;
+        ?>
+      </td>
+    </tr>
+    <?php endif ?>
+  </tbody>
+</table>
+
+</div>
+