| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* Bookings from AGGREGATE parameter adding ONLY to PARENT booking resource !. But unavailable dates from Booking > Availability page added to PARENT and CHILDREN booking resources. |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Merge unavailable dates from AGGREGATE resources into REAL resource |
| 12 |
* |
| 13 |
* @param $unavailable_dates__per_resources__arr = [ |
| 14 |
3 => [ 0 => '2023-12-01', |
| 15 |
1 => '2023-12-02', |
| 16 |
2 => '2023-12-03' |
| 17 |
], |
| 18 |
4 => [ |
| 19 |
0 => '2023-12-03', |
| 20 |
1 => '2023-12-04', |
| 21 |
2 => '2023-12-05', |
| 22 |
], |
| 23 |
25 =>[ |
| 24 |
0 => '2023-12-09', |
| 25 |
1 => '2023-12-10', |
| 26 |
] |
| 27 |
] |
| 28 |
* @param $aggregate_resource_id_arr = [ 3, 25 ] |
| 29 |
* @param $aggregate_resource_id_arr = [ 4 ] |
| 30 |
* |
| 31 |
* @return mixed |
| 32 |
*/ |
| 33 |
function wpbc_aggregate_merge_availability( $unavailable_dates__per_resources__arr, $source_res_id__with_children__arr, $aggregate_resource_id_arr ) { |
| 34 |
|
| 35 |
$new_resource_arr__dates_arr = array(); |
| 36 |
|
| 37 |
// Get availability only for Real Resource |
| 38 |
foreach ( $source_res_id__with_children__arr as $source_res_id ) { |
| 39 |
|
| 40 |
if ( ! in_array( $source_res_id, $aggregate_resource_id_arr ) ) { |
| 41 |
$new_resource_arr__dates_arr[ $source_res_id ] = array(); |
| 42 |
|
| 43 |
if ( ! empty( $unavailable_dates__per_resources__arr[ $source_res_id ] ) ) { |
| 44 |
$new_resource_arr__dates_arr[ $source_res_id ] = $unavailable_dates__per_resources__arr[ $source_res_id ]; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
// Add aggregate resources availability |
| 51 |
foreach ( $aggregate_resource_id_arr as $aggregate_res_id ) { |
| 52 |
if ( ! empty( $unavailable_dates__per_resources__arr[ $aggregate_res_id ] ) ) { |
| 53 |
|
| 54 |
// Go by dates |
| 55 |
foreach ( $unavailable_dates__per_resources__arr[ $aggregate_res_id ] as $date_unavailable ) { |
| 56 |
|
| 57 |
// Now need to add this date into all Real resources (which can be parent and child resources ) |
| 58 |
foreach ( $new_resource_arr__dates_arr as $merged_resource_id => $dates_array ) { |
| 59 |
|
| 60 |
$new_resource_arr__dates_arr[ $merged_resource_id ][] = $date_unavailable; // Append date to merged real resource array |
| 61 |
|
| 62 |
$dates_array = array_unique( $new_resource_arr__dates_arr[ $merged_resource_id ] ); // Remove Duplicates |
| 63 |
sort( $new_resource_arr__dates_arr[ $merged_resource_id ] ); // Sort |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
return $new_resource_arr__dates_arr; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Merge all bookings from AGGREGATE resources into REAL resources |
| 77 |
* |
| 78 |
* @param $resources__booking_id__obj = [ |
| 79 |
* 3 = [ |
| 80 |
* 5370 = {stdClass} |
| 81 |
* ] |
| 82 |
* 4 = [ |
| 83 |
* 5369 = {stdClass} |
| 84 |
* 5364 = {stdClass} |
| 85 |
* 5367 = {stdClass} |
| 86 |
* 5366 = {stdClass} |
| 87 |
* 5368 = {stdClass} |
| 88 |
* ] |
| 89 |
* ] |
| 90 |
* @param $source_res_id__with_children__arr [ 3, 25 ] |
| 91 |
* @param $aggregate_resource_id_arr [ 4 ] |
| 92 |
* |
| 93 |
* @return array = [ |
| 94 |
* 4 = [ |
| 95 |
* 5370 = {stdClass} |
| 96 |
* 5369 = {stdClass} |
| 97 |
* 5364 = {stdClass} |
| 98 |
* 5367 = {stdClass} |
| 99 |
* 5366 = {stdClass} |
| 100 |
* 5368 = {stdClass} |
| 101 |
* ] |
| 102 |
* ] |
| 103 |
*/ |
| 104 |
function wpbc_aggregate__merge_bookings__from_aggregate__in_source_resource__arr( $resources__booking_id__obj, $source_res_id__with_children__arr, $aggregate_resource_id_arr ) { |
| 105 |
|
| 106 |
$new_resource_arr__bookings_arr = array(); |
| 107 |
|
| 108 |
// Get availability only for Real Resource |
| 109 |
foreach ( $source_res_id__with_children__arr as $source_res_id ) { |
| 110 |
|
| 111 |
if ( ! in_array( $source_res_id, $aggregate_resource_id_arr ) ) { |
| 112 |
$new_resource_arr__bookings_arr[ $source_res_id ] = array(); |
| 113 |
|
| 114 |
if ( ! empty( $resources__booking_id__obj[ $source_res_id ] ) ) { |
| 115 |
$new_resource_arr__bookings_arr[ $source_res_id ] = wpbc_clone_array_of_objects( $resources__booking_id__obj[ $source_res_id ] ); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
// Add aggregate resources bookings into ALL source resources |
| 123 |
foreach ( $aggregate_resource_id_arr as $aggregate_res_id ) { |
| 124 |
if ( ! empty( $resources__booking_id__obj[ $aggregate_res_id ] ) ) { |
| 125 |
|
| 126 |
// Go by bookings |
| 127 |
foreach ( $resources__booking_id__obj[ $aggregate_res_id ] as $aggregate_booking_id => $aggregate_booking_obj ) { |
| 128 |
|
| 129 |
// Now need to add this $aggregate_bookings_arr into all Real resources (which can be parent and child resources ) |
| 130 |
foreach ( $new_resource_arr__bookings_arr as $merged_resource_id => $bookings_array ) { |
| 131 |
/* |
| 132 |
// $aggregate_booking_obj->form it has ID as $aggregate_res_id but we need to have NEW as $merged_resource_id |
| 133 |
|
| 134 |
$new_resource_id = $merged_resource_id; |
| 135 |
$old_resource_id = $aggregate_res_id; |
| 136 |
$aggregate_booking_obj->form = wpbc_get__form_data__with_replaced_id( $aggregate_booking_obj->form, $new_resource_id, $old_resource_id ); |
| 137 |
$aggregate_booking_obj->type = $new_resource_id; |
| 138 |
*/ |
| 139 |
$new_resource_arr__bookings_arr[ $merged_resource_id ][ $aggregate_booking_id ] = wpbc_clone_array_of_objects( $aggregate_booking_obj ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
return $new_resource_arr__bookings_arr; |
| 148 |
} |