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
|
<?php
namespace Icinga\Module\Director\Web\Widget;
use Icinga\Module\Director\Auth\Permission;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use gipfl\IcingaWeb2\Icon;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Web\Table\FilterableByUsage;
class AdditionalTableActions
{
use TranslationHelper;
/** @var Auth */
protected $auth;
/** @var Url */
protected $url;
/** @var ZfQueryBasedTable */
protected $table;
public function __construct(Auth $auth, Url $url, ZfQueryBasedTable $table)
{
$this->auth = $auth;
$this->url = $url;
$this->table = $table;
}
public function appendTo(HtmlDocument $parent)
{
$links = [];
if ($this->hasPermission(Permission::ADMIN)) {
$links[] = $this->createDownloadJsonLink();
}
if ($this->hasPermission(Permission::SHOW_SQL)) {
$links[] = $this->createShowSqlToggle();
}
if ($this->table instanceof FilterableByUsage) {
$parent->add($this->showUsageFilter($this->table));
}
if (! empty($links)) {
$parent->add($this->moreOptions($links));
}
return $this;
}
protected function createDownloadJsonLink()
{
return Link::create(
$this->translate('Download as JSON'),
$this->url->with('format', 'json'),
null,
['target' => '_blank']
);
}
protected function createShowSqlToggle()
{
if ($this->url->getParam('format') === 'sql') {
$link = Link::create(
$this->translate('Hide SQL'),
$this->url->without('format')
);
} else {
$link = Link::create(
$this->translate('Show SQL'),
$this->url->with('format', 'sql')
);
}
return $link;
}
protected function showUsageFilter(FilterableByUsage $table)
{
$active = $this->url->getParam('usage', 'all');
$links = [
Link::create($this->translate('all'), $this->url->without('usage')),
Link::create($this->translate('used'), $this->url->with('usage', 'used')),
Link::create($this->translate('unused'), $this->url->with('usage', 'unused')),
];
if ($active === 'used') {
$table->showOnlyUsed();
} elseif ($active === 'unused') {
$table->showOnlyUnUsed();
}
$options = $this->ul(
$this->li([
Link::create(
sprintf($this->translate('Usage (%s)'), $active),
'#',
null,
[
'class' => 'icon-sitemap'
]
),
$subUl = Html::tag('ul')
]),
['class' => 'nav']
);
foreach ($links as $link) {
$subUl->add($this->li($link));
}
return $options;
}
protected function moreOptions($links)
{
$options = $this->ul(
$this->li([
// TODO: extend link for dropdown-toggle from Web 2, doesn't
// seem to work: [..], null, ['class' => 'dropdown-toggle']
Link::create(Icon::create('down-open'), '#'),
$subUl = Html::tag('ul')
]),
['class' => 'nav']
);
foreach ($links as $link) {
$subUl->add($this->li($link));
}
return $options;
}
protected function ulLi($content)
{
return $this->ul($this->li($content));
}
protected function ul($content, $attributes = null)
{
return Html::tag('ul', $attributes, $content);
}
protected function li($content)
{
return Html::tag('li', null, $content);
}
protected function hasPermission($permission)
{
return $this->auth->hasPermission($permission);
}
}
|