| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoAnalytics; |
| 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 $_integrationID; |
| 24 |
private $_logID; |
| 25 |
private $_logResponse; |
| 26 |
|
| 27 |
public function __construct($tokenDetails, $integId, $logID) |
| 28 |
{ |
| 29 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 30 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 31 |
$this->_tokenDetails = $tokenDetails; |
| 32 |
$this->_integrationID = $integId; |
| 33 |
$this->_logID = $logID; |
| 34 |
$this->_logResponse = new UtilApiResponse(); |
| 35 |
} |
| 36 |
|
| 37 |
public function insertRecord($workspace, $table, $ownerEmail, $dataCenter, $data) |
| 38 |
{ |
| 39 |
$insertRecordEndpoint = "https://analyticsapi.zoho.{$dataCenter}/api/{$ownerEmail}/{$workspace}/{$table}?ZOHO_ACTION=ADDROW&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0"; |
| 40 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 41 |
} |
| 42 |
|
| 43 |
public function updateRecord($workspace, $table, $ownerEmail, $dataCenter, $criteria, $data) |
| 44 |
{ |
| 45 |
$updateRecordEndpoint = "https://analyticsapi.zoho.{$dataCenter}/api/{$ownerEmail}/{$workspace}/{$table}?ZOHO_ACTION=UPDATE&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&ZOHO_CRITERIA={$criteria}"; |
| 46 |
|
| 47 |
return HttpHelper::post($updateRecordEndpoint, $data, $this->_defaultHeader); |
| 48 |
} |
| 49 |
|
| 50 |
public function shareTable($dataCenter, $ownerEmail, $workspace, $table, $data) |
| 51 |
{ |
| 52 |
$shareTableEndpoint = "https://analyticsapi.zoho.{$dataCenter}/api/{$ownerEmail}/{$workspace}/?ZOHO_VIEWS={$table}&ZOHO_ACTION=SHARE&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_VERSION=1.0&"; |
| 53 |
|
| 54 |
$shareTableEndpoint .= build_query($data); |
| 55 |
|
| 56 |
return HttpHelper::post($shareTableEndpoint, null, $this->_defaultHeader); |
| 57 |
} |
| 58 |
|
| 59 |
public function executeRecordApi($workspace, $table, $ownerEmail, $dataCenter, $actions, $defaultConf, $fieldValues, $fieldMap) |
| 60 |
{ |
| 61 |
$fieldData = []; |
| 62 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 63 |
if (!empty($fieldPair->zohoFormField) && !empty($fieldPair->formField)) { |
| 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 |
if (isset($actions->update->criteria)) { |
| 73 |
$recordApiResponse = $this->updateRecord($workspace, $table, $ownerEmail, $dataCenter, $actions->update->criteria, $fieldData); |
| 74 |
|
| 75 |
$recordApiResponse = json_decode(preg_replace("/\\\'/", "'", $recordApiResponse)); |
| 76 |
|
| 77 |
if (isset($recordApiResponse->response->error)) { |
| 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->response->result->updatedRows) { |
| 84 |
$recordApiResponse = $this->insertRecord($workspace, $table, $ownerEmail, $dataCenter, $fieldData); |
| 85 |
|
| 86 |
if (isset($recordApiResponse->response->error)) { |
| 87 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 88 |
} else { |
| 89 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 90 |
} |
| 91 |
} |
| 92 |
} else { |
| 93 |
$recordApiResponse = $this->insertRecord($workspace, $table, $ownerEmail, $dataCenter, $fieldData); |
| 94 |
if (isset($recordApiResponse->response->error)) { |
| 95 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', $recordApiResponse); |
| 96 |
} else { |
| 97 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'success', $recordApiResponse); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (isset($actions->share)) { |
| 102 |
$share_arr = []; |
| 103 |
|
| 104 |
if (!empty($actions->share->email)) { |
| 105 |
$share_arr['ZOHO_EMAILS'] = FieldValueHandler::replaceFieldWithValue($actions->share->email, $fieldValues); |
| 106 |
} |
| 107 |
|
| 108 |
if (isset($actions->share->permissions)) { |
| 109 |
$permissions = $actions->share->permissions; |
| 110 |
|
| 111 |
foreach ($permissions as $permission) { |
| 112 |
foreach ($permission as $perm) { |
| 113 |
$share_arr[$perm] = 'true'; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (!empty($share_arr['ZOHO_EMAILS'])) { |
| 119 |
$recordApiResponse = $this->shareTable($dataCenter, $ownerEmail, $workspace, $table, $share_arr); |
| 120 |
if (isset($recordApiResponse->response->error)) { |
| 121 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'share', 'type_name' => $table], 'error', $recordApiResponse); |
| 122 |
} else { |
| 123 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $table], 'success', $recordApiResponse); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $recordApiResponse; |
| 129 |
} |
| 130 |
} |
| 131 |
|