1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
<?php
namespace Icinga\Module\Director\Web\Table;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\Objects\IcingaHost;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Table\Extension\MultiSelect;
use gipfl\IcingaWeb2\Link;
use Ramsey\Uuid\Uuid;
class ObjectsTableService extends ObjectsTable
{
use MultiSelect;
/** @var IcingaHost */
protected $host;
protected $type = 'service';
protected $title;
/** @var IcingaHost */
protected $inheritedBy;
/** @var bool */
protected $readonly = false;
/** @var string|null */
protected $highlightedService;
protected $columns = [
'object_name' => 'o.object_name',
'disabled' => 'o.disabled',
'host' => 'h.object_name',
'host_id' => 'h.id',
'host_object_type' => 'h.object_type',
'host_disabled' => 'h.disabled',
'id' => 'o.id',
'uuid' => 'o.uuid',
'blacklisted' => "CASE WHEN hsb.service_id IS NULL THEN 'n' ELSE 'y' END",
];
protected $searchColumns = [
'o.object_name',
'h.object_name'
];
public function assemble()
{
$this->enableMultiSelect(
'director/services/edit',
'director/services',
['uuid']
);
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setHost(IcingaHost $host)
{
$this->host = $host;
$this->getAttributes()->set('data-base-target', '_self');
return $this;
}
public function setInheritedBy(IcingaHost $host)
{
$this->inheritedBy = $host;
return $this;
}
/**
* 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;
}
public function getColumnsToBeRendered()
{
if ($this->title) {
return [$this->title];
}
if ($this->host) {
return [$this->translate('Servicename')];
}
return [
'host' => $this->translate('Host'),
'object_name' => $this->translate('Service Name'),
];
}
public function renderRow($row)
{
$caption = $row->host === null
? Html::tag('span', ['class' => 'error'], '- none -')
: $row->host;
$hostField = static::td($caption);
if ($row->host === null) {
$hostField->getAttributes()->add('class', 'error');
}
if ($this->host) {
$tr = static::tr([
static::td($this->getServiceLink($row))
]);
} else {
$tr = static::tr([
$hostField,
static::td($this->getServiceLink($row))
]);
}
$attributes = $tr->getAttributes();
$classes = $this->getRowClasses($row);
if ($row->host_disabled === 'y' || $row->disabled === 'y') {
$classes[] = 'disabled';
}
if ($row->blacklisted === 'y') {
$classes[] = 'strike-links';
}
$attributes->add('class', $classes);
return $tr;
}
protected function getInheritedServiceLink($row, $target)
{
$params = [
'name' => $target->object_name,
'service' => $row->object_name,
'inheritedFrom' => $row->host,
];
return Link::create(
$row->object_name,
'director/host/inheritedservice',
$params
);
}
protected function getServiceLink($row)
{
if ($this->readonly) {
if ($this->highlightedService === $row->object_name) {
return Html::tag('span', ['class' => 'icon-right-big'], $row->object_name);
} else {
return $row->object_name;
}
}
$params = [
'uuid' => Uuid::fromBytes(DbUtil::binaryResult($row->uuid))->toString(),
];
if ($row->host !== null) {
$params['host'] = $row->host;
}
if ($target = $this->inheritedBy) {
return $this->getInheritedServiceLink($row, $target);
}
return Link::create(
$row->object_name,
'director/service/edit',
$params
);
}
public function prepareQuery()
{
$query = parent::prepareQuery();
if ($this->branchUuid) {
$queries = [$this->leftSubQuery, $this->rightSubQuery];
} else {
$queries = [$query];
}
foreach ($queries as $subQuery) {
$subQuery->joinLeft(
['h' => 'icinga_host'],
'o.host_id = h.id',
[]
)->joinLeft(
['hsb' => 'icinga_host_service_blacklist'],
'hsb.service_id = o.id AND hsb.host_id = o.host_id',
[]
)->where('o.service_set_id IS NULL')
->group(['o.id', 'h.id','hsb.service_id', 'hsb.host_id'])
->order('o.object_name')->order('h.object_name');
if ($this->branchUuid) {
$subQuery->where('bo.service_set IS NULL')
->group(['bo.uuid', 'bo.branch_uuid']);
}
if ($this->host) {
if ($this->branchUuid) {
$subQuery->where('COALESCE(h.object_name, bo.host) = ?', $this->host->getObjectName());
} else {
$subQuery->where('h.id = ?', $this->host->get('id'));
}
}
}
return $query;
}
}
|