PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V3.3.2 3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 All 139 releases
bit-form / includes / Core / Integration / ZohoCRM / FilesApiHelper.php

FilesApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Integration/ZohoCRM/FilesApiHelper.php

99 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoCrm Files Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoCRM;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\HttpHelper;
12
13 /**
14 * Provide functionality for Upload files
15 */
16 final class FilesApiHelper {
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, $integId, $logID) {
29 $this->_integId = $integId;
30 $this->_logID = $logID;
31 $this->_logResponse = new UtilApiResponse();
32 $this->_payloadBoundary = wp_generate_password(24);
33 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
34 $this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary;
35 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
36 $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
37 }
38
39 /**
40 * Helps to execute upload files api
41 *
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
46 *
47 * @return Array $uploadedFiles ID's of uploaded file in Zoho CRM
48 */
49 public function uploadFiles($files, $isAttachment = false, $module = '', $recordID = 0) {
50 $uploadFileEndpoint = $isAttachment ?
51 "{$this->_apiDomain}/crm/v2/{$module}/{$recordID}/Attachments"
52 : "{$this->_apiDomain}/crm/v2/files";
53 $payload = '';
54 if (is_array($files)) {
55 foreach ($files as $fileIndex => $fileName) {
56 if (file_exists("{$this->_basepath}{$fileName}")) {
57 $payload .= '--' . $this->_payloadBoundary;
58 $payload .= "\r\n";
59 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
60 '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n";
61 $payload .= "\r\n";
62 $payload .= file_get_contents("{$this->_basepath}{$fileName}");
63 $payload .= "\r\n";
64 }
65 }
66 } elseif (file_exists("{$this->_basepath}{$files}")) {
67 $payload .= '--' . $this->_payloadBoundary;
68 $payload .= "\r\n";
69 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
70 '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
71 $payload .= "\r\n";
72 $payload .= file_get_contents("{$this->_basepath}{$files}");
73 $payload .= "\r\n";
74 }
75 if (empty($payload)) {
76 return false;
77 }
78 $payload .= '--' . $this->_payloadBoundary . '--';
79 $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
80 if (!$isAttachment) {
81 $uploadedFiles = [];
82 if (!empty($uploadResponse->data) && \is_array($uploadResponse->data)) {
83 foreach ($uploadResponse->data as $singleFileResponse) {
84 if (!empty($singleFileResponse->code) && 'SUCCESS' === $singleFileResponse->code) {
85 $uploadedFiles[] = $singleFileResponse->details->id;
86 }
87 }
88 }
89 if (isset($uploadResponse->status) && 'error' === $uploadResponse->status) {
90 $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'error', $uploadResponse);
91 } else {
92 $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'success', $uploadResponse);
93 }
94 return $uploadedFiles;
95 }
96 return $uploadResponse;
97 }
98 }
99