| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\Calendars; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Models\Meta; |
| 7 |
use FluentBooking\Framework\Support\Arr; |
| 8 |
|
| 9 |
abstract class BaseCalendar |
| 10 |
{ |
| 11 |
protected $calendarKey; |
| 12 |
|
| 13 |
protected $calendarTitle; |
| 14 |
|
| 15 |
protected $logo; |
| 16 |
|
| 17 |
public function boot() |
| 18 |
{ |
| 19 |
add_filter('fluent_booking/settings_menu_items', [$this, 'pushToGlobalMenu'], 10, 1); |
| 20 |
add_filter('fluent_booking/get_client_settings_' . $this->calendarKey, [$this, 'getClientSettingsForView'], 10, 1); |
| 21 |
add_filter('fluent_booking/get_client_field_settings_' . $this->calendarKey, [$this, 'getClientFieldSettings'], 10, 1); |
| 22 |
add_action('fluent_booking/save_client_settings_' . $this->calendarKey, [$this, 'saveClientSettings'], 10, 1); |
| 23 |
|
| 24 |
/* |
| 25 |
* oAuth From Handlers from Calendar |
| 26 |
*/ |
| 27 |
add_filter('fluent_booking/remote_calendar_providers', [$this, 'addAsProvider'], 10, 2); |
| 28 |
add_filter('fluent_booking/remote_calendar_connection_feeds', [$this, 'pushFeeds'], 10, 2); |
| 29 |
add_action('fluent_calendar/patch_calendar_config_settings__' . $this->calendarKey . '_user_token', [$this, 'updateConflictIds'], 10, 2); |
| 30 |
add_action('fluent_calendar/patch_calendar_additional_settings__' . $this->calendarKey . '_user_token', [$this, 'updateAdditionalSettings'], 10, 2); |
| 31 |
add_action('fluent_calendar/disconnect_remote_calendar__' . $this->calendarKey . '_user_token', [$this, 'authDisconnect'], 10, 1); |
| 32 |
|
| 33 |
/* |
| 34 |
* Booking Handlers |
| 35 |
*/ |
| 36 |
add_filter('fluent_booking/remote_booked_events', [$this, 'getBookedSlots'], 10, 6); |
| 37 |
add_action('fluent_booking/create_remote_calendar_event_' . $this->calendarKey, [$this, 'createEvent'], 10, 2); |
| 38 |
add_action('fluent_booking/refresh_remote_calendar_group_members_' . $this->calendarKey, [$this, 'maybeAddOrRemoveGroupMembers'], 10, 4); |
| 39 |
|
| 40 |
add_action('fluent_booking/cancel_remote_calendar_event_' . $this->calendarKey, [$this, 'cancelEvent'], 10, 2); |
| 41 |
add_action('fluent_booking/patch_remote_calendar_event_' . $this->calendarKey, [$this, 'patchEvent'], 10, 4); |
| 42 |
add_action('fluent_booking/delete_remote_calendar_event_' . $this->calendarKey, [$this, 'deleteEvent'], 10, 2); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
public function pushToGlobalMenu($menuItems) |
| 47 |
{ |
| 48 |
$menuItems[$this->calendarKey]['disable'] = false; |
| 49 |
|
| 50 |
return $menuItems; |
| 51 |
} |
| 52 |
|
| 53 |
abstract public function getClientSettingsForView($settings); |
| 54 |
|
| 55 |
abstract public function getClientFieldSettings($settings); |
| 56 |
|
| 57 |
abstract public function saveClientSettings($settings); |
| 58 |
|
| 59 |
public function addAsProvider($providers, $userId) |
| 60 |
{ |
| 61 |
$providers[$this->calendarKey] = [ |
| 62 |
'key' => $this->calendarKey, |
| 63 |
'icon' => $this->logo, |
| 64 |
'title' => $this->calendarTitle, |
| 65 |
/* translators: %s is the name of the calendar title. */ |
| 66 |
'subtitle' => sprintf(__('Configure %s to sync your events', 'fluent-booking'), $this->calendarTitle), |
| 67 |
/* translators: %s is the name of the calendar title. */ |
| 68 |
'btn_text' => sprintf(__('Connect with %s', 'fluent-booking'), $this->calendarTitle), |
| 69 |
'auth_url' => $this->getAuthUrl($userId), |
| 70 |
'is_global_configured' => $this->isConfigured(), |
| 71 |
'global_config_url' => admin_url('admin.php?page=fluent-booking#/settings/configure-integrations/'.$this->calendarKey), |
| 72 |
]; |
| 73 |
|
| 74 |
return $providers; |
| 75 |
} |
| 76 |
|
| 77 |
abstract public function pushFeeds($feeds, $userId); |
| 78 |
|
| 79 |
public function updateConflictIds($conflictIds, $meta) |
| 80 |
{ |
| 81 |
$meta = Meta::where('object_type', '_' . $this->calendarKey . '_user_token') |
| 82 |
->where('id', $meta->id) |
| 83 |
->first(); |
| 84 |
|
| 85 |
$settings = $meta->value; |
| 86 |
$settings['conflict_check_ids'] = $conflictIds; |
| 87 |
$meta->value = $settings; |
| 88 |
$meta->save(); |
| 89 |
} |
| 90 |
|
| 91 |
public function updateAdditionalSettings($additionalSettings, $meta) |
| 92 |
{ |
| 93 |
$meta = Meta::where('object_type', '_' . $this->calendarKey . '_user_token') |
| 94 |
->where('id', $meta->id) |
| 95 |
->first(); |
| 96 |
|
| 97 |
$settings = $meta->value; |
| 98 |
$settings['additional_settings'] = $additionalSettings; |
| 99 |
$meta->value = $settings; |
| 100 |
$meta->save(); |
| 101 |
} |
| 102 |
|
| 103 |
abstract public function authDisconnect($meta); |
| 104 |
|
| 105 |
abstract public function getBookedSlots($books, $calendarSlot, $toTimeZone, $dateRange, $hostId, $isDoingBooking); |
| 106 |
|
| 107 |
abstract public function createEvent($config, Booking $booking); |
| 108 |
|
| 109 |
abstract public function cancelEvent($config, Booking $booking); |
| 110 |
|
| 111 |
abstract public function deleteEvent($config, Booking $booking); |
| 112 |
|
| 113 |
abstract public function patchEvent($config, Booking $booking, $updateData, $isRescheduling); |
| 114 |
|
| 115 |
abstract public function maybeAddOrRemoveGroupMembers($config, Booking $booking, $allGroupBookings, $isRescheduling); |
| 116 |
|
| 117 |
abstract public function getAuthUrl($userId = null); |
| 118 |
|
| 119 |
abstract public function isConfigured(); |
| 120 |
|
| 121 |
protected function getStanadrdFields() |
| 122 |
{ |
| 123 |
return [ |
| 124 |
'client_id' => [ |
| 125 |
'type' => 'text', |
| 126 |
'label' => __('Client ID', 'fluent-booking'), |
| 127 |
'placeholder' => __('Enter Your Client ID', 'fluent-booking'), |
| 128 |
], |
| 129 |
'client_secret' => [ |
| 130 |
'type' => 'text', |
| 131 |
'label' => __('Secret Key', 'fluent-booking'), |
| 132 |
'placeholder' => __('Enter Your Secret Key', 'fluent-booking'), |
| 133 |
], |
| 134 |
'redirect_url' => [ |
| 135 |
'type' => 'text', |
| 136 |
'label' => __('Redirect URI', 'fluent-booking'), |
| 137 |
'placeholder' => __('Enter Your Redirect URI', 'fluent-booking'), |
| 138 |
'readonly' => true, |
| 139 |
'copy_btn' => true, |
| 140 |
], |
| 141 |
'caching_time' => [ |
| 142 |
'type' => 'select', |
| 143 |
'options' => [ |
| 144 |
'1' => __('1 minute', 'fluent-booking'), |
| 145 |
'5' => __('5 minutes', 'fluent-booking'), |
| 146 |
'10' => __('10 minutes', 'fluent-booking'), |
| 147 |
'15' => __('15 minutes', 'fluent-booking'), |
| 148 |
], |
| 149 |
'label' => __('Caching Time', 'fluent-booking'), |
| 150 |
/* translators: Explanation for the cache duration setting. %1$s is the calendar title, %2$s is the calendar title repeated. */ |
| 151 |
'inline_help' => sprintf(__('Select for how many minutes the %1$s event API call will be cached. Recommended 5/10 minutes. If you add lots of manual events in %2$s then you may lower the value', 'fluent-booking'), $this->calendarTitle, $this->calendarTitle) |
| 152 |
], |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
public function getConflictCheckCalendars($hostIds) |
| 157 |
{ |
| 158 |
$metaItems = Meta::where('object_type', '_'.$this->calendarKey.'_user_token') |
| 159 |
->whereIn('object_id', $hostIds) |
| 160 |
->get(); |
| 161 |
|
| 162 |
if ($metaItems->isEmpty()) { |
| 163 |
return []; |
| 164 |
} |
| 165 |
|
| 166 |
$calendars = []; |
| 167 |
|
| 168 |
foreach ($metaItems as $item) { |
| 169 |
$settings = $item->value; |
| 170 |
$checkIds = Arr::get($settings, 'conflict_check_ids', []); |
| 171 |
if (empty($checkIds)) { |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
$itemValidCalendars = []; |
| 176 |
$allCalendars = Arr::get($settings, 'calendar_lists', []); |
| 177 |
foreach ($allCalendars as $calendar) { |
| 178 |
if (in_array($calendar['id'], $checkIds)) { |
| 179 |
$itemValidCalendars[] = $calendar['id']; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if ($itemValidCalendars) { |
| 184 |
$calendars[] = [ |
| 185 |
'item' => $item, |
| 186 |
'check_ids' => $itemValidCalendars |
| 187 |
]; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return $calendars; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|