Deduplicate code and fix layout

Move icon rendering code into a separate for better maintainabillity.

refs #8665
This commit is contained in:
Matthias Jentsch 2015-05-21 11:40:08 +02:00
parent c0cf6e4918
commit ee6c669441
8 changed files with 57 additions and 25 deletions

View File

@ -50,6 +50,7 @@ class Monitoring_ServicesController extends Controller
protected function handleCommandForm(ObjectsCommandForm $form) protected function handleCommandForm(ObjectsCommandForm $form)
{ {
$this->serviceList->setColumns(array( $this->serviceList->setColumns(array(
'host_icon_image',
'host_name', 'host_name',
'host_output', 'host_output',
'host_state', 'host_state',
@ -94,6 +95,7 @@ class Monitoring_ServicesController extends Controller
->handleRequest(); ->handleRequest();
$this->view->checkNowForm = $checkNowForm; $this->view->checkNowForm = $checkNowForm;
$this->serviceList->setColumns(array( $this->serviceList->setColumns(array(
'host_icon_image',
'host_name', 'host_name',
'host_output', 'host_output',
'host_state', 'host_state',

View File

@ -0,0 +1,46 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
/**
* Generate icons to describe a given hosts state
*/
class Zend_View_Helper_IconImage extends Zend_View_Helper_Abstract
{
/**
* Create dispatch instance
*
* @return \Zend_View_Helper_IconImage
*/
public function iconImage()
{
return $this;
}
/**
* Display the image_icon of a MonitoredObject
*
* @param MonitoredObject|stdClass $object The host or service
* @return string
*/
public function host($object)
{
if ($object->host_icon_image && ! preg_match('/[\'"]/', $object->host_icon_image)) {
return $this->view->icon($this->view->resolveMacros($object->host_icon_image, $object));
}
return '';
}
/**
* Display the image_icon of a MonitoredObject
*
* @param MonitoredObject|stdClass $object The host or service
* @return string
*/
public function service($object)
{
if ($object->service_icon_image && ! preg_match('/[\'"]/', $object->service_icon_image)) {
return $this->view->icon($this->view->resolveMacros($object->service_icon_image, $object));
}
return '';
}
}

View File

@ -52,9 +52,7 @@ if (count($hosts) === 0) {
<!-- Host / Status / Output --> <!-- Host / Status / Output -->
<td> <td>
<?php if ($host->host_icon_image && ! preg_match('/[\'"]/', $host->host_icon_image)): ?> <?= $this->iconImage()->host($host) ?>
<?= $this->icon($this->resolveMacros($host->host_icon_image, $host)) ?>
<?php endif ?>
<?= implode(' ', $this->hostFlags($host)) ?> <?= implode(' ', $this->hostFlags($host)) ?>
<?= $this->qlink( <?= $this->qlink(
$host->host_display_name, $host->host_display_name,
@ -75,7 +73,7 @@ if (count($hosts) === 0) {
'host' => $host->host_name, 'host' => $host->host_name,
'service_problem' => 1, 'service_problem' => 1,
'service_handled' => 0 'service_handled' => 0
), ),
array( array(
'style' => 'font-weight: normal', 'style' => 'font-weight: normal',
'title' => sprintf( 'title' => sprintf(

View File

@ -58,12 +58,8 @@ if (count($services) === 0) {
<td> <td>
<div class="sparkline-box"><?= $this->perfdata($service->service_perfdata, true, 8) ?> </div> <div class="sparkline-box"><?= $this->perfdata($service->service_perfdata, true, 8) ?> </div>
<?= $this->iconImage()->service($service) ?>
<?= implode(' ', $this->serviceFlags($service)); ?> <?= implode(' ', $this->serviceFlags($service)); ?>
<?php if ($service->service_icon_image && ! preg_match('/[\'"]/', $service->service_icon_image)): ?>
<?= $this->icon($this->resolveMacros($service->service_icon_image, $service)) ?>
<?php endif ?>
<?= $this->qlink( <?= $this->qlink(
$service->service_display_name, $service->service_display_name,
$serviceLink, $serviceLink,

View File

@ -9,9 +9,7 @@ use Icinga\Module\Monitoring\Object\Host;
<?= $this->prefixedTimeSince($object->host_last_state_change, true); ?> <?= $this->prefixedTimeSince($object->host_last_state_change, true); ?>
</td> </td>
<td> <td>
<?php if ($object->host_icon_image && ! preg_match('/[\'"]/', $object->host_icon_image)): ?> <?= $this->iconImage()->host($object) ?>
<?= $this->icon($this->resolveMacros($object->host_icon_image, $object)) ?>
<?php endif ?>
<strong><?= $this->escape($object->host_display_name); ?></strong> <strong><?= $this->escape($object->host_display_name); ?></strong>
<?php if ($object->host_display_name !== $object->host_name): ?> <?php if ($object->host_display_name !== $object->host_name): ?>
<small>(<?= $this->escape($object->host_name); ?>)</small> <small>(<?= $this->escape($object->host_name); ?>)</small>
@ -23,4 +21,4 @@ use Icinga\Module\Monitoring\Object\Host;
<?= $this->render('partials/host/statusicons.phtml'); ?> <?= $this->render('partials/host/statusicons.phtml'); ?>
</td> </td>
</tr> </tr>
</table> </table>

View File

@ -17,10 +17,8 @@ $i = 0;
<tr class="state <?= Host::getStateText($host->host_state); ?><?= $host->host_handled ? ' handled' : '' ?>"> <tr class="state <?= Host::getStateText($host->host_state); ?><?= $host->host_handled ? ' handled' : '' ?>">
<td class="state"><?= Host::getStateText($host->host_state, true); ?><br /></td> <td class="state"><?= Host::getStateText($host->host_state, true); ?><br /></td>
<td> <td>
<?= $this->iconImage()->host($host) ?>
<?= implode(' ', $this->hostFlags($host)) ?> <?= implode(' ', $this->hostFlags($host)) ?>
<?php if ($host->host_icon_image && ! preg_match('/[\'"]/', $host->host_icon_image)): ?>
<?= $this->icon($this->resolveMacros($host->host_icon_image, $host)) ?>
<?php endif ?>
<b><?= $this->escape($host->getName()); ?></b><br> <b><?= $this->escape($host->getName()); ?></b><br>
<?= $this->escape($host->host_output) ?> <?= $this->escape($host->host_output) ?>
</td> </td>

View File

@ -10,9 +10,7 @@ use Icinga\Module\Monitoring\Object\Service;
<?= $this->prefixedTimeSince($object->host_last_state_change, true); ?> <?= $this->prefixedTimeSince($object->host_last_state_change, true); ?>
</td> </td>
<td> <td>
<?php if ($object->host_icon_image && ! preg_match('/[\'"]/', $object->host_icon_image)): ?> <?= $this->iconImage()->service($object) ?>
<?= $this->icon($this->resolveMacros($object->host_icon_image, $object)) ?>
<?php endif ?>
<strong><?= $this->escape($object->host_display_name); ?></strong> <strong><?= $this->escape($object->host_display_name); ?></strong>
<?php if ($object->host_display_name !== $object->host_name): ?> <?php if ($object->host_display_name !== $object->host_name): ?>
<small>(<?= $this->escape($object->host_name); ?>)</small> <small>(<?= $this->escape($object->host_name); ?>)</small>
@ -30,9 +28,7 @@ use Icinga\Module\Monitoring\Object\Service;
<?= $this->prefixedTimeSince($object->service_last_state_change, true); ?> <?= $this->prefixedTimeSince($object->service_last_state_change, true); ?>
</td> </td>
<td> <td>
<?php if ($object->service_icon_image && ! preg_match('/[\'"]/', $object->service_icon_image)): ?> <?= $this->iconImage()->host($object) ?>
<?= $this->icon($this->resolveMacros($object->service_icon_image, $object)) ?>
<?php endif ?>
<strong><?= $this->translate('Service'); ?>: <?= $this->escape($object->service_display_name); ?></strong> <strong><?= $this->translate('Service'); ?>: <?= $this->escape($object->service_display_name); ?></strong>
<?php if ($object->service_display_name !== $object->service_description): ?> <?php if ($object->service_display_name !== $object->service_description): ?>
<small>(<?= $this->escape($object->service_description); ?>)</small> <small>(<?= $this->escape($object->service_description); ?>)</small>

View File

@ -15,10 +15,8 @@ $i = 0;
<tr class="state <?= Service::getStateText($service->service_state); ?><?= $service->service_handled ? ' handled' : '' ?>"> <tr class="state <?= Service::getStateText($service->service_state); ?><?= $service->service_handled ? ' handled' : '' ?>">
<td class="state"><?= Service::getStateText($service->service_state, true); ?><br /></td> <td class="state"><?= Service::getStateText($service->service_state, true); ?><br /></td>
<td> <td>
<?= $this->iconImage()->service($service) ?>
<?= implode(' ', $this->serviceFlags($service)) ?> <?= implode(' ', $this->serviceFlags($service)) ?>
<?php if ($service->service_icon_image && ! preg_match('/[\'"]/', $service->service_icon_image)): ?>
<?= $this->icon($this->resolveMacros($service->service_icon_image, $service)) ?>
<?php endif ?>
<b> <b>
<?= $this->escape($service->getName()); ?> <?= $this->escape($service->getName()); ?>
<?= $this->translate('on') ?> <?= $this->translate('on') ?>