| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Handlers\CleanupHandlers; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Models\BookingHost; |
| 7 |
use FluentBooking\App\Models\CalendarSlot; |
| 8 |
use FluentBooking\App\Models\Availability; |
| 9 |
use FluentBooking\App\Models\Meta; |
| 10 |
use FluentBooking\Framework\Support\Arr; |
| 11 |
|
| 12 |
class CalenderCleaner |
| 13 |
{ |
| 14 |
public function register() |
| 15 |
{ |
| 16 |
add_action('fluent_booking/before_delete_calendar', [$this, 'handleBeforeDelete']); |
| 17 |
} |
| 18 |
|
| 19 |
public function handleBeforeDelete($calendar) |
| 20 |
{ |
| 21 |
if (empty($calendar)) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
Meta::query() |
| 26 |
->where('object_type', 'Calendar') |
| 27 |
->where('object_id', $calendar->id)->delete(); |
| 28 |
|
| 29 |
$calendarEvents = CalendarSlot::query() |
| 30 |
->where('calendar_id', $calendar->id)->get(); |
| 31 |
|
| 32 |
if ($calendarEvents->count()) { |
| 33 |
foreach ($calendarEvents as $event) { |
| 34 |
$eventId = $event->id; |
| 35 |
do_action('fluent_booking/before_delete_calendar_event', $event, $calendar); |
| 36 |
$event->delete(); |
| 37 |
do_action('fluent_booking/after_delete_calendar_event', $eventId, $calendar); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$teamEvents = CalendarSlot::query() |
| 42 |
->where('calendar_id', '!=', $calendar->id) |
| 43 |
->where(function ($query) { |
| 44 |
$query->where('event_type', 'round_robin') |
| 45 |
->orWhere('event_type', 'collective'); |
| 46 |
}) |
| 47 |
->get(); |
| 48 |
|
| 49 |
if ($teamEvents->count()) { |
| 50 |
foreach ($teamEvents as $event) { |
| 51 |
$teamMembers = Arr::get($event->settings, 'team_members', []); |
| 52 |
if (in_array($calendar->user_id, $teamMembers)) { |
| 53 |
$updatedTeamMembers = array_filter($teamMembers, function ($memberId) use ($calendar) { |
| 54 |
return $memberId != $calendar->user_id; |
| 55 |
}); |
| 56 |
$event->settings = [ |
| 57 |
'team_members' => array_values($updatedTeamMembers) ?: [(int)$event->user_id] |
| 58 |
]; |
| 59 |
$event->save(); |
| 60 |
|
| 61 |
$calendarUserId = $calendar->user_id; |
| 62 |
Booking::query() |
| 63 |
->where('event_id', $event->id) |
| 64 |
->whereHas('hosts', function ($query) use ($calendarUserId){ |
| 65 |
$query->where('user_id', $calendarUserId); |
| 66 |
}) |
| 67 |
->withCount('hosts') |
| 68 |
->chunkById(100, function ($bookings) use ($calendarUserId) { |
| 69 |
$sharedBookingIds = []; |
| 70 |
|
| 71 |
foreach ($bookings as $booking) { |
| 72 |
// A collective booking still happens with its other hosts. |
| 73 |
if ($booking->hosts_count > 1) { |
| 74 |
$sharedBookingIds[] = $booking->id; |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
$bookingId = $booking->id; |
| 79 |
do_action('fluent_booking/before_delete_booking', $booking); |
| 80 |
$booking->delete(); |
| 81 |
do_action('fluent_booking/after_delete_booking', $bookingId); |
| 82 |
} |
| 83 |
|
| 84 |
if ($sharedBookingIds) { |
| 85 |
BookingHost::query()->where('user_id', $calendarUserId)->whereIn('booking_id', $sharedBookingIds)->delete(); |
| 86 |
} |
| 87 |
}); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if ($calendar->type != 'simple') { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$availabilitySchedules = Availability::where('object_id', $calendar->user_id)->get(); |
| 97 |
|
| 98 |
if ($availabilitySchedules->isNotEmpty()) { |
| 99 |
foreach ($availabilitySchedules as $schedule) { |
| 100 |
$eventAssociated = CalendarSlot::where('availability_id', $schedule->id) |
| 101 |
->where('user_id', '!=', $schedule->object_id) |
| 102 |
->count(); |
| 103 |
|
| 104 |
$scheduleId = $schedule->id; |
| 105 |
if ($eventAssociated == 0) { |
| 106 |
do_action('fluent_booking/before_delete_availability_schedule', $schedule); |
| 107 |
$schedule->delete(); |
| 108 |
do_action('fluent_booking/after_delete_availability_schedule', $scheduleId); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |