-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathReportForm.php
More file actions
230 lines (188 loc) · 7.32 KB
/
Copy pathReportForm.php
File metadata and controls
230 lines (188 loc) · 7.32 KB
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
<?php
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting\Web\Forms;
use Icinga\Authentication\Auth;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Hook\ReportHook;
use Icinga\Module\Reporting\ProvidedReports;
use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\Form;
use ipl\Html\FormElement\Collection;
use ipl\Web\Compat\CompatForm;
use ipl\Web\FormDecorator\IcingaFormDecorator;
use ipl\Web\Widget\Icon;
class ReportForm extends CompatForm
{
use Database;
use ProvidedReports;
protected $id;
/**
* Create a new form instance with the given report id
*
* @param $id
*
* @return static
*/
public static function fromId($id): self
{
$form = new static();
$form->id = $id;
return $form;
}
public function hasBeenSubmitted(): bool
{
return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove'));
}
public function __construct()
{
$this->on(static::ON_SENT, function () {
if ($this->getPressedSubmitElement() && $this->getPressedSubmitElement()->getName() === 'remove') {
$this->getDb()->delete('report', ['id = ?' => $this->id]);
}
});
}
protected function assemble()
{
$this->setDefaultElementDecorator(new CompatDecorator());
$this->addElement('text', 'name', [
'required' => true,
'label' => $this->translate('Name'),
'description' => $this->translate(
'A unique name of this report. It is used when exporting to pdf, json or csv format'
. ' and also when listing the reports in the cli'
)
]);
$this->addElement('select', 'timeframe', [
'required' => true,
'class' => 'autosubmit',
'label' => $this->translate('Timeframe'),
'options' => [null => $this->translate('Please choose')] + $this->listTimeframes(),
'description' => $this->translate(
'Specifies the time frame in which this report is to be generated'
)
]);
$this->addElement('select', 'template', [
'label' => $this->translate('Template'),
'options' => [null => $this->translate('Please choose')] + $this->listTemplates(),
'description' => $this->translate(
'Specifies the template to use when exporting this report to pdf. (Default Icinga template)'
)
]);
$collection = (new Collection('reportlet'))
->setLabel('Reportlets')
->setAddElement('select', 'reportlet_class', [
'required' => false,
'label' => 'Reportlet',
'options' => [null => 'Please choose'] + $this->listReports(),
'class' => 'autosubmit'
])
->setRemoveElement('submitButton', 'remove_reportlet', [
'label' => new Icon('trash'),
'class' => 'btn-remove-reportlet',
'formnovalidate' => true,
'title' => 'Remove Reportlet'
]);
$collection->onAssembleGroup(function ($group, $addElement, $removeElement) {
$group->setDefaultElementDecorator(new IcingaFormDecorator());
$this->decorate($addElement);
$group
->registerElement($addElement)
->addHtml($addElement)
->registerElement($removeElement);
$addElement->getWrapper()->ensureAssembled()->add($removeElement);
$reportletClass = $group->getPopulatedValue('reportlet_class');
if (! empty($reportletClass)) {
$config = new Form();
/** @var ReportHook $reportlet */
$reportlet = new $reportletClass();
$reportlet->initConfigForm($config);
foreach ($config->getElements() as $element) {
$this->decorate($element);
$group
->registerElement($element)
->addHtml($element);
}
}
});
$this->registerElement($collection);
$this->add($collection);
$this->addElement('submit', 'submit', [
'label' => $this->id === null ? 'Create Report' : 'Update Report'
]);
$this->setSubmitButton($this->getElement('submit'));
if ($this->id !== null) {
/** @var FormSubmitElement $removeButton */
$removeButton = $this->createElement('submit', 'remove', [
'label' => 'Remove Report',
'class' => 'btn-remove',
'formnovalidate' => true
]);
$this->registerElement($removeButton);
$this->getElement('submit')->getWrapper()->prepend($removeButton);
}
}
public function onSuccess()
{
$db = $this->getDb();
if ($this->getPopulatedValue('remove')) {
$db->delete('report', ['id = ?' => $this->id]);
return;
}
$values = $this->getValues();
$now = time() * 1000;
$db->beginTransaction();
if ($this->id === null) {
$db->insert('report', [
'name' => $values['name'],
'author' => Auth::getInstance()->getUser()->getUsername(),
'timeframe_id' => $values['timeframe'],
'template_id' => $values['template'],
'ctime' => $now,
'mtime' => $now
]);
$reportId = $db->lastInsertId();
} else {
$db->update('report', [
'name' => $values['name'],
'timeframe_id' => $values['timeframe'],
'template_id' => $values['template'],
'mtime' => $now
], ['id = ?' => $this->id]);
$reportId = $this->id;
}
if ($this->id !== null) {
$db->delete('reportlet', ['report_id = ?' => $reportId]);
}
foreach ($this->getPopulatedValue('reportlet') as $reportlet) {
array_walk($reportlet, function (&$value) {
if ($value === '') {
$value = null;
}
});
if (empty($reportlet['reportlet_class'])) {
continue;
}
$db->insert('reportlet', [
'report_id' => $reportId,
'class' => $reportlet['reportlet_class'],
'ctime' => $now,
'mtime' => $now
]);
$reportletId = $db->lastInsertId();
foreach ($reportlet as $key => $value) {
if ($key === 'reportlet_class') {
continue;
}
$db->insert('config', [
'reportlet_id' => $reportletId,
'name' => $key,
'value' => $value,
'ctime' => $now,
'mtime' => $now
]);
}
}
$db->commitTransaction();
}
}