PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/Integration/ActiveCampaign/RecordApiHelper.php

136 lines 5.2 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 private $_defaultHeader;
19 private $_integrationID;
20 private $_logID;
21 private $_logResponse;
22 private $_entryID;
23 private $_apiEndpoint;
24
25 public function __construct($api_key, $api_url, $integId, $logID, $entryID) {
26 // wp_send_json_success($tokenDetails);
27 $this->_defaultHeader['Api-Token'] = $api_key;
28 $this->_apiEndpoint = $api_url . '/api/3';
29 $this->_integrationID = $integId;
30 $this->_logID = $logID;
31 $this->_logResponse = new UtilApiResponse();
32 $this->_entryID = $entryID;
33 }
34
35 // for insert data
36 public function storeOrModifyRecord($method, $data) {
37 $insertRecordEndpoint = "{$this->_apiEndpoint}/{$method}";
38 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
39 }
40
41 public function updateRecord($id, $data) {
42 $updateRecordEndpoint = "{$this->_apiEndpoint}/contacts/{$id}";
43 return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader);
44 }
45
46 private function existContact($email) {
47 $searchEndPoint = "{$this->_apiEndpoint}/contacts?email={$email}";
48 return HttpHelper::get($searchEndPoint, null, $this->_defaultHeader);
49 }
50
51 public function executeRecordApi($fieldValues, $fieldMap, $actions, $listId, $tags) {
52 $fieldData = [];
53 $customFields = [];
54
55 foreach ($fieldMap as $fieldKey => $fieldPair) {
56 if (!empty($fieldPair->activeCampaignField)) {
57 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue) && !is_numeric($fieldPair->activeCampaignField)) {
58 $fieldData[$fieldPair->activeCampaignField] = $fieldPair->customValue;
59 } elseif (is_numeric($fieldPair->activeCampaignField) && 'custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
60 array_push($customFields, ['field' => (int) $fieldPair->activeCampaignField, 'value' => $fieldPair->customValue]);
61 } elseif (is_numeric($fieldPair->activeCampaignField)) {
62 array_push($customFields, ['field' => (int) $fieldPair->activeCampaignField, 'value' => $fieldValues[$fieldPair->formField]]);
63 } else {
64 $fieldData[$fieldPair->activeCampaignField] = $fieldValues[$fieldPair->formField];
65 }
66 }
67 }
68
69 if (!empty($customFields)) {
70 $fieldData['fieldValues'] = $customFields;
71 }
72 $activeCampaign['contact'] = (object) $fieldData;
73
74 $model = new FormEntryLogModel();
75
76 $data = null;
77 $recordApiResponse = null;
78 $type = null;
79 if ($this->_entryID) {
80 $result = $model->entryLogCheck($this->_entryID, $this->_integrationID);
81 if (!count($result) || isset($result->errors['result_empty'])) {
82 $recordApiResponse = $this->storeOrModifyRecord('contacts', wp_json_encode($activeCampaign));
83 $type = 'insert';
84
85 if (isset($recordApiResponse->contact)) {
86 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
87 if (isset($listId) && !empty($listId)) {
88 $data['contactList'] = (object) [
89 'list' => $listId,
90 'contact' => $recordApiResponse['id'],
91 'status' => 1
92 ];
93 $this->storeOrModifyRecord('contactLists', wp_json_encode($data));
94 }
95 if (isset($tags) && !empty($tags)) {
96 foreach ($tags as $tag) {
97 $data['contactTag'] = (object) [
98 'contact' => $recordApiResponse['id'],
99 'tag' => $tag
100 ];
101 $this->storeOrModifyRecord('contactTags', wp_json_encode($data));
102 }
103 }
104 }
105
106 if (
107 !empty($actions->update)
108 && !empty($recordApiResponse->errors)
109 && 'Email address already exists in the system' === $recordApiResponse->errors[0]->title
110 ) {
111 $existContact = $this->existContact($activeCampaign['contact']->email);
112 $recordApiResponse = $this->updateRecord($existContact->contacts[0]->id, wp_json_encode($activeCampaign));
113 if (isset($recordApiResponse->contact)) {
114 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
115 }
116 $type = 'update';
117 }
118 } else {
119 $contactId = json_decode($result[0]->response_obj);
120 $recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($activeCampaign));
121 if (isset($recordApiResponse->contact)) {
122 $recordApiResponse = ['success' => true, 'id' => $recordApiResponse->contact->id];
123 }
124 $type = 'update';
125 }
126 }
127
128 if ($recordApiResponse && isset($recordApiResponse->errors)) {
129 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse->errors);
130 } else {
131 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse);
132 }
133 return $recordApiResponse;
134 }
135 }
136