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
|
<?php
namespace Icinga\Module\Director\Web\Table;
use gipfl\IcingaWeb2\Table\Extension\MultiSelect;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
use Icinga\Module\Director\Objects\IcingaObjectGroup;
use Exception;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
class GroupMemberTable extends ZfQueryBasedTable
{
use MultiSelect;
protected $searchColumns = [
'o.object_name',
// membership_type
];
protected $type;
/** @var IcingaObjectGroup */
protected $group;
/**
* @param $type
* @param Db $db
* @return static
*/
public static function create($type, Db $db)
{
$class = __NAMESPACE__ . '\\GroupMemberTable' . ucfirst($type);
if (! class_exists($class)) {
$class = __CLASS__;
}
/** @var static $table */
$table = new $class($db);
$table->type = $type;
return $table;
}
public function assemble()
{
if ($this->type === 'host') {
$this->enableMultiSelect(
'director/hosts/edit',
'director/hosts',
['name']
);
}
}
public function setGroup(IcingaObjectGroup $group)
{
$this->group = $group;
return $this;
}
public function getType()
{
return $this->type;
}
public function getColumnsToBeRendered()
{
if ($this->group === null) {
return [
$this->translate('Group'),
$this->translate('Member'),
$this->translate('via')
];
} else {
return [
$this->translate('Member'),
$this->translate('via')
];
}
}
public function renderRow($row)
{
$type = $this->getType();
if ($row->object_type === 'apply') {
$params = [
'id' => $row->id
];
} elseif (isset($row->host_id)) {
// I would prefer to see host=<name> and set=<name>, but joining
// them here is pointless. We should use DeferredHtml for these,
// remember hosts/sets we need and fetch them in a single query at
// rendering time. For now, this works fine - just... the URLs are
// not so nice
$params = [
'name' => $row->object_name,
'host_id' => $row->host_id
];
} elseif (isset($row->service_set_id)) {
$params = [
'name' => $row->object_name,
'set_id' => $row->service_set_id
];
} else {
$params = [
'name' => $row->object_name
];
}
$url = Url::fromPath("director/${type}", $params);
$tr = $this::tr();
if ($this->group === null) {
$tr->add($this::td($row->group_name));
}
$link = Link::create($row->object_name, $url);
if ($row->object_type === 'apply') {
$link = [
$link,
' (where ',
$this->renderApplyFilter($row->assign_filter),
')'
];
}
$tr->add([
$this::td($link),
$this::td($row->membership_type)
]);
return $tr;
}
protected function renderApplyFilter($assignFilter)
{
try {
$string = AssignRenderer::forFilter(
Filter::fromQueryString($assignFilter)
)->renderAssign();
// Do not prefix it
$string = preg_replace('/^assign where /', '', $string);
} catch (Exception $e) {
// ignore errors in filter rendering
$string = 'Error in Filter rendering: ' . $e->getMessage();
}
return $string;
}
protected function prepareQuery()
{
// select h.object_name, hg.object_name,
// CASE WHEN hgh.host_id IS NULL THEN 'apply' ELSE 'direct' END AS assi
// from icinga_hostgroup_host_resolved hgr join icinga_host h on h.id = hgr.host_id
// join icinga_hostgroup hg on hgr.hostgroup_id = hg.id
// left join icinga_hostgroup_host hgh on hgh.host_id = h.id and hgh.hostgroup_id = hg.id;
$type = $this->getType();
$columns = [
'o.id',
'o.object_type',
'o.object_name',
'membership_type' => "CASE WHEN go.${type}_id IS NULL THEN 'apply' ELSE 'direct' END"
];
if ($this->group === null) {
$columns = ['group_name' => 'g.object_name'] + $columns;
}
if ($type === 'service') {
$columns[] = 'o.assign_filter';
$columns[] = 'o.host_id';
$columns[] = 'o.service_set_id';
}
$query = $this->db()->select()->from(
['gro' => "icinga_${type}group_${type}_resolved"],
$columns
)->join(
['o' => "icinga_${type}"],
"o.id = gro.${type}_id",
[]
)->join(
['g' => "icinga_${type}group"],
"gro.${type}group_id = g.id",
[]
)->joinLeft(
['go' => "icinga_${type}group_${type}"],
"go.${type}_id = o.id AND go.${type}group_id = g.id",
[]
)->order('o.object_name');
if ($this->group !== null) {
$query->where('g.id = ?', $this->group->get('id'));
}
return $query;
}
}
|