| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @package Booking Calendar |
| 5 |
* @subpackage Create new bookings functions |
| 6 |
* @category Bookings |
| 7 |
* |
| 8 |
* @author wpdevelop |
| 9 |
* @link https://wpbookingcalendar.com/ |
| 10 |
* @email info@wpbookingcalendar.com |
| 11 |
* |
| 12 |
* @modified 2014.04.23 |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 16 |
|
| 17 |
|
| 18 |
// TODO: re-update this function: wpbc_check_dates_intersections. we need to call the where_to_save function |
| 19 |
|
| 20 |
//TODO: 2023-10-03 14:49 the new workflow have to be in bookings__actions.php in line 1190 |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Check if dates intersect with other dates array |
| 25 |
* |
| 26 |
* @param array $dates_for_check - Dates Array of specific booking, which we checking - date in SQL format: '2014-11-21 10:00:01' |
| 27 |
* @param array $dates_exist - Other dates from booking resource(s), that already exist - date in SQL format: '2014-11-21 15:00:02' |
| 28 |
* @return bool true - intersect, false - not intersect |
| 29 |
*/ |
| 30 |
function wpbc_check_dates_intersections( $dates_for_check, $dates_exist ) { //FixIn: 5.4.5 |
| 31 |
|
| 32 |
$is_intersected = false; |
| 33 |
|
| 34 |
$booked_dates = array(); |
| 35 |
$what_dates_to_check = array(); |
| 36 |
|
| 37 |
//debuge($dates_for_check, $dates_exist); |
| 38 |
|
| 39 |
foreach ( $dates_exist as $value ) { |
| 40 |
|
| 41 |
if ( ( is_object( $value ) ) && ( isset( $value->booking_date ) ) ) |
| 42 |
$booking_date = $value->booking_date; // Its object with date value |
| 43 |
else |
| 44 |
$booking_date = $value; // Its array of string dates |
| 45 |
|
| 46 |
|
| 47 |
if ( intval( substr( $booking_date, -1 ) ) == 1 ) { // We require time shift for situation, when previous booking end in the same time, when new booking start |
| 48 |
$time_shift = 10; // Plus 10 seconds |
| 49 |
} elseif ( intval( substr( $booking_date, -1 ) ) == 2 ) { |
| 50 |
$time_shift = -10; // Minus 10 seconds |
| 51 |
} else |
| 52 |
$time_shift = 0; |
| 53 |
|
| 54 |
// Booked dates in destination resource, that can intersect |
| 55 |
$booked_dates[ $booking_date ] = strtotime( $booking_date ) + $time_shift;; |
| 56 |
|
| 57 |
// Get here only dates, without times: [2015-11-09] => 1447027200 |
| 58 |
$what_dates_to_check[ substr($booking_date, 0, 10) ] = strtotime( substr($booking_date, 0, 10) ); |
| 59 |
} |
| 60 |
|
| 61 |
asort( $booked_dates ); // Sort dates |
| 62 |
|
| 63 |
|
| 64 |
$keyi=0; |
| 65 |
$dates_to_add = array(); |
| 66 |
foreach ( $booked_dates as $date_key => $date_value ) { |
| 67 |
|
| 68 |
if ( $keyi == 0 ) { // First element |
| 69 |
if ( intval( substr( $date_key, -1 ) ) == 2 ) { |
| 70 |
// We are having first date as ending date, its means that starting date exist somewhere before, and we need to set it at the begining |
| 71 |
$dates_to_add[ substr($date_key, 0, 10) . ' 00:00:11' ] = strtotime( substr($date_key, 0, 10) . ' 00:00:11' ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( $keyi == ( count($booked_dates) - 1 ) ) { // last element |
| 76 |
if ( intval( substr( $date_key, -1 ) ) == 1 ) { |
| 77 |
// We are having last date as ending date, its means that ending date exist somewhere after, and we need to set it at the end of array |
| 78 |
$dates_to_add[ substr($date_key, 0, 10) . ' 23:59:42' ] = strtotime( substr($date_key, 0, 10) . ' 23:59:42' ); |
| 79 |
} |
| 80 |
} |
| 81 |
$keyi++; |
| 82 |
} |
| 83 |
$booked_dates = array_merge($booked_dates, $dates_to_add); |
| 84 |
asort( $booked_dates ); // Sort dates |
| 85 |
|
| 86 |
|
| 87 |
// Skip dates (in original booking) that does not exist in destination resource at all |
| 88 |
$check_dates = array(); |
| 89 |
foreach ( $dates_for_check as $value ) { |
| 90 |
|
| 91 |
if ( ( is_object( $value ) ) && ( isset( $value->booking_date ) ) ) |
| 92 |
$booking_date = $value->booking_date; // Its object with date value |
| 93 |
else |
| 94 |
$booking_date = $value; // Its array of string dates |
| 95 |
|
| 96 |
// Check dates only if these dates already exist in $what_dates_to_check array |
| 97 |
if ( isset( $what_dates_to_check[ substr($booking_date, 0, 10) ] ) ) |
| 98 |
$check_dates[] = $value; |
| 99 |
} |
| 100 |
|
| 101 |
if ( count( $check_dates ) == 0 ) return $is_intersected; // No intersected dates at all in exist bookings. Return. // FixIn: 6.0.1.13. |
| 102 |
|
| 103 |
foreach ( $check_dates as $value ) { |
| 104 |
|
| 105 |
if ( ( is_object( $value ) ) && ( isset( $value->booking_date ) ) ) |
| 106 |
$booking_date = $value->booking_date; // Its object with date value |
| 107 |
else |
| 108 |
$booking_date = $value; // Its array of string dates |
| 109 |
|
| 110 |
if ( isset( $booked_dates[ $booking_date ] ) ) { // Already have exactly this date as booked |
| 111 |
$is_intersected = true; |
| 112 |
break; |
| 113 |
} |
| 114 |
|
| 115 |
if ( intval( substr( $booking_date, -1 ) ) == 1 ) { // We require time shift for situation, when previous booking end in the same time, when new booking start |
| 116 |
$time_shift = 10; // Plus 10 seconds |
| 117 |
} elseif ( intval( substr( $booking_date, -1 ) ) == 2 ) { |
| 118 |
$time_shift = -10; // Minus 10 seconds |
| 119 |
} else |
| 120 |
$time_shift = 0; |
| 121 |
|
| 122 |
$booked_dates[ $booking_date ] = strtotime( $booking_date ) + $time_shift; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
asort( $booked_dates ); // Sort dates |
| 127 |
|
| 128 |
//debuge($booked_dates); |
| 129 |
if ( ! $is_intersected ) { |
| 130 |
|
| 131 |
// check dates and times for intersections |
| 132 |
$previos_date_key = 0; |
| 133 |
foreach ( $booked_dates as $date_key => $value ) { |
| 134 |
|
| 135 |
$date_key = intval( substr( $date_key, -1 ) ); // Get last second |
| 136 |
|
| 137 |
// Check if the date fully booked (key = 0), or we are having 2 the same keys, like 1 and 1 or 2 and 2 one under other. Its means that we are having time intersection. |
| 138 |
if ( ( $date_key !== 0 ) && ( $date_key != $previos_date_key ) ) |
| 139 |
$previos_date_key = $date_key; |
| 140 |
else { |
| 141 |
$is_intersected = true; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $is_intersected ; |
| 148 |
} |
| 149 |
|