| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentBooking\App\Models\Calendar; |
| 7 |
use FluentBooking\App\Models\CalendarSlot; |
| 8 |
|
| 9 |
class CalendarService |
| 10 |
{ |
| 11 |
public static function getSlotOptions($calendarId) |
| 12 |
{ |
| 13 |
$calendarSlots = CalendarSlot::select(['id', 'title']) |
| 14 |
->where('calendar_id', $calendarId) |
| 15 |
->where('status', '!=', 'expired') |
| 16 |
->latest() |
| 17 |
->get(); |
| 18 |
|
| 19 |
$options = []; |
| 20 |
foreach ($calendarSlots as $slot) { |
| 21 |
$options[] = [ |
| 22 |
'id' => $slot->id, |
| 23 |
'label' => $slot->title, |
| 24 |
]; |
| 25 |
} |
| 26 |
return apply_filters('fluent_booking/calendar_event_options', $options, $calendarId); |
| 27 |
} |
| 28 |
|
| 29 |
public static function getCalendarOptionsByHost() |
| 30 |
{ |
| 31 |
$calendars = Calendar::select(['id', 'title']) |
| 32 |
->when(!PermissionManager::hasAllCalendarAccess(true), function ($query) { |
| 33 |
return $query->where('user_id', get_current_user_id()); |
| 34 |
}) |
| 35 |
->with(['slots' => function($query) { |
| 36 |
$query->where('status', '!=', 'expired'); |
| 37 |
}]) |
| 38 |
->latest() |
| 39 |
->get(); |
| 40 |
|
| 41 |
$formattedCalendars = []; |
| 42 |
foreach ($calendars as $index => $calendar) { |
| 43 |
$slots = Arr::get($calendar, 'slots'); |
| 44 |
if (!empty($slots)) { |
| 45 |
$options = []; |
| 46 |
foreach ($slots as $slot) { |
| 47 |
$options[] = [ |
| 48 |
'label' => Arr::get($slot, 'title'), |
| 49 |
'value' => Arr::get($slot, 'id') |
| 50 |
]; |
| 51 |
} |
| 52 |
if (!empty($options)) { |
| 53 |
$formattedCalendars[$index] = [ |
| 54 |
'label' => Arr::get($calendar, 'title'), |
| 55 |
'options' => $options |
| 56 |
]; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return $formattedCalendars; |
| 61 |
} |
| 62 |
|
| 63 |
public static function getCalendarOptionsByTitle($condition = '') |
| 64 |
{ |
| 65 |
$calendarQuery = Calendar::select(['id', 'title']) |
| 66 |
->where('status', '!=', 'expired') |
| 67 |
->when(!PermissionManager::hasAllCalendarAccess(true), function ($query) { |
| 68 |
return $query->where('user_id', get_current_user_id()); |
| 69 |
}); |
| 70 |
|
| 71 |
switch ($condition) { |
| 72 |
case 'only_hosts': |
| 73 |
$calendarQuery->where('type', 'simple'); |
| 74 |
break; |
| 75 |
case 'only_teams': |
| 76 |
$calendarQuery->where('type', 'team'); |
| 77 |
break; |
| 78 |
case 'only_events': |
| 79 |
$calendarQuery->where('type', 'event'); |
| 80 |
break; |
| 81 |
} |
| 82 |
|
| 83 |
$calendars = $calendarQuery->with(['slots' => function($query) { |
| 84 |
$query->where('status', '!=', 'expired'); |
| 85 |
}])->latest()->get(); |
| 86 |
|
| 87 |
$formattedCalendars = []; |
| 88 |
foreach ($calendars as $index => $calendar) { |
| 89 |
$slots = Arr::get($calendar, 'slots'); |
| 90 |
if (!empty($slots)) { |
| 91 |
$options = []; |
| 92 |
foreach ($slots as $slot) { |
| 93 |
$options[] = [ |
| 94 |
'id' => Arr::get($slot, 'id'), |
| 95 |
'title' => Arr::get($slot, 'title') |
| 96 |
]; |
| 97 |
} |
| 98 |
if (!empty($options)) { |
| 99 |
$formattedCalendars[$index] = [ |
| 100 |
'id' => Arr::get($calendar, 'id'), |
| 101 |
'title' => Arr::get($calendar, 'title'), |
| 102 |
'options' => $options |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return apply_filters('fluent_booking/calendar_options_by_title', $formattedCalendars); |
| 108 |
} |
| 109 |
|
| 110 |
public static function updateCalendarEventsSchedule($calendarId, $oldTimezone, $updatedTimezone) |
| 111 |
{ |
| 112 |
$calendarEvents = CalendarSlot::query()->where('calendar_id', $calendarId)->get(); |
| 113 |
|
| 114 |
foreach ($calendarEvents as $event) |
| 115 |
{ |
| 116 |
if ($weeklySchedule = Arr::get($event->settings, 'weekly_schedules', [])) { |
| 117 |
$originalSchedule = SanitizeService::weeklySchedules($weeklySchedule, 'UTC', $oldTimezone); |
| 118 |
$weeklySchedule = SanitizeService::weeklySchedules($originalSchedule, $updatedTimezone, 'UTC'); |
| 119 |
} |
| 120 |
|
| 121 |
if ($dateOverride = Arr::get($event->settings, 'date_overrides', [])) { |
| 122 |
$originalOverride = SanitizeService::slotDateOverrides($dateOverride, 'UTC', $oldTimezone); |
| 123 |
$dateOverride = SanitizeService::slotDateOverrides($originalOverride, $updatedTimezone, 'UTC'); |
| 124 |
} |
| 125 |
|
| 126 |
$event->settings = [ |
| 127 |
'weekly_schedules' => $weeklySchedule, |
| 128 |
'date_overrides' => $dateOverride |
| 129 |
]; |
| 130 |
|
| 131 |
$event->save(); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|