PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.3
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Services / Integrations / FluentForm / FeedIntegration.php

FeedIntegration.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.5.3, at app/Services/Integrations/FluentForm/FeedIntegration.php

390 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'attachments' => '',
72 'product_id' => '',
73 'product_id_selection_type' => 'simple',
74 'product_routers' => [],
75 'customer_other_fields' => [
76 [
77 'item_value' => '',
78 'label' => ''
79 ]
80 ],
81 'conditionals' => [
82 'conditions' => [],
83 'status' => false,
84 'type' => 'all'
85 ],
86 'enabled' => true
87 ];
88 }
89
90 public function getSettingsFields($settings, $formId)
91 {
92 $fieldOptions = [];
93 foreach (Customer::mappables() as $key => $column) {
94 $fieldOptions[$key] = $column;
95 }
96 return [
97 'fields' => [
98 [
99 'key' => 'name',
100 'label' => __('Feed Name', 'fluentform'),
101 'required' => true,
102 'placeholder' => __('Your Feed Name', 'fluentform'),
103 'component' => 'text'
104 ],
105 [
106 'key' => 'list_id',
107 'label' => __('Business', 'fluentform'),
108 'placeholder' => __('Select Business', 'fluentform'),
109 'tips' => __('Select the Business you would like to add your Support Ticket to.', 'fluentform'),
110 'component' => 'select',
111 'required' => true,
112 'options' => $this->geMailBoxes(),
113 ],
114 [
115 'key' => 'product_id',
116 'require_list' => false,
117 'label' => __('Product', 'fluentform'),
118 'placeholder' => __('Select Support Product', 'fluentform'),
119 'component' => 'selection_routing',
120 'simple_component' => 'select',
121 'routing_input_type' => 'select',
122 'routing_key' => 'product_id_selection_type',
123 'settings_key' => 'product_routers',
124 'is_multiple' => false,
125 'labels' => [
126 'choice_label' => __('Enable Dynamic Product Selection', 'fluentform'),
127 'input_label' => '',
128 'input_placeholder' => __('Set Product', 'fluentform')
129 ],
130 'options' => $this->getProducts()
131 ],
132 [
133 'key' => 'ticket_title',
134 'require_list' => false,
135 'label' => __('Ticket Title', 'fluentform'),
136 'placeholder' => __('Ticket Title', 'fluentform'),
137 'component' => 'value_text'
138 ],
139 [
140 'key' => 'ticket_content',
141 'require_list' => false,
142 'label' => __('Ticket Content', 'fluentform'),
143 'placeholder' => __('Ticket Content', 'fluentform'),
144 'component' => 'value_textarea'
145 ],
146 [
147 'key' => 'ticket_attachments',
148 'require_list' => false,
149 'label' => __('Ticket Attachments', 'fluentform'),
150 'Placeholder' => __('Ticket Attachments', 'fluentform'),
151 'tips' => __('Please input your file upload or image upload field shortcode here', 'fluentform'),
152 'component' => 'value_text'
153 ],
154 $this->getCustomField(), //Getting Fluent Support Custom Field To Map
155 [
156 'component' => 'html_info',
157 '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')
158 ],
159 [
160 'key' => 'CustomerFields',
161 'require_list' => false,
162 'label' => __('Customer Data', 'fluentform'),
163 'tips' => __('Please Map Your Customer Data for this form. If your customer already logged in you can leave this', 'fluentform'),
164 'component' => 'map_fields',
165 'field_label_remote' => __('Support Customer Field', 'fluentform'),
166 'field_label_local' => __('Form Field', 'fluentform'),
167 'primary_fileds' => [
168 [
169 'key' => 'email',
170 'label' => __('Email Address', 'fluentform'),
171 'required' => true,
172 'input_options' => 'emails'
173 ],
174 [
175 'key' => 'first_name',
176 'label' => __('First Name', 'fluentform')
177 ],
178 [
179 'key' => 'last_name',
180 'label' => __('Last Name', 'fluentform')
181 ]
182 ]
183 ],
184 [
185 'key' => 'customer_other_fields',
186 'require_list' => false,
187 'label' => __('Customer Other Fields', 'fluentform'),
188 'tips' => 'Select which Fluent Forms fields pair with their<br /> respective Fluent Support fields.',
189 'field_label_remote' => __('Fluent Support Field', 'fluentform'),
190 'field_label_local' => __('Form Field', 'fluentform'),
191 'component' => 'dropdown_many_fields',
192 'options' => $fieldOptions
193 ],
194 ],
195 'button_require_list' => false,
196 'integration_title' => $this->title
197 ];
198 }
199
200 private function geMailBoxes()
201 {
202 $items = MailBox::all();
203 $formattedItems = [];
204 foreach ($items as $item) {
205 $formattedItems[strval($item->id)] = $item->name;
206 }
207 return $formattedItems;
208 }
209
210 private function getProducts()
211 {
212 $products = Product::all();
213 $formattedProducts = [];
214 foreach ($products as $product) {
215 $formattedProducts[strval($product->id)] = $product->title;
216 }
217 return $formattedProducts;
218 }
219
220 private function getCustomField()
221 {
222 $customFields = apply_filters('fluent_support/ticket_custom_fields', []);
223
224 if(empty($customFields)){
225 return[
226 'component' => 'html_info',
227 'html_info' => __('', 'fluentform')
228 ];
229 }
230
231 $fields = [];
232 foreach ($customFields as $customFieldKey=>$customFieldValue) {
233
234 $fields[] = [
235 'key' => $customFieldValue['slug'],
236 'label' => __($customFieldValue['label'], 'fluentform')
237 ];
238 }
239
240 return $customFieldsMapping = [
241 'key' => 'TicketCustomFields',
242 'require_list' => false,
243 'label' => __('Ticket Custom Fields', 'fluentform'),
244 'tips' => __('Please Map Your Ticket Custom Field Data for this form.', 'fluentform'),
245 'component' => 'map_fields',
246 'field_label_remote' => __('Fluent Support Custom Field', 'fluentform'),
247 'field_label_local' => __('Form Field', 'fluentform'),
248 'primary_fileds' => $fields
249 ];
250 }
251
252 public function getMergeFields($list, $listId, $formId)
253 {
254 return [];
255 }
256
257 public function notify($feed, $formData, $entry, $form)
258 {
259 $data = $feed['processedValues'];
260
261
262 if (!empty($data['email']) && !is_email($data['email'])) {
263 $data['email'] = Arr::get($formData, $data['email']);
264 }
265
266 $ticketCustomField = array_filter($data, function($key) {
267 return strpos($key, 'cf_') === 0;
268 }, ARRAY_FILTER_USE_KEY);
269
270 $customFields = apply_filters('fluent_support/ticket_custom_fields', []);
271
272 foreach($customFields as $key=>$value){
273 $type = [$key=>$value][$key]['type'];
274 $slug = [$key=>$value][$key]['slug'];
275 $type=='checkbox' && array_key_exists($slug, $ticketCustomField) ?
276 $ticketCustomField[$slug] = explode(',', Arr::get($ticketCustomField, $slug)) : $ticketCustomField;
277 }
278
279 $ticketData = [
280 'product_source' => 'local',
281 'mailbox_id' => Arr::get($data, 'list_id'),
282 'title' => sanitize_text_field(wp_unslash(Arr::get($data, 'ticket_title'))),
283 'content' => wp_unslash(wp_kses_post(Arr::get($data, 'ticket_content'))),
284 'attachments' => sanitize_text_field(Arr::get($data, 'ticket_attachments')),
285 'custom_fields' => $ticketCustomField,
286 'source' => 'web'
287 ];
288
289 $selectedProductArray = (array) $this->getSelectedTagIds($data, $formData, 'product_id', 'product_id_selection_type', 'product_routers');
290
291 if($selectedProductArray) {
292 $selectedProduct = $selectedProductArray[0];
293 $ticketData['product_id'] = $selectedProduct;
294 }
295
296 $customerData = Arr::only($data, ['first_name', 'last_name', 'email']);
297
298 foreach (Arr::get($data, 'customer_other_fields') as $field) {
299 if ($field['item_value']) {
300 $customerData[$field['label']] = $field['item_value'];
301 }
302 }
303
304
305 $user = get_user_by('ID', get_current_user_id());
306
307 if(!$user) {
308 $user = get_user_by('email', $customerData['email']);
309 }
310
311 if($user) {
312 $customerData['email'] = $user->user_email;
313 $customerData['user_id'] = $user->ID;
314 }
315
316 if(empty($customerData['email'])) {
317 do_action('ff_log_data', [
318 'title' => $feed['settings']['name'],
319 'status' => 'failed',
320 'description' => __('Support ticket creation failed, because no valid customer email found', 'fluentform'),
321 'parent_source_id' => $form->id,
322 'source_id' => $entry->id,
323 'component' => $this->integrationKey,
324 'source_type' => 'submission_item'
325 ]);
326 return false;
327 }
328
329 $customerData['last_ip_address'] = $entry->ip;
330
331 $customer = Customer::maybeCreateCustomer($customerData);
332
333 $ticketData['customer_id'] = $customer->id;
334
335 $ticketData = apply_filters('fluent_support/create_ticket_data', $ticketData, $customer);
336 do_action('fluent_support/before_ticket_create', $ticketData, $customer);
337
338 $ticket = Ticket::create($ticketData);
339
340 if(defined('FLUENTSUPPORTPRO') && !empty($ticketData['custom_fields'])) {
341 $ticket->syncCustomFields($ticketData['custom_fields']);
342 $ticket->custom_fields = $ticket->customData();
343 }
344
345 do_action('fluent_support/ticket_created', $ticket, $customer);
346
347 do_action('ff_log_data', [
348 'title' => $feed['settings']['name'],
349 'status' => 'success',
350 'description' => __('Support ticket has been created at Fluent Support. Ticket ID: '.$ticket->id, 'fluentform'),
351 'parent_source_id' => $form->id,
352 'source_id' => $entry->id,
353 'component' => $this->integrationKey,
354 'source_type' => 'submission_item'
355 ]);
356
357 if($ticketData['attachments']) {
358 $attachments = explode(',', $ticketData['attachments']);
359
360 foreach ($attachments as $attachment){
361 $fileName = explode('/', $attachment);
362 $fileName = end($fileName);
363 Attachment::create(
364 [
365 'ticket_id' => $ticket->id,
366 'file_path' => esc_url($attachment),
367 'full_url' => esc_url($attachment),
368 'title' => $fileName,
369 'person_id' => $customer->id
370 ]
371 );
372 }
373 $ticket->load($attachments);
374 }
375
376 return true;
377 }
378
379 public function isConfigured()
380 {
381 return true;
382 }
383
384 public function isEnabled()
385 {
386 return true;
387 }
388
389 }
390