PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / ActiveCampaign / RecordApiHelper.php

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

177 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Active Campaign Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ActiveCampaign;
9
10 use BitCode\BitForm\Core\Database\FormEntryLogModel;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for Record insert,update, exist
16 */
17 class RecordApiHelper
18 {
19 private $_defaultHeader;
20 private $_integrationID;
21 private $_logID;
22 private $_logResponse;
23 private $_entryID;
24 private $_apiEndpoint;
25
26 public function __construct($api_key, $api_url, $integId, $logID, $entryID)
27 {
28 // wp_send_json_success($tokenDetails);
29 $this->_defaultHeader['Api-Token'] = $api_key;
30 $this->_apiEndpoint = $api_url . '/api/3';
31 $this->_integrationID = $integId;
32 $this->_logID = $logID;
33 $this->_logResponse = new UtilApiResponse();
34 $this->_entryID = $entryID;
35 }
36
37 // for insert data
38 public function storeOrModifyRecord($method, $data)
39 {
40 $insertRecordEndpoint = "{$this->_apiEndpoint}/{$method}";
41 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
42 }
43
44 public function updateRecord($id, $data)
45 {
46 $updateRecordEndpoint = "{$this->_apiEndpoint}/contacts/{$id}";
47 return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader);
48 }
49
50 private function existContact($email)
51 {
52 $searchEndPoint = "{$this->_apiEndpoint}/contacts?email={$email}";
53 return HttpHelper::get($searchEndPoint, null, $this->_defaultHeader);
54 }
55
56 public function executeRecordApi($fieldValues, $fieldMap, $actions, $listId, $tags)
57 {
58 $fieldData = [];
59 $customFields = [];
60
61 foreach ($fieldMap as $fieldKey => $fieldPair) {
62 if (!empty($fieldPair->activeCampaignField)) {
63 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue) && !is_numeric($fieldPair->activeCampaignField)) {
64 $fieldData[$fieldPair->activeCampaignField] = $fieldPair->customValue;
65 } elseif (is_numeric($fieldPair->activeCampaignField) && 'custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
66 array_push($customFields, ['field' => (int) $fieldPair->activeCampaignField, 'value' => $fieldPair->customValue]);
67 } elseif (is_numeric($fieldPair->activeCampaignField)) {
68 array_push($customFields, ['field' => (int) $fieldPair->activeCampaignField, 'value' => $fieldValues[$fieldPair->formField]]);
69 } else {
70 $fieldData[$fieldPair->activeCampaignField] = $fieldValues[$fieldPair->formField];
71 }
72 }
73 }
74
75 if (!empty($customFields)) {
76 $fieldData['fieldValues'] = $customFields;
77 }
78 $activeCampaign['contact'] = (object) $fieldData;
79
80 $model = new FormEntryLogModel();
81
82 $data = null;
83 $recordApiResponse = null;
84 $type = null;
85 if ($this->_entryID) {
86 $result = $model->entryLogCheck($this->_entryID, $this->_integrationID);
87 if (!count($result) || isset($result->errors['result_empty'])) {
88 $recordApiResponse = $this->storeOrModifyRecord('contacts', wp_json_encode($activeCampaign));
89 $type = 'insert';
90
91 if (isset($recordApiResponse->contact)) {
92 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
93 if (isset($listId) && !empty($listId)) {
94 $data['contactList'] = (object) [
95 'list' => $listId,
96 'contact' => $recordApiResponse['id'],
97 'status' => 1
98 ];
99 $this->storeOrModifyRecord('contactLists', wp_json_encode($data));
100 }
101 if (isset($tags) && !empty($tags)) {
102 foreach ($tags as $tag) {
103 $data['contactTag'] = (object) [
104 'contact' => $recordApiResponse['id'],
105 'tag' => $tag
106 ];
107 $this->storeOrModifyRecord('contactTags', wp_json_encode($data));
108 }
109 }
110 }
111
112 if (
113 !empty($actions->update)
114 && !empty($recordApiResponse->errors)
115 && 'duplicate' === $recordApiResponse->errors[0]->code
116 ) {
117 $existContact = $this->existContact($activeCampaign['contact']->email);
118 $recordApiResponse = $this->updateRecord($existContact->contacts[0]->id, wp_json_encode($activeCampaign));
119 if (isset($recordApiResponse->contact)) {
120 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
121
122 if (isset($listId) && !empty($listId)) {
123 $data['contactList'] = (object) [
124 'list' => $listId,
125 'contact' => $recordApiResponse['id'],
126 'status' => 1
127 ];
128 $this->storeOrModifyRecord('contactLists', wp_json_encode($data));
129 }
130 if (isset($tags) && !empty($tags)) {
131 foreach ($tags as $tag) {
132 $data['contactTag'] = (object) [
133 'contact' => $recordApiResponse['id'],
134 'tag' => $tag
135 ];
136 $this->storeOrModifyRecord('contactTags', wp_json_encode($data));
137 }
138 }
139 }
140 $type = 'update';
141 }
142 } else {
143 $contactId = json_decode($result[0]->response_obj);
144 $recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($activeCampaign));
145 if (isset($recordApiResponse->contact)) {
146 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
147
148 if (isset($listId) && !empty($listId)) {
149 $data['contactList'] = (object) [
150 'list' => $listId,
151 'contact' => $recordApiResponse['id'],
152 'status' => 1
153 ];
154 $this->storeOrModifyRecord('contactLists', wp_json_encode($data));
155 }
156 if (isset($tags) && !empty($tags)) {
157 foreach ($tags as $tag) {
158 $data['contactTag'] = (object) [
159 'contact' => $recordApiResponse['id'],
160 'tag' => $tag
161 ];
162 $this->storeOrModifyRecord('contactTags', wp_json_encode($data));
163 }
164 }
165 }
166 $type = 'update';
167 }
168 }
169
170 if ($recordApiResponse && isset($recordApiResponse->errors)) {
171 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse->errors);
172 } else {
173 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse);
174 }
175 return $recordApiResponse;
176 }
177 }