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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
<?php
namespace Icinga\Module\Director\Web\Table;
use Icinga\Module\Director\Data\Db\ServiceSetQueryBuilder;
use Icinga\Module\Director\Db;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use Icinga\Module\Director\Forms\RemoveLinkForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
class IcingaServiceSetServiceTable extends ZfQueryBasedTable
{
use TableWithBranchSupport;
/** @var IcingaServiceSet */
protected $set;
protected $title;
/** @var IcingaHost */
protected $host;
/** @var IcingaHost */
protected $affectedHost;
protected $searchColumns = [
'service',
];
/** @var bool */
protected $readonly = false;
/** @var string|null */
protected $highlightedService;
/**
* @param IcingaServiceSet $set
* @return static
*/
public static function load(IcingaServiceSet $set)
{
$table = new static($set->getConnection());
$table->set = $set;
$table->getAttributes()->set('data-base-target', '_self');
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;
}
/**
* 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;
}
/**
* @param $row
* @return BaseHtmlElement
*/
protected function getServiceLink($row)
{
if ($this->readonly) {
if ($this->highlightedService === $row->service) {
return Html::tag('span', ['class' => 'ro-service icon-right-big'], $row->service);
}
return Html::tag('span', ['class' => 'ro-service'], $row->service);
}
if ($this->affectedHost) {
$params = [
'uuid' => $this->affectedHost->getUniqueId()->toString(),
'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)
]);
$classes = $this->getRowClasses($row);
if ($row->disabled === 'y') {
$classes[] = 'disabled';
}
if ($row->blacklisted === 'y') {
$classes[] = 'strike-links';
}
if (! empty($classes)) {
$tr->getAttributes()->add('class', $classes);
}
return $tr;
}
protected function getRowClasses($row)
{
if ($row->branch_uuid !== null) {
return ['branch_modified'];
}
return [];
}
protected function getTitle()
{
return $this->title ?: $this->translate('Servicename');
}
protected function renderTitleColumns()
{
if (! $this->host || ! $this->affectedHost) {
return Html::tag('th', $this->getTitle());
}
if ($this->readonly) {
$link = $this->createFakeRemoveLinkForReadonlyView();
} elseif ($this->affectedHost->get('id') !== $this->host->get('id')) {
$link = $this->linkToHost($this->host);
} else {
$link = $this->createRemoveLinkForm();
}
return $this::th([$this->getTitle(), $link]);
}
/**
* @return \Zend_Db_Select
* @throws \Zend_Db_Select_Exception
*/
public function prepareQuery()
{
$connection = $this->connection();
assert($connection instanceof Db);
$builder = new ServiceSetQueryBuilder($connection, $this->branchUuid);
return $builder->selectServicesForSet($this->set)->limit(100);
}
protected function createFakeRemoveLinkForReadonlyView()
{
return Html::tag('span', [
'class' => 'icon-paste',
'style' => 'float: right; font-weight: normal',
], $this->host->getObjectName());
}
protected function linkToHost(IcingaHost $host)
{
$hostname = $host->getObjectName();
return Link::create($hostname, 'director/host/services', ['name' => $hostname], [
'class' => 'icon-paste',
'style' => 'float: right; font-weight: normal',
'data-base-target' => '_next',
'title' => sprintf(
$this->translate('This set has been inherited from %s'),
$hostname
)
]);
}
protected function createRemoveLinkForm()
{
$deleteLink = new RemoveLinkForm(
$this->translate('Remove'),
sprintf(
$this->translate('Remove "%s" from this host'),
$this->getTitle()
),
Url::fromPath('director/host/services', [
'name' => $this->host->getObjectName()
]),
['title' => $this->getTitle()]
);
$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();
});
$deleteLink->handleRequest();
return $deleteLink;
}
public function removeQueryLimit()
{
$query = $this->getQuery();
$query->reset($query::LIMIT_OFFSET);
$query->reset($query::LIMIT_COUNT);
return $this;
}
}
|