| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\SendinBlue; |
| 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,upsert |
| 16 |
*/ |
| 17 |
class RecordApiHelper |
| 18 |
{ |
| 19 |
private $_defaultHeader; |
| 20 |
private $_integrationID; |
| 21 |
private $_logID; |
| 22 |
private $_logResponse; |
| 23 |
private $_entryID; |
| 24 |
|
| 25 |
private const BREVO_API_ENDPOINT = 'https://api.brevo.com/v3'; |
| 26 |
|
| 27 |
public function __construct($api_key, $integId, $logID, $entryID) |
| 28 |
{ |
| 29 |
// wp_send_json_success($tokenDetails); |
| 30 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 31 |
$this->_defaultHeader['api-key'] = $api_key; |
| 32 |
$this->_integrationID = $integId; |
| 33 |
$this->_logID = $logID; |
| 34 |
$this->_logResponse = new UtilApiResponse(); |
| 35 |
$this->_entryID = $entryID; |
| 36 |
} |
| 37 |
|
| 38 |
public function insertRecord($data) |
| 39 |
{ |
| 40 |
$insertRecordEndpoint = self::BREVO_API_ENDPOINT . '/contacts'; |
| 41 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 42 |
} |
| 43 |
|
| 44 |
public function updateRecord($id, $data) |
| 45 |
{ |
| 46 |
$updateRecordEndpoint = self::BREVO_API_ENDPOINT . "/contacts/{$id}"; |
| 47 |
return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader); |
| 48 |
} |
| 49 |
|
| 50 |
public function executeRecordApi($lists, $defaultDataConf, $fieldValues, $fieldMap, $actions, $formId) |
| 51 |
{ |
| 52 |
$fieldData = []; |
| 53 |
$attributes = []; |
| 54 |
foreach ($fieldMap as $fieldPair) { |
| 55 |
if (!empty($fieldPair->sendinBlueField)) { |
| 56 |
if ('email' === $fieldPair->sendinBlueField) { |
| 57 |
$fieldData['email'] = $fieldValues[$fieldPair->formField]; |
| 58 |
} elseif ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 59 |
$attributes[$fieldPair->sendinBlueField] = $fieldPair->customValue; |
| 60 |
} else { |
| 61 |
$attributes[$fieldPair->sendinBlueField] = $fieldValues[$fieldPair->formField]; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
$arrLists = array_map(function ($val) { |
| 66 |
return (int) $val; |
| 67 |
}, $lists); |
| 68 |
|
| 69 |
$fieldData['attributes'] = (object) $attributes; |
| 70 |
$fieldData['listIds'] = $arrLists; |
| 71 |
$model = new FormEntryLogModel(); |
| 72 |
|
| 73 |
$recordApiResponse = null; |
| 74 |
$type = null; |
| 75 |
if ($this->_entryID) { |
| 76 |
$result = $model->entryLogCheck($this->_entryID, $this->_integrationID); |
| 77 |
if (!count($result) || isset($result->errors['result_empty'])) { |
| 78 |
$recordApiResponse = $this->insertRecord(wp_json_encode($fieldData)); |
| 79 |
$type = 'insert'; |
| 80 |
|
| 81 |
if (!empty($actions->update) && !empty($recordApiResponse->message) && 'email' === $recordApiResponse->metadata->duplicate_identifiers[0]) { |
| 82 |
$contactEmail = $fieldData['email']; |
| 83 |
$recordApiResponse = $this->updateRecord($contactEmail, wp_json_encode($fieldData)); |
| 84 |
if (empty($recordApiResponse)) { |
| 85 |
$recordApiResponse = ['success' => true, 'id' => $fieldData['email']]; |
| 86 |
} |
| 87 |
$type = 'update'; |
| 88 |
} |
| 89 |
} else { |
| 90 |
$contactId = json_decode($result[0]->response_obj); |
| 91 |
$recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($fieldData)); |
| 92 |
if (empty($recordApiResponse)) { |
| 93 |
$recordApiResponse = ['success' => true, 'id' => $contactId->id]; |
| 94 |
} |
| 95 |
$type = 'update'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$entryDetails = [ |
| 100 |
'formId' => $formId, |
| 101 |
'entryId' => $this->_entryID, |
| 102 |
'fieldValues' => $fieldValues |
| 103 |
]; |
| 104 |
|
| 105 |
if ($recordApiResponse && isset($recordApiResponse->code)) { |
| 106 |
$this->_logResponse->apiResponse( |
| 107 |
$this->_logID, |
| 108 |
$this->_integrationID, |
| 109 |
['type' => 'record', 'type_name' => $type], |
| 110 |
'error', |
| 111 |
$recordApiResponse, |
| 112 |
$entryDetails |
| 113 |
); |
| 114 |
} else { |
| 115 |
$this->_logResponse->apiResponse( |
| 116 |
$this->_logID, |
| 117 |
$this->_integrationID, |
| 118 |
['type' => 'record', 'type_name' => $type], |
| 119 |
'success', |
| 120 |
$recordApiResponse, |
| 121 |
$entryDetails |
| 122 |
); |
| 123 |
} |
| 124 |
return $recordApiResponse; |
| 125 |
} |
| 126 |
} |
| 127 |
|