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

Migration verwaiste Buchungen #1308

Merged
merged 16 commits into from
Aug 14, 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
40 changes: 40 additions & 0 deletions assets/admin/js/src/massoperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(function ($) {
'use strict';
$(function () {

$('#orphans-migration-start').on('click', function (event) {
event.preventDefault();

$('#orphans-migration-in-progress').show();

let checkedBoxes = $('.post-checkboxes:checkbox:checked');
let ids = [];
checkedBoxes.each(function () {
ids.push($(this).val());
});
let data = ids;

$.post(
cb_ajax_orphaned_booking_migration.ajax_url,
{
_ajax_nonce: cb_ajax_orphaned_booking_migration.nonce,
action: "cb_orphaned_booking_migration",
data: data
}).done(function (data) {
if (data.success) {
$('#orphans-migration-in-progress').hide();
$('#orphans-migration-done').show();
$('#orphans-migration-done span').text(data.message);
//now remove all rows that have been migrated
ids.forEach(function (id) {
$('#row-booking-' + id).remove();
});
} else {
$('#orphans-migration-in-progress').hide();
$('#orphans-migration-failed').show();
$('#orphans-migration-failed span').text(data.message);
}
});
})
});
})(jQuery);
8 changes: 8 additions & 0 deletions assets/admin/sass/partials/_migration.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
animation: blinkText 1s infinite alternate;
}

#orphans-migration-in-progress,
#orphans-migration-done,
#orphans-migration-failed
{
margin-bottom: 1rem;
display: none;
}

@keyframes blinkText {
from {opacity:0;}
to {opacity: 1;}
Expand Down
10 changes: 10 additions & 0 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ function commonsbooking_admin() {
. 'cb_applied_booking_rules=' . \CommonsBooking\Service\BookingRuleApplied::getRulesJSON() . ';',
);


//orphaned bookings migration - re-assign booking when timeframe has changed
wp_localize_script(
'cb-scripts-admin',
'cb_ajax_orphaned_booking_migration',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'cb_orphaned_booking_migration' ),
)
);
/**
* Ajax - cache warmup
*/
Expand Down
1 change: 1 addition & 0 deletions includes/Public.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function commonsbooking_public() {

//getting booking code for new backend booking AJAX
add_action( 'wp_ajax_cb_get_booking_code', array( \CommonsBooking\View\Booking::class, 'getBookingCode_AJAX' ) );
add_action( 'wp_ajax_cb_orphaned_booking_migration', array( \CommonsBooking\Service\MassOperations::class, 'ajaxMigrateOrphaned' ) );
}

// Map ajax
Expand Down
53 changes: 50 additions & 3 deletions src/Model/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Booking extends \CommonsBooking\Model\Timeframe {
*/
const META_OVERBOOKED_DAYS = 'days-overbooked';

const META_LAST_TIMEFRAME = 'last-connected-timeframe';

public const ERROR_TYPE = 'BookingValidationFailed';

/**
Expand Down Expand Up @@ -409,17 +411,27 @@ public function getOverbookedDays(): int {
}
return intval ( $metaField );
}

public function getFormattedStartDate(): string {
$date_format = commonsbooking_sanitizeHTML( get_option( 'date_format' ) );
return date_i18n( $date_format, $this->getStartDate() );
}

public function getFormattedEndDate(): string {
$date_format = commonsbooking_sanitizeHTML( get_option( 'date_format' ) );
return date_i18n( $date_format, $this->getRawEndDate() );
}

/**
* Get the booking date in a human-readable format.
* This is used in the booking confirmation email as a template tag.
*
* @return string
*/
public function formattedBookingDate(): string {
$date_format = commonsbooking_sanitizeHTML( get_option( 'date_format' ) );

$startdate = date_i18n( $date_format, $this->getStartDate() );
$enddate = date_i18n( $date_format, $this->getRawEndDate() );
$startdate = $this->getFormattedStartDate();
$enddate = $this->getFormattedEndDate();

if ( $startdate === $enddate ) {
/* translators: %s = date in WordPress defined format */
Expand Down Expand Up @@ -691,6 +703,24 @@ public function isUserPrivileged( WP_User $user = null): bool {
return parent::isUserPrivileged($user);
}

/**
* Will indicate if booking is orphaned meaning that the booking is not connectable to a bookable timeframe.
* This can happen when a bookable timeframe is deleted but the booking is still in the database,
* when a bookable timeframe has a location changed and the booking is still in the database,
* or when a bookable timeframe is changed to a non-bookable timeframe and the booking is still in the database.
*
* This can also happen when a booking is created without a bookable timeframe, e.g. when a booking is created through the backend.
* @return bool
* @throws Exception
*/
public function isOrphaned(): bool {
if ($this->getBookableTimeFrame() === null) {
return true;
} else {
return false;
}
}

/**
* Returns true when booking has ended.
* Will determine this by comparing the end date of the booking with the current time.
Expand Down Expand Up @@ -837,6 +867,23 @@ public function getFormattedEditLink() {
return '<a href=" ' . get_edit_post_link( $this->ID ) . '"> Booking #' . $this->ID . ' : ' . $this->formattedBookingDate() . ' | User: ' . $this->getUserData()->user_nicename . '</a>';
}

/**
* Will return a location where an orphaned booking can be moved to. This is
* the new location of the timeframe the booking was previously attached to.
* @return ?Location
*/
public function getMoveableLocation(): ?Location {
if (!$this->isOrphaned()) {
return null;
}
$attachedTFMeta = intval( get_post_meta( $this->ID, self::META_LAST_TIMEFRAME, true ) );
if ($attachedTFMeta === false) {
return null;
}
$attachedTF = new \CommonsBooking\Model\Timeframe( $attachedTFMeta );
return $attachedTF->getLocation();
}

/**
* Updates internal booking comment by adding new comment in a new line
*
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Timeframe extends CustomPost {
*/
public const ERROR_TYPE = 'timeframeValidationFailed';

public const ORPHANED_TYPE = 'timeframehasOrphanedBookings';

public const REPETITION_START = 'repetition-start';

public const REPETITION_END = 'repetition-end';
Expand Down
12 changes: 12 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CommonsBooking\Settings\Settings;
use CommonsBooking\Repository\BookingCodes;
use CommonsBooking\View\Dashboard;
use CommonsBooking\View\MassOperations;
use CommonsBooking\Wordpress\CustomPostType\CustomPostType;
use CommonsBooking\Wordpress\CustomPostType\Item;
use CommonsBooking\Wordpress\CustomPostType\Location;
Expand Down Expand Up @@ -317,6 +318,16 @@ public static function addMenuPages() {
admin_url( 'edit-tags.php' ) . '?taxonomy=' . Location::$postType . 's_category',
''
);

//Add menu item for mass operations
add_submenu_page(
'cb-dashboard',
esc_html__( 'Mass Operations', 'commonsbooking' ),
esc_html__( 'Mass Operations', 'commonsbooking' ),
'manage_' . COMMONSBOOKING_PLUGIN_SLUG,
'cb-mass-operations',
array( MassOperations::class, 'index' )
);
}
}

Expand Down Expand Up @@ -473,6 +484,7 @@ public static function registerLocationTaxonomy() {
public static function renderError() {
$errorTypes = [
Model\Timeframe::ERROR_TYPE,
Model\Timeframe::ORPHANED_TYPE,
BookingCode::ERROR_TYPE,
OptionsTab::ERROR_TYPE,
Model\Booking::ERROR_TYPE,
Expand Down
Loading