Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentformpro/src/Integrations/Amocrm/Bootstrap.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace FluentFormPro\Integrations\Amocrm;
4 +
5 + use FluentForm\App\Http\Controllers\IntegrationManagerController;
6 + use FluentForm\Framework\Foundation\Application;
7 + use FluentForm\Framework\Helpers\ArrayHelper;
8 +
9 + class Bootstrap extends IntegrationManagerController
10 + {
11 + public function __construct(Application $app)
12 + {
13 + parent::__construct(
14 + $app,
15 + 'Amocrm',
16 + 'amocrm',
17 + '_fluentform_amocrm_settings',
18 + 'amocrm_feed',
19 + 36
20 + );
21 +
22 + // add_filter('fluentform/notifying_async_amocrm', '__return_false');
23 +
24 + $this->logo = fluentFormMix('img/integrations/amocrm.png');
25 +
26 + $this->description = 'It can be a great way to manage your leads and tasks with amoCRM and Fluent Forms.';
27 +
28 + $this->registerAdminHooks();
29 +
30 + add_action('admin_init', function () {
31 + if (isset($_REQUEST['ff_amocrm_auth'])) {
32 + $client = $this->getRemoteClient();
33 + if(isset($_REQUEST['code'])) {
34 + $code = sanitize_text_field($_REQUEST['code']);
35 + $referer = sanitize_text_field($_REQUEST['referer']);
36 + $settings = $this->getGlobalSettings([]);
37 + $settings = $client->generateAccessToken($code, $referer, $settings);
38 + if (!is_wp_error($settings)) {
39 + $settings['status'] = true;
40 + update_option($this->optionKey, $settings, 'no');
41 + }
42 + wp_redirect(admin_url('admin.php?page=fluent_forms_settings#general-amocrm-settings'));
43 + exit();
44 + }
45 + else {
46 + $client->redirectToAuthServer();
47 + }
48 + die();
49 + }
50 + });
51 +
52 + add_filter(
53 + 'fluentform/get_integration_values_amocrm',
54 + [$this, 'resolveIntegrationSettings'],
55 + 10,
56 + 3
57 + );
58 +
59 + add_filter(
60 + 'fluentform/save_integration_value_' . $this->integrationKey,
61 + [$this, 'validate'],
62 + 10,
63 + 3
64 + );
65 + }
66 +
67 + public function resolveIntegrationSettings($settings, $feed, $formId)
68 + {
69 + $serviceName = $this->app->request->get('serviceName', '');
70 + $serviceId = $this->app->request->get('serviceId', '');
71 +
72 + if ($serviceName) {
73 + $settings['name'] = $serviceName;
74 + }
75 +
76 + if ($serviceId) {
77 + $settings['list_id'] = $serviceId;
78 + }
79 + return $settings;
80 + }
81 +
82 + public function validate($settings, $integrationId, $formId)
83 + {
84 + $error = false;
85 + $errors = [];
86 +
87 + foreach ($this->getFields($settings['list_id']) as $field) {
88 + if ($field['required'] && empty($settings[$field['key']])) {
89 + $error = true;
90 + $errors[$field['key']] = [__($field['label'] . ' is required', 'fluentformpro')];
91 + }
92 + }
93 +
94 + if ($error) {
95 + wp_send_json_error([
96 + 'message' => __('Validation Failed', 'fluentformpro'),
97 + 'errors' => $errors
98 + ], 423);
99 + }
100 +
101 + return $settings;
102 + }
103 +
104 + public function getRemoteClient()
105 + {
106 + $settings = $this->getGlobalSettings([]);
107 +
108 + return new API($settings);
109 + }
110 +
111 + public function getGlobalSettings($settings)
112 + {
113 + $globalSettings = get_option($this->optionKey);
114 +
115 + if (!$globalSettings) {
116 + $globalSettings = [];
117 + }
118 +
119 + $defaults = [
120 + 'client_id' => '',
121 + 'client_secret' => '',
122 + 'status' => false,
123 + 'access_token' => '',
124 + 'refresh_token' => '',
125 + 'referer_url' => ''
126 + ];
127 +
128 + return wp_parse_args($globalSettings, $defaults);
129 + }
130 +
131 + public function getGlobalFields($fields)
132 + {
133 + return [
134 + 'logo' => $this->logo,
135 + 'menu_title' => __('Amocrm Settings', 'fluentformpro'),
136 + 'menu_description' => __($this->description, 'fluentformpro'),
137 + 'valid_message' => __('Your Amocrm Secret Key is valid', 'fluentformpro'),
138 + 'invalid_message' => __('Your Amocrm Secret Key is not valid', 'fluentformpro'),
139 + 'save_button_text' => __('Save Settings', 'fluentformpro'),
140 + 'config_instruction' => $this->getConfigInstructions(),
141 + 'fields' => [
142 + 'client_id' => [
143 + 'type' => 'text',
144 + 'placeholder' => __('Amocrm Integration ID', 'fluentformpro'),
145 + 'label_tips' => __('Enter your Amocrm Integration ID', 'fluentformpro'),
146 + 'label' => __('Amocrm Integration ID', 'fluentformpro'),
147 + ],
148 + 'client_secret' => [
149 + 'type' => 'password',
150 + 'placeholder' => __('Amocrm Integration Secret Key', 'fluentformpro'),
151 + 'label_tips' => __('Enter your Amocrm Integration Secret Key', 'fluentformpro'),
152 + 'label' => __('Amocrm Secret Key', 'fluentformpro'),
153 + ],
154 + ],
155 + 'hide_on_valid' => true,
156 + 'discard_settings' => [
157 + 'section_description' => __('Your Amocrm API integration is up and running', 'fluentformpro'),
158 + 'button_text' => __('Disconnect Amocrm', 'fluentformpro'),
159 + 'data' => [
160 + 'client_id' => '',
161 + 'client_secret' => '',
162 + 'access_token' => '',
163 + ],
164 + 'show_verify' => true
165 + ]
166 + ];
167 + }
168 +
169 + protected function getConfigInstructions()
170 + {
171 + $authLink = admin_url('?ff_amocrm_auth=true');
172 + ob_start(); ?>
173 + <div>
174 + <ol>
175 + <li>Open Settings -> Integrations -> Then from upper right side click on the button Create Integration.
176 + <li>Set the redirect URL as <b><?php echo $authLink; ?></b> then check the Allow access: All.</br>Set your Integration Name and give a short description and save the settings.
177 + </li>
178 + <li>Under private integrations find your integration. Click on the integration and go to Keys and scopes. Here you will find Secret key and Integration ID.</li>
179 + </ol>
180 + </div>
181 + <?php
182 + return ob_get_clean();
183 + }
184 +
185 + public function saveGlobalSettings($settings)
186 + {
187 + if (empty($settings['client_id']) || empty($settings['client_secret'])) {
188 + $integrationSettings = [
189 + 'client_id' => '',
190 + 'client_secret' => '',
191 + 'access_token' => '',
192 + 'status' => false
193 + ];
194 + // Update the details with siteKey & secretKey.
195 + update_option($this->optionKey, $integrationSettings, 'no');
196 +
197 + wp_send_json_success([
198 + 'message' => __('Your settings has been updated', 'fluentformpro'),
199 + 'status' => false
200 + ], 200);
201 + }
202 +
203 + try {
204 + $oldSettings = $this->getGlobalSettings([]);
205 + $oldSettings['client_id'] = sanitize_text_field($settings['client_id']);
206 + $oldSettings['client_secret'] = sanitize_text_field($settings['client_secret']);
207 + $oldSettings['status'] = false;
208 +
209 + update_option($this->optionKey, $oldSettings, 'no');
210 +
211 + wp_send_json_success([
212 + 'message' => __('You are redirect to authenticate', 'fluentformpro'),
213 + 'redirect_url' => admin_url('?ff_amocrm_auth=true')
214 + ], 200);
215 + }
216 + catch (\Exception $exception) {
217 + wp_send_json_error([
218 + 'message' => $exception->getMessage()
219 + ], 400);
220 + }
221 + }
222 +
223 + public function pushIntegration($integrations, $formId)
224 + {
225 + $integrations[$this->integrationKey] = [
226 + 'title' => $this->title . ' Integration',
227 + 'logo' => $this->logo,
228 + 'is_active' => $this->isConfigured(),
229 + 'configure_title' => __('Configuration required!', 'fluentformpro'),
230 + 'global_configure_url' => admin_url('admin.php?page=fluent_forms_settings#general-amocrm-settings'),
231 + 'configure_message' => __('Amocrm is not configured yet! Please configure your Amocrm api first', 'fluentformpro'),
232 + 'configure_button_text' => __('Set Amocrm API', 'fluentformpro')
233 + ];
234 + return $integrations;
235 + }
236 +
237 + public function getIntegrationDefaults($settings, $formId)
238 + {
239 + $listId = $this->app->request->get('serviceId');
240 +
241 + return [
242 + 'name' => '',
243 + 'list_id' => $listId,
244 + 'fields' => [
245 + [
246 + 'item_value' => '',
247 + 'label' => ''
248 + ]
249 + ],
250 + 'conditionals' => [
251 + 'conditions' => [],
252 + 'status' => false,
253 + 'type' => 'all'
254 + ],
255 + 'enabled' => true
256 + ];
257 + }
258 +
259 + public function getSettingsFields($settings, $formId)
260 + {
261 + $fieldSettings = [
262 + 'fields' => [
263 + [
264 + 'key' => 'name',
265 + 'label' => __('Feed Name', 'fluentformpro'),
266 + 'required' => true,
267 + 'placeholder' => __('Your Feed Name', 'fluentformpro'),
268 + 'component' => 'text'
269 + ],
270 + [
271 + 'key' => 'list_id',
272 + 'label' => __('Amocrm Services', 'fluentformpro'),
273 + 'placeholder' => __('Select Amocrm Service', 'fluentformpro'),
274 + 'required' => true,
275 + 'component' => 'refresh',
276 + 'options' => $this->getLists()
277 + ],
278 + ],
279 + 'button_require_list' => false,
280 + 'integration_title' => $this->title
281 + ];
282 +
283 + $listId = $this->app->request->get('serviceId', ArrayHelper::get($settings, 'list_id'));
284 +
285 + if ($listId) {
286 + $fields = $this->getFields($listId);
287 +
288 + if (empty($fields)) {
289 + wp_send_json_error([
290 + 'message' => __("The selected service doesn't have any field settings.", 'fluentformpro'),
291 + ], 423);
292 + }
293 +
294 + $fields = array_merge($fieldSettings['fields'], $fields);
295 + $fieldSettings['fields'] = $fields;
296 + }
297 +
298 + $fieldSettings['fields'] = array_merge($fieldSettings['fields'], [
299 + [
300 + 'require_list' => false,
301 + 'key' => 'conditionals',
302 + 'label' => __('Conditional Logics', 'fluentformpro'),
303 + 'tips' => __('Allow this integration conditionally based on your submission values', 'fluentformpro'),
304 + 'component' => 'conditional_block'
305 + ],
306 + [
307 + 'require_list' => false,
308 + 'key' => 'enabled',
309 + 'label' => __('Status', 'fluentformpro'),
310 + 'component' => 'checkbox-single',
311 + 'checkbox_label' => __('Enable this feed', 'fluentformpro')
312 + ]
313 + ]);
314 +
315 + return $fieldSettings;
316 + }
317 +
318 + protected function getLists()
319 + {
320 + $lists = [
321 + 'leads' => 'Lead',
322 + 'companies' => 'Company',
323 + 'contacts' => 'Contact',
324 + 'catalogs' => 'List',
325 + 'tasks' => 'Task'
326 + ];
327 +
328 + $client = $this->getRemoteClient();
329 + $settings = $this->getGlobalSettings([]);
330 +
331 + $url = 'https://' . $settings['referer_url'] . '/api/v4/catalogs';
332 +
333 + try {
334 + $elements = $client->makeRequest($url, null);
335 +
336 + if (!$elements) {
337 + return $lists;
338 + }
339 + }
340 + catch (\Exception $exception) {
341 + return false;
342 + }
343 +
344 + if (is_wp_error($elements)) {
345 + $error = $elements->get_error_message();
346 + $code = $elements->get_error_code();
347 + wp_send_json_error([
348 + 'message' => __($error, 'fluentformpro')
349 + ], $code);
350 + }
351 +
352 + foreach ($elements['_embedded']['catalogs'] as $catalog) {
353 + $lists['elements_' . $catalog['id']] = $catalog['name'];
354 + }
355 +
356 + return $lists;
357 + }
358 +
359 + public function getMergeFields($list, $listId, $formId)
360 + {
361 + return false;
362 + }
363 +
364 + public function getFields($listId)
365 + {
366 + $settings = $this->getGlobalSettings([]);
367 + $mergedFields = [];
368 +
369 + switch ($listId) {
370 + case 'leads':
371 + $mergedFields =
372 + [
373 + [
374 + 'key' => 'leads_name',
375 + 'placeholder' => __('Enter Lead Name', 'fluentformpro'),
376 + 'label' => __('Lead Name', 'fluentformpro'),
377 + 'required' => true,
378 + 'tips' => __('Lead name is a required string type field.', 'fluentformpro'),
379 + 'component' => 'value_text'
380 + ],
381 + [
382 + 'key' => 'price',
383 + 'placeholder' => __('Lead Sale', 'fluentformpro'),
384 + 'label' => __('Lead Sale', 'fluentformpro'),
385 + 'required' => false,
386 + 'tips' => __('Lead Sale is a int type field.', 'fluentformpro'),
387 + 'component' => 'value_text'
388 + ],
389 + [
390 + 'key' => 'unix_created_at',
391 + 'placeholder' => __('Created at', 'fluentformpro'),
392 + 'label' => __('Created at', 'fluentformpro'),
393 + 'required' => false,
394 + 'tips' => __('Created at is a Date type field in Unix Timestamp format.', 'fluentformpro'),
395 + 'component' => 'datetime'
396 + ],
397 + [
398 + 'key' => 'unix_closed_at',
399 + 'placeholder' => __('Closed at', 'fluentformpro'),
400 + 'label' => __('Closed at', 'fluentformpro'),
401 + 'required' => false,
402 + 'tips' => __('Closed at is a Date type field in Unix Timestamp format.', 'fluentformpro'),
403 + 'component' => 'datetime'
404 + ],
405 + ];
406 +
407 + $url = 'https://' . $settings['referer_url'] . '/api/v4/leads/custom_fields';
408 + $customFields = $this->getCustomFields($url);
409 + $mergedFields = array_merge($mergedFields, $customFields);
410 +
411 + break;
412 + case 'companies':
413 + $mergedFields =
414 + [
415 + [
416 + 'key' => 'companies_name',
417 + 'placeholder' => __('Enter Company Name', 'fluentformpro'),
418 + 'label' => __('Company Name', 'fluentformpro'),
419 + 'required' => true,
420 + 'tips' => __('Company name is a required string type field.', 'fluentformpro'),
421 + 'component' => 'value_text'
422 + ],
423 + [
424 + 'key' => 'unix_created_at',
425 + 'placeholder' => __('Created at', 'fluentformpro'),
426 + 'label' => __('Created at', 'fluentformpro'),
427 + 'required' => false,
428 + 'tips' => __('Created at is a Date type field in Unix Timestamp format.', 'fluentformpro'),
429 + 'component' => 'datetime'
430 + ]
431 + ];
432 +
433 + $url = 'https://' . $settings['referer_url'] . '/api/v4/companies/custom_fields';
434 + $customFields = $this->getCustomFields($url);
435 + $mergedFields = array_merge($mergedFields, $customFields);
436 +
437 + break;
438 + case 'contacts':
439 + $mergedFields =
440 + [
441 + [
442 + 'key' => 'contacts_name',
443 + 'placeholder' => __('Enter Contacts Name', 'fluentformpro'),
444 + 'label' => __('Contacts Name', 'fluentformpro'),
445 + 'required' => true,
446 + 'tips' => __('Contacts name is a required string type field.', 'fluentformpro'),
447 + 'component' => 'value_text'
448 + ],
449 + [
450 + 'key' => 'first_name',
451 + 'placeholder' => __('Enter First Name', 'fluentformpro'),
452 + 'label' => __('First Name', 'fluentformpro'),
453 + 'required' => false,
454 + 'tips' => __('First name is a string type field.', 'fluentformpro'),
455 + 'component' => 'value_text'
456 + ],
457 + [
458 + 'key' => 'last_name',
459 + 'placeholder' => __('Enter Last Name', 'fluentformpro'),
460 + 'label' => __('Last Name', 'fluentformpro'),
461 + 'required' => false,
462 + 'tips' => __('Last name is a string type field.', 'fluentformpro'),
463 + 'component' => 'value_text'
464 + ],
465 + [
466 + 'key' => 'unix_created_at',
467 + 'placeholder' => __('Created at', 'fluentformpro'),
468 + 'label' => __('Created at', 'fluentformpro'),
469 + 'required' => false,
470 + 'tips' => __('Created at is a Date type field in Unix Timestamp format.', 'fluentformpro'),
471 + 'component' => 'datetime'
472 + ]
473 + ];
474 +
475 + $url = 'https://' . $settings['referer_url'] . '/api/v4/contacts/custom_fields';
476 + $customFields = $this->getCustomFields($url);
477 + $mergedFields = array_merge($mergedFields, $customFields);
478 +
479 + break;
480 + case 'catalogs':
481 + $mergedFields =
482 + [
483 + [
484 + 'key' => 'catalogs_name',
485 + 'placeholder' => __('Enter List Name', 'fluentformpro'),
486 + 'label' => __('List Name', 'fluentformpro'),
487 + 'required' => true,
488 + 'tips' => __('List name is a required string type field.', 'fluentformpro'),
489 + 'component' => 'value_text'
490 + ],
491 + [
492 + 'key' => 'can_add_elements',
493 + 'label' => __('Add elements to the list', 'fluentformpro'),
494 + 'required' => false,
495 + 'tips' => __('Add elements to the list is a bool type field.', 'fluentformpro'),
496 + 'component' => 'radio_choice',
497 + 'options' => [
498 + 'true' => __('yes', 'fluentformpro'),
499 + 'false' => __('no', 'fluentformpro')
500 + ]
501 + ],
502 + [
503 + 'key' => 'can_link_multiple',
504 + 'label' => __('Link elements to multiple list', 'fluentformpro'),
505 + 'required' => false,
506 + 'tips' => __('Link elements can to the multiple list is a bool type field.', 'fluentformpro'),
507 + 'component' => 'radio_choice',
508 + 'options' => [
509 + 'true' => __('yes', 'fluentformpro'),
510 + 'false' => __('no', 'fluentformpro')
511 + ]
512 + ]
513 + ];
514 + break;
515 + case 'tasks':
516 + $mergedFields =
517 + [
518 + [
519 + 'key' => 'tasks_name',
520 + 'placeholder' => __('Enter Task Name', 'fluentformpro'),
521 + 'label' => __('Task Name', 'fluentformpro'),
522 + 'required' => true,
523 + 'tips' => __('Task name is a required string type field.', 'fluentformpro'),
524 + 'component' => 'value_text'
525 + ],
526 + [
527 + 'key' => 'tasks_entity_type',
528 + 'placeholder' => __('Enter Task Entity Type', 'fluentformpro'),
529 + 'label' => __('Task Entity Type', 'fluentformpro'),
530 + 'required' => false,
531 + 'tips' => __('Task Entity Type is a select type field.', 'fluentformpro'),
532 + 'component' => 'select',
533 + 'options' => [
534 + 'leads' => 'Lead',
535 + 'contacts' => 'Contact',
536 + 'companies' => 'Company',
537 + 'customers' => 'Customer'
538 + ]
539 + ],
540 + [
541 + 'key' => 'text',
542 + 'placeholder' => __('Enter Task Details', 'fluentformpro'),
543 + 'label' => __('Task Details', 'fluentformpro'),
544 + 'required' => true,
545 + 'tips' => __('Task Details is a required string type field.', 'fluentformpro'),
546 + 'component' => 'value_text'
547 + ],
548 + [
549 + 'key' => 'tasks_duration',
550 + 'placeholder' => __('Enter Task Duration', 'fluentformpro'),
551 + 'label' => __('Task Duration', 'fluentformpro'),
552 + 'required' => false,
553 + 'tips' => __('Task Duration is a int type field.', 'fluentformpro'),
554 + 'component' => 'value_text'
555 + ],
556 + [
557 + 'key' => 'tasks_result[text]',
558 + 'placeholder' => __('Enter Task Result Details', 'fluentformpro'),
559 + 'label' => __('Task Result Details', 'fluentformpro'),
560 + 'required' => false,
561 + 'tips' => __('Task Details is a string type field.', 'fluentformpro'),
562 + 'component' => 'value_text'
563 + ],
564 + [
565 + 'key' => 'is_completed',
566 + 'label' => __('Is Task Completed', 'fluentformpro'),
567 + 'required' => false,
568 + 'tips' => __('Is Task Completed is a bool type field.', 'fluentformpro'),
569 + 'component' => 'radio_choice',
570 + 'options' => [
571 + 'true' => __('yes', 'fluentformpro'),
572 + 'false' => __('no', 'fluentformpro')
573 + ]
574 + ],
575 + [
576 + 'key' => 'unix_complete_till',
577 + 'placeholder' => __('Complete At', 'fluentformpro'),
578 + 'label' => __('Complete at', 'fluentformpro'),
579 + 'required' => true,
580 + 'tips' => __('Complete at is a Required Date type field in Unix Timestamp format.', 'fluentformpro'),
581 + 'component' => 'datetime'
582 + ]
583 + ];
584 + break;
585 + case 'customers':
586 + $mergedFields =
587 + [
588 + [
589 + 'key' => 'customers_name',
590 + 'placeholder' => __('Enter Customer Name', 'fluentformpro'),
591 + 'label' => __('Customer Name', 'fluentformpro'),
592 + 'required' => true,
593 + 'tips' => __('Customer name is a required string type field.', 'fluentformpro'),
594 + 'component' => 'value_text'
595 + ],
596 + [
597 + 'key' => 'next_price',
598 + 'placeholder' => __('Enter Expected purchase value', 'fluentformpro'),
599 + 'label' => __('Enter Expected purchase value', 'fluentformpro'),
600 + 'required' => false,
601 + 'tips' => __('Expected purchase value is a required number type field.', 'fluentformpro'),
602 + 'component' => 'value_text'
603 + ],
604 + ];
605 +
606 + $url = 'https://' . $settings['referer_url'] . '/api/v4/customers/custom_fields';
607 + $customFields = $this->getCustomFields($url);
608 + $mergedFields = array_merge($mergedFields, $customFields);
609 +
610 + break;
611 + default:
612 + $mergedFields =
613 + [
614 + [
615 + 'key' => 'elements_name',
616 + 'placeholder' => __('Enter List element name' , 'fluentformpro'),
617 + 'label' => __('List element name', 'fluentformpro'),
618 + 'required' => true,
619 + 'tips' => __('List element name is a required string type field.', 'fluentformpro'),
620 + 'component' => 'value_text',
621 + ]
622 + ];
623 +
624 + $listNumber = '';
625 + if (strpos($listId, 'elements') !== false) {
626 + $listNumber = explode('_', $listId)[1];
627 + }
628 +
629 + $url = 'https://' . $settings['referer_url'] . '/api/v4/catalogs/' . $listNumber . '/custom_fields';
630 + $customFields = $this->getCustomFields($url);
631 + $mergedFields = array_merge($mergedFields, $customFields);
632 + }
633 +
634 + return $mergedFields;
635 + }
636 +
637 + protected function getCustomFields($url)
638 + {
639 + $client = $this->getRemoteClient();
640 +
641 + try {
642 + $lists = $client->makeRequest($url, null);
643 + if (!$lists) {
644 + return [];
645 + }
646 + }
647 + catch (\Exception $exception) {
648 + return false;
649 + }
650 +
651 + if (is_wp_error($lists)) {
652 + $error = $lists->get_error_message();
653 + $code = $lists->get_error_code();
654 + wp_send_json_error([
655 + 'message' => __($error, 'fluentformpro')
656 + ], $code);
657 + }
658 +
659 + $customFields = [];
660 +
661 + foreach ($lists['_embedded']['custom_fields'] as $field) {
662 + if($enums = ArrayHelper::get($field, 'enums')) {
663 + $data = [
664 + 'key' => 'custom*' . $field['id'] . '*select*' . $field['name'],
665 + 'placeholder' => __($field['name'] . ' Type', 'fluentformpro'),
666 + 'label' => __($field['name'] . ' Type', 'fluentformpro'),
667 + 'required' => false,
668 + 'tips' => __($field['name'] . ' is a ' .$field['type']. ' type of field.', 'fluentformpro'),
669 + 'component' => 'select'
670 + ];
671 + $options = [];
672 + foreach ($enums as $option) {
673 + $options[$option['value']] = $option['value'];
674 + }
675 + $data['options'] = $options;
676 +
677 + array_push($customFields, $data);
678 +
679 + if($field['code'] == 'PHONE' || $field['code'] == 'EMAIL') {
680 + $data = [
681 + 'key' => 'custom*' . $field['id'] . '*text*' . $field['code'],
682 + 'placeholder' => __($field['name'], 'fluentformpro'),
683 + 'label' => __($field['name'], 'fluentformpro'),
684 + 'required' => false,
685 + 'tips' => __($field['name'] . ' is a string type of field.', 'fluentformpro'),
686 + 'component' => 'value_text'
687 + ];
688 + array_push($customFields, $data);
689 + }
690 + }
691 + elseif ($field['type'] == 'checkbox') {
692 + $data = [
693 + 'key' => 'custom*' . $field['id'] . '*checkbox*' . $field['name'],
694 + 'label' => __('Is Discussable', 'fluentformpro'),
695 + 'required' => false,
696 + 'tips' => __('Is discussable is a bool type field.', 'fluentformpro'),
697 + 'component' => 'radio_choice',
698 + 'options' => [
699 + 'true' => __('yes', 'fluentformpro'),
700 + 'false' => __('no', 'fluentformpro')
701 + ]
702 + ];
703 + array_push($customFields, $data);
704 + }
705 + else {
706 + $data = [
707 + 'key' => 'custom*' . $field['id'] . '*normal*' . $field['name'],
708 + 'placeholder' => __($field['name'], 'fluentformpro'),
709 + 'label' => __($field['name'], 'fluentformpro'),
710 + 'required' => false,
711 + 'tips' => __($field['name'] . ' is a ' .$field['type']. ' type of field.', 'fluentformpro'),
712 + 'component' => 'value_text'
713 + ];
714 + array_push($customFields, $data);
715 + }
716 + }
717 +
718 + return $customFields;
719 + }
720 +
721 + protected function getAllFields($listId)
722 + {
723 + $fields = $this->getFields($listId);
724 +
725 + $allFields = [];
726 +
727 + foreach ($fields as $field) {
728 + $keyData = [];
729 + $keyData['key'] = $field['key'];
730 + if ($field['required']) {
731 + $keyData['required'] = $field['required'];
732 + array_push($allFields, $keyData);
733 + } else {
734 + $keyData['required'] = 0;
735 + array_push($allFields, $keyData);
736 + }
737 + }
738 +
739 + return $allFields;
740 + }
741 +
742 + public function notify($feed, $formData, $entry, $form)
743 + {
744 + $feedData = $feed['processedValues'];
745 + $subscriber = [];
746 +
747 + $subscriber['list_id'] = $feedData['list_id'];
748 +
749 + if (strpos($feedData['list_id'], 'elements') !== false) {
750 + $listArray = explode('_', $feedData['list_id']);
751 + $subscriber['type'] = $listArray[0];
752 + $subscriber['list_id'] = $listArray[1];
753 + }
754 +
755 + if ($subscriber['list_id'] == 'tags') {
756 + $subscriber['entity_id'] = $feedData['entity_id'];
757 + unset($feedData['entity_id']);
758 + }
759 +
760 + $allFields = $this->getAllFields($feedData['list_id']);
761 +
762 + $subscriber['attributes']['custom_fields_values'] = [];
763 + $enumCode = '';
764 +
765 + foreach ($allFields as $field) {
766 + $key = $field['key'];
767 +
768 + if (!empty($feedData[$key])) {
769 + if (strpos($key, 'name') !== false) {
770 + $subscriber['attributes']['name'] = ArrayHelper::get($feedData, $key);
771 + }
772 + elseif(strpos($key, 'can_') !== false) {
773 + if ($feedData[$key] == 'true') {
774 + $subscriber['attributes'][$key] = true;
775 + }
776 + else {
777 + $subscriber['attributes'][$key] = false;
778 + }
779 + }
780 + elseif(strpos($key, 'is_') !== false) {
781 + if ($feedData[$key] == 'true') {
782 + $subscriber['attributes'][$key] = true;
783 + }
784 + else {
785 + $subscriber['attributes'][$key] = false;
786 + }
787 + }
788 + elseif(strpos($key, 'unix_') !== false) {
789 + $dateField = explode('unix_', $key);
790 + $dateFieldKey = $dateField[1];
791 + $subscriber['attributes'][$dateFieldKey] = strtotime(ArrayHelper::get($feedData, $key));
792 + }
793 + elseif (strpos($key, 'custom*') !== false) {
794 + $customField = explode('*', $key);
795 + $fieldId = $customField[1];
796 + $fieldType = $customField[2];
797 + $fieldName = $customField[3];
798 +
799 + if($fieldType == 'select') {
800 + if($fieldName == 'Unit') {
801 + $customFields = [
802 + 'field_id' => $fieldId,
803 + 'values' => [
804 + [
805 + 'value' => ArrayHelper::get($feedData, $key)
806 + ]
807 + ]
808 + ];
809 + }
810 + else {
811 + $enumCode = ArrayHelper::get($feedData, $key);
812 + continue;
813 + }
814 + }
815 +
816 + if($fieldType == 'text') {
817 + $customFields = [
818 + 'field_code' => $fieldName,
819 + 'values' => [
820 + [
821 + 'enum_code' => $enumCode,
822 + 'value' => ArrayHelper::get($feedData, $key)
823 + ]
824 + ]
825 + ];
826 + }
827 +
828 + if($fieldType == 'normal') {
829 + $customFields = [
830 + 'field_id' => $fieldId,
831 + 'values' => [
832 + [
833 + 'value' => ArrayHelper::get($feedData, $key)
834 + ]
835 + ]
836 + ];
837 + }
838 +
839 + if($fieldType == 'checkbox') {
840 + if ($feedData[$key] == 'true') {
841 + $checkboxValue = true;
842 + }
843 + else {
844 + $checkboxValue = false;
845 + }
846 +
847 + $customFields = [
848 + 'field_id' => $fieldId,
849 + 'values' => [
850 + [
851 + 'value' => $checkboxValue
852 + ]
853 + ]
854 + ];
855 + }
856 +
857 + array_push($subscriber['attributes']['custom_fields_values'], $customFields);
858 + }
859 +
860 + else {
861 + $subscriber['attributes'][$key] = ArrayHelper::get($feedData, $key);
862 + }
863 + }
864 + }
865 + if (empty($subscriber['attributes']['custom_fields_values'])) {
866 + unset($subscriber['attributes']['custom_fields_values']);
867 + }
868 +
869 + $client = $this->getRemoteClient();
870 + $response = $client->subscribe($subscriber);
871 +
872 + if (!is_wp_error($response)) {
873 + do_action('fluentform/integration_action_result', $feed, 'success', __('Amocrm feed has been successfully initiated and pushed data', 'fluentformpro'));
874 + } else {
875 + $error = $response->get_error_message();
876 + do_action('fluentform/integration_action_result', $feed, 'failed', $error);
877 + }
878 + }
879 + }
880 +