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
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 / SendFox / 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/SendFox/RecordApiHelper.php

185 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * SendFox Record Api
5 */
6
7 namespace BitCode\BitForm\Core\Integration\SendFox;
8
9 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
10 use BitCode\BitForm\Core\Util\HttpHelper;
11
12 /**
13 * Provide functionality for Record insert, upsert
14 */
15 class RecordApiHelper
16 {
17 private $_integrationID;
18 private $logID;
19
20 private $_logResponse;
21
22 private $_entryID;
23
24 public function __construct($auth_token, $integId, $logID, $entryID)
25 {
26 $this->_integrationID = $integId;
27 $this->_logResponse = new UtilApiResponse();
28 $this->logID = $logID;
29
30 $this->_entryID = $entryID;
31 }
32
33 public function addContact($access_token, $listId, $finalData)
34 {
35 $apiEndpoints = 'https://api.sendfox.com/contacts';
36 $listId = explode(',', $listId);
37 $header = [
38 'Authorization' => "Bearer {$access_token}",
39 'Accept' => 'application/json',
40 ];
41
42 $data = [
43 'email' => $finalData['email'],
44 'first_name' => $finalData['first_name'],
45 'last_name' => $finalData['last_name'],
46 'lists' => $listId,
47 ];
48
49 return HttpHelper::post($apiEndpoints, $data, $header);
50 }
51
52 public function createContactList($access_token, $finalData)
53 {
54 $apiEndpoints = 'https://api.sendfox.com/lists';
55
56 $header = [
57 'Authorization' => "Bearer {$access_token}",
58 'Accept' => 'application/json',
59 ];
60
61 $data = [
62 'name' => $finalData['name'],
63 ];
64
65 return HttpHelper::post($apiEndpoints, $data, $header);
66 }
67
68 public function generateReqDataFromFieldMap($data, $fieldMap)
69 {
70 $dataFinal = [];
71
72 foreach ($fieldMap as $key => $value) {
73 $triggerValue = $value->formField;
74 $actionValue = $value->sendFoxFormField;
75 if ('custom' === $triggerValue) {
76 $dataFinal[$actionValue] = $value->customValue;
77 } elseif (!is_null($data[$triggerValue])) {
78 $dataFinal[$actionValue] = $data[$triggerValue];
79 }
80 }
81 return $dataFinal;
82 }
83
84 public function generateListReqDataFromFieldMap($data, $fieldMap)
85 {
86 $dataFinal = [];
87
88 foreach ($fieldMap as $key => $value) {
89 $triggerValue = $value->formField;
90 $actionValue = $value->sendFoxListFormField;
91 if ('custom' === $triggerValue) {
92 $dataFinal[$actionValue] = $value->customValue;
93 } elseif (!is_null($data[$triggerValue])) {
94 $dataFinal[$actionValue] = $data[$triggerValue];
95 }
96 }
97 return $dataFinal;
98 }
99
100 public function generateReqUnsubscribeDataFromFieldMap($data, $fieldMap)
101 {
102 $dataFinal = [];
103
104 foreach ($fieldMap as $key => $value) {
105 $triggerValue = $value->formField;
106 $actionValue = $value->sendFoxUnsubscribeFormField;
107 if ('custom' === $triggerValue) {
108 $dataFinal[$actionValue] = $value->customValue;
109 } elseif (!is_null($data[$triggerValue])) {
110 $dataFinal[$actionValue] = $data[$triggerValue];
111 }
112 }
113 return $dataFinal;
114 }
115
116 public function unsubscribeContact($access_token, $finalData)
117 {
118 $apiEndpoints = 'https://api.sendfox.com/unsubscribe';
119
120 $header = [
121 'Authorization' => "Bearer {$access_token}",
122 'Accept' => 'application/json',
123 ];
124
125 $data = [
126 'email' => $finalData['email'],
127 ];
128 return HttpHelper::request($apiEndpoints, 'PATCH', $data, $header);
129 }
130
131 public function execute(
132 $listId,
133 $mainAction,
134 $fieldValues,
135 $fieldMap,
136 $access_token,
137 $integrationDetails,
138 $formId
139 ) {
140 $fieldData = [];
141 $apiResponse = null;
142
143 $entryDetails = [
144 'formId' => $formId,
145 'entryId' => $this->_entryID,
146 'fieldValues' => $fieldValues
147 ];
148
149 if ('1' === $integrationDetails->mainAction) {
150 $type_name = 'Create List';
151 $finalData = $this->generateListReqDataFromFieldMap($fieldValues, $integrationDetails->field_map_list);
152 $apiResponse = $this->createContactList($access_token, $finalData);
153
154 if (property_exists($apiResponse, 'id')) {
155 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse, $entryDetails);
156 } else {
157 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse, $entryDetails);
158 }
159 }
160 if ('2' === $integrationDetails->mainAction) {
161 $type_name = 'Create Contact';
162 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
163 $apiResponse = $this->addContact($access_token, $listId, $finalData);
164 if (property_exists($apiResponse, 'errors')) {
165 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse, $entryDetails);
166 } else {
167 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse, $entryDetails);
168 }
169 }
170
171 if ('3' === $integrationDetails->mainAction) {
172 $type_name = 'Unsubscribe';
173 $finalData = $this->generateReqUnsubscribeDataFromFieldMap($fieldValues, $integrationDetails->field_map_unsubscribe);
174 $apiResponse = $this->unsubscribeContact($access_token, $finalData);
175 if (property_exists($apiResponse, 'id')) {
176 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse, $entryDetails);
177 } else {
178 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse, $entryDetails);
179 }
180 }
181
182 return $apiResponse;
183 }
184 }
185