PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.0
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Hooks / Handlers / CleanupHandlers / CalenderCleaner.php

CalenderCleaner.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.3.0, at app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php

91 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }