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

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

78 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\ElasticEmail;
4
5 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
6 use BitCode\BitForm\Core\Util\HttpHelper;
7
8 class RecordApiHelper {
9 private $_defaultHeader;
10
11 private $_logID;
12
13 private $_logResponse;
14
15 private $integrationDetails;
16
17 public function __construct($api_key, $integId, $logID, $integrationDetails) {
18 $this->integrationDetails = $integrationDetails;
19 $this->_defaultHeader['Content-Type'] = 'application/json';
20 $this->_defaultHeader['Authorization'] = "Bearer $api_key";
21 $this->_logResponse = new UtilApiResponse();
22 $this->_logID = $logID;
23 }
24
25 public function createContact($data, $listName, $apiKey) {
26 $tmpData = \is_string($data) ? $data : \json_encode([(object) $data]);
27 $header = [
28 'X-ElasticEmail-ApiKey' => $apiKey,
29 'Content-Type' => 'application/json',
30 ];
31
32 $insertRecordEndpoint = "https://api.elasticemail.com/v4/contacts?$listName";
33 return HttpHelper::post($insertRecordEndpoint, $tmpData, $header);
34 }
35
36 public function generateReqDataFromFieldMap($data, $fieldMap) {
37 $dataFinal = [];
38
39 foreach ($fieldMap as $value) {
40 $triggerValue = $value->formField;
41 $actionValue = $value->elasticEmailField;
42 if ('custom' === $triggerValue) {
43 $dataFinal[$actionValue] = $value->customValue;
44 } elseif (!is_null($data[$triggerValue])) {
45 $dataFinal[$actionValue] = $data[$triggerValue];
46 }
47 }
48 if ($this->integrationDetails->actions) {
49 if (property_exists($this->integrationDetails->actions, 'status') && $this->integrationDetails->actions->status) {
50 $dataFinal['Status'] = $this->integrationDetails->status;
51 }
52 }
53 return $dataFinal;
54 }
55
56 public function execute($integId, $fieldValues, $fieldMap, $integrationDetails) {
57 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
58 $listName = $integrationDetails->list_id;
59 $query = '';
60
61 foreach ($listName as $key => $val) {
62 $query .= 'listnames=' . $val . '&';
63 };
64 if (strlen($query)) {
65 $query = substr($query, 0, -1);
66 }
67 $api_key = $integrationDetails->api_key;
68 $apiResponse = $this->createContact($finalData, $query, $api_key);
69
70 if (isset($apiResponse->Error)) {
71 $this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'errors', $apiResponse);
72 } else {
73 $this->_logResponse->apiResponse($this->_logID, $integId, ['type' => 'contact', 'type_name' => 'contact_add'], 'success', $apiResponse);
74 }
75 return $apiResponse;
76 }
77 }
78