PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.2.5
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.2.5
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.2.5, at app/Services/Integrations/CalendarIntegrationService.php

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