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

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

165 lines 5.3 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 private $_integrationID;
17 private $logID;
18
19 private $_logResponse;
20
21 public function __construct($auth_token, $integId, $logID, $entryID) {
22 $this->_integrationID = $integId;
23 $this->_logResponse = new UtilApiResponse();
24 $this->logID = $logID;
25 }
26
27 public function addContact($access_token, $listId, $finalData) {
28 $apiEndpoints = 'https://api.sendfox.com/contacts';
29 $listId = explode(',', $listId);
30 $header = [
31 'Authorization' => "Bearer {$access_token}",
32 'Accept' => 'application/json',
33 ];
34
35 $data = [
36 'email' => $finalData['email'],
37 'first_name' => $finalData['first_name'],
38 'last_name' => $finalData['last_name'],
39 'lists' => $listId,
40 ];
41
42 return HttpHelper::post($apiEndpoints, $data, $header);
43 }
44
45 public function createContactList($access_token, $finalData) {
46 $apiEndpoints = 'https://api.sendfox.com/lists';
47
48 $header = [
49 'Authorization' => "Bearer {$access_token}",
50 'Accept' => 'application/json',
51 ];
52
53 $data = [
54 'name' => $finalData['name'],
55 ];
56
57 return HttpHelper::post($apiEndpoints, $data, $header);
58 }
59
60 public function generateReqDataFromFieldMap($data, $fieldMap) {
61 $dataFinal = [];
62
63 foreach ($fieldMap as $key => $value) {
64 $triggerValue = $value->formField;
65 $actionValue = $value->sendFoxFormField;
66 if ('custom' === $triggerValue) {
67 $dataFinal[$actionValue] = $value->customValue;
68 } elseif (!is_null($data[$triggerValue])) {
69 $dataFinal[$actionValue] = $data[$triggerValue];
70 }
71 }
72 return $dataFinal;
73 }
74
75 public function generateListReqDataFromFieldMap($data, $fieldMap) {
76 $dataFinal = [];
77
78 foreach ($fieldMap as $key => $value) {
79 $triggerValue = $value->formField;
80 $actionValue = $value->sendFoxListFormField;
81 if ('custom' === $triggerValue) {
82 $dataFinal[$actionValue] = $value->customValue;
83 } elseif (!is_null($data[$triggerValue])) {
84 $dataFinal[$actionValue] = $data[$triggerValue];
85 }
86 }
87 return $dataFinal;
88 }
89
90 public function generateReqUnsubscribeDataFromFieldMap($data, $fieldMap) {
91 $dataFinal = [];
92
93 foreach ($fieldMap as $key => $value) {
94 $triggerValue = $value->formField;
95 $actionValue = $value->sendFoxUnsubscribeFormField;
96 if ('custom' === $triggerValue) {
97 $dataFinal[$actionValue] = $value->customValue;
98 } elseif (!is_null($data[$triggerValue])) {
99 $dataFinal[$actionValue] = $data[$triggerValue];
100 }
101 }
102 return $dataFinal;
103 }
104
105 public function unsubscribeContact($access_token, $finalData) {
106 $apiEndpoints = 'https://api.sendfox.com/unsubscribe';
107
108 $header = [
109 'Authorization' => "Bearer {$access_token}",
110 'Accept' => 'application/json',
111 ];
112
113 $data = [
114 'email' => $finalData['email'],
115 ];
116 return HttpHelper::request($apiEndpoints, 'PATCH', $data, $header);
117 }
118
119 public function execute(
120 $listId,
121 $mainAction,
122 $fieldValues,
123 $fieldMap,
124 $access_token,
125 $integrationDetails
126 ) {
127 $fieldData = [];
128 $apiResponse = null;
129 if ('1' === $integrationDetails->mainAction) {
130 $type_name = 'Create List';
131 $finalData = $this->generateListReqDataFromFieldMap($fieldValues, $integrationDetails->field_map_list);
132 $apiResponse = $this->createContactList($access_token, $finalData);
133
134 if (property_exists($apiResponse, 'id')) {
135 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse);
136 } else {
137 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse);
138 }
139 }
140 if ('2' === $integrationDetails->mainAction) {
141 $type_name = 'Create Contact';
142 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
143 $apiResponse = $this->addContact($access_token, $listId, $finalData);
144 if (property_exists($apiResponse, 'errors')) {
145 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse);
146 } else {
147 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse);
148 }
149 }
150
151 if ('3' === $integrationDetails->mainAction) {
152 $type_name = 'Unsubscribe';
153 $finalData = $this->generateReqUnsubscribeDataFromFieldMap($fieldValues, $integrationDetails->field_map_unsubscribe);
154 $apiResponse = $this->unsubscribeContact($access_token, $finalData);
155 if (property_exists($apiResponse, 'id')) {
156 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'success', $apiResponse);
157 } else {
158 $this->_logResponse->apiResponse($this->logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type_name], 'error', $apiResponse);
159 }
160 }
161
162 return $apiResponse;
163 }
164 }
165