Skip to content

Commit de24f3c

Browse files
committed
Utilize ScheduleRepository where suitable
1 parent f1f9c89 commit de24f3c

2 files changed

Lines changed: 56 additions & 117 deletions

File tree

application/controllers/ScheduleController.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Icinga\Module\Notifications\Forms\ScheduleForm;
1515
use Icinga\Module\Notifications\Model\Schedule;
1616
use Icinga\Module\Notifications\Repository\RotationRepository;
17+
use Icinga\Module\Notifications\Repository\ScheduleRepository;
1718
use Icinga\Module\Notifications\Widget\Detail\ScheduleDetail;
1819
use Icinga\Module\Notifications\Widget\TimezoneWarning;
1920
use Icinga\Web\Session;
@@ -43,11 +44,7 @@ public function indexAction(): void
4344
{
4445
$id = (int) $this->params->getRequired('id');
4546

46-
$query = Schedule::on(Database::get())
47-
->filter(Filter::equal('schedule.id', $id));
48-
49-
/** @var ?Schedule $schedule */
50-
$schedule = $query->first();
47+
$schedule = (new ScheduleRepository(Database::get()))->find($id);
5148
if ($schedule === null) {
5249
$this->httpNotFound(t('Schedule not found'));
5350
}
@@ -115,20 +112,31 @@ public function settingsAction(): void
115112
$this->setTitle($this->translate('Edit Schedule'));
116113
$scheduleId = (int) $this->params->getRequired('id');
117114

118-
$form = new ScheduleForm(Database::get());
115+
$schedule = (new ScheduleRepository(Database::get()))->find($scheduleId);
116+
if ($schedule === null) {
117+
$this->httpNotFound(t('Schedule not found'));
118+
}
119+
120+
$form = new ScheduleForm();
119121
$form->setShowRemoveButton();
120-
$form->loadSchedule($scheduleId);
122+
$form->setSchedule($schedule);
121123
$form->setSubmitLabel($this->translate('Save Changes'));
122124
$form->setAction($this->getRequest()->getUrl()->getAbsoluteUrl());
123125
$form->on(Form::ON_SUBMIT, function ($form) use ($scheduleId) {
124-
$form->editSchedule($scheduleId);
126+
$schedule = $form->getSchedule();
127+
Database::get()->transaction(function (Connection $db) use ($schedule) {
128+
(new ScheduleRepository($db))->update($schedule);
129+
});
125130

126131
$this->sendExtraUpdates(['#col1']);
127132
$this->redirectNow(Links::schedule($scheduleId));
128133
});
129-
$form->on(Form::ON_SENT, function ($form) use ($scheduleId) {
134+
$form->on(Form::ON_SENT, function (ScheduleForm $form) use ($scheduleId) {
130135
if ($form->hasBeenRemoved()) {
131-
$form->removeSchedule($scheduleId);
136+
$schedule = $form->getSchedule();
137+
Database::get()->transaction(function (Connection $db) use ($schedule) {
138+
(new ScheduleRepository($db))->delete($schedule);
139+
});
132140

133141
$this->redirectNow('__CLOSE__');
134142
}
@@ -142,15 +150,18 @@ public function settingsAction(): void
142150
public function addAction(): void
143151
{
144152
$this->setTitle($this->translate('Create Schedule'));
145-
$form = (new ScheduleForm(Database::get()))
153+
$form = (new ScheduleForm())
146154
->setShowTimezoneSuggestionInput()
147155
->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl())
148156
->on(Form::ON_SUBMIT, function (ScheduleForm $form) {
149-
$scheduleId = $form->addSchedule();
157+
$schedule = $form->getSchedule();
158+
Database::get()->transaction(function (Connection $db) use ($schedule) {
159+
(new ScheduleRepository($db))->create($schedule);
160+
});
150161

151162
$this->sendExtraUpdates(['#col1']);
152163
$this->getResponse()->setHeader('X-Icinga-Container', 'col2');
153-
$this->redirectNow(Links::schedule($scheduleId));
164+
$this->redirectNow(Links::schedule($schedule->id));
154165
})
155166
->handleRequest($this->getServerRequest());
156167

application/forms/ScheduleForm.php

Lines changed: 32 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@
77

88
use DateTime;
99
use DateTimeZone;
10-
use Icinga\Exception\Http\HttpNotFoundException;
11-
use Icinga\Module\Notifications\Model\Rotation;
12-
use Icinga\Module\Notifications\Model\RuleEscalationRecipient;
1310
use Icinga\Module\Notifications\Model\Schedule;
14-
use Icinga\Module\Notifications\Repository\RotationRepository;
1511
use Icinga\Web\Session;
1612
use IntlTimeZone;
1713
use ipl\Html\Attributes;
1814
use ipl\Html\HtmlDocument;
1915
use ipl\Html\HtmlElement;
2016
use ipl\Html\Text;
21-
use ipl\Sql\Connection;
22-
use ipl\Stdlib\Filter;
2317
use ipl\Validator\CallbackValidator;
2418
use ipl\Web\Common\CsrfCounterMeasure;
2519
use ipl\Web\Compat\CompatForm;
@@ -30,22 +24,14 @@ class ScheduleForm extends CompatForm
3024
{
3125
use CsrfCounterMeasure;
3226

27+
protected Schedule $schedule;
28+
3329
protected ?string $submitLabel = null;
3430

3531
protected bool $showRemoveButton = false;
3632

3733
protected bool $showTimezoneSuggestionInput = false;
3834

39-
private Connection $db;
40-
41-
private ?int $scheduleId = null;
42-
43-
public function __construct(Connection $db)
44-
{
45-
$this->db = $db;
46-
$this->applyDefaultElementDecorators();
47-
}
48-
4935
public function setSubmitLabel(string $label): static
5036
{
5137
$this->submitLabel = $label;
@@ -87,93 +73,34 @@ public function hasBeenRemoved(): bool
8773
return $csrf !== null && $csrf->isValid() && $btn !== null && $btn->getName() === 'delete';
8874
}
8975

90-
public function loadSchedule(int $id): void
91-
{
92-
$this->scheduleId = $id;
93-
$this->populate($this->fetchDbValues());
94-
}
95-
96-
public function addSchedule(): int
76+
public function __construct()
9777
{
98-
return $this->db->transaction(function (Connection $db) {
99-
$db->insert('schedule', [
100-
'name' => $this->getValue('name'),
101-
'changed_at' => (int) (new DateTime())->format("Uv"),
102-
'timezone' => $this->getValue('timezone')
103-
]);
78+
$this->schedule = new Schedule();
10479

105-
return $db->lastInsertId();
106-
});
80+
$this->applyDefaultElementDecorators();
10781
}
10882

109-
public function editSchedule(int $id): void
83+
public function setSchedule(Schedule $schedule): static
11084
{
111-
$this->db->beginTransaction();
112-
113-
$values = $this->getValues();
114-
$storedValues = $this->fetchDbValues();
115-
116-
if ($values === $storedValues) {
117-
return;
118-
}
119-
120-
$this->db->update('schedule', [
121-
'name' => $values['name'],
122-
'changed_at' => (int) (new DateTime())->format("Uv")
123-
], ['id = ?' => $id]);
85+
$this->schedule = $schedule;
86+
$this->populate($this->fetchDbValues());
12487

125-
$this->db->commitTransaction();
88+
return $this;
12689
}
12790

128-
public function removeSchedule(int $id): void
91+
public function getSchedule(): Schedule
12992
{
130-
$this->db->beginTransaction();
131-
132-
$rotations = Rotation::on($this->db)
133-
->columns(['id', 'schedule_id', 'priority', 'timeperiod.id'])
134-
->filter(Filter::equal('schedule_id', $id))
135-
->orderBy('priority', SORT_DESC);
136-
137-
/** @var Rotation $rotation */
138-
foreach ($rotations as $rotation) {
139-
(new RotationRepository($this->db))->delete($rotation);
93+
// TODO: ! $this->schedule->isNew() &&
94+
if (! $this->hasChanges()) {
95+
return $this->schedule;
14096
}
14197

142-
$markAsDeleted = ['changed_at' => (int) (new DateTime())->format("Uv"), 'deleted' => 'y'];
143-
144-
$escalationIds = $this->db->fetchCol(
145-
RuleEscalationRecipient::on($this->db)
146-
->columns('rule_escalation_id')
147-
->filter(Filter::equal('schedule_id', $id))
148-
->assembleSelect()
149-
);
150-
151-
$this->db->update('rule_escalation_recipient', $markAsDeleted, ['schedule_id = ?' => $id]);
152-
153-
if (! empty($escalationIds)) {
154-
$escalationIdsWithOtherRecipients = $this->db->fetchCol(
155-
RuleEscalationRecipient::on($this->db)
156-
->columns('rule_escalation_id')
157-
->filter(Filter::all(
158-
Filter::equal('rule_escalation_id', $escalationIds),
159-
Filter::unequal('schedule_id', $id)
160-
))->assembleSelect()
161-
);
162-
163-
$toRemoveEscalations = array_diff($escalationIds, $escalationIdsWithOtherRecipients);
164-
165-
if (! empty($toRemoveEscalations)) {
166-
$this->db->update(
167-
'rule_escalation',
168-
$markAsDeleted + ['position' => null],
169-
['id IN (?)' => $toRemoveEscalations]
170-
);
171-
}
98+
$this->schedule->name = $this->getValue('name');
99+
if ($this->showTimezoneSuggestionInput) {
100+
$this->schedule->timezone = $this->getValue('timezone');
172101
}
173102

174-
$this->db->update('schedule', $markAsDeleted, ['id = ?' => $id]);
175-
176-
$this->db->commitTransaction();
103+
return $this->schedule;
177104
}
178105

179106
protected function assemble(): void
@@ -256,23 +183,24 @@ protected function assemble(): void
256183
* Fetch the values from the database
257184
*
258185
* @return string[]
259-
*
260-
* @throws HttpNotFoundException
261186
*/
262187
private function fetchDbValues(): array
263188
{
264-
/** @var ?Schedule $schedule */
265-
$schedule = Schedule::on($this->db)
266-
->columns($this->showTimezoneSuggestionInput ? ['name', 'timezone'] : 'name')
267-
->filter(Filter::equal('id', $this->scheduleId))
268-
->first();
269-
270-
if ($schedule === null) {
271-
throw new HttpNotFoundException($this->translate('Schedule not found'));
272-
}
273-
274189
return $this->showTimezoneSuggestionInput
275-
? ['name' => $schedule->name, 'timezone' => $schedule->timezone]
276-
: ['name' => $schedule->name];
190+
? ['name' => $this->schedule->name, 'timezone' => $this->schedule->timezone]
191+
: ['name' => $this->schedule->name];
192+
}
193+
194+
/**
195+
* Check if the user changed something
196+
*
197+
* @return bool
198+
*/
199+
private function hasChanges(): bool
200+
{
201+
$values = $this->getValues();
202+
$storedValues = $this->fetchDbValues();
203+
204+
return $values !== $storedValues;
277205
}
278206
}

0 commit comments

Comments
 (0)