PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.0
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.0
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.4.0, at app/Services/Integrations/FluentForm/FeedIntegration.php

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