| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Acumbamail; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Provide functionality for Record insert, upsert |
| 10 |
*/ |
| 11 |
class RecordApiHelper |
| 12 |
{ |
| 13 |
private $_integrationID; |
| 14 |
private $_logID; |
| 15 |
|
| 16 |
private $_logResponse; |
| 17 |
|
| 18 |
private $_entryID; |
| 19 |
|
| 20 |
public function __construct($auth_token, $integId, $logID, $entryID) |
| 21 |
{ |
| 22 |
$this->_integrationID = $integId; |
| 23 |
$this->_logResponse = new ApiResponse(); |
| 24 |
$this->_logID = $logID; |
| 25 |
$this->_entryID = $entryID; |
| 26 |
} |
| 27 |
|
| 28 |
public function addSubscriber($auth_token, $listId, $finalData) |
| 29 |
{ |
| 30 |
$apiEndpoints = 'https://acumbamail.com/api/1/addSubscriber/'; |
| 31 |
$header = [ |
| 32 |
'Content-Type' => 'application/x-www-form-urlencoded' |
| 33 |
]; |
| 34 |
|
| 35 |
$requestParams = [ |
| 36 |
'auth_token' => $auth_token, |
| 37 |
'list_id' => $listId, |
| 38 |
'welcome_email' => 1, |
| 39 |
'update_subscriber' => 1, |
| 40 |
'merge_fields[EMAIL]' => $finalData['email'], |
| 41 |
|
| 42 |
]; |
| 43 |
foreach ($finalData as $key => $value) { |
| 44 |
if ('email' !== $key) { |
| 45 |
$requestParams['merge_fields[' . $key . ']'] = $value; |
| 46 |
} |
| 47 |
} |
| 48 |
return HttpHelper::post($apiEndpoints, $requestParams, $header); |
| 49 |
} |
| 50 |
|
| 51 |
public function deleteSubscriber($auth_token, $listId, $finalData) |
| 52 |
{ |
| 53 |
$apiEndpoints = 'https://acumbamail.com/api/1/deleteSubscriber/'; |
| 54 |
|
| 55 |
$header = [ |
| 56 |
'Content-Type' => 'application/x-www-form-urlencoded' |
| 57 |
]; |
| 58 |
|
| 59 |
$requestParams = [ |
| 60 |
'auth_token' => $auth_token, |
| 61 |
'list_id' => $listId, |
| 62 |
'email' => $finalData['email'], |
| 63 |
]; |
| 64 |
|
| 65 |
return HttpHelper::post($apiEndpoints, $requestParams, $header); |
| 66 |
} |
| 67 |
|
| 68 |
public function generateReqDataFromFieldMap($data, $fieldMap) |
| 69 |
{ |
| 70 |
$dataFinal = []; |
| 71 |
|
| 72 |
foreach ($fieldMap as $value) { |
| 73 |
$triggerValue = $value->formField; |
| 74 |
$actionValue = $value->acumbamailFormField; |
| 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 execute( |
| 85 |
$listId, |
| 86 |
$mainAction, |
| 87 |
$defaultDataConf, |
| 88 |
$fieldValues, |
| 89 |
$fieldMap, |
| 90 |
$auth_token, |
| 91 |
$formId |
| 92 |
) { |
| 93 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 94 |
$apiResponse = null; |
| 95 |
$type = null; |
| 96 |
if ('1' === $mainAction) { |
| 97 |
$apiResponse = $this->addSubscriber($auth_token, $listId, $finalData); |
| 98 |
$type = 'add subscriber'; |
| 99 |
} elseif ('2' === $mainAction) { |
| 100 |
$apiResponse = $this->deleteSubscriber($auth_token, $listId, $finalData); |
| 101 |
$type = 'delete subscriber'; |
| 102 |
} |
| 103 |
|
| 104 |
$entryDetails = [ |
| 105 |
'formId' => $formId, |
| 106 |
'entryId' => $this->_entryID, |
| 107 |
'fieldValues' => $fieldValues |
| 108 |
]; |
| 109 |
|
| 110 |
if (property_exists($apiResponse, 'errors')) { |
| 111 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'errors', $apiResponse, $entryDetails); |
| 112 |
} else { |
| 113 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $apiResponse, $entryDetails); |
| 114 |
} |
| 115 |
return $apiResponse; |
| 116 |
} |
| 117 |
} |
| 118 |
|