PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.0
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Autonami / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.0, at includes/Core/Integration/Autonami/RecordApiHelper.php

98 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Autonami Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Autonami;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BWF_Contacts;
12 use BWFCRM_Contact;
13
14 /**
15 * Provide functionality for Record insert
16 */
17 class RecordApiHelper
18 {
19 private $_integrationID;
20 private $_logID;
21 private $_logResponse;
22
23 private $_entryID;
24
25 public function __construct($integId, $logID, $entryID)
26 {
27 $this->_integrationID = $integId;
28 $this->_logID = $logID;
29 $this->_logResponse = new UtilApiResponse();
30 $this->_entryID = $entryID;
31 }
32
33 public function insertRecord($data, $actions)
34 {
35 $contact_obj = BWF_Contacts::get_instance();
36 $contact = $contact_obj->get_contact_by('email', $data['email']);
37 $userExist = (absint($contact->get_id()) > 0);
38
39 if ($userExist && isset($actions->skip_if_exists) && $actions->skip_if_exists) {
40 $response = ['success' => false, 'messages' => 'Contact already exists!'];
41 } else {
42 foreach ($data as $key => $item) {
43 $obj = 'set_' . $key;
44 $contact->$obj($item);
45 }
46 $contact->set_status(1);
47 $contact->save();
48
49 $customContact = new BWFCRM_Contact($data['email']);
50 foreach ($data as $key => $item) {
51 if ('address' === $key) {
52 $key = 'address-1';
53 }
54 $customContact->set_field_by_slug($key, $item);
55 }
56 $customContact->save_fields();
57
58 if (absint($contact->get_id()) > 0) {
59 $response = ['success' => true, 'messages' => 'Insert successfully!'];
60 } else {
61 $response = ['success' => false, 'messages' => 'Something wrong!'];
62 }
63 }
64 return $response;
65 }
66
67 public function executeRecordApi($fieldValues, $fieldMap, $actions, $lists, $tags, $formId)
68 {
69 $fieldData = [];
70 foreach ($fieldMap as $fieldPair) {
71 if (!empty($fieldPair->autonamiField)) {
72 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
73 $fieldData[$fieldPair->autonamiField] = $fieldPair->customValue;
74 } else {
75 $fieldData[$fieldPair->autonamiField] = is_array($fieldValues[$fieldPair->formField]) ? wp_json_encode($fieldValues[$fieldPair->formField]) : $fieldValues[$fieldPair->formField];
76 }
77 }
78 }
79 $fieldData['lists'] = $lists;
80 $fieldData['tags'] = $tags;
81
82 $recordApiResponse = $this->insertRecord($fieldData, $actions);
83
84 $entryDetails = [
85 'formId' => $formId,
86 'entryId' => $this->_entryID,
87 'fieldValues' => $fieldValues
88 ];
89
90 if ($recordApiResponse['success']) {
91 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse, $entryDetails);
92 } else {
93 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse, $entryDetails);
94 }
95 return $recordApiResponse;
96 }
97 }
98