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