| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoMail; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 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 $_tokenDetails; |
| 21 |
|
| 22 |
private $_logID; |
| 23 |
|
| 24 |
private $_integrationID; |
| 25 |
|
| 26 |
private $_logResponse; |
| 27 |
|
| 28 |
public function __construct($tokenDetails, $integId, $logID) |
| 29 |
{ |
| 30 |
$this->_tokenDetails = $tokenDetails; |
| 31 |
$this->_integrationID = $integId; |
| 32 |
$this->_logID = $logID; |
| 33 |
$this->_logResponse = new UtilApiResponse(); |
| 34 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 35 |
$this->_defaultHeader['accountId'] = $tokenDetails->accountId; |
| 36 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 37 |
} |
| 38 |
|
| 39 |
public function insertRecord($dataCenter, $data) |
| 40 |
{ |
| 41 |
$insertRecordEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$this->_defaultHeader['accountId']}/messages"; |
| 42 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 43 |
} |
| 44 |
|
| 45 |
public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID) |
| 46 |
{ |
| 47 |
$mailInfo = [ |
| 48 |
'fromAddress' => $integrationDetails->tokenDetails->accountEmail, |
| 49 |
'toAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->to, $fieldValues)), |
| 50 |
'ccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->cc, $fieldValues)), |
| 51 |
'bccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->bcc, $fieldValues)), |
| 52 |
'subject' => FieldValueHandler::replaceFieldWithValue($integrationDetails->subject, $fieldValues), |
| 53 |
'content' => FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues) |
| 54 |
]; |
| 55 |
|
| 56 |
if ('draft' === $integrationDetails->mailType) { |
| 57 |
$mailInfo['mode'] = 'draft'; |
| 58 |
} |
| 59 |
|
| 60 |
if (!empty($integrationDetails->actions->attachments)) { |
| 61 |
$mailAttachments = []; |
| 62 |
$fileFound = 0; |
| 63 |
$responseType = 'success'; |
| 64 |
$attachmentApiResponses = []; |
| 65 |
$filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID); |
| 66 |
$attachments = explode(',', $integrationDetails->actions->attachments); |
| 67 |
foreach ($attachments as $fileField) { |
| 68 |
if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) { |
| 69 |
$fileFound = 1; |
| 70 |
if (is_array($fieldValues[$fileField])) { |
| 71 |
foreach ($fieldValues[$fileField] as $singleFile) { |
| 72 |
$attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter); |
| 73 |
if (isset($attachmentApiResponse->data)) { |
| 74 |
array_push($mailAttachments, $attachmentApiResponse->data); |
| 75 |
} |
| 76 |
if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) { |
| 77 |
$responseType = 'error'; |
| 78 |
} |
| 79 |
$attachmentApiResponses[] = $attachmentApiResponse; |
| 80 |
} |
| 81 |
} else { |
| 82 |
$attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter); |
| 83 |
if (isset($attachmentApiResponse->data)) { |
| 84 |
array_push($mailAttachments, $attachmentApiResponse->data); |
| 85 |
} |
| 86 |
if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) { |
| 87 |
$responseType = 'error'; |
| 88 |
} |
| 89 |
|
| 90 |
$attachmentApiResponses[] = $attachmentApiResponse; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$mailInfo['attachments'] = $mailAttachments; |
| 96 |
if ($fileFound) { |
| 97 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => 'mail'], $responseType, $attachmentApiResponses); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$recordApiResponse = $this->insertRecord($integrationDetails->dataCenter, wp_json_encode($mailInfo)); |
| 102 |
if (200 === $recordApiResponse->status->code) { |
| 103 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'success', $recordApiResponse); |
| 104 |
} else { |
| 105 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'error', $recordApiResponse); |
| 106 |
} |
| 107 |
|
| 108 |
return $recordApiResponse; |
| 109 |
} |
| 110 |
} |
| 111 |
|