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