Skip to content

Commit 7ace1dd

Browse files
committed
schedule: Allow to duplicate the entire schedule
1 parent 813e7b9 commit 7ace1dd

2 files changed

Lines changed: 59 additions & 9 deletions

File tree

application/controllers/ScheduleController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,22 @@ public function settingsAction(): void
121121
$form->setShowRemoveButton();
122122
$form->setSchedule($schedule);
123123
$form->setSubmitLabel($this->translate('Save Changes'));
124-
$form->setAction($this->getRequest()->getUrl()->getAbsoluteUrl());
125-
$form->on(Form::ON_SUBMIT, function ($form) use ($scheduleId) {
124+
$form->setAction($this->getRequest()->getUrl()->setParam('showCompact')->getAbsoluteUrl());
125+
$form->on(Form::ON_SUBMIT, function (ScheduleForm $form) {
126126
$schedule = $form->getSchedule();
127-
Database::get()->transaction(function (Connection $db) use ($schedule) {
128-
(new ScheduleRepository($db))->update($schedule);
129-
});
127+
128+
if ($form->hasBeenDuplicated()) {
129+
Database::get()->transaction(function (Connection $db) use ($schedule) {
130+
(new ScheduleRepository($db))->duplicate($schedule);
131+
});
132+
} else {
133+
Database::get()->transaction(function (Connection $db) use ($schedule) {
134+
(new ScheduleRepository($db))->update($schedule);
135+
});
136+
}
130137

131138
$this->sendExtraUpdates(['#col1']);
132-
$this->redirectNow(Links::schedule($scheduleId));
139+
$this->redirectNow(Links::schedule($schedule->id));
133140
});
134141
$form->on(Form::ON_SENT, function (ScheduleForm $form) use ($scheduleId) {
135142
if ($form->hasBeenRemoved()) {

application/forms/ScheduleForm.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
use DateTime;
99
use DateTimeZone;
10+
use Icinga\Module\Notifications\Common\Database;
1011
use Icinga\Module\Notifications\Model\Schedule;
1112
use Icinga\Web\Session;
1213
use IntlTimeZone;
1314
use ipl\Html\Attributes;
1415
use ipl\Html\HtmlDocument;
1516
use ipl\Html\HtmlElement;
1617
use ipl\Html\Text;
18+
use ipl\Stdlib\Filter;
1719
use ipl\Validator\CallbackValidator;
1820
use ipl\Web\Common\CsrfCounterMeasure;
1921
use ipl\Web\Compat\CompatForm;
@@ -73,6 +75,16 @@ public function hasBeenRemoved(): bool
7375
return $csrf !== null && $csrf->isValid() && $btn !== null && $btn->getName() === 'delete';
7476
}
7577

78+
/**
79+
* Get whether the duplicate button was pressed
80+
*
81+
* @return bool
82+
*/
83+
public function hasBeenDuplicated(): bool
84+
{
85+
return $this->getPressedSubmitElement()?->getName() === 'duplicate';
86+
}
87+
7688
public function __construct()
7789
{
7890
$this->schedule = new Schedule();
@@ -120,7 +132,25 @@ protected function assemble(): void
120132
$this->addElement('text', 'name', [
121133
'required' => true,
122134
'label' => $this->translate('Schedule Name'),
123-
'placeholder' => $this->translate('e.g. working hours, on call, etc ...')
135+
'placeholder' => $this->translate('e.g. working hours, on call, etc ...'),
136+
'validators' => [
137+
new CallbackValidator(function ($value, $validator) {
138+
$schedules = Schedule::on(Database::get())
139+
->columns('id')
140+
->filter(Filter::equal('name', $value));
141+
if (! $this->hasBeenDuplicated()) {
142+
$schedules->filter(Filter::unequal('id', $this->schedule->id));
143+
}
144+
145+
if ($schedules->first() !== null) {
146+
$validator->addMessage($this->translate('A rotation with this name already exists'));
147+
148+
return false;
149+
}
150+
151+
return true;
152+
})
153+
]
124154
]);
125155

126156
if ($this->showTimezoneSuggestionInput) {
@@ -165,17 +195,25 @@ protected function assemble(): void
165195
'label' => $this->getSubmitLabel()
166196
]);
167197

198+
$additionalButtons = [];
168199
if ($this->showRemoveButton) {
169200
$removeBtn = $this->createElement('submit', 'delete', [
170201
'label' => $this->translate('Delete'),
171202
'class' => 'btn-remove',
172203
'formnovalidate' => true
173204
]);
174205
$this->registerElement($removeBtn);
175-
176-
$this->getElement('submit')->prependWrapper((new HtmlDocument())->setHtmlContent($removeBtn));
206+
$additionalButtons[] = $removeBtn;
177207
}
178208

209+
$duplicateBtn = $this->createElement('submit', 'duplicate', [
210+
'label' => $this->translate('Duplicate')
211+
]);
212+
$this->registerElement($duplicateBtn);
213+
$additionalButtons[] = $duplicateBtn;
214+
215+
$this->getElement('submit')->prependWrapper((new HtmlDocument())->setHtmlContent(...$additionalButtons));
216+
179217
$this->addCsrfCounterMeasure(Session::getSession()->getId());
180218
}
181219

@@ -203,4 +241,9 @@ private function hasChanges(): bool
203241

204242
return $values !== $storedValues;
205243
}
244+
245+
public function hasBeenSubmitted()
246+
{
247+
return parent::hasBeenSubmitted() || ($this->hasBeenSent() && $this->hasBeenDuplicated());
248+
}
206249
}

0 commit comments

Comments
 (0)