| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Integrations\FluentForm; |
| 4 |
|
| 5 |
use FluentForm\App\Services\Integrations\IntegrationManager; |
| 6 |
use FluentForm\Framework\Foundation\Application; |
| 7 |
use FluentSupport\App\App; |
| 8 |
use FluentSupport\App\Models\Attachment; |
| 9 |
use FluentSupport\App\Models\Customer; |
| 10 |
use FluentSupport\App\Models\MailBox; |
| 11 |
use FluentSupport\App\Models\Product; |
| 12 |
use FluentSupport\App\Models\Ticket; |
| 13 |
use FluentSupport\Framework\Support\Arr; |
| 14 |
|
| 15 |
class FeedIntegration extends IntegrationManager |
| 16 |
{ |
| 17 |
|
| 18 |
public $hasGlobalMenu = false; |
| 19 |
|
| 20 |
public $disableGlobalSettings = 'yes'; |
| 21 |
|
| 22 |
public function __construct(Application $app) |
| 23 |
{ |
| 24 |
parent::__construct( |
| 25 |
$app, |
| 26 |
'FluentSupport', |
| 27 |
'fluent_support', |
| 28 |
'_fluentsupport_settings', |
| 29 |
'fluentform_fluentsupport_feed', |
| 30 |
16 |
| 31 |
); |
| 32 |
|
| 33 |
$app = App::getInstance(); |
| 34 |
|
| 35 |
$assets = $app['url.assets']; |
| 36 |
|
| 37 |
$this->logo = $assets.'/images/fluent-support-color-logo.png'; |
| 38 |
|
| 39 |
$this->description = __('Create Support Ticket From Your Form Submission in FluentSupport', 'fluentform'); |
| 40 |
|
| 41 |
$this->registerAdminHooks(); |
| 42 |
|
| 43 |
add_filter('fluentform_notifying_async_fluent_support', '__return_false'); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
public function pushIntegration($integrations, $formId) |
| 48 |
{ |
| 49 |
$integrations[$this->integrationKey] = [ |
| 50 |
'title' => $this->title . ' Integration', |
| 51 |
'logo' => $this->logo, |
| 52 |
'is_active' => $this->isConfigured(), |
| 53 |
'configure_title' => __('Configuration required!', 'fluentform'), |
| 54 |
'global_configure_url' => '#', |
| 55 |
'configure_message' => __('FluentSupport is not configured yet! Please configure your FluentSupport api first', 'fluentform'), |
| 56 |
'configure_button_text' => __('Set FluentSupport', 'fluentform') |
| 57 |
]; |
| 58 |
return $integrations; |
| 59 |
} |
| 60 |
|
| 61 |
public function getIntegrationDefaults($settings, $formId) |
| 62 |
{ |
| 63 |
return [ |
| 64 |
'name' => '', |
| 65 |
'first_name' => '', |
| 66 |
'last_name' => '', |
| 67 |
'email' => '', |
| 68 |
'list_id' => '', // this is the business ID |
| 69 |
'ticket_title' => '', |
| 70 |
'ticket_body' => '', |
| 71 |
'client_priority' => '', |
| 72 |
'attachments' => '', |
| 73 |
'product_id' => '', |
| 74 |
'product_id_selection_type' => 'simple', |
| 75 |
'product_routers' => [], |
| 76 |
'customer_other_fields' => [ |
| 77 |
[ |
| 78 |
'item_value' => '', |
| 79 |
'label' => '' |
| 80 |
] |
| 81 |
], |
| 82 |
'conditionals' => [ |
| 83 |
'conditions' => [], |
| 84 |
'status' => false, |
| 85 |
'type' => 'all' |
| 86 |
], |
| 87 |
'enabled' => true |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
public function getSettingsFields($settings, $formId) |
| 92 |
{ |
| 93 |
$fieldOptions = []; |
| 94 |
foreach (Customer::mappables() as $key => $column) { |
| 95 |
$fieldOptions[$key] = $column; |
| 96 |
} |
| 97 |
return [ |
| 98 |
'fields' => [ |
| 99 |
[ |
| 100 |
'key' => 'name', |
| 101 |
'label' => __('Feed Name', 'fluentform'), |
| 102 |
'required' => true, |
| 103 |
'placeholder' => __('Your Feed Name', 'fluentform'), |
| 104 |
'component' => 'text' |
| 105 |
], |
| 106 |
[ |
| 107 |
'key' => 'list_id', |
| 108 |
'label' => __('Business', 'fluentform'), |
| 109 |
'placeholder' => __('Select Business', 'fluentform'), |
| 110 |
'tips' => __('Select the Business you would like to add your Support Ticket to.', 'fluentform'), |
| 111 |
'component' => 'select', |
| 112 |
'required' => true, |
| 113 |
'options' => $this->geMailBoxes(), |
| 114 |
], |
| 115 |
[ |
| 116 |
'key' => 'product_id', |
| 117 |
'require_list' => false, |
| 118 |
'label' => __('Product', 'fluentform'), |
| 119 |
'placeholder' => __('Select Support Product', 'fluentform'), |
| 120 |
'component' => 'selection_routing', |
| 121 |
'simple_component' => 'select', |
| 122 |
'routing_input_type' => 'select', |
| 123 |
'routing_key' => 'product_id_selection_type', |
| 124 |
'settings_key' => 'product_routers', |
| 125 |
'is_multiple' => false, |
| 126 |
'labels' => [ |
| 127 |
'choice_label' => __('Enable Dynamic Product Selection', 'fluentform'), |
| 128 |
'input_label' => '', |
| 129 |
'input_placeholder' => __('Set Product', 'fluentform') |
| 130 |
], |
| 131 |
'options' => $this->getProducts() |
| 132 |
], |
| 133 |
[ |
| 134 |
'key' => 'ticket_title', |
| 135 |
'require_list' => false, |
| 136 |
'label' => __('Ticket Title', 'fluentform'), |
| 137 |
'placeholder' => __('Ticket Title', 'fluentform'), |
| 138 |
'component' => 'value_text' |
| 139 |
], |
| 140 |
[ |
| 141 |
'key' => 'ticket_content', |
| 142 |
'require_list' => false, |
| 143 |
'label' => __('Ticket Content', 'fluentform'), |
| 144 |
'placeholder' => __('Ticket Content', 'fluentform'), |
| 145 |
'component' => 'value_textarea' |
| 146 |
], |
| 147 |
[ |
| 148 |
'key' => 'ticket_attachments', |
| 149 |
'require_list' => false, |
| 150 |
'label' => __('Ticket Attachments', 'fluentform'), |
| 151 |
'Placeholder' => __('Ticket Attachments', 'fluentform'), |
| 152 |
'tips' => __('Please input your file upload or image upload field shortcode here', 'fluentform'), |
| 153 |
'component' => 'value_text' |
| 154 |
], |
| 155 |
[ |
| 156 |
'key' => 'client_priority', |
| 157 |
'require_list' => false, |
| 158 |
'label' => __('Ticket Priority', 'fluentform'), |
| 159 |
'Placeholder' => __('Ticket Priority', 'fluentform'), |
| 160 |
'tips' => __('Make sure form field values match with Fluent Support priorities', 'fluentform'), |
| 161 |
'component' => 'value_text' |
| 162 |
], |
| 163 |
$this->getCustomField(), //Getting Fluent Support Custom Field To Map |
| 164 |
[ |
| 165 |
'component' => 'html_info', |
| 166 |
'html_info' => __('<h4>Please provide the ticket provider info. If user is logged in then it will use that info. For Public users you can set your customer info</h4>', 'fluentform') |
| 167 |
], |
| 168 |
[ |
| 169 |
'key' => 'CustomerFields', |
| 170 |
'require_list' => false, |
| 171 |
'label' => __('Customer Data', 'fluentform'), |
| 172 |
'tips' => __('Please Map Your Customer Data for this form. If your customer already logged in you can leave this', 'fluentform'), |
| 173 |
'component' => 'map_fields', |
| 174 |
'field_label_remote' => __('Support Customer Field', 'fluentform'), |
| 175 |
'field_label_local' => __('Form Field', 'fluentform'), |
| 176 |
'primary_fileds' => [ |
| 177 |
[ |
| 178 |
'key' => 'email', |
| 179 |
'label' => __('Email Address', 'fluentform'), |
| 180 |
'required' => true, |
| 181 |
'input_options' => 'emails' |
| 182 |
], |
| 183 |
[ |
| 184 |
'key' => 'first_name', |
| 185 |
'label' => __('First Name', 'fluentform') |
| 186 |
], |
| 187 |
[ |
| 188 |
'key' => 'last_name', |
| 189 |
'label' => __('Last Name', 'fluentform') |
| 190 |
] |
| 191 |
] |
| 192 |
], |
| 193 |
[ |
| 194 |
'key' => 'customer_other_fields', |
| 195 |
'require_list' => false, |
| 196 |
'label' => __('Customer Other Fields', 'fluentform'), |
| 197 |
'tips' => 'Select which Fluent Forms fields pair with their<br /> respective Fluent Support fields.', |
| 198 |
'field_label_remote' => __('Fluent Support Field', 'fluentform'), |
| 199 |
'field_label_local' => __('Form Field', 'fluentform'), |
| 200 |
'component' => 'dropdown_many_fields', |
| 201 |
'options' => $fieldOptions |
| 202 |
], |
| 203 |
], |
| 204 |
'button_require_list' => false, |
| 205 |
'integration_title' => $this->title |
| 206 |
]; |
| 207 |
} |
| 208 |
|
| 209 |
private function geMailBoxes() |
| 210 |
{ |
| 211 |
$items = MailBox::all(); |
| 212 |
$formattedItems = []; |
| 213 |
foreach ($items as $item) { |
| 214 |
$formattedItems[strval($item->id)] = $item->name; |
| 215 |
} |
| 216 |
return $formattedItems; |
| 217 |
} |
| 218 |
|
| 219 |
private function getProducts() |
| 220 |
{ |
| 221 |
$products = Product::all(); |
| 222 |
$formattedProducts = []; |
| 223 |
foreach ($products as $product) { |
| 224 |
$formattedProducts[strval($product->id)] = $product->title; |
| 225 |
} |
| 226 |
return $formattedProducts; |
| 227 |
} |
| 228 |
|
| 229 |
private function getCustomField() |
| 230 |
{ |
| 231 |
$customFields = apply_filters('fluent_support/ticket_custom_fields', []); |
| 232 |
$fieldToExclude = apply_filters('fluent_support/custom_field_types', []); |
| 233 |
|
| 234 |
if(empty($customFields)){ |
| 235 |
return[ |
| 236 |
'component' => 'html_info', |
| 237 |
'html_info' => __('', 'fluentform') |
| 238 |
]; |
| 239 |
} |
| 240 |
|
| 241 |
$fields = []; |
| 242 |
foreach ($customFields as $customFieldKey=>$customFieldValue) { |
| 243 |
|
| 244 |
if(in_array($customFieldValue['type'], array_keys($fieldToExclude))){ |
| 245 |
unset($customFieldValue); |
| 246 |
} |
| 247 |
|
| 248 |
$fields[] = [ |
| 249 |
'key' => $customFieldValue['slug'], |
| 250 |
'label' => __($customFieldValue['label'], 'fluentform') |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
$fields = array_map('array_filter', $fields); |
| 255 |
$fields = array_filter( $fields ); |
| 256 |
|
| 257 |
return $customFieldsMapping = [ |
| 258 |
'key' => 'TicketCustomFields', |
| 259 |
'require_list' => false, |
| 260 |
'label' => __('Ticket Custom Fields', 'fluentform'), |
| 261 |
'tips' => __('Please Map Your Ticket Custom Field Data for this form.', 'fluentform'), |
| 262 |
'component' => 'map_fields', |
| 263 |
'field_label_remote' => __('Fluent Support Custom Field', 'fluentform'), |
| 264 |
'field_label_local' => __('Form Field', 'fluentform'), |
| 265 |
'primary_fileds' => $fields |
| 266 |
]; |
| 267 |
} |
| 268 |
|
| 269 |
public function getMergeFields($list, $listId, $formId) |
| 270 |
{ |
| 271 |
return []; |
| 272 |
} |
| 273 |
|
| 274 |
public function notify($feed, $formData, $entry, $form) |
| 275 |
{ |
| 276 |
$data = $feed['processedValues']; |
| 277 |
|
| 278 |
|
| 279 |
if (!empty($data['email']) && !is_email($data['email'])) { |
| 280 |
$data['email'] = Arr::get($formData, $data['email']); |
| 281 |
} |
| 282 |
|
| 283 |
$ticketCustomField = array_filter($data, function($key) { |
| 284 |
return strpos($key, 'cf_') === 0; |
| 285 |
}, ARRAY_FILTER_USE_KEY); |
| 286 |
|
| 287 |
$customFields = apply_filters('fluent_support/ticket_custom_fields', []); |
| 288 |
|
| 289 |
foreach($customFields as $key=>$value){ |
| 290 |
$type = [$key=>$value][$key]['type']; |
| 291 |
$slug = [$key=>$value][$key]['slug']; |
| 292 |
$type=='checkbox' && array_key_exists($slug, $ticketCustomField) ? |
| 293 |
$ticketCustomField[$slug] = explode(',', Arr::get($ticketCustomField, $slug)) : $ticketCustomField; |
| 294 |
} |
| 295 |
|
| 296 |
$ticketData = [ |
| 297 |
'product_source' => 'local', |
| 298 |
'mailbox_id' => Arr::get($data, 'list_id'), |
| 299 |
'title' => sanitize_text_field(wp_unslash(Arr::get($data, 'ticket_title'))), |
| 300 |
'content' => wp_unslash(wp_kses_post(Arr::get($data, 'ticket_content'))), |
| 301 |
'attachments' => sanitize_text_field(Arr::get($data, 'ticket_attachments')), |
| 302 |
'client_priority' => strtolower(sanitize_text_field(Arr::get($data, 'client_priority'))), |
| 303 |
'priority' => strtolower(sanitize_text_field(Arr::get($data, 'client_priority'))), |
| 304 |
'custom_fields' => $ticketCustomField, |
| 305 |
'source' => 'web' |
| 306 |
]; |
| 307 |
|
| 308 |
$selectedProductArray = (array) $this->getSelectedTagIds($data, $formData, 'product_id', 'product_id_selection_type', 'product_routers'); |
| 309 |
|
| 310 |
if($selectedProductArray) { |
| 311 |
$selectedProduct = $selectedProductArray[0]; |
| 312 |
$ticketData['product_id'] = $selectedProduct; |
| 313 |
} |
| 314 |
|
| 315 |
$customerData = Arr::only($data, ['first_name', 'last_name', 'email']); |
| 316 |
|
| 317 |
foreach (Arr::get($data, 'customer_other_fields') as $field) { |
| 318 |
if ($field['item_value']) { |
| 319 |
$customerData[$field['label']] = $field['item_value']; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
$user = get_user_by('ID', get_current_user_id()); |
| 325 |
|
| 326 |
if(!$user) { |
| 327 |
$user = get_user_by('email', $customerData['email']); |
| 328 |
} |
| 329 |
|
| 330 |
if($user) { |
| 331 |
$customerData['email'] = $user->user_email; |
| 332 |
$customerData['user_id'] = $user->ID; |
| 333 |
} |
| 334 |
|
| 335 |
if(empty($customerData['email'])) { |
| 336 |
do_action('ff_log_data', [ |
| 337 |
'title' => $feed['settings']['name'], |
| 338 |
'status' => 'failed', |
| 339 |
'description' => __('Support ticket creation failed, because no valid customer email found', 'fluentform'), |
| 340 |
'parent_source_id' => $form->id, |
| 341 |
'source_id' => $entry->id, |
| 342 |
'component' => $this->integrationKey, |
| 343 |
'source_type' => 'submission_item' |
| 344 |
]); |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
$customerData['last_ip_address'] = $entry->ip; |
| 349 |
|
| 350 |
$customer = Customer::maybeCreateCustomer($customerData); |
| 351 |
|
| 352 |
$ticketData['customer_id'] = $customer->id; |
| 353 |
|
| 354 |
$ticketData = apply_filters('fluent_support/create_ticket_data', $ticketData, $customer); |
| 355 |
do_action('fluent_support/before_ticket_create', $ticketData, $customer); |
| 356 |
|
| 357 |
$ticket = Ticket::create($ticketData); |
| 358 |
|
| 359 |
if(defined('FLUENTSUPPORTPRO') && !empty($ticketData['custom_fields'])) { |
| 360 |
$ticket->syncCustomFields($ticketData['custom_fields']); |
| 361 |
$ticket->custom_fields = $ticket->customData(); |
| 362 |
} |
| 363 |
|
| 364 |
do_action('fluent_support/ticket_created', $ticket, $customer); |
| 365 |
|
| 366 |
do_action('ff_log_data', [ |
| 367 |
'title' => $feed['settings']['name'], |
| 368 |
'status' => 'success', |
| 369 |
'description' => __('Support ticket has been created at Fluent Support. Ticket ID: '.$ticket->id, 'fluentform'), |
| 370 |
'parent_source_id' => $form->id, |
| 371 |
'source_id' => $entry->id, |
| 372 |
'component' => $this->integrationKey, |
| 373 |
'source_type' => 'submission_item' |
| 374 |
]); |
| 375 |
|
| 376 |
if($ticketData['attachments']) { |
| 377 |
$attachments = explode(',', $ticketData['attachments']); |
| 378 |
|
| 379 |
foreach ($attachments as $attachment){ |
| 380 |
$fileName = explode('/', $attachment); |
| 381 |
$fileName = end($fileName); |
| 382 |
Attachment::create( |
| 383 |
[ |
| 384 |
'ticket_id' => $ticket->id, |
| 385 |
'file_path' => esc_url($attachment), |
| 386 |
'full_url' => esc_url($attachment), |
| 387 |
'title' => $fileName, |
| 388 |
'person_id' => $customer->id |
| 389 |
] |
| 390 |
); |
| 391 |
} |
| 392 |
$ticket->load($attachments); |
| 393 |
} |
| 394 |
|
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
public function isConfigured() |
| 399 |
{ |
| 400 |
return true; |
| 401 |
} |
| 402 |
|
| 403 |
public function isEnabled() |
| 404 |
{ |
| 405 |
return true; |
| 406 |
} |
| 407 |
|
| 408 |
} |
| 409 |
|