77
88use DateTime ;
99use DateTimeZone ;
10- use Icinga \Exception \Http \HttpNotFoundException ;
11- use Icinga \Module \Notifications \Model \Rotation ;
12- use Icinga \Module \Notifications \Model \RuleEscalationRecipient ;
1310use Icinga \Module \Notifications \Model \Schedule ;
14- use Icinga \Module \Notifications \Repository \RotationRepository ;
1511use Icinga \Web \Session ;
1612use IntlTimeZone ;
1713use ipl \Html \Attributes ;
1814use ipl \Html \HtmlDocument ;
1915use ipl \Html \HtmlElement ;
2016use ipl \Html \Text ;
21- use ipl \Sql \Connection ;
22- use ipl \Stdlib \Filter ;
2317use ipl \Validator \CallbackValidator ;
2418use ipl \Web \Common \CsrfCounterMeasure ;
2519use 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