PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Http / Controllers / CalendarIntegrationController.php

CalendarIntegrationController.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.5.0, at app/Http/Controllers/CalendarIntegrationController.php

225 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $fromEvent = CalendarSlot::find($fromEventId);
96
97 if (!$fromEvent) {
98 return $this->sendError([
99 'message' => __('Source event not found', 'fluent-booking')
100 ], 422);
101 }
102
103 if (!PermissionManager::canUpdateCalendarEvent($fromEvent->id)) {
104 return $this->sendError([
105 'message' => __('You do not have permission to clone from the selected event.', 'fluent-booking')
106 ], 403);
107 }
108
109 $fromEventIntegrations = Meta::where('object_id', $fromEvent->id)
110 ->where('object_type', 'integration')
111 ->get();
112
113 if ($fromEventIntegrations->isEmpty()) {
114 return $this->sendError([
115 'message' => __('Integrations not found', 'fluent-booking')
116 ], 422);
117 }
118
119 foreach ($fromEventIntegrations as $feed) {
120 $cloneIntegration = $feed->replicate();
121 $cloneIntegration->object_id = $calendarEvent->id;
122 $cloneIntegration->save();
123 }
124
125 return [
126 'message' => __('Integrations have been successfully cloned.', 'fluent-booking')
127 ];
128 }
129
130 public function integrationListComponent($calendarId, $slotId, $integrationId)
131 {
132 try {
133 $this->getCalendarEvent($calendarId, $slotId);
134 $integrationName = $this->request->get('integration_name');
135 $listId = $this->request->get('list_id');
136 $merge_fields = false;
137
138 $merge_fields = apply_filters('fluent_booking/get_integration_merge_fields_' . $integrationName, $merge_fields, $listId, $slotId);
139
140 return $this->sendSuccess([
141 'merge_fields' => $merge_fields,
142 ]);
143 } catch (Exception $e) {
144 return $this->sendError([
145 'message' => $e->getMessage(),
146 ], 422);
147 }
148 }
149
150 public function getConfigFieldOptions($calendarId, $calendarEventId, $integrationId)
151 {
152 try {
153 $this->getCalendarEvent($calendarId, $calendarEventId);
154 $integrationName = $this->request->get('integration_name');
155 $settings = $this->request->get('settings');
156
157 $fieldOptions = apply_filters('fluent_booking/get_integration_config_field_options_' . $integrationName, $settings, $calendarEventId);
158
159 return $this->sendSuccess([
160 'field_options' => $fieldOptions,
161 ]);
162 } catch (Exception $e) {
163 return $this->sendError([
164 'message' => $e->getMessage(),
165 ], 422);
166 }
167 }
168
169 public function createCrmTaxonomy($calendarId, $slotId, $integrationId)
170 {
171 try {
172 $this->getCalendarEvent($calendarId, $slotId);
173
174 if (!CrmContactService::isActive()) {
175 return $this->sendError([
176 'message' => __('FluentCRM is not active.', 'fluent-booking'),
177 ], 422);
178 }
179
180 if (!CrmPermissionManager::currentUserCan('fcrm_manage_contact_cats')) {
181 return $this->sendError([
182 'message' => __('You do not have permission to create FluentCRM tags or lists.', 'fluent-booking'),
183 ], 403);
184 }
185
186 $type = $this->request->get('type');
187 $title = trim((string) $this->request->get('title'));
188
189 if (!in_array($type, ['tags', 'lists'], true)) {
190 return $this->sendError([
191 'message' => __('Invalid taxonomy type.', 'fluent-booking'),
192 ], 422);
193 }
194
195 if ($title === '') {
196 return $this->sendError([
197 'message' => __('Please provide a name.', 'fluent-booking'),
198 ], 422);
199 }
200
201 $item = CrmContactService::createTaxonomy($type, $title);
202
203 if (!$item) {
204 return $this->sendError([
205 'message' => __('Please use a name that contains letters or numbers.', 'fluent-booking'),
206 ], 422);
207 }
208
209 return $this->sendSuccess([
210 'item' => $item,
211 'message' => __('Successfully created.', 'fluent-booking'),
212 ]);
213 } catch (Exception $e) {
214 return $this->sendError([
215 'message' => $e->getMessage(),
216 ], 422);
217 }
218 }
219
220 private function getCalendarEvent($calendarId, $eventId)
221 {
222 return CalendarSlot::where('calendar_id', $calendarId)->findOrFail($eventId);
223 }
224 }
225