diff --git a/app/controllers/resources/booking.php b/app/controllers/resources/booking.php index 36ca595f8c74bbabd8332fa19f8106249c5ea15a..b342d30695ca7541f88227c18883c6ee6dc229c4 100644 --- a/app/controllers/resources/booking.php +++ b/app/controllers/resources/booking.php @@ -1873,4 +1873,118 @@ class Resources_BookingController extends AuthenticatedController } } } + + + /** + * Transforms a reservation to a booking and vice versa. + */ + public function transform_action($booking_id = null) + { + $resource = $this->booking->resource; + if (!$resource) { + PageLayout::postError(_('Die Buchung ist ungültig!')); + return; + } + $resource = $resource->getDerivedClassInstance(); + if (!$resource) { + PageLayout::postError(_('Die Buchung ist ungültig!')); + return; + } + + if ($this->booking->booking_type == '0') { + PageLayout::setTitle(_('Buchung in Reservierung umwandeln')); + } elseif ($this->booking->booking_type == '1') { + PageLayout::setTitle(_('Reservierung in Buchung umwandeln')); + } else { + PageLayout::postError( + _('Die Buchung hat keinen passenden Buchungstyp!') + ); + return; + } + + if (!$resource->userHasBookingRights(User::findCurrent())) { + throw new AccessDeniedException(); + } + + $intervals = $this->booking->getTimeIntervals(); + + if (!$intervals) { + PageLayout::postError(_('Die Buchung ist ungültig!')); + return; + } + + if ($this->booking->booking_type == '1') { + $fully_available = true; + foreach ($intervals as $interval) { + $begin = new DateTime(); + $begin->setTimestamp($interval->begin); + $end = new DateTime(); + $end->setTimestamp($interval->end); + if (!$resource->isAvailable($begin, $end)) { + PageLayout::postError( + sprintf( + _('Die Reservierung kann nicht in eine Buchung umgewandelt werden, weil der Zeitbereich von %1$s bis %2$s bereits belegt ist!'), + $begin->format('d.m.Y H:i'), + $end->format('d.m.Y H:i') + ) + ); + $fully_available = false; + break; + } + } + if (!$fully_available) { + return; + } + } + + if (Request::submitted('transform')) { + CSRFProtection::verifyUnsafeRequest(); + + if ($this->booking->booking_type == '0') { + $this->booking->booking_type = '1'; + } elseif ($this->booking->booking_type == '1') { + $this->booking->booking_type = '0'; + } + + $saved = false; + if ($this->booking->isDirty()) { + $saved = $this->booking->store(); + } else { + $saved = true; + } + if ($saved) { + if ($this->booking->booking_type == '0') { + PageLayout::postSuccess( + _('Die Buchung wurde gespeichert.') + ); + } elseif ($this->booking->booking_type == '1') { + PageLayout::postSuccess( + _('Die Reservierung wurde gespeichert.') + ); + } + $this->response->add_header('X-Dialog-Close', '1'); + $this->render_nothing(); + } else { + if ($this->booking->booking_type == '0') { + PageLayout::postError( + _('Die Buchung konnte nicht gespeichert werden.') + ); + } elseif ($this->booking->booking_type == '1') { + PageLayout::postError( + _('Die Reservierung konnte nicht gespeichert werden.') + ); + } + } + } else { + if ($this->booking->booking_type == '0') { + PageLayout::postWarning( + _('Soll die folgende Buchung wirklich in eine Reservierung umgewandelt werden?') + ); + } else { + PageLayout::postWarning( + _('Soll die folgende Reservierung wirklich in eine Buchung umgewandelt werden?') + ); + } + } + } } diff --git a/app/views/resources/booking/edit.php b/app/views/resources/booking/edit.php index 1f8e48b4857f78bac4e8176e024671b6859066ff..25c9d5ebae52a9c9964888242853b8c2d7c27123 100644 --- a/app/views/resources/booking/edit.php +++ b/app/views/resources/booking/edit.php @@ -22,6 +22,23 @@ 'data-dialog' => 'size=auto' ] ) ?> + <? if ($booking->booking_type == '1') : ?> + <?= \Studip\LinkButton::create( + _('In Buchung umwandeln'), + $controller->url_for('resources/booking/transform/' . $booking->id), + [ + 'data-dialog' => 'size=auto' + ] + ) ?> + <? elseif ($booking->booking_type == '0') : ?> + <?= \Studip\LinkButton::create( + _('In Reservierung umwandeln'), + $controller->url_for('resources/booking/transform/' . $booking->id), + [ + 'data-dialog' => 'size=auto' + ] + ) ?> + <? endif ?> <?= \Studip\LinkButton::create( _('Löschen'), $controller->url_for('resources/booking/delete/' . $booking->id), @@ -30,4 +47,3 @@ ] ) ?> </div> - diff --git a/app/views/resources/booking/transform.php b/app/views/resources/booking/transform.php new file mode 100644 index 0000000000000000000000000000000000000000..298b16ae13b13ce52b79f943266b3f76c70565d0 --- /dev/null +++ b/app/views/resources/booking/transform.php @@ -0,0 +1,8 @@ +<form class="default" method="post" data-dialog="reload-on-close" + action="<?= $controller->link_for('resources/booking/transform' . '/' . $booking->id) ?>"> + <?= CSRFProtection::tokenTag() ?> + <?= $this->render_partial('resources/booking/index') ?> + <div data-dialog-button> + <?= \Studip\Button::create(_('Umwandeln'), 'transform') ?> + </div> +</form>