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