| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Http\Controllers; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentBooking\App\Models\Meta; |
| 7 |
use FluentBooking\App\Models\CalendarSlot; |
| 8 |
use FluentBooking\App\Services\Helper; |
| 9 |
use FluentBooking\App\Services\PermissionManager; |
| 10 |
use FluentBooking\App\Services\Integrations\CalendarIntegrationService; |
| 11 |
|
| 12 |
class CalendarIntegrationController extends Controller |
| 13 |
{ |
| 14 |
public function index(CalendarIntegrationService $integrationService, $calendarId, $eventId) |
| 15 |
{ |
| 16 |
try { |
| 17 |
$calendarEvent = $this->getCalendarEvent($calendarId, $eventId); |
| 18 |
$settings = $integrationService->get($eventId); |
| 19 |
|
| 20 |
$settings['smart_codes'] = [ |
| 21 |
'texts' => Helper::getEditorShortCodes($calendarEvent), |
| 22 |
'html' => Helper::getEditorShortCodes($calendarEvent, true) |
| 23 |
]; |
| 24 |
|
| 25 |
return $this->sendSuccess($settings); |
| 26 |
} catch (Exception $e) { |
| 27 |
return $this->sendError([ |
| 28 |
'message' => $e->getMessage(), |
| 29 |
], 422); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public function find(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 34 |
{ |
| 35 |
try { |
| 36 |
$this->getCalendarEvent($calendarId, $slotId); |
| 37 |
$data = $this->request->all(); |
| 38 |
$data['slot_id'] = $slotId; |
| 39 |
$integration = $integrationService->find($data); |
| 40 |
return $this->sendSuccess($integration); |
| 41 |
} catch (Exception $e) { |
| 42 |
return $this->sendError([ |
| 43 |
'message' => $e->getMessage(), |
| 44 |
], 422); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function update(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 49 |
{ |
| 50 |
|
| 51 |
$data = $this->request->all(); |
| 52 |
$data['slot_id'] = $slotId; |
| 53 |
$data['integration_id'] = $integrationId; |
| 54 |
|
| 55 |
try { |
| 56 |
$integration = $integrationService->update($data); |
| 57 |
return $this->sendSuccess($integration); |
| 58 |
} catch (Exception $e) { |
| 59 |
return $this->sendError([ |
| 60 |
'message' => $e->getMessage(), |
| 61 |
'errors' => $e->errors(), |
| 62 |
], 422); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function delete(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 67 |
{ |
| 68 |
try { |
| 69 |
$deleted = $integrationService->delete($integrationId, $slotId); |
| 70 |
|
| 71 |
if (!$deleted) { |
| 72 |
return $this->sendError([ |
| 73 |
'message' => __('Integration not found for this event.', 'fluent-booking'), |
| 74 |
], 404); |
| 75 |
} |
| 76 |
|
| 77 |
return $this->sendSuccess([ |
| 78 |
'message' => __('Successfully deleted the Integration.', 'fluent-booking'), |
| 79 |
]); |
| 80 |
} catch (Exception $e) { |
| 81 |
return $this->sendError([ |
| 82 |
'message' => $e->getMessage(), |
| 83 |
], 422); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function cloneIntegrations(CalendarIntegrationService $integrationService, $calendarId, $slotId) |
| 88 |
{ |
| 89 |
$calendarEvent = CalendarSlot::where('calendar_id', $calendarId)->findOrFail($slotId); |
| 90 |
|
| 91 |
$fromEventId = intval($this->request->get('from_event_id')); |
| 92 |
|
| 93 |
if (!$fromEventId || !PermissionManager::canUpdateCalendarEvent($fromEventId)) { |
| 94 |
return $this->sendError([ |
| 95 |
'message' => __('You do not have permission to clone from the selected event.', 'fluent-booking') |
| 96 |
], 403); |
| 97 |
} |
| 98 |
|
| 99 |
$fromEventIntegrations = Meta::where('object_id', $fromEventId) |
| 100 |
->where('object_type', 'integration') |
| 101 |
->get(); |
| 102 |
|
| 103 |
if ($fromEventIntegrations->isEmpty()) { |
| 104 |
return $this->sendError([ |
| 105 |
'message' => __('Integrations not found', 'fluent-booking') |
| 106 |
], 422); |
| 107 |
} |
| 108 |
|
| 109 |
foreach ($fromEventIntegrations as $feed) { |
| 110 |
$cloneIntegration = $feed->replicate(); |
| 111 |
$cloneIntegration->object_id = $calendarEvent->id; |
| 112 |
$cloneIntegration->save(); |
| 113 |
} |
| 114 |
|
| 115 |
return [ |
| 116 |
'message' => __('Integrations have been successfully cloned.', 'fluent-booking') |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
public function integrationListComponent($calendarId, $slotId, $integrationId) |
| 121 |
{ |
| 122 |
try { |
| 123 |
$this->getCalendarEvent($calendarId, $slotId); |
| 124 |
$integrationName = $this->request->get('integration_name'); |
| 125 |
$listId = $this->request->get('list_id'); |
| 126 |
$merge_fields = false; |
| 127 |
|
| 128 |
$merge_fields = apply_filters('fluent_booking/get_integration_merge_fields_' . $integrationName, $merge_fields, $listId, $slotId); |
| 129 |
|
| 130 |
return $this->sendSuccess([ |
| 131 |
'merge_fields' => $merge_fields, |
| 132 |
]); |
| 133 |
} catch (Exception $e) { |
| 134 |
return $this->sendError([ |
| 135 |
'message' => $e->getMessage(), |
| 136 |
], 422); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public function getConfigFieldOptions($calendarId, $calendarEventId, $integrationId) |
| 141 |
{ |
| 142 |
try { |
| 143 |
$this->getCalendarEvent($calendarId, $calendarEventId); |
| 144 |
$integrationName = $this->request->get('integration_name'); |
| 145 |
$settings = $this->request->get('settings'); |
| 146 |
|
| 147 |
$fieldOptions = apply_filters('fluent_booking/get_integration_config_field_options_' . $integrationName, $settings, $calendarEventId); |
| 148 |
|
| 149 |
return $this->sendSuccess([ |
| 150 |
'field_options' => $fieldOptions, |
| 151 |
]); |
| 152 |
} catch (Exception $e) { |
| 153 |
return $this->sendError([ |
| 154 |
'message' => $e->getMessage(), |
| 155 |
], 422); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private function getCalendarEvent($calendarId, $eventId) |
| 160 |
{ |
| 161 |
return CalendarSlot::where('calendar_id', $calendarId)->findOrFail($eventId); |
| 162 |
} |
| 163 |
} |
| 164 |
|