-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveRotationForm.php
More file actions
161 lines (137 loc) · 4.79 KB
/
Copy pathMoveRotationForm.php
File metadata and controls
161 lines (137 loc) · 4.79 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
<?php
// SPDX-FileCopyrightText: 2024 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Forms;
use DateTime;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Notifications\Model\Rotation;
use Icinga\Web\Session;
use ipl\Html\Form;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use ipl\Sql\Select;
use ipl\Stdlib\Filter;
use ipl\Web\Common\CsrfCounterMeasure;
use LogicException;
class MoveRotationForm extends Form
{
use CsrfCounterMeasure;
protected $defaultAttributes = ['hidden' => true];
protected $method = 'POST';
/** @var Connection */
protected $db;
/** @var int */
protected $scheduleId;
/**
* Create a new MoveRotationForm
*
* @param ?Connection $db
*/
public function __construct(?Connection $db = null)
{
$this->db = $db;
}
/**
* Get the schedule ID
*
* @return int
*/
public function getScheduleId(): int
{
if ($this->scheduleId === null) {
throw new LogicException('The form must be successfully submitted first');
}
return $this->scheduleId;
}
public function getMessages()
{
$messages = parent::getMessages();
foreach ($this->getElements() as $element) {
foreach ($element->getMessages() as $message) {
$messages[] = sprintf('%s: %s', $element->getName(), $message);
}
}
return $messages;
}
protected function assemble()
{
$this->addElement('hidden', 'rotation', ['required' => true]);
$this->addElement('hidden', 'priority', ['required' => true]);
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
}
protected function onError()
{
$this->removeAttribute('hidden');
parent::onError();
}
protected function onSuccess()
{
$rotationId = $this->getValue('rotation');
$newPriority = $this->getValue('priority');
/** @var ?Rotation $rotation */
$rotation = Rotation::on($this->db)
->columns(['schedule_id', 'priority'])
->filter(Filter::equal('id', $rotationId))
->first();
if ($rotation === null) {
throw new HttpNotFoundException('Rotation not found');
}
$transactionStarted = ! $this->db->inTransaction();
if ($transactionStarted) {
$this->db->beginTransaction();
}
$this->scheduleId = $rotation->schedule_id;
$changedAt = (int) (new DateTime())->format("Uv");
// Free up the current priority used by the rotation in question
$this->db->update('rotation', ['priority' => null, 'deleted' => 'y'], ['id = ?' => $rotationId]);
// Update the priorities of the rotations that are affected by the move
if ($newPriority < $rotation->priority) {
$affectedRotations = $this->db->select(
(new Select())
->columns('id')
->from('rotation')
->where([
'schedule_id = ?' => $rotation->schedule_id,
'priority >= ?' => $newPriority,
'priority < ?' => $rotation->priority
])
->orderBy('priority DESC')
);
foreach ($affectedRotations as $rotation) {
$this->db->update(
'rotation',
['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt],
['id = ?' => $rotation->id]
);
}
} elseif ($newPriority > $rotation->priority) {
$affectedRotations = $this->db->select(
(new Select())
->columns('id')
->from('rotation')
->where([
'schedule_id = ?' => $rotation->schedule_id,
'priority > ?' => $rotation->priority,
'priority <= ?' => $newPriority
])
->orderBy('priority ASC')
);
foreach ($affectedRotations as $rotation) {
$this->db->update(
'rotation',
['priority' => new Expression('priority - 1'), 'changed_at' => $changedAt],
['id = ?' => $rotation->id]
);
}
}
// Now insert the rotation at the new priority
$this->db->update(
'rotation',
['priority' => $newPriority, 'changed_at' => $changedAt, 'deleted' => 'n'],
['id = ?' => $rotationId]
);
if ($transactionStarted) {
$this->db->commitTransaction();
}
}
}