Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1590 (iCal Download storniert Buchung) #1591

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Service/iCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ public static function getCurrentUserCalendarLink() {
);
}

/**
* Get the ics file for an existing booking. Will be called, when the "Add to Calendar" button on the booking page is pressed
*
* @param $bookingID
*
* @return void
* @throws Exception
*/
public static function downloadICS( $bookingID ): void {
$postID = $bookingID;
$booking = new Booking( $postID );
$template_objects = [
'booking' => $booking,
'item' => $booking->getItem(),
'location' => $booking->getLocation(),
'user' => $booking->getUserData(),
];

$eventTitle = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-title' );
$eventTitle = commonsbooking_sanitizeHTML( commonsbooking_parse_template( $eventTitle, $template_objects ) );
$eventDescription = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-description' );
$eventDescription = commonsbooking_sanitizeHTML( strip_tags( commonsbooking_parse_template( $eventDescription, $template_objects ) ) );
$calendar = $booking->getiCal( $eventTitle, $eventDescription );
header( 'Content-Type: text/calendar; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="booking.ics"' );
echo $calendar;
}

/**
* Adds Model\Booking to Calendar.
* This will take all the information like title, description, location, start and end date and add it to the calendar as an event.
Expand Down
36 changes: 12 additions & 24 deletions src/Wordpress/CustomPostType/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ public static function handleBookingRequest(
?string $postType,
int $overbookedDays = 0
): int {

if ( isset ( $_POST['calendar-download'] ) ) {
try {
iCalendar::downloadICS( $post_ID );
} catch ( Exception $e ) {
//redirect to booking page and do nothing
return $post_ID;
}
exit;
}

if ( $itemId === null || ! get_post( $itemId ) ) {
// translators: $s = id of the item
throw new BookingDeniedException( sprintf( __( 'Item does not exist. (%s)', 'commonsbooking' ), $itemId ) );
Expand Down Expand Up @@ -346,30 +357,7 @@ public static function handleBookingRequest(
PHP_EOL . $postId->get_error_messages()
);
}
elseif (
function_exists( 'wp_verify_nonce' ) &&
isset( $_REQUEST[ static::getWPNonceId() ] ) &&
wp_verify_nonce( $_REQUEST[ static::getWPNonceId() ], static::getWPAction() ) &&
isset ( $_POST['calendar-download'] )
){
$postID = intval($_POST['post_ID']);
$booking = New \CommonsBooking\Model\Booking( $postID );
$template_objects = [
'booking' => $booking,
'item' => $booking->getItem(),
'location' => $booking->getLocation(),
'user' => $booking->getUserData(),
];
$eventTitle = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-title' );
$eventTitle = commonsbooking_sanitizeHTML ( commonsbooking_parse_template ( $eventTitle, $template_objects ) );
$eventDescription = Settings::getOption( 'commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_event-description' );
$eventDescription = commonsbooking_sanitizeHTML ( strip_tags ( commonsbooking_parse_template ( $eventDescription, $template_objects ) ) );
$calendar = $booking->getiCal($eventTitle,$eventDescription);
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="booking.ics"');
echo $calendar;
exit;
}

return $postId;
}

Expand Down