| 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 |
public function __construct($api_key, $integId, $logID, $entryID) |
| 26 |
{ |
| 27 |
// wp_send_json_success($tokenDetails); |
| 28 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 29 |
$this->_defaultHeader['api-key'] = $api_key; |
| 30 |
$this->_integrationID = $integId; |
| 31 |
$this->_logID = $logID; |
| 32 |
$this->_logResponse = new UtilApiResponse(); |
| 33 |
$this->_entryID = $entryID; |
| 34 |
} |
| 35 |
|
| 36 |
public function insertRecord($data) |
| 37 |
{ |
| 38 |
$insertRecordEndpoint = 'https://api.sendinblue.com/v3/contacts'; |
| 39 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 40 |
} |
| 41 |
|
| 42 |
public function updateRecord($id, $data) |
| 43 |
{ |
| 44 |
$updateRecordEndpoint = "https://api.sendinblue.com/v3/contacts/{$id}"; |
| 45 |
return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader); |
| 46 |
} |
| 47 |
|
| 48 |
public function executeRecordApi($lists, $defaultDataConf, $fieldValues, $fieldMap, $actions) |
| 49 |
{ |
| 50 |
$fieldData = []; |
| 51 |
$attributes = []; |
| 52 |
foreach ($fieldMap as $fieldPair) { |
| 53 |
if (!empty($fieldPair->sendinBlueField)) { |
| 54 |
if ('email' === $fieldPair->sendinBlueField) { |
| 55 |
$fieldData['email'] = $fieldValues[$fieldPair->formField]; |
| 56 |
} elseif ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 57 |
$attributes[$fieldPair->sendinBlueField] = $fieldPair->customValue; |
| 58 |
} else { |
| 59 |
$attributes[$fieldPair->sendinBlueField] = $fieldValues[$fieldPair->formField]; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
$arrLists = array_map(function ($val) { |
| 64 |
return (int) $val; |
| 65 |
}, $lists); |
| 66 |
|
| 67 |
$fieldData['attributes'] = (object) $attributes; |
| 68 |
$fieldData['listIds'] = $arrLists; |
| 69 |
$model = new FormEntryLogModel(); |
| 70 |
|
| 71 |
$recordApiResponse = null; |
| 72 |
$type = null; |
| 73 |
if ($this->_entryID) { |
| 74 |
$result = $model->entryLogCheck($this->_entryID, $this->_integrationID); |
| 75 |
if (!count($result) || isset($result->errors['result_empty'])) { |
| 76 |
$recordApiResponse = $this->insertRecord(wp_json_encode($fieldData)); |
| 77 |
$type = 'insert'; |
| 78 |
|
| 79 |
if (!empty($actions->update) && !empty($recordApiResponse->message) && 'Contact already exist' === $recordApiResponse->message) { |
| 80 |
$contactEmail = $fieldData['email']; |
| 81 |
$recordApiResponse = $this->updateRecord($contactEmail, wp_json_encode($fieldData)); |
| 82 |
if (empty($recordApiResponse)) { |
| 83 |
$recordApiResponse = ['success' => true, 'id' => $fieldData['email']]; |
| 84 |
} |
| 85 |
$type = 'update'; |
| 86 |
} |
| 87 |
} else { |
| 88 |
$contactId = json_decode($result[0]->response_obj); |
| 89 |
$recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($fieldData)); |
| 90 |
if (empty($recordApiResponse)) { |
| 91 |
$recordApiResponse = ['success' => true, 'id' => $contactId->id]; |
| 92 |
} |
| 93 |
$type = 'update'; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ($recordApiResponse && isset($recordApiResponse->code)) { |
| 98 |
$this->_logResponse->apiResponse( |
| 99 |
$this->_logID, |
| 100 |
$this->_integrationID, |
| 101 |
['type' => 'record', 'type_name' => $type], |
| 102 |
'error', |
| 103 |
$recordApiResponse |
| 104 |
); |
| 105 |
} else { |
| 106 |
$this->_logResponse->apiResponse( |
| 107 |
$this->_logID, |
| 108 |
$this->_integrationID, |
| 109 |
['type' => 'record', 'type_name' => $type], |
| 110 |
'success', |
| 111 |
$recordApiResponse |
| 112 |
); |
| 113 |
} |
| 114 |
return $recordApiResponse; |
| 115 |
} |
| 116 |
} |
| 117 |
|