| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoRecruit; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use BitCode\BitForm\Core\Util\IpTool; |
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
/** |
| 16 |
* Provide functionality for ZohoCrm integration |
| 17 |
*/ |
| 18 |
class ZohoRecruitHandler |
| 19 |
{ |
| 20 |
private $_formID; |
| 21 |
private $_integrationID; |
| 22 |
|
| 23 |
public function __construct($integrationID, $fromID) |
| 24 |
{ |
| 25 |
$this->_formID = $fromID; |
| 26 |
$this->_integrationID = $integrationID; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Helps to register ajax function's with wp |
| 31 |
* |
| 32 |
* @return null |
| 33 |
*/ |
| 34 |
public static function registerAjax() |
| 35 |
{ |
| 36 |
add_action('wp_ajax_bitforms_zrecruit_generate_token', [__CLASS__, 'generateTokens']); |
| 37 |
add_action('wp_ajax_bitforms_zrecruit_refresh_modules', [__CLASS__, 'refreshModulesAjaxHelper']); |
| 38 |
add_action('wp_ajax_bitforms_zrecruit_refresh_notetypes', [__CLASS__, 'refreshNoteTypesAjaxHelper']); |
| 39 |
add_action('wp_ajax_bitforms_zrecruit_refresh_related_lists', [__CLASS__, 'refreshRelatedModulesAjaxHelper']); |
| 40 |
add_action('wp_ajax_bitforms_zrecruit_get_fields', [__CLASS__, 'getFields']); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Process ajax request for generate_token |
| 45 |
* |
| 46 |
* @return JSON zoho recruit api response and status |
| 47 |
*/ |
| 48 |
public static function generateTokens() |
| 49 |
{ |
| 50 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 51 |
$inputJSON = file_get_contents('php://input'); |
| 52 |
$requestsParams = json_decode($inputJSON); |
| 53 |
if ( |
| 54 |
empty($requestsParams->{'accounts-server'}) |
| 55 |
|| empty($requestsParams->dataCenter) |
| 56 |
|| empty($requestsParams->clientId) |
| 57 |
|| empty($requestsParams->clientSecret) |
| 58 |
|| empty($requestsParams->redirectURI) |
| 59 |
|| empty($requestsParams->code) |
| 60 |
) { |
| 61 |
wp_send_json_error( |
| 62 |
__( |
| 63 |
'Requested parameter is empty', |
| 64 |
'bit-form' |
| 65 |
), |
| 66 |
400 |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 71 |
$requestParams = [ |
| 72 |
'grant_type' => 'authorization_code', |
| 73 |
'client_id' => $requestsParams->clientId, |
| 74 |
'client_secret' => $requestsParams->clientSecret, |
| 75 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 76 |
'code' => $requestsParams->code |
| 77 |
]; |
| 78 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 79 |
|
| 80 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 81 |
wp_send_json_error( |
| 82 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 83 |
400 |
| 84 |
); |
| 85 |
} |
| 86 |
$apiResponse->generates_on = \time(); |
| 87 |
wp_send_json_success($apiResponse, 200); |
| 88 |
} else { |
| 89 |
wp_send_json_error( |
| 90 |
__( |
| 91 |
'Token expired', |
| 92 |
'bit-form' |
| 93 |
), |
| 94 |
401 |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Process ajax request for refresh recruit modules |
| 101 |
* |
| 102 |
* @return JSON recruit module data |
| 103 |
*/ |
| 104 |
public static function refreshModulesAjaxHelper() |
| 105 |
{ |
| 106 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 107 |
$authorizationHeader = null; |
| 108 |
$inputJSON = file_get_contents('php://input'); |
| 109 |
$queryParams = json_decode($inputJSON); |
| 110 |
if ( |
| 111 |
empty($queryParams->tokenDetails) |
| 112 |
|| empty($queryParams->dataCenter) |
| 113 |
|| empty($queryParams->clientId) |
| 114 |
|| empty($queryParams->clientSecret) |
| 115 |
) { |
| 116 |
wp_send_json_error( |
| 117 |
__( |
| 118 |
'Requested parameter is empty', |
| 119 |
'bit-form' |
| 120 |
), |
| 121 |
400 |
| 122 |
); |
| 123 |
} |
| 124 |
$response = []; |
| 125 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 126 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 127 |
} |
| 128 |
$zohosIntegratedModules = [ |
| 129 |
'zohosign__ZohoSign_Document_Events', |
| 130 |
'zohoshowtime__ShowTime_Sessions', |
| 131 |
'zohoshowtime__Zoho_ShowTime', |
| 132 |
'zohosign__ZohoSign_Documents', |
| 133 |
'zohosign__ZohoSign_Recipients' |
| 134 |
]; |
| 135 |
$modulesMetaApiEndpoint = "https://recruit.zoho.{$queryParams->dataCenter}/recruit/private/json/Info/getModules?authtoken={$queryParams->tokenDetails->access_token}&scope=ZohoRecruit.modules.all&version=2"; |
| 136 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 137 |
$modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, null, $authorizationHeader); |
| 138 |
if (!is_wp_error($modulesMetaResponse) && (empty($modulesMetaResponse->status) || (!empty($modulesMetaResponse->status) && 'error' !== $modulesMetaResponse->status))) { |
| 139 |
$retriveModuleData = $modulesMetaResponse->response->result->row; |
| 140 |
$allModules = [ |
| 141 |
'Tasks' => (object) [ |
| 142 |
'aMod' => 'Tasks', |
| 143 |
'pl' => 'Tasks' |
| 144 |
], |
| 145 |
'Events' => (object) [ |
| 146 |
'aMod' => 'Events', |
| 147 |
'pl' => 'Events' |
| 148 |
], |
| 149 |
'Calls' => (object) [ |
| 150 |
'aMod' => 'Calls', |
| 151 |
'pl' => 'Calls' |
| 152 |
], |
| 153 |
]; |
| 154 |
foreach ($retriveModuleData as $value) { |
| 155 |
if (preg_match('/CustomModule/', $value->aMod) || preg_match('/Candidates/', $value->aMod)) { |
| 156 |
$allModules[$value->aMod] = (object) [ |
| 157 |
'aMod' => $value->aMod, |
| 158 |
'pl' => $value->pl |
| 159 |
]; |
| 160 |
} |
| 161 |
} |
| 162 |
uksort($allModules, 'strnatcasecmp'); |
| 163 |
$response['modules'] = $allModules; |
| 164 |
} else { |
| 165 |
wp_send_json_error( |
| 166 |
empty($modulesMetaResponse->error) ? 'Unknown' : $modulesMetaResponse->error, |
| 167 |
400 |
| 168 |
); |
| 169 |
} |
| 170 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 171 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['modules']); |
| 172 |
} |
| 173 |
wp_send_json_success($response, 200); |
| 174 |
} else { |
| 175 |
wp_send_json_error( |
| 176 |
__( |
| 177 |
'Token expired', |
| 178 |
'bit-form' |
| 179 |
), |
| 180 |
401 |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
public static function refreshNoteTypesAjaxHelper() |
| 186 |
{ |
| 187 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 188 |
$authorizationHeader = null; |
| 189 |
$inputJSON = file_get_contents('php://input'); |
| 190 |
$queryParams = json_decode($inputJSON); |
| 191 |
if ( |
| 192 |
empty($queryParams->tokenDetails) |
| 193 |
|| empty($queryParams->dataCenter) |
| 194 |
|| empty($queryParams->clientId) |
| 195 |
|| empty($queryParams->clientSecret) |
| 196 |
) { |
| 197 |
wp_send_json_error( |
| 198 |
__( |
| 199 |
'Requested parameter is empty', |
| 200 |
'bit-form' |
| 201 |
), |
| 202 |
400 |
| 203 |
); |
| 204 |
} |
| 205 |
$response = []; |
| 206 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 207 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 208 |
} |
| 209 |
|
| 210 |
$notesMetaApiEndpoint = "https: //recruit.zoho.com/recruit/private/json/Notes/getNoteTypes?authtoken={$queryParams->tokenDetails->access_token}&scope=ZohoRecruit.modules.call.all&version=2"; |
| 211 |
|
| 212 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 213 |
$notesMetaResponse = HttpHelper::get($notesMetaApiEndpoint, null, $authorizationHeader); |
| 214 |
|
| 215 |
// wp_send_json_success($notesMetaResponse, 200); |
| 216 |
|
| 217 |
if (!is_wp_error($notesMetaResponse) && (empty($notesMetaResponse->status) || (!empty($notesMetaResponse->status) && 'error' !== $notesMetaResponse->status))) { |
| 218 |
$retriveModuleData = $notesMetaResponse->response->result->Notes->row; |
| 219 |
$allNoteTypes = []; |
| 220 |
foreach ($retriveModuleData as $value) { |
| 221 |
$allNoteTypes[$value->FL[0]->dv] = (object) [ |
| 222 |
'noteTypeId' => $value->FL[1]->content, |
| 223 |
'noteTypeName' => $value->FL[0]->dv |
| 224 |
]; |
| 225 |
} |
| 226 |
uksort($allNoteTypes, 'strnatcasecmp'); |
| 227 |
$response['noteTypes'] = $allNoteTypes; |
| 228 |
} else { |
| 229 |
wp_send_json_error( |
| 230 |
empty($notesMetaResponse->error) ? 'Unknown' : $notesMetaResponse->error, |
| 231 |
400 |
| 232 |
); |
| 233 |
} |
| 234 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 235 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['notetypes']); |
| 236 |
} |
| 237 |
wp_send_json_success($response, 200); |
| 238 |
} else { |
| 239 |
wp_send_json_error( |
| 240 |
__( |
| 241 |
'Token expired', |
| 242 |
'bit-form' |
| 243 |
), |
| 244 |
401 |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Process ajax request for refresh recruit modules |
| 251 |
* |
| 252 |
* @return JSON recruit module data |
| 253 |
*/ |
| 254 |
public static function refreshRelatedModulesAjaxHelper() |
| 255 |
{ |
| 256 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 257 |
$inputJSON = file_get_contents('php://input'); |
| 258 |
$queryParams = json_decode($inputJSON); |
| 259 |
if ( |
| 260 |
empty($queryParams->tokenDetails) |
| 261 |
|| empty($queryParams->dataCenter) |
| 262 |
|| empty($queryParams->clientId) |
| 263 |
|| empty($queryParams->clientSecret) |
| 264 |
|| empty($queryParams->module) |
| 265 |
) { |
| 266 |
wp_send_json_error( |
| 267 |
__( |
| 268 |
'Requested parameter is empty', |
| 269 |
'bit-form' |
| 270 |
), |
| 271 |
400 |
| 272 |
); |
| 273 |
} |
| 274 |
$response = []; |
| 275 |
$relatedModules = []; |
| 276 |
$allModules = [ |
| 277 |
'Tasks' => (object) [ |
| 278 |
'aMod' => 'Tasks', |
| 279 |
'pl' => 'Tasks' |
| 280 |
], |
| 281 |
'Events' => (object) [ |
| 282 |
'aMod' => 'Events', |
| 283 |
'pl' => 'Events' |
| 284 |
], |
| 285 |
'Calls' => (object) [ |
| 286 |
'aMod' => 'Calls', |
| 287 |
'pl' => 'Calls' |
| 288 |
], |
| 289 |
// 'Notes' => (object) array( |
| 290 |
// 'aMod' => 'Notes', |
| 291 |
// 'pl' => 'Notes' |
| 292 |
// ), |
| 293 |
]; |
| 294 |
foreach ($allModules as $module) { |
| 295 |
if ($module->aMod !== $queryParams->module) { |
| 296 |
$relatedModules[$module->aMod] = (object) [ |
| 297 |
'aMod' => $module->aMod, |
| 298 |
'pl' => $module->pl |
| 299 |
]; |
| 300 |
} |
| 301 |
} |
| 302 |
uksort($relatedModules, 'strnatcasecmp'); |
| 303 |
$response['related_modules'] = $relatedModules; |
| 304 |
|
| 305 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 306 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['related_modules']); |
| 307 |
} |
| 308 |
wp_send_json_success($response, 200); |
| 309 |
} else { |
| 310 |
wp_send_json_error( |
| 311 |
__( |
| 312 |
'Token expired', |
| 313 |
'bit-form' |
| 314 |
), |
| 315 |
401 |
| 316 |
); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Process ajax request for refesh recruit layouts |
| 322 |
* |
| 323 |
* @return JSON recruit layout data |
| 324 |
*/ |
| 325 |
public static function getFields() |
| 326 |
{ |
| 327 |
$authorizationHeader = null; |
| 328 |
$requiredParams = null; |
| 329 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 330 |
$inputJSON = file_get_contents('php://input'); |
| 331 |
$queryParams = json_decode($inputJSON); |
| 332 |
if ( |
| 333 |
empty($queryParams->module) |
| 334 |
|| empty($queryParams->tokenDetails) |
| 335 |
|| empty($queryParams->dataCenter) |
| 336 |
|| empty($queryParams->clientId) |
| 337 |
|| empty($queryParams->clientSecret) |
| 338 |
) { |
| 339 |
wp_send_json_error( |
| 340 |
__( |
| 341 |
'Requested parameter is empty', |
| 342 |
'bit-form' |
| 343 |
), |
| 344 |
400 |
| 345 |
); |
| 346 |
} |
| 347 |
$response = []; |
| 348 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 349 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 350 |
} |
| 351 |
$fieldsMetaApiEndpoint = "https://recruit.zoho.{$queryParams->dataCenter}/recruit/private/json/{$queryParams->module}/getFields?authtoken={$queryParams->tokenDetails->access_token}&scope=recruitapi&version=2"; |
| 352 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 353 |
$requiredParams['module'] = $queryParams->module; |
| 354 |
$fieldsMetaResponse = HttpHelper::get($fieldsMetaApiEndpoint, $requiredParams, $authorizationHeader); |
| 355 |
|
| 356 |
if (!is_wp_error($fieldsMetaResponse) && (empty($fieldsMetaResponse->status) || (!empty($fieldsMetaResponse->status) && 'error' !== $fieldsMetaResponse->status))) { |
| 357 |
$retriveFieldsData = $fieldsMetaResponse->{$queryParams->module}->section; |
| 358 |
$fields = []; |
| 359 |
$fileUploadFields = []; |
| 360 |
$requiredFields = []; |
| 361 |
$requiredFileUploadFiles = []; |
| 362 |
if ('Candidates' === $queryParams->module) { |
| 363 |
$fileUploadFields['Candidate Photo'] = (object) [ |
| 364 |
'display_label' => 'Candidate Profile Photo', |
| 365 |
'length' => 1000, |
| 366 |
'data_type' => 'UploadText', |
| 367 |
'required' => 'false' |
| 368 |
]; |
| 369 |
} |
| 370 |
if (count($retriveFieldsData) > 1) { |
| 371 |
foreach ($retriveFieldsData as $fieldValue) { |
| 372 |
foreach ($fieldValue->FL as $sectionValue) { |
| 373 |
if (null === $sectionValue->dv) { |
| 374 |
continue; |
| 375 |
} |
| 376 |
if ('UploadText' === $sectionValue->type) { |
| 377 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 378 |
'display_label' => $sectionValue->dv, |
| 379 |
'length' => $sectionValue->maxlength, |
| 380 |
'data_type' => $sectionValue->type, |
| 381 |
'required' => $sectionValue->req |
| 382 |
]; |
| 383 |
if ('true' === $sectionValue->req) { |
| 384 |
$requiredFileUploadFiles[] = $sectionValue->dv; |
| 385 |
} |
| 386 |
} else { |
| 387 |
$fields[$sectionValue->dv] = (object) [ |
| 388 |
'display_label' => $sectionValue->dv, |
| 389 |
'length' => $sectionValue->maxlength, |
| 390 |
'data_type' => $sectionValue->type, |
| 391 |
'required' => $sectionValue->req |
| 392 |
]; |
| 393 |
if ('true' === $sectionValue->req) { |
| 394 |
$requiredFields[] = $sectionValue->dv; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} else { |
| 400 |
foreach ($retriveFieldsData->FL as $sectionValue) { |
| 401 |
if (null === $sectionValue->dv) { |
| 402 |
continue; |
| 403 |
} |
| 404 |
if ('Candidates' === $sectionValue->aMod) { |
| 405 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 406 |
'display_label' => 'Candidate Profile Photo', |
| 407 |
'length' => 1000, |
| 408 |
'data_type' => 'UploadText', |
| 409 |
'required' => 'false' |
| 410 |
]; |
| 411 |
} |
| 412 |
if ('UploadText' === $sectionValue->type) { |
| 413 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 414 |
'display_label' => $sectionValue->dv, |
| 415 |
'length' => $sectionValue->maxlength, |
| 416 |
'data_type' => $sectionValue->type, |
| 417 |
'required' => $sectionValue->req |
| 418 |
]; |
| 419 |
if ('true' === $sectionValue->req) { |
| 420 |
$requiredFileUploadFiles[] = $sectionValue->dv; |
| 421 |
} |
| 422 |
} else { |
| 423 |
$fields[$sectionValue->dv] = (object) [ |
| 424 |
'display_label' => $sectionValue->dv, |
| 425 |
'length' => $sectionValue->maxlength, |
| 426 |
'data_type' => $sectionValue->type, |
| 427 |
'required' => $sectionValue->req |
| 428 |
]; |
| 429 |
if ('true' === $sectionValue->req) { |
| 430 |
$requiredFields[] = $sectionValue->dv; |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if ('Candidates' === $queryParams->module && isset($fields['Zip/Postal Code'])) { |
| 437 |
$fields['Zip Code'] = $fields['Zip/Postal Code']; |
| 438 |
unset($fields['Zip/Postal Code']); |
| 439 |
} |
| 440 |
|
| 441 |
uksort($fields, 'strnatcasecmp'); |
| 442 |
uksort($fileUploadFields, 'strnatcasecmp'); |
| 443 |
usort($requiredFields, 'strnatcasecmp'); |
| 444 |
usort($requiredFileUploadFiles, 'strnatcasecmp'); |
| 445 |
|
| 446 |
$fieldDetails = (object) [ |
| 447 |
'fields' => $fields, |
| 448 |
'fileUploadFields' => $fileUploadFields, |
| 449 |
'required' => $requiredFields, |
| 450 |
'requiredFileUploadFields' => $requiredFileUploadFiles |
| 451 |
]; |
| 452 |
$response['fieldDetails'] = $fieldDetails; |
| 453 |
} else { |
| 454 |
wp_send_json_error( |
| 455 |
'error' === $fieldsMetaResponse->status ? $fieldsMetaResponse->message : 'Unknown', |
| 456 |
400 |
| 457 |
); |
| 458 |
} |
| 459 |
if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { |
| 460 |
$response['queryModule'] = $queryParams->module; |
| 461 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); |
| 462 |
} |
| 463 |
wp_send_json_success($response, 200); |
| 464 |
} else { |
| 465 |
wp_send_json_error( |
| 466 |
__( |
| 467 |
'Token expired', |
| 468 |
'bit-form' |
| 469 |
), |
| 470 |
401 |
| 471 |
); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Helps to refresh zoho recruit access_token |
| 477 |
* |
| 478 |
* @param Array $apiData Contains required data for refresh access token |
| 479 |
* @return JSON $tokenDetails API token details |
| 480 |
*/ |
| 481 |
protected static function _refreshAccessToken($apiData) |
| 482 |
{ |
| 483 |
if ( |
| 484 |
empty($apiData->dataCenter) |
| 485 |
|| empty($apiData->clientId) |
| 486 |
|| empty($apiData->clientSecret) |
| 487 |
|| empty($apiData->tokenDetails) |
| 488 |
) { |
| 489 |
return false; |
| 490 |
} |
| 491 |
$tokenDetails = $apiData->tokenDetails; |
| 492 |
|
| 493 |
$dataCenter = $apiData->dataCenter; |
| 494 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 495 |
$requestParams = [ |
| 496 |
'grant_type' => 'refresh_token', |
| 497 |
'client_id' => $apiData->clientId, |
| 498 |
'client_secret' => $apiData->clientSecret, |
| 499 |
'refresh_token' => $tokenDetails->refresh_token, |
| 500 |
]; |
| 501 |
|
| 502 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 503 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
$tokenDetails->generates_on = \time(); |
| 507 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 508 |
return $tokenDetails; |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Save updated access_token to avoid unnecessary token generation |
| 513 |
* |
| 514 |
* @param Integer $fromID ID of Integration related form |
| 515 |
* @param Integer $integrationID ID of Zoho recruit Integration |
| 516 |
* @param Obeject $tokenDetails refreshed token info |
| 517 |
* |
| 518 |
* @return null |
| 519 |
*/ |
| 520 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 521 |
{ |
| 522 |
if (empty($formID) || empty($integrationID)) { |
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 527 |
$zrecruitDetails = $integrationHandler->getAIntegration($integrationID); |
| 528 |
|
| 529 |
if (is_wp_error($zrecruitDetails)) { |
| 530 |
return; |
| 531 |
} |
| 532 |
$newDetails = json_decode($zrecruitDetails[0]->integration_details); |
| 533 |
|
| 534 |
$newDetails->tokenDetails = $tokenDetails; |
| 535 |
if (!empty($others['modules'])) { |
| 536 |
$newDetails->default->modules = $others['modules']; |
| 537 |
} |
| 538 |
if (!empty($others['related_modules'])) { |
| 539 |
$newDetails->default->relatedlist['modules'] = $others['related_modules']; |
| 540 |
} |
| 541 |
|
| 542 |
$integrationHandler->updateIntegration($integrationID, $zrecruitDetails[0]->integration_name, 'Zoho Recruit', \json_encode($newDetails), 'form'); |
| 543 |
} |
| 544 |
|
| 545 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 546 |
{ |
| 547 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 548 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 549 |
$module = $integrationDetails->module; |
| 550 |
$fieldMap = $integrationDetails->field_map; |
| 551 |
$actions = $integrationDetails->actions; |
| 552 |
$defaultDataConf = $integrationDetails->default; |
| 553 |
if ( |
| 554 |
empty($tokenDetails) |
| 555 |
|| empty($module) |
| 556 |
|| empty($fieldMap) |
| 557 |
) { |
| 558 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho recruit api', 'bit-form')); |
| 559 |
} |
| 560 |
if (empty($defaultDataConf->moduleData->{$module}->fields) || empty($defaultDataConf->modules->{$module})) { |
| 561 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho recruit api', 'bit-form')); |
| 562 |
} |
| 563 |
$requiredParams = null; |
| 564 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 565 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 566 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 567 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 568 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 569 |
$newTokenDetails = ZohoRecruitHandler::_refreshAccessToken((object)$requiredParams); |
| 570 |
if ($newTokenDetails) { |
| 571 |
ZohoRecruitHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 572 |
$tokenDetails = $newTokenDetails; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
$required = !empty($defaultDataConf->moduleData->{$module}->required) ? |
| 577 |
$defaultDataConf->moduleData->{$module}->required : []; |
| 578 |
|
| 579 |
$actions = $integrationDetails->actions; |
| 580 |
$fileMap = $integrationDetails->upload_field_map; |
| 581 |
$dataCenter = $integrationDetails->dataCenter; |
| 582 |
$recordApiHelper = new RecordApiHelper($dataCenter, $tokenDetails, $this->_integrationID, $logID); |
| 583 |
$zRecruitApiResponse = $recordApiHelper->executeRecordApi( |
| 584 |
$this->_formID, |
| 585 |
$entryID, |
| 586 |
$defaultDataConf, |
| 587 |
$module, |
| 588 |
$fieldValues, |
| 589 |
$fieldMap, |
| 590 |
$actions, |
| 591 |
$required, |
| 592 |
$fileMap |
| 593 |
); |
| 594 |
if (is_wp_error($zRecruitApiResponse)) { |
| 595 |
return $zRecruitApiResponse; |
| 596 |
} |
| 597 |
|
| 598 |
if ( |
| 599 |
count($integrationDetails->relatedlists) |
| 600 |
&& !empty($zRecruitApiResponse->response->result->row->success->details->FL[0]) |
| 601 |
&& 'Id' === $zRecruitApiResponse->response->result->row->success->details->FL[0]->val |
| 602 |
) { |
| 603 |
foreach ($integrationDetails->relatedlists as $relatedlist) { |
| 604 |
if (!empty($relatedlist->module)) { |
| 605 |
$recordID = $zRecruitApiResponse->response->result->row->success->details->FL[0]->content; |
| 606 |
$relatedListModule = $relatedlist->module; |
| 607 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEMODULE'} = (object) [ |
| 608 |
'length' => \strlen($relatedListModule), |
| 609 |
'required' => true, |
| 610 |
'data_type' => 'string', |
| 611 |
]; |
| 612 |
$fieldValues['SEMODULE'] = $relatedListModule; |
| 613 |
$relatedlist->field_map[] = (object) |
| 614 |
[ |
| 615 |
'formField' => 'SEMODULE', |
| 616 |
'zohoFormField' => 'SEMODULE' |
| 617 |
]; |
| 618 |
|
| 619 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEID'} = (object) [ |
| 620 |
'length' => \strlen($recordID), |
| 621 |
'required' => true, |
| 622 |
'data_type' => 'string', |
| 623 |
]; |
| 624 |
$fieldValues['SEID'] = $recordID; |
| 625 |
$relatedlist->field_map[] = (object) |
| 626 |
[ |
| 627 |
'formField' => 'SEID', |
| 628 |
'zohoFormField' => 'SEID' |
| 629 |
]; |
| 630 |
|
| 631 |
$zRecruitRelatedRecResp = $recordApiHelper->executeRecordApi( |
| 632 |
$this->_formID, |
| 633 |
$entryID, |
| 634 |
$defaultDataConf, |
| 635 |
$relatedListModule, |
| 636 |
$fieldValues, |
| 637 |
$relatedlist->field_map, |
| 638 |
$relatedlist->actions, |
| 639 |
!empty($defaultDataConf->moduleData->{$relatedListModule}->required) ? |
| 640 |
$defaultDataConf->moduleData->{$relatedListModule}->required : [], |
| 641 |
$relatedlist->upload_field_map |
| 642 |
); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
return $zRecruitApiResponse; |
| 647 |
} |
| 648 |
} |
| 649 |
|