← All changes
|
includes/Core/Integration/ZohoCRM/FilesApiHelper.php
+52
-14
2.0
→
V3.3.2
View file →
| @@ -7,26 +7,39 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoCRM; |
| 9 | 9 | |
| 10 | 10 | use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 | +use BitCode\BitForm\Core\Util\FileHandler; | |
| 11 | 12 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | 15 | * Provide functionality for Upload files |
| 15 | 16 | */ |
| 16 | -final class FilesApiHelper { | |
| 17 | +final class FilesApiHelper | |
| 18 | +{ | |
| 17 | 19 | private $_defaultHeader; |
| 18 | 20 | private $_apiDomain; |
| 19 | 21 | private $_payloadBoundary; |
| 20 | 22 | private $_basepath; |
| 21 | 23 | |
| 24 | + private $_integId; | |
| 25 | + | |
| 26 | + private $_logID; | |
| 27 | + | |
| 28 | + private $_logResponse; | |
| 29 | + | |
| 30 | + private $_formId; | |
| 31 | + | |
| 32 | + private $_entryId; | |
| 33 | + | |
| 22 | 34 | /** |
| 23 | 35 | * |
| 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 | |
| 36 | + * @param object $tokenDetails Api token details | |
| 37 | + * @param integer $formID ID of the form, for which integration is executing | |
| 38 | + * @param integer $entryID Current submission ID | |
| 27 | 39 | */ |
| 28 | - public function __construct($tokenDetails, $formID, $entryID, $integId, $logID) { | |
| 40 | + public function __construct($tokenDetails, $formID, $entryID, $integId, $logID) | |
| 41 | + { | |
| 29 | 42 | $this->_integId = $integId; |
| 30 | 43 | $this->_logID = $logID; |
| 31 | 44 | $this->_logResponse = new UtilApiResponse(); |
| 32 | 45 | $this->_payloadBoundary = wp_generate_password(24); |
| @@ -32,22 +45,31 @@ | ||
| 32 | 45 | $this->_payloadBoundary = wp_generate_password(24); |
| 33 | 46 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 34 | 47 | $this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary; |
| 35 | 48 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 36 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 49 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 50 | + | |
| 51 | + $this->_formId = $formID; | |
| 52 | + $this->_entryId = $entryID; | |
| 37 | 53 | } |
| 38 | 54 | |
| 39 | 55 | /** |
| 40 | 56 | * Helps to execute upload files api |
| 41 | 57 | * |
| 42 | - * @param Mixed $files Files path | |
| 43 | - * @param Bool $isAttachment Check upload type | |
| 44 | - * @param Mixed $module Attachment Module name | |
| 45 | - * @param Mixed $recordID Record id | |
| 58 | + * @param mixed $files Files path | |
| 59 | + * @param bool $isAttachment Check upload type | |
| 60 | + * @param mixed $module Attachment Module name | |
| 61 | + * @param mixed | boolean $recordID Record id | |
| 46 | 62 | * |
| 47 | - * @return Array $uploadedFiles ID's of uploaded file in Zoho CRM | |
| 63 | + * @return mixed $uploadedFiles ID's of uploaded file in Zoho CRM | |
| 48 | 64 | */ |
| 49 | - public function uploadFiles($files, $isAttachment = false, $module = '', $recordID = 0) { | |
| 65 | + public function uploadFiles($files, $isAttachment = false, $module = '', $recordID = 0) | |
| 66 | + { | |
| 67 | + $files = self::normalizeFileNames($files); | |
| 68 | + $entryDetails = [ | |
| 69 | + 'formId' => $this->_formId, | |
| 70 | + 'entryId' => $this->_entryId, | |
| 71 | + ]; | |
| 50 | 72 | $uploadFileEndpoint = $isAttachment ? |
| 51 | 73 | "{$this->_apiDomain}/crm/v2/{$module}/{$recordID}/Attachments" |
| 52 | 74 | : "{$this->_apiDomain}/crm/v2/files"; |
| 53 | 75 | $payload = ''; |
| @@ -86,13 +108,29 @@ | ||
| 86 | 108 | } |
| 87 | 109 | } |
| 88 | 110 | } |
| 89 | 111 | if (isset($uploadResponse->status) && 'error' === $uploadResponse->status) { |
| 90 | - $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'error', $uploadResponse); | |
| 112 | + $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'error', $uploadResponse, $entryDetails); | |
| 91 | 113 | } else { |
| 92 | - $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'success', $uploadResponse); | |
| 114 | + $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'success', $uploadResponse, $entryDetails); | |
| 93 | 115 | } |
| 94 | 116 | return $uploadedFiles; |
| 95 | 117 | } |
| 96 | 118 | return $uploadResponse; |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), | |
| 123 | + * so reduce each to the stored file name before resolving against the entry directory. | |
| 124 | + * | |
| 125 | + * @param mixed $files file name(s) or URL(s) | |
| 126 | + * | |
| 127 | + * @return mixed | |
| 128 | + */ | |
| 129 | + private static function normalizeFileNames($files) | |
| 130 | + { | |
| 131 | + if (is_array($files)) { | |
| 132 | + return array_map([__CLASS__, 'normalizeFileNames'], $files); | |
| 133 | + } | |
| 134 | + return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; | |
| 97 | 135 | } |
| 98 | 136 | } |