2017-08-16 23:27:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
|
|
|
|
2019-05-02 13:23:06 +02:00
|
|
|
use ipl\Html\Html;
|
2017-08-16 23:27:13 +02:00
|
|
|
use Icinga\Data\DataArray\ArrayDatasource;
|
|
|
|
use Icinga\Data\Filter\Filter;
|
2019-05-06 14:28:57 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
|
|
|
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
|
2017-08-16 23:27:13 +02:00
|
|
|
use Icinga\Module\Director\Objects\HostApplyMatches;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
2019-05-02 13:23:06 +02:00
|
|
|
use gipfl\IcingaWeb2\Link;
|
|
|
|
use gipfl\IcingaWeb2\Table\SimpleQueryBasedTable;
|
2017-08-16 23:27:13 +02:00
|
|
|
|
|
|
|
class IcingaHostAppliedServicesTable extends SimpleQueryBasedTable
|
|
|
|
{
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/** @var IcingaHost */
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
/** @var \Zend_Db_Adapter_Abstract */
|
|
|
|
protected $db;
|
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
/** @var bool */
|
|
|
|
protected $readonly = false;
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
protected $highlightedService;
|
|
|
|
|
2017-08-16 23:27:13 +02:00
|
|
|
private $allApplyRules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IcingaHost $host
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function load(IcingaHost $host)
|
|
|
|
{
|
2017-08-25 13:47:04 +02:00
|
|
|
$table = (new static())->setHost($host);
|
2018-05-05 00:24:49 +02:00
|
|
|
$table->getAttributes()->set('data-base-target', '_self');
|
2017-08-25 13:47:04 +02:00
|
|
|
return $table;
|
2017-08-16 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumnsToBeRendered()
|
|
|
|
{
|
|
|
|
return [$this->title];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHost(IcingaHost $host)
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
$this->db = $host->getDb();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
/**
|
|
|
|
* Show no related links
|
|
|
|
*
|
|
|
|
* @param bool $readonly
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setReadonly($readonly = true)
|
|
|
|
{
|
|
|
|
$this->readonly = (bool) $readonly;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function highlightService($service)
|
|
|
|
{
|
|
|
|
$this->highlightedService = $service;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:27:13 +02:00
|
|
|
public function renderRow($row)
|
|
|
|
{
|
2018-07-16 09:19:53 +02:00
|
|
|
$classes = [];
|
2018-05-25 17:03:33 +02:00
|
|
|
if ($row->blacklisted === 'y') {
|
2018-07-16 09:19:53 +02:00
|
|
|
$classes[] = 'strike-links';
|
2018-05-25 17:03:33 +02:00
|
|
|
}
|
2018-07-16 09:19:53 +02:00
|
|
|
if ($row->disabled === 'y') {
|
|
|
|
$classes[] = 'disabled';
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes = empty($classes) ? null : ['class' => $classes];
|
2018-05-25 17:03:33 +02:00
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
if ($this->readonly) {
|
|
|
|
if ($this->highlightedService === $row->name) {
|
|
|
|
$link = Html::tag('a', ['class' => 'icon-right-big'], $row->name);
|
|
|
|
} else {
|
|
|
|
$link = Html::tag('a', $row->name);
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-06 14:38:17 +02:00
|
|
|
$applyFor = '';
|
|
|
|
if (! empty($row->apply_for)) {
|
|
|
|
$applyFor = sprintf('(apply for %s) ', $row->apply_for);
|
|
|
|
}
|
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
$link = Link::create(sprintf(
|
2019-05-06 14:38:17 +02:00
|
|
|
$this->translate('%s %s(%s)'),
|
2019-02-13 11:41:26 +01:00
|
|
|
$row->name,
|
2019-05-06 14:38:17 +02:00
|
|
|
$applyFor,
|
2019-05-06 14:28:57 +02:00
|
|
|
$this->renderApplyFilter($row->filter)
|
2019-02-13 11:41:26 +01:00
|
|
|
), 'director/host/appliedservice', [
|
|
|
|
'name' => $this->host->getObjectName(),
|
|
|
|
'service_id' => $row->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this::row([$link], $attributes);
|
2017-08-16 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 14:28:57 +02:00
|
|
|
/**
|
|
|
|
* @param Filter $assignFilter
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderApplyFilter(Filter $assignFilter)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$string = AssignRenderer::forFilter($assignFilter)->renderAssign();
|
|
|
|
} catch (IcingaException $e) {
|
|
|
|
$string = 'Error in Filter rendering: ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:27:05 +02:00
|
|
|
/**
|
|
|
|
* @return \Icinga\Data\SimpleQuery
|
|
|
|
*/
|
2017-08-16 23:27:13 +02:00
|
|
|
public function prepareQuery()
|
|
|
|
{
|
|
|
|
$services = [];
|
|
|
|
$matcher = HostApplyMatches::prepare($this->host);
|
|
|
|
foreach ($this->getAllApplyRules() as $rule) {
|
|
|
|
if ($matcher->matchesFilter($rule->filter)) {
|
|
|
|
$services[] = $rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ds = new ArrayDatasource($services);
|
|
|
|
return $ds->select()->columns([
|
|
|
|
'id' => 'id',
|
|
|
|
'name' => 'name',
|
2018-05-25 17:03:33 +02:00
|
|
|
'filter' => 'filter',
|
2018-07-16 09:19:53 +02:00
|
|
|
'disabled' => 'disabled',
|
2018-05-25 17:03:33 +02:00
|
|
|
'blacklisted' => 'blacklisted',
|
2017-08-16 23:27:13 +02:00
|
|
|
'assign_filter' => 'assign_filter',
|
2019-05-06 14:38:17 +02:00
|
|
|
'apply_for' => 'apply_for',
|
2017-08-16 23:27:13 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:27:05 +02:00
|
|
|
/***
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-16 23:27:13 +02:00
|
|
|
protected function getAllApplyRules()
|
|
|
|
{
|
|
|
|
if ($this->allApplyRules === null) {
|
|
|
|
$this->allApplyRules = $this->fetchAllApplyRules();
|
|
|
|
foreach ($this->allApplyRules as $rule) {
|
|
|
|
$rule->filter = Filter::fromQueryString($rule->assign_filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->allApplyRules;
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:27:05 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-16 23:27:13 +02:00
|
|
|
protected function fetchAllApplyRules()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
2021-12-17 13:57:18 +01:00
|
|
|
$hostId = $this->host->get('id');
|
2017-08-16 23:27:13 +02:00
|
|
|
$query = $db->select()->from(
|
|
|
|
['s' => 'icinga_service'],
|
|
|
|
[
|
|
|
|
'id' => 's.id',
|
|
|
|
'name' => 's.object_name',
|
|
|
|
'assign_filter' => 's.assign_filter',
|
2019-05-06 14:38:17 +02:00
|
|
|
'apply_for' => 's.apply_for',
|
2018-07-16 09:19:53 +02:00
|
|
|
'disabled' => 's.disabled',
|
2021-12-17 13:57:18 +01:00
|
|
|
'blacklisted' => $hostId ? "CASE WHEN hsb.service_id IS NULL THEN 'n' ELSE 'y' END" : "('n')",
|
2017-08-16 23:27:13 +02:00
|
|
|
]
|
2019-05-06 19:42:24 +02:00
|
|
|
)->where('object_type = ? AND assign_filter IS NOT NULL', 'apply')
|
|
|
|
->order('s.object_name');
|
2021-12-17 13:57:18 +01:00
|
|
|
if ($hostId) {
|
|
|
|
$query->joinLeft(
|
|
|
|
['hsb' => 'icinga_host_service_blacklist'],
|
|
|
|
$db->quoteInto('s.id = hsb.service_id AND hsb.host_id = ?', $hostId),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
2017-08-16 23:27:13 +02:00
|
|
|
|
|
|
|
return $db->fetchAll($query);
|
|
|
|
}
|
|
|
|
}
|