-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleForm.php
More file actions
147 lines (114 loc) · 4.01 KB
/
Copy pathScheduleForm.php
File metadata and controls
147 lines (114 loc) · 4.01 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
<?php
/* Icinga Notifications Web | (c) 2023 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Notifications\Forms;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Model\Schedule;
use Icinga\Module\Notifications\Model\Timeperiod;
use Icinga\Web\Session;
use ipl\Html\HtmlDocument;
use ipl\Stdlib\Filter;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Compat\CompatForm;
class ScheduleForm extends CompatForm
{
use CsrfCounterMeasure;
/** @var string */
protected $submitLabel;
/** @var bool */
protected $showRemoveButton = false;
public function setSubmitLabel(string $label): self
{
$this->submitLabel = $label;
return $this;
}
public function getSubmitLabel(): string
{
return $this->submitLabel ?? $this->translate('Create Schedule');
}
public function setShowRemoveButton(bool $state = true): self
{
$this->showRemoveButton = $state;
return $this;
}
public function hasBeenCancelled(): bool
{
$btn = $this->getPressedSubmitElement();
return $btn !== null && $btn->getName() === 'cancel';
}
public function hasBeenRemoved(): bool
{
$btn = $this->getPressedSubmitElement();
$csrf = $this->getElement('CSRFToken');
return $csrf !== null && $csrf->isValid() && $btn !== null && $btn->getName() === 'remove';
}
public function loadSchedule(int $id): void
{
$db = Database::get();
$schedule = Schedule::on($db)
->filter(Filter::equal('id', $id))
->first();
if ($schedule === null) {
throw new HttpNotFoundException($this->translate('Schedule not found'));
}
$this->populate(['name' => $schedule->name]);
}
public function addSchedule(): int
{
$db = Database::get();
$db->insert('schedule', [
'name' => $this->getValue('name')
]);
return $db->lastInsertId();
}
public function editSchedule(int $id): void
{
$db = Database::get();
$db->update('schedule', [
'name' => $this->getValue('name')
], ['id = ?' => $id]);
}
public function removeSchedule(int $id): void
{
$db = Database::get();
$db->beginTransaction();
$timeperiods = Timeperiod::on($db)
->filter(Filter::equal('owned_by_schedule_id', $id));
foreach ($timeperiods as $timeperiod) {
$db->delete('timeperiod_entry', ['timeperiod_id = ?' => $timeperiod->id]);
$db->delete('schedule_member', ['timeperiod_id = ?' => $timeperiod->id]);
$db->delete('timeperiod', ['id = ?' => $timeperiod->id]);
}
$db->delete('schedule', ['id = ?' => $id]);
$db->commitTransaction();
}
protected function assemble()
{
$this->addElement('text', 'name', [
'required' => true,
'label' => $this->translate('Name')
]);
$this->addElement('submit', 'submit', [
'label' => $this->getSubmitLabel()
]);
$additionalButtons = [];
if ($this->showRemoveButton) {
$removeBtn = $this->createElement('submit', 'remove', [
'label' => $this->translate('Delete Schedule'),
'class' => 'btn-remove',
'formnovalidate' => true
]);
$this->registerElement($removeBtn);
$additionalButtons[] = $removeBtn;
}
$cancelBtn = $this->createElement('submit', 'cancel', [
'label' => $this->translate('Cancel'),
'class' => 'btn-cancel btn-default',
'formnovalidate' => true
]);
$this->registerElement($cancelBtn);
$additionalButtons[] = $cancelBtn;
$this->getElement('submit')->prependWrapper((new HtmlDocument())->setHtmlContent(...$additionalButtons));
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
}
}