PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.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 1.7.2 All 33 releases
fluent-booking / app / Services / Integrations / CalendarIntegrationService.php

CalendarIntegrationService.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.3.0, at app/Services/Integrations/CalendarIntegrationService.php

202 lines 7.2 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\Services\Integrations;
4
5 use FluentBooking\App\Models\Meta;
6 use FluentBooking\Framework\Support\Arr;
7 use FluentBooking\Framework\Validator\ValidationException;
8
9 class CalendarIntegrationService
10 {
11 public function find($attr)
12 {
13 $slotId = intval(Arr::get($attr, 'slot_id'));
14 $integrationId = intval(Arr::get($attr, 'integration_id'));
15 $integrationName = sanitize_text_field(Arr::get($attr, 'integration_name'));
16
17 $settings = [
18 'conditionals' => [
19 'conditions' => [],
20 'status' => false,
21 'type' => 'all',
22 ],
23 'enabled' => true,
24 'list_id' => '',
25 'list_name' => '',
26 'name' => '',
27 'merge_fields' => [],
28 ];
29
30 $mergeFields = false;
31 if ($integrationId) {
32 $feed = Meta::where('id', $integrationId)
33 ->where('object_id', $slotId)
34 ->where('object_type', 'integration')
35 ->where('key', $integrationName . '_feeds')
36 ->first();
37
38 if ($feed && $feed->value) {
39 $settings = $feed->value;
40 $settings = apply_filters('fluent_booking/get_integration_values_' . $integrationName, $settings, $feed, $slotId);
41 if (!empty($settings['list_id'])) {
42 $mergeFields = apply_filters('fluent_booking/get_integration_merge_fields_' . $integrationName, false, $settings['list_id'], $slotId);
43 }
44 }
45 } else {
46 $settings = apply_filters('fluent_booking/get_integration_defaults_' . $integrationName, false, $slotId);
47 }
48
49 $enabled = Arr::get($settings, 'enabled');
50 if ('true' === $enabled) {
51 $settings['enabled'] = true;
52 } elseif ('false' === $enabled) {
53 $settings['enabled'] = false;
54 } else {
55 $settings['enabled'] = (bool) $enabled;
56 }
57
58 $settingsFields = apply_filters('fluent_booking/get_integration_settings_fields_' . $integrationName, $settings, $slotId, $settings);
59
60 return [
61 'settings' => $settings,
62 'settings_fields' => $settingsFields,
63 'merge_fields' => $mergeFields,
64 ];
65 }
66
67 public function update($attr)
68 {
69 $slotId = intval(Arr::get($attr, 'slot_id'));
70 $integrationId = intval(Arr::get($attr, 'integration_id'));
71 $integrationName = sanitize_text_field(Arr::get($attr, 'integration_name'));
72 $dataType = sanitize_text_field(Arr::get($attr, 'data_type'));
73 $status = Arr::get($attr, 'status', true);
74 $metaValue = Arr::get($attr, 'integration');
75
76 $errors = [];
77
78 if ('stringify' == $dataType) {
79 $metaValue = \json_decode($metaValue, true);
80 } else {
81 $metaValue = wp_unslash($metaValue);
82 }
83
84 $isUpdatingStatus = empty($metaValue);
85
86 if ($isUpdatingStatus) {
87 $integrationData = Meta::findOrFail($integrationId);
88 $metaValue = $integrationData->value;
89 $metaValue['enabled'] = $status;
90 $metaKey = $integrationData->key;
91 } else {
92 if (empty($metaValue['name'])) {
93 $errors['name'] = [__('Feed name is required', 'fluent-booking')];
94 throw new ValidationException(__('Validation Failed! Feed name is required', 'fluent-booking'), 422, null, $errors); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
95 }
96 $metaValue = apply_filters('fluent_booking/save_integration_value_' . $integrationName, $metaValue, $integrationId, $slotId);
97 $metaKey = $integrationName . '_feeds';
98 }
99
100 // Validate the meta values
101 if ($metaValue['enabled']) {
102 // Required fields
103
104 if(empty($metaValue['email'])) {
105 $errors['email'] = [__('Email is required', 'fluent-booking')];
106 }
107
108 if(empty($metaValue['event_trigger'])) {
109 $errors['event_trigger'] = [__('Event trigger is required', 'fluent-booking')];
110 }
111
112 if($errors) {
113 throw new ValidationException(__('Validation Failed! Please fill up required fields', 'fluent-booking'), 422, null, $errors); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
114 }
115 }
116
117 $data = [
118 'object_id' => $slotId,
119 'object_type' => 'integration',
120 'key' => $metaKey,
121 'value' => $metaValue,
122 ];
123
124 $data = apply_filters('fluent_booking/save_integration_settings_' . $integrationName, $data, $integrationId);
125 $created = false;
126
127 if ($integrationId) {
128 $integration = Meta::where('object_id', $slotId)
129 ->where('object_type', 'integration')
130 ->findOrFail($integrationId);
131 $integration->value = $data['value'];
132 $integration->save();
133 } else {
134 $integration = Meta::create($data);
135 $integrationId = $integration->id;
136 $created = true;
137 }
138
139 return [
140 'message' => __('Integration successfully saved', 'fluent-booking'),
141 'integration_id' => $integrationId,
142 'integration_name' => $integrationName,
143 'created' => $created,
144 ];
145 }
146
147 public function get($slotId)
148 {
149 $notificationKeys = apply_filters('fluent_booking/global_notification_types', [], $slotId);
150
151 $feeds = [];
152 if ($notificationKeys) {
153 $feeds = Meta::whereIn('key', $notificationKeys)->where('object_id', $slotId)->get();
154 }
155 $formattedFeeds = [];
156
157 if (!empty($feeds)) {
158 foreach ($feeds as $feed) {
159 $data = $feed->value;
160 $enabled = Arr::get($data, 'enabled');
161 if ($enabled == 'true') {
162 $enabled = true;
163 } else {
164 $enabled = false;
165 }
166
167 $feedData = [
168 'id' => $feed->id,
169 'name' => Arr::get($data, 'name'),
170 'enabled' => $enabled,
171 'provider' => $feed->key,
172 'feed' => $data,
173 ];
174
175 $feedData = apply_filters('fluent_booking/global_notification_feed_' . $feed->key, $feedData, $slotId);
176
177 $formattedFeeds[] = $feedData;
178 }
179 }
180
181 $availableIntegrations = apply_filters('fluent_booking/get_available_form_integrations', [], $slotId);
182
183 return [
184 'feeds' => $formattedFeeds,
185 'available_integrations' => $availableIntegrations,
186 // 'all_module_config_url' => admin_url('admin.php?page=fluent_forms_add_ons'),
187 ];
188 }
189
190 public function delete($integrationId, $eventId = null)
191 {
192 $query = Meta::where('id', $integrationId);
193
194 if ($eventId !== null) {
195 $query->where('object_id', $eventId)
196 ->where('object_type', 'integration');
197 }
198
199 return $query->delete();
200 }
201 }
202