| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoProjects Files Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoProjects; |
| 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 |
|
| 22 |
/** |
| 23 |
* |
| 24 |
* @param Object $tokenDetails Api token details |
| 25 |
* @param Integer $formID ID of the form, for which integration is executing |
| 26 |
* @param Integer $entryID Current submittion ID |
| 27 |
*/ |
| 28 |
public function __construct($tokenDetails, $formID, $entryID) |
| 29 |
{ |
| 30 |
$this->_payloadBoundary = wp_generate_password(24); |
| 31 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 32 |
$this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 33 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 34 |
$this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Helps to execute upload files api |
| 39 |
* |
| 40 |
* @param Mixed $files Files path |
| 41 |
* @param Bool $isAttachment Check upload type |
| 42 |
* @param Mixed $module Attachment Module name |
| 43 |
* @param Mixed $recordID Record id |
| 44 |
* |
| 45 |
* @return Array $uploadedFiles ID's of uploaded file in Zoho Projects |
| 46 |
*/ |
| 47 |
public function uploadFiles($files, $portalId, $projectId, $event, $eventId, $dataCenter) |
| 48 |
{ |
| 49 |
$uploadFileEndpoint = "https://projectsapi.zoho.{$dataCenter}/restapi/portal/{$portalId}/projects/{$projectId}/" . ('task' === $event || 'subtask' === $event ? 'tasks' : 'bugs') . "/${eventId}/attachments/"; |
| 50 |
|
| 51 |
$payload = ''; |
| 52 |
if (is_array($files)) { |
| 53 |
foreach ($files as $fileIndex => $fileName) { |
| 54 |
if (file_exists("{$this->_basepath}{$fileName}")) { |
| 55 |
$payload .= '--' . $this->_payloadBoundary; |
| 56 |
$payload .= "\r\n"; |
| 57 |
$payload .= 'Content-Disposition: form-data; name="' . 'uploaddoc' . |
| 58 |
'"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n"; |
| 59 |
$payload .= "\r\n"; |
| 60 |
$payload .= file_get_contents("{$this->_basepath}{$fileName}"); |
| 61 |
$payload .= "\r\n"; |
| 62 |
} |
| 63 |
} |
| 64 |
} elseif (file_exists("{$this->_basepath}{$files}")) { |
| 65 |
$payload .= '--' . $this->_payloadBoundary; |
| 66 |
$payload .= "\r\n"; |
| 67 |
$payload .= 'Content-Disposition: form-data; name="' . 'uploaddoc' . |
| 68 |
'"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; |
| 69 |
$payload .= "\r\n"; |
| 70 |
$payload .= file_get_contents("{$this->_basepath}{$files}"); |
| 71 |
$payload .= "\r\n"; |
| 72 |
} |
| 73 |
|
| 74 |
if (empty($payload)) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
$payload .= '--' . $this->_payloadBoundary . '--'; |
| 78 |
$uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 79 |
|
| 80 |
return $uploadResponse; |
| 81 |
} |
| 82 |
} |
| 83 |
|