| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Files Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoRecruit; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provide functionality for Upload files |
| 14 |
*/ |
| 15 |
final class FilesApiHelper |
| 16 |
{ |
| 17 |
private $_defaultHeader; |
| 18 |
private $_apiDomain; |
| 19 |
private $_payloadBoundary; |
| 20 |
private $_basepath; |
| 21 |
private $_module; |
| 22 |
|
| 23 |
private $_dataCenter; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param String $module zoho recruit module dname |
| 27 |
* @param Object $tokenDetails Api token details |
| 28 |
* @param Integer $formID ID of the form, for which integration is executing |
| 29 |
* @param Integer $entryID Current submittion ID |
| 30 |
*/ |
| 31 |
public function __construct($module, $dataCenter, $tokenDetails, $formID, $entryID) |
| 32 |
{ |
| 33 |
$this->_module = $module; |
| 34 |
$this->_dataCenter = $dataCenter; |
| 35 |
$this->_payloadBoundary = wp_generate_password(24); |
| 36 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 37 |
$this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary; |
| 38 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 39 |
$this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Helps to execute upload files api |
| 44 |
* |
| 45 |
* @param Mixed $files Files path |
| 46 |
* @param Mixed $recordID Record id |
| 47 |
* @param String $zohoField zoho recruit upload fieldname |
| 48 |
* |
| 49 |
* @return Array $uploadedFiles ID's of uploaded file in Zoho Recruit |
| 50 |
*/ |
| 51 |
public function uploadFiles($files, $recordID, $zohoField) |
| 52 |
{ |
| 53 |
$uploadFileEndpoint = ''; |
| 54 |
|
| 55 |
if ('Candidate Photo' === $zohoField) { |
| 56 |
$uploadFileEndpoint = "https://recruit.zoho.{$this->_dataCenter}/recruit/private/xml/{$this->_module}/uploadPhoto?Scope=recruitapi&type={$zohoField}&version=2&id={$recordID}"; |
| 57 |
} else { |
| 58 |
$uploadFileEndpoint = "https://recruit.zoho.{$this->_dataCenter}/recruit/private/json/{$this->_module}/uploadFile?Scope=ZohoRecruit.modules.all&type={$zohoField}&version=2&id={$recordID}"; |
| 59 |
} |
| 60 |
|
| 61 |
$payload = ''; |
| 62 |
if (is_array($files)) { |
| 63 |
foreach ($files as $fileIndex => $fileName) { |
| 64 |
if (file_exists("{$this->_basepath}{$fileName}")) { |
| 65 |
$payload .= '--' . $this->_payloadBoundary; |
| 66 |
$payload .= "\r\n"; |
| 67 |
$payload .= 'Content-Disposition: form-data; name="' . 'content' . |
| 68 |
'"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n"; |
| 69 |
$payload .= "\r\n"; |
| 70 |
$payload .= file_get_contents("{$this->_basepath}{$fileName}"); |
| 71 |
$payload .= "\r\n"; |
| 72 |
} |
| 73 |
} |
| 74 |
} elseif (file_exists("{$this->_basepath}{$files}")) { |
| 75 |
$payload .= '--' . $this->_payloadBoundary; |
| 76 |
$payload .= "\r\n"; |
| 77 |
$payload .= 'Content-Disposition: form-data; name="' . 'content' . |
| 78 |
'"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; |
| 79 |
$payload .= "\r\n"; |
| 80 |
$payload .= file_get_contents("{$this->_basepath}{$files}"); |
| 81 |
$payload .= "\r\n"; |
| 82 |
} |
| 83 |
if (empty($payload)) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
$payload .= '--' . $this->_payloadBoundary . '--'; |
| 87 |
return HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 88 |
} |
| 89 |
} |
| 90 |
|