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\BaseHtmlElement;
|
|
|
|
use ipl\Html\Html;
|
2018-08-06 13:58:29 +02:00
|
|
|
use Icinga\Module\Director\Forms\RemoveLinkForm;
|
2017-08-16 23:27:13 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaServiceSet;
|
2019-05-02 13:23:06 +02:00
|
|
|
use ipl\Html\HtmlElement;
|
|
|
|
use gipfl\IcingaWeb2\Link;
|
|
|
|
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
|
|
|
|
use gipfl\IcingaWeb2\Url;
|
2017-08-16 23:27:13 +02:00
|
|
|
|
|
|
|
class IcingaServiceSetServiceTable extends ZfQueryBasedTable
|
|
|
|
{
|
|
|
|
/** @var IcingaServiceSet */
|
|
|
|
protected $set;
|
|
|
|
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/** @var IcingaHost */
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
/** @var IcingaHost */
|
|
|
|
protected $affectedHost;
|
|
|
|
|
|
|
|
protected $searchColumns = [
|
|
|
|
'service',
|
|
|
|
];
|
|
|
|
|
2019-02-10 11:15:05 +01:00
|
|
|
/** @var bool */
|
|
|
|
protected $readonly = false;
|
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
/** @var string|null */
|
|
|
|
protected $highlightedService;
|
|
|
|
|
2017-08-16 23:27:13 +02:00
|
|
|
/**
|
|
|
|
* @param IcingaServiceSet $set
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function load(IcingaServiceSet $set)
|
|
|
|
{
|
|
|
|
$table = new static($set->getConnection());
|
|
|
|
$table->set = $set;
|
2018-05-05 00:24:49 +02:00
|
|
|
$table->getAttributes()->set('data-base-target', '_self');
|
2017-08-16 23:27:13 +02:00
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IcingaHost $host
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHost(IcingaHost $host)
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IcingaHost $host
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setAffectedHost(IcingaHost $host)
|
|
|
|
{
|
|
|
|
$this->affectedHost = $host;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-10 11:15:05 +01:00
|
|
|
/**
|
|
|
|
* Show no related links
|
|
|
|
*
|
|
|
|
* @param bool $readonly
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setReadonly($readonly = true)
|
|
|
|
{
|
|
|
|
$this->readonly = (bool) $readonly;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 11:41:26 +01:00
|
|
|
public function highlightService($service)
|
|
|
|
{
|
|
|
|
$this->highlightedService = $service;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-05 18:29:17 +02:00
|
|
|
protected function addHeaderColumnsTo(HtmlElement $parent)
|
2017-08-16 23:27:13 +02:00
|
|
|
{
|
|
|
|
if ($this->host || $this->affectedHost) {
|
|
|
|
$this->addHostHeaderTo($parent);
|
|
|
|
} else {
|
|
|
|
parent::addHeaderColumnsTo($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:03:33 +02:00
|
|
|
/**
|
|
|
|
* @param $row
|
2019-02-13 11:41:26 +01:00
|
|
|
* @return BaseHtmlElement
|
2018-05-25 17:03:33 +02:00
|
|
|
*/
|
2017-08-16 23:27:13 +02:00
|
|
|
protected function getServiceLink($row)
|
|
|
|
{
|
2019-02-10 11:15:05 +01:00
|
|
|
if ($this->readonly) {
|
2019-02-13 11:41:26 +01:00
|
|
|
if ($this->highlightedService === $row->service) {
|
|
|
|
return Html::tag('span', ['class' => 'icon-right-big'], $row->service);
|
|
|
|
} else {
|
|
|
|
return $row->service;
|
|
|
|
}
|
2019-02-10 11:15:05 +01:00
|
|
|
}
|
|
|
|
|
2017-08-16 23:27:13 +02:00
|
|
|
if ($this->affectedHost) {
|
|
|
|
$params = [
|
|
|
|
'name' => $this->affectedHost->getObjectName(),
|
|
|
|
'service' => $row->service,
|
|
|
|
'set' => $row->service_set
|
|
|
|
];
|
|
|
|
$url = 'director/host/servicesetservice';
|
|
|
|
} else {
|
|
|
|
$params = [
|
|
|
|
'name' => $row->service,
|
|
|
|
'set' => $row->service_set
|
|
|
|
];
|
|
|
|
$url = 'director/service';
|
|
|
|
}
|
|
|
|
|
|
|
|
return Link::create(
|
|
|
|
$row->service,
|
|
|
|
$url,
|
|
|
|
$params
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderRow($row)
|
|
|
|
{
|
|
|
|
$tr = $this::row([
|
|
|
|
$this->getServiceLink($row)
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($row->disabled === 'y') {
|
2018-05-05 00:24:49 +02:00
|
|
|
$tr->getAttributes()->add('class', 'disabled');
|
2017-08-16 23:27:13 +02:00
|
|
|
}
|
2018-05-25 17:03:33 +02:00
|
|
|
if ($row->blacklisted === 'y') {
|
|
|
|
$tr->getAttributes()->add('class', 'strike-links');
|
|
|
|
}
|
2017-08-16 23:27:13 +02:00
|
|
|
|
|
|
|
return $tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumnsToBeRendered()
|
|
|
|
{
|
|
|
|
return ['Will not be rendered'];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title ?: $this->translate('Servicename');
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:03:33 +02:00
|
|
|
/**
|
|
|
|
* @param HtmlElement $parent
|
|
|
|
*/
|
2018-05-05 18:29:17 +02:00
|
|
|
protected function addHostHeaderTo(HtmlElement $parent)
|
2017-08-16 23:27:13 +02:00
|
|
|
{
|
|
|
|
if (! $this->host) {
|
|
|
|
$deleteLink = '';
|
2019-02-10 11:15:05 +01:00
|
|
|
} elseif ($this->readonly) {
|
|
|
|
$deleteLink = Html::tag('span', [
|
|
|
|
'class' => 'icon-paste',
|
|
|
|
'style' => 'float: right; font-weight: normal',
|
|
|
|
], $this->host->getObjectName());
|
2017-08-16 23:27:13 +02:00
|
|
|
} elseif ($this->affectedHost->get('id') !== $this->host->get('id')) {
|
|
|
|
$host = $this->host;
|
|
|
|
$deleteLink = Link::create(
|
|
|
|
$host->getObjectName(),
|
|
|
|
'director/host/services',
|
|
|
|
['name' => $host->getObjectName()],
|
|
|
|
[
|
|
|
|
'class' => 'icon-paste',
|
|
|
|
'style' => 'float: right; font-weight: normal',
|
|
|
|
'data-base-target' => '_next',
|
|
|
|
'title' => sprintf(
|
|
|
|
$this->translate('This set has been inherited from %s'),
|
|
|
|
$host->getObjectName()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} else {
|
2018-08-06 13:58:29 +02:00
|
|
|
$deleteLink = new RemoveLinkForm(
|
|
|
|
$this->translate('Remove'),
|
2018-10-16 16:09:53 +02:00
|
|
|
sprintf(
|
|
|
|
$this->translate('Remove "%s" from this host'),
|
|
|
|
$this->getTitle()
|
|
|
|
),
|
2018-08-13 11:21:54 +02:00
|
|
|
Url::fromPath('director/host/services', [
|
|
|
|
'name' => $this->host->getObjectName()
|
2018-10-16 16:09:53 +02:00
|
|
|
]),
|
|
|
|
['title' => $this->getTitle()]
|
2018-08-06 13:58:29 +02:00
|
|
|
);
|
2018-10-04 18:30:27 +02:00
|
|
|
$deleteLink->runOnSuccess(function () {
|
|
|
|
$conn = $this->set->getConnection();
|
|
|
|
$db = $conn->getDbAdapter();
|
|
|
|
$query = $db->select()->from(
|
|
|
|
['ss' => 'icinga_service_set'],
|
|
|
|
'ss.id'
|
|
|
|
)->join(
|
|
|
|
['ssih' => 'icinga_service_set_inheritance'],
|
|
|
|
'ssih.service_set_id = ss.id',
|
|
|
|
[]
|
|
|
|
)->where(
|
|
|
|
'ssih.parent_service_set_id = ?',
|
|
|
|
$this->set->get('id')
|
|
|
|
)->where('ss.host_id = ?', $this->host->get('id'));
|
|
|
|
IcingaServiceSet::loadWithAutoIncId(
|
|
|
|
$db->fetchOne($query),
|
|
|
|
$conn
|
|
|
|
)->delete();
|
2018-08-13 11:21:54 +02:00
|
|
|
});
|
2018-08-06 13:58:29 +02:00
|
|
|
$deleteLink->handleRequest();
|
2017-08-16 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$parent->add($this::th([$this->getTitle(), $deleteLink]));
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:03:33 +02:00
|
|
|
/**
|
|
|
|
* @return \Zend_Db_Select
|
|
|
|
* @throws \Zend_Db_Select_Exception
|
|
|
|
*/
|
2017-08-16 23:27:13 +02:00
|
|
|
public function prepareQuery()
|
|
|
|
{
|
2018-05-25 18:27:05 +02:00
|
|
|
$db = $this->db();
|
|
|
|
$query = $db->select()->from(
|
2017-08-16 23:27:13 +02:00
|
|
|
['s' => 'icinga_service'],
|
|
|
|
[
|
|
|
|
'id' => 's.id',
|
|
|
|
'service_set_id' => 's.service_set_id',
|
|
|
|
'host_id' => 'ss.host_id',
|
|
|
|
'service_set' => 'ss.object_name',
|
|
|
|
'service' => 's.object_name',
|
|
|
|
'disabled' => 's.disabled',
|
|
|
|
'object_type' => 's.object_type',
|
|
|
|
]
|
|
|
|
)->joinLeft(
|
|
|
|
['ss' => 'icinga_service_set'],
|
|
|
|
'ss.id = s.service_set_id',
|
|
|
|
[]
|
|
|
|
)->where(
|
|
|
|
's.service_set_id = ?',
|
|
|
|
$this->set->get('id')
|
|
|
|
)->order('s.object_name');
|
2018-05-25 17:03:33 +02:00
|
|
|
|
|
|
|
if ($this->affectedHost) {
|
|
|
|
$query->joinLeft(
|
|
|
|
['hsb' => 'icinga_host_service_blacklist'],
|
2018-05-25 18:27:05 +02:00
|
|
|
$db->quoteInto(
|
|
|
|
's.id = hsb.service_id AND hsb.host_id = ?',
|
|
|
|
$this->affectedHost->get('id')
|
|
|
|
),
|
2018-05-25 17:03:33 +02:00
|
|
|
[]
|
2018-06-01 14:55:14 +02:00
|
|
|
)->columns([
|
2018-05-25 17:03:33 +02:00
|
|
|
'blacklisted' => "CASE WHEN hsb.service_id IS NULL THEN 'n' ELSE 'y' END",
|
|
|
|
]);
|
|
|
|
} else {
|
2018-05-30 00:45:08 +02:00
|
|
|
$query->columns(['blacklisted' => "('n')"]);
|
2018-05-25 17:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
2017-08-16 23:27:13 +02:00
|
|
|
}
|
|
|
|
}
|