| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoCampaigns; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Record insert,upsert |
| 16 |
*/ |
| 17 |
class RecordApiHelper |
| 18 |
{ |
| 19 |
private $_defaultHeader; |
| 20 |
private $_apiDomain; |
| 21 |
private $_tokenDetails; |
| 22 |
|
| 23 |
private $_integrationID; |
| 24 |
|
| 25 |
private $_logID; |
| 26 |
|
| 27 |
private $_logResponse; |
| 28 |
|
| 29 |
public function __construct($tokenDetails, $integId, $logID) |
| 30 |
{ |
| 31 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 32 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 33 |
$this->_tokenDetails = $tokenDetails; |
| 34 |
$this->_integrationID = $integId; |
| 35 |
$this->_logID = $logID; |
| 36 |
$this->_logResponse = new UtilApiResponse(); |
| 37 |
} |
| 38 |
|
| 39 |
public function insertRecord($list, $dataCenter, $data) |
| 40 |
{ |
| 41 |
$insertRecordEndpoint = "https://campaigns.zoho.{$dataCenter}/api/v1.1/json/listsubscribe?resfmt=JSON&listkey={$list}&contactinfo=" . urlencode($data); |
| 42 |
|
| 43 |
return HttpHelper::post($insertRecordEndpoint, null, $this->_defaultHeader); |
| 44 |
} |
| 45 |
|
| 46 |
public function executeRecordApi($list, $dataCenter, $fieldValues, $fieldMap, $required) |
| 47 |
{ |
| 48 |
$fieldData = []; |
| 49 |
foreach ($fieldMap as $fieldPair) { |
| 50 |
if (!empty($fieldPair->zohoFormField) && !empty($fieldPair->formField)) { |
| 51 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 52 |
$fieldData[$fieldPair->zohoFormField] = $fieldPair->customValue; |
| 53 |
} else { |
| 54 |
$fieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField]; |
| 55 |
} |
| 56 |
} |
| 57 |
if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) { |
| 58 |
$error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for zoho campaigns', 'bit-form'), $fieldPair->zohoFormField)); |
| 59 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'field'], 'validation', $error); |
| 60 |
return $error; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$recordApiResponse = $this->insertRecord($list, $dataCenter, wp_json_encode($fieldData)); |
| 65 |
if (isset($recordApiResponse->status) && 'error' === $recordApiResponse->status) { |
| 66 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'list'], 'error', $recordApiResponse); |
| 67 |
} else { |
| 68 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'list'], 'success', $recordApiResponse); |
| 69 |
} |
| 70 |
return $recordApiResponse; |
| 71 |
} |
| 72 |
} |
| 73 |
|