| 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 |
use FluentBooking\App\Services\Integrations\FluentCRM\CrmContactService; |
| 12 |
use FluentCrm\App\Services\PermissionManager as CrmPermissionManager; |
| 13 |
|
| 14 |
class CalendarIntegrationController extends Controller |
| 15 |
{ |
| 16 |
public function index(CalendarIntegrationService $integrationService, $calendarId, $eventId) |
| 17 |
{ |
| 18 |
try { |
| 19 |
$calendarEvent = $this->getCalendarEvent($calendarId, $eventId); |
| 20 |
$settings = $integrationService->get($eventId); |
| 21 |
|
| 22 |
$settings['smart_codes'] = [ |
| 23 |
'texts' => Helper::getEditorShortCodes($calendarEvent), |
| 24 |
'html' => Helper::getEditorShortCodes($calendarEvent, true) |
| 25 |
]; |
| 26 |
|
| 27 |
return $this->sendSuccess($settings); |
| 28 |
} catch (Exception $e) { |
| 29 |
return $this->sendError([ |
| 30 |
'message' => $e->getMessage(), |
| 31 |
], 422); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public function find(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 36 |
{ |
| 37 |
try { |
| 38 |
$this->getCalendarEvent($calendarId, $slotId); |
| 39 |
$data = $this->request->all(); |
| 40 |
$data['slot_id'] = $slotId; |
| 41 |
$integration = $integrationService->find($data); |
| 42 |
return $this->sendSuccess($integration); |
| 43 |
} catch (Exception $e) { |
| 44 |
return $this->sendError([ |
| 45 |
'message' => $e->getMessage(), |
| 46 |
], 422); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public function update(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 51 |
{ |
| 52 |
|
| 53 |
$data = $this->request->all(); |
| 54 |
$data['slot_id'] = $slotId; |
| 55 |
$data['integration_id'] = $integrationId; |
| 56 |
|
| 57 |
try { |
| 58 |
$integration = $integrationService->update($data); |
| 59 |
return $this->sendSuccess($integration); |
| 60 |
} catch (Exception $e) { |
| 61 |
return $this->sendError([ |
| 62 |
'message' => $e->getMessage(), |
| 63 |
'errors' => $e->errors(), |
| 64 |
], 422); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function delete(CalendarIntegrationService $integrationService, $calendarId, $slotId, $integrationId) |
| 69 |
{ |
| 70 |
try { |
| 71 |
$deleted = $integrationService->delete($integrationId, $slotId); |
| 72 |
|
| 73 |
if (!$deleted) { |
| 74 |
return $this->sendError([ |
| 75 |
'message' => __('Integration not found for this event.', 'fluent-booking'), |
| 76 |
], 404); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->sendSuccess([ |
| 80 |
'message' => __('Successfully deleted the Integration.', 'fluent-booking'), |
| 81 |
]); |
| 82 |
} catch (Exception $e) { |
| 83 |
return $this->sendError([ |
| 84 |
'message' => $e->getMessage(), |
| 85 |
], 422); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public function cloneIntegrations(CalendarIntegrationService $integrationService, $calendarId, $slotId) |
| 90 |
{ |
| 91 |
$calendarEvent = CalendarSlot::where('calendar_id', $calendarId)->findOrFail($slotId); |
| 92 |
|
| 93 |
$fromEventId = intval($this->request->get('from_event_id')); |
| 94 |
|
| 95 |
if (!$fromEventId || !PermissionManager::canUpdateCalendarEvent($fromEventId)) { |
| 96 |
return $this->sendError([ |
| 97 |
'message' => __('You do not have permission to clone from the selected event.', 'fluent-booking') |
| 98 |
], 403); |
| 99 |
} |
| 100 |
|
| 101 |
$fromEventIntegrations = Meta::where('object_id', $fromEventId) |
| 102 |
->where('object_type', 'integration') |
| 103 |
->get(); |
| 104 |
|
| 105 |
if ($fromEventIntegrations->isEmpty()) { |
| 106 |
return $this->sendError([ |
| 107 |
'message' => __('Integrations not found', 'fluent-booking') |
| 108 |
], 422); |
| 109 |
} |
| 110 |
|
| 111 |
foreach ($fromEventIntegrations as $feed) { |
| 112 |
$cloneIntegration = $feed->replicate(); |
| 113 |
$cloneIntegration->object_id = $calendarEvent->id; |
| 114 |
$cloneIntegration->save(); |
| 115 |
} |
| 116 |
|
| 117 |
return [ |
| 118 |
'message' => __('Integrations have been successfully cloned.', 'fluent-booking') |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
public function integrationListComponent($calendarId, $slotId, $integrationId) |
| 123 |
{ |
| 124 |
try { |
| 125 |
$this->getCalendarEvent($calendarId, $slotId); |
| 126 |
$integrationName = $this->request->get('integration_name'); |
| 127 |
$listId = $this->request->get('list_id'); |
| 128 |
$merge_fields = false; |
| 129 |
|
| 130 |
$merge_fields = apply_filters('fluent_booking/get_integration_merge_fields_' . $integrationName, $merge_fields, $listId, $slotId); |
| 131 |
|
| 132 |
return $this->sendSuccess([ |
| 133 |
'merge_fields' => $merge_fields, |
| 134 |
]); |
| 135 |
} catch (Exception $e) { |
| 136 |
return $this->sendError([ |
| 137 |
'message' => $e->getMessage(), |
| 138 |
], 422); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function getConfigFieldOptions($calendarId, $calendarEventId, $integrationId) |
| 143 |
{ |
| 144 |
try { |
| 145 |
$this->getCalendarEvent($calendarId, $calendarEventId); |
| 146 |
$integrationName = $this->request->get('integration_name'); |
| 147 |
$settings = $this->request->get('settings'); |
| 148 |
|
| 149 |
$fieldOptions = apply_filters('fluent_booking/get_integration_config_field_options_' . $integrationName, $settings, $calendarEventId); |
| 150 |
|
| 151 |
return $this->sendSuccess([ |
| 152 |
'field_options' => $fieldOptions, |
| 153 |
]); |
| 154 |
} catch (Exception $e) { |
| 155 |
return $this->sendError([ |
| 156 |
'message' => $e->getMessage(), |
| 157 |
], 422); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public function createCrmTaxonomy($calendarId, $slotId, $integrationId) |
| 162 |
{ |
| 163 |
try { |
| 164 |
$this->getCalendarEvent($calendarId, $slotId); |
| 165 |
|
| 166 |
if (!CrmContactService::isActive()) { |
| 167 |
return $this->sendError([ |
| 168 |
'message' => __('FluentCRM is not active.', 'fluent-booking'), |
| 169 |
], 422); |
| 170 |
} |
| 171 |
|
| 172 |
if (!CrmPermissionManager::currentUserCan('fcrm_manage_contact_cats')) { |
| 173 |
return $this->sendError([ |
| 174 |
'message' => __('You do not have permission to create FluentCRM tags or lists.', 'fluent-booking'), |
| 175 |
], 403); |
| 176 |
} |
| 177 |
|
| 178 |
$type = $this->request->get('type'); |
| 179 |
$title = trim((string) $this->request->get('title')); |
| 180 |
|
| 181 |
if (!in_array($type, ['tags', 'lists'], true)) { |
| 182 |
return $this->sendError([ |
| 183 |
'message' => __('Invalid taxonomy type.', 'fluent-booking'), |
| 184 |
], 422); |
| 185 |
} |
| 186 |
|
| 187 |
if ($title === '') { |
| 188 |
return $this->sendError([ |
| 189 |
'message' => __('Please provide a name.', 'fluent-booking'), |
| 190 |
], 422); |
| 191 |
} |
| 192 |
|
| 193 |
$item = CrmContactService::createTaxonomy($type, $title); |
| 194 |
|
| 195 |
if (!$item) { |
| 196 |
return $this->sendError([ |
| 197 |
'message' => __('Please use a name that contains letters or numbers.', 'fluent-booking'), |
| 198 |
], 422); |
| 199 |
} |
| 200 |
|
| 201 |
return $this->sendSuccess([ |
| 202 |
'item' => $item, |
| 203 |
'message' => __('Successfully created.', 'fluent-booking'), |
| 204 |
]); |
| 205 |
} catch (Exception $e) { |
| 206 |
return $this->sendError([ |
| 207 |
'message' => $e->getMessage(), |
| 208 |
], 422); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
private function getCalendarEvent($calendarId, $eventId) |
| 213 |
{ |
| 214 |
return CalendarSlot::where('calendar_id', $calendarId)->findOrFail($eventId); |
| 215 |
} |
| 216 |
} |
| 217 |
|