| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoSheet; |
| 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 $_apiDomain; |
| 21 |
private $_tokenDetails; |
| 22 |
|
| 23 |
private $_logID; |
| 24 |
private $_integrationID; |
| 25 |
private $_logResponse; |
| 26 |
|
| 27 |
public function __construct($tokenDetails, $integId, $logID) |
| 28 |
{ |
| 29 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 30 |
$this->_defaultHeader['Content-Type'] = 'application/json'; |
| 31 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 32 |
$this->_tokenDetails = $tokenDetails; |
| 33 |
$this->_integrationID = $integId; |
| 34 |
$this->_logID = $logID; |
| 35 |
$this->_logResponse = new UtilApiResponse(); |
| 36 |
} |
| 37 |
|
| 38 |
public function insertRecord($workbook, $worksheet, $headerRow, $dataCenter, $data) |
| 39 |
{ |
| 40 |
$insertRecordEndpoint = "https://sheet.zoho.{$dataCenter}/api/v2/{$workbook}?method=worksheet.records.add&worksheet_name={$worksheet}&header_row={$headerRow}&json_data={$data}"; |
| 41 |
|
| 42 |
return HttpHelper::post($insertRecordEndpoint, null, $this->_defaultHeader); |
| 43 |
} |
| 44 |
|
| 45 |
public function updateRecord($workbook, $worksheet, $headerRow, $dataCenter, $criteria, $firstMatch, $data) |
| 46 |
{ |
| 47 |
$updateRecordEndpoint = "https://sheet.zoho.{$dataCenter}/api/v2/{$workbook}?method=worksheet.records.update&worksheet_name={$worksheet}&header_row={$headerRow}&first_match_only=" . json_encode($firstMatch) . "&data={$data}&criteria={$criteria}"; |
| 48 |
|
| 49 |
return HttpHelper::post($updateRecordEndpoint, $data, $this->_defaultHeader); |
| 50 |
} |
| 51 |
|
| 52 |
public function shareWorkbook($workbook, $dataCenter, $data) |
| 53 |
{ |
| 54 |
$insertRecordEndpoint = "https://sheet.zoho.{$dataCenter}/api/v2/share?method=workbook.share&resource_id={$workbook}&share_json={$data}"; |
| 55 |
|
| 56 |
return HttpHelper::post($insertRecordEndpoint, null, $this->_defaultHeader); |
| 57 |
} |
| 58 |
|
| 59 |
public function executeRecordApi($workbook, $worksheet, $headerRow, $dataCenter, $actions, $defaultConf, $fieldValues, $fieldMap) |
| 60 |
{ |
| 61 |
$fieldData = []; |
| 62 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 63 |
if (!empty($fieldPair->zohoFormField)) { |
| 64 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 65 |
$fieldData[$fieldPair->zohoFormField] = $fieldPair->customValue; |
| 66 |
} else { |
| 67 |
$fieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField]; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$fieldData = wp_json_encode($fieldData); |
| 73 |
$newFieldData = '[' . $fieldData . ']'; |
| 74 |
|
| 75 |
if (isset($actions->update->criteria)) { |
| 76 |
$recordApiResponse = $this->updateRecord($workbook, $worksheet, $headerRow, $dataCenter, $actions->update->criteria, $actions->update->firstMatch, $fieldData); |
| 77 |
if (isset($recordApiResponse->error_message) || 'failure' === $recordApiResponse->status || 'error' === $recordApiResponse->status) { |
| 78 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'update'], 'error', $recordApiResponse); |
| 79 |
} else { |
| 80 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'update'], 'success', $recordApiResponse); |
| 81 |
} |
| 82 |
|
| 83 |
if ($actions->update->insert && 0 === $recordApiResponse->no_of_affected_rows) { |
| 84 |
$recordApiResponse = $this->insertRecord($workbook, $worksheet, $headerRow, $dataCenter, $newFieldData); |
| 85 |
if (isset($recordApiResponse->error_message) || 'failure' === $recordApiResponse->status || 'error' === $recordApiResponse->status) { |
| 86 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 87 |
} else { |
| 88 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 89 |
} |
| 90 |
} |
| 91 |
} else { |
| 92 |
$recordApiResponse = $this->insertRecord($workbook, $worksheet, $headerRow, $dataCenter, $newFieldData); |
| 93 |
if (isset($recordApiResponse->error_message) || 'failure' === $recordApiResponse->status || 'error' === $recordApiResponse->status) { |
| 94 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 95 |
} else { |
| 96 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (isset($actions->share)) { |
| 101 |
$share_json = []; |
| 102 |
foreach ($actions->share as $user_share) { |
| 103 |
if (!empty($user_share->email)) { |
| 104 |
$emails = explode(',', $user_share->email); |
| 105 |
foreach ($emails as $email) { |
| 106 |
$email = FieldValueHandler::replaceFieldWithValue($email, $fieldValues); |
| 107 |
if (is_email($email)) { |
| 108 |
$share_json[] = [ |
| 109 |
'user_email' => $email, |
| 110 |
'access_level' => $user_share->access |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
$shareApiResponse = $this->shareWorkbook($workbook, $dataCenter, wp_json_encode($share_json)); |
| 117 |
if (isset($shareApiResponse->error_message) || 'failure' === $shareApiResponse->status || 'error' === $shareApiResponse->status) { |
| 118 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'share', 'type_name' => 'workbook'], 'error', $shareApiResponse); |
| 119 |
} else { |
| 120 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'share', 'type_name' => 'workbook'], 'success', $shareApiResponse); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return $recordApiResponse; |
| 125 |
} |
| 126 |
} |
| 127 |
|