| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoRecruit; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\DateTimeHelper; |
| 12 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 13 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* Provide functionality for Record insert,upsert |
| 18 |
*/ |
| 19 |
class RecordApiHelper |
| 20 |
{ |
| 21 |
private $_defaultHeader; |
| 22 |
private $_apiDomain; |
| 23 |
private $_tokenDetails; |
| 24 |
private $_dataCenter; |
| 25 |
private $_integrationID; |
| 26 |
private $_logID; |
| 27 |
private $_logResponse; |
| 28 |
|
| 29 |
public function __construct($dataCenter, $tokenDetails, $integId, $logID) |
| 30 |
{ |
| 31 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 32 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 33 |
$this->_dataCenter = $dataCenter; |
| 34 |
$this->_tokenDetails = $tokenDetails; |
| 35 |
$this->_integrationID = $integId; |
| 36 |
$this->_logID = $logID; |
| 37 |
$this->_logResponse = new UtilApiResponse(); |
| 38 |
} |
| 39 |
|
| 40 |
public function insertRecord($module, $data) |
| 41 |
{ |
| 42 |
$insertRecordEndpoint = "https://recruit.zoho.{$this->_dataCenter}/recruit/private/json/{$module}/addRecords"; |
| 43 |
return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 44 |
} |
| 45 |
|
| 46 |
public function executeRecordApi($formID, $entryID, $defaultConf, $module, $fieldValues, $fieldMap, $actions, $required, $fileMap) |
| 47 |
{ |
| 48 |
$fieldData = []; |
| 49 |
foreach ($fieldMap as $fieldKey => $fieldPair) { |
| 50 |
if (!empty($fieldPair->zohoFormField)) { |
| 51 |
if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) { |
| 52 |
$fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldPair->customValue, $defaultConf->moduleData->{$module}->fields->{$fieldPair->zohoFormField}); |
| 53 |
} else { |
| 54 |
$fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldValues[$fieldPair->formField], $defaultConf->moduleData->{$module}->fields->{$fieldPair->zohoFormField}); |
| 55 |
if ('Zip/Postal Code' === $fieldPair->zohoFormField) { |
| 56 |
$fieldData['Zip Code'] = $fieldData[$fieldPair->zohoFormField]; |
| 57 |
unset($fieldData[$fieldPair->zohoFormField]); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) { |
| 62 |
$error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for zoho recruit, %s module', 'bit-form'), $fieldPair->zohoFormField, $module)); |
| 63 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'field'], 'validation', $error); |
| 64 |
return $error; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
$xmlData = "<$module><row no='1'>"; |
| 70 |
|
| 71 |
foreach ($fieldData as $field => $value) { |
| 72 |
$xmlData .= "<FL val='$field'>$value</FL>"; |
| 73 |
} |
| 74 |
|
| 75 |
if (!empty($actions->recordOwner)) { |
| 76 |
$xmlData .= "<FL val='SMOWNERID'>$actions->recordOwner</FL>"; |
| 77 |
} |
| 78 |
$xmlData .= "</row></$module>"; |
| 79 |
$requestParams['scope'] = 'ZohoRecruit.modules.all'; |
| 80 |
$requestParams['version'] = 4; |
| 81 |
|
| 82 |
$requestParams['xmlData'] = $xmlData; |
| 83 |
if (!empty($actions->workflow)) { |
| 84 |
$requestParams['wfTrigger'] = 'true'; //api accept string true | false |
| 85 |
} |
| 86 |
if (!empty($actions->upsert)) { |
| 87 |
$requestParams['duplicateCheck'] = 2; |
| 88 |
} |
| 89 |
if (!empty($actions->approval)) { |
| 90 |
$requestParams['isApproval'] = 'true'; //api accept string true | false |
| 91 |
} |
| 92 |
|
| 93 |
$recordApiResponse = $this->insertRecord($module, $requestParams); |
| 94 |
if (isset($recordApiResponse->response->error)) { |
| 95 |
return $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $module], 'error', $recordApiResponse); |
| 96 |
} else { |
| 97 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $module], 'success', $recordApiResponse); |
| 98 |
} |
| 99 |
|
| 100 |
if (isset($recordApiResponse->response->error)) { |
| 101 |
return new WP_Error('INSERT_ERROR', $recordApiResponse->response->error->message); |
| 102 |
} |
| 103 |
if (!empty($recordApiResponse->response->result->row->success->details->FL[0]) && 'Id' === $recordApiResponse->response->result->row->success->details->FL[0]->val) { |
| 104 |
$recordID = $recordApiResponse->response->result->row->success->details->FL[0]->content; |
| 105 |
if (isset($actions->note) && !empty($actions->note->type)) { |
| 106 |
$noteDetails = $actions->note; |
| 107 |
$typeDetails = explode('__', $noteDetails->type); |
| 108 |
$content = FieldValueHandler::replaceFieldWithValue($noteDetails->content, $fieldValues); |
| 109 |
$xmlData = "<Notes><row no='1'>"; |
| 110 |
$xmlData .= "<FL val='entityId'>$recordID</FL>"; |
| 111 |
$xmlData .= "<FL val='Note Type'>$typeDetails[1]</FL>"; |
| 112 |
$xmlData .= "<FL val='Type Id'>$typeDetails[0]</FL>"; |
| 113 |
$xmlData .= "<FL val='Note Content'>$content</FL>"; |
| 114 |
$xmlData .= "<FL val='Parent Module'>$module</FL>"; |
| 115 |
$xmlData .= '</row></Notes>'; |
| 116 |
$requestParams['version'] = 2; |
| 117 |
$requestParams['xmlData'] = $xmlData; |
| 118 |
|
| 119 |
$noteApiResponse = $this->insertRecord('Notes', $requestParams); |
| 120 |
if (isset($noteApiResponse->response->error)) { |
| 121 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'note', 'type_name' => $module], 'error', $noteApiResponse); |
| 122 |
} else { |
| 123 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'note', 'type_name' => $module], 'success', $noteApiResponse); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
$filesApiHelper = new FilesApiHelper($module, $this->_dataCenter, $this->_tokenDetails, $formID, $entryID); |
| 128 |
if (count($fileMap)) { |
| 129 |
$fileFound = 0; |
| 130 |
$responseType = 'success'; |
| 131 |
$fileUpResponses = []; |
| 132 |
foreach ($fileMap as $fileKey => $filePair) { |
| 133 |
if (!empty($filePair->zohoFormField) && !empty($fieldValues[$filePair->formField])) { |
| 134 |
$fileFound = 1; |
| 135 |
if (@property_exists($defaultConf->moduleData->{$module}->fileUploadFields, $filePair->zohoFormField) && 'UploadText' === $defaultConf->moduleData->{$module}->fileUploadFields->{$filePair->zohoFormField}->data_type) { |
| 136 |
$fileUpResponse = $filesApiHelper->uploadFiles($fieldValues[$filePair->formField], $recordID, $filePair->zohoFormField); |
| 137 |
if (isset($fileUpResponse->response->error)) { |
| 138 |
$responseType = 'error'; |
| 139 |
} |
| 140 |
$fileUpResponses[] = $fileUpResponse; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
if ($fileFound) { |
| 145 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => $module], $responseType, $fileUpResponses); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $recordApiResponse; |
| 151 |
} |
| 152 |
|
| 153 |
public function formatFieldValue($value, $formatSpecs) |
| 154 |
{ |
| 155 |
if (empty($value)) { |
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
switch ($formatSpecs->data_type) { |
| 160 |
case 'AutoNumber': |
| 161 |
$apiFormat = 'integer'; |
| 162 |
break; |
| 163 |
|
| 164 |
case 'Text': |
| 165 |
case 'Picklist': |
| 166 |
case 'Email': |
| 167 |
case 'Website': |
| 168 |
case 'Currency': |
| 169 |
case 'TextArea': |
| 170 |
$apiFormat = 'string'; |
| 171 |
break; |
| 172 |
|
| 173 |
case 'Date': |
| 174 |
$apiFormat = 'date'; |
| 175 |
break; |
| 176 |
|
| 177 |
case 'DateTime': |
| 178 |
$apiFormat = 'datetime'; |
| 179 |
break; |
| 180 |
|
| 181 |
case 'Double': |
| 182 |
$apiFormat = 'double'; |
| 183 |
break; |
| 184 |
|
| 185 |
case 'Boolean': |
| 186 |
$apiFormat = 'boolean'; |
| 187 |
break; |
| 188 |
|
| 189 |
default: |
| 190 |
$apiFormat = $formatSpecs->data_type; |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
$formatedValue = ''; |
| 195 |
$fieldFormat = gettype($value); |
| 196 |
if ($fieldFormat === $apiFormat && 'datetime' !== $formatSpecs->data_type) { |
| 197 |
$formatedValue = $value; |
| 198 |
} else { |
| 199 |
if ('string' === $apiFormat && 'datetime' !== $formatSpecs->data_type) { |
| 200 |
$formatedValue = !is_string($value) ? json_encode($value) : $value; |
| 201 |
} elseif ('datetime' === $apiFormat) { |
| 202 |
$dateTimeHelper = new DateTimeHelper(); |
| 203 |
$formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d\TH:i', DateTimeHelper::wp_timezone(), 'Y-m-d H:i:s', null); |
| 204 |
} elseif ('date' === $apiFormat) { |
| 205 |
$dateTimeHelper = new DateTimeHelper(); |
| 206 |
$formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d', DateTimeHelper::wp_timezone(), 'm/d/Y', null); |
| 207 |
} else { |
| 208 |
$stringyfiedValue = !is_string($value) ? json_encode($value) : $value; |
| 209 |
|
| 210 |
switch ($apiFormat) { |
| 211 |
case 'double': |
| 212 |
$formatedValue = (float) $stringyfiedValue; |
| 213 |
break; |
| 214 |
|
| 215 |
case 'boolean': |
| 216 |
$formatedValue = (bool) $stringyfiedValue; |
| 217 |
break; |
| 218 |
|
| 219 |
case 'integer': |
| 220 |
$formatedValue = (int) $stringyfiedValue; |
| 221 |
break; |
| 222 |
|
| 223 |
default: |
| 224 |
$formatedValue = $stringyfiedValue; |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
$formatedValueLenght = 'array' === $apiFormat || 'object' === $apiFormat ? (is_countable($formatedValue) ? \count($formatedValue) : @count($formatedValue)) : \strlen($formatedValue); |
| 230 |
if ($formatedValueLenght > $formatSpecs->length) { |
| 231 |
$formatedValue = 'array' === $apiFormat || 'object' === $apiFormat ? array_slice($formatedValue, 0, $formatSpecs->length) : substr($formatedValue, 0, $formatSpecs->length); |
| 232 |
} |
| 233 |
|
| 234 |
return $formatedValue; |
| 235 |
} |
| 236 |
} |
| 237 |
|