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

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