PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.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 2.10.1 All 138 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.10.2, at includes/Core/Integration/ZohoCRM/FilesApiHelper.php

108 lines 3.8 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 {
18 private $_defaultHeader;
19 private $_apiDomain;
20 private $_payloadBoundary;
21 private $_basepath;
22
23 private $_integId;
24
25 private $_logID;
26
27 private $_logResponse;
28
29 /**
30 *
31 * @param object $tokenDetails Api token details
32 * @param integer $formID ID of the form, for which integration is executing
33 * @param integer $entryID Current submission ID
34 */
35 public function __construct($tokenDetails, $formID, $entryID, $integId, $logID)
36 {
37 $this->_integId = $integId;
38 $this->_logID = $logID;
39 $this->_logResponse = new UtilApiResponse();
40 $this->_payloadBoundary = wp_generate_password(24);
41 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
42 $this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary;
43 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
44 $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
45 }
46
47 /**
48 * Helps to execute upload files api
49 *
50 * @param mixed $files Files path
51 * @param bool $isAttachment Check upload type
52 * @param mixed $module Attachment Module name
53 * @param mixed | boolean $recordID Record id
54 *
55 * @return mixed $uploadedFiles ID's of uploaded file in Zoho CRM
56 */
57 public function uploadFiles($files, $isAttachment = false, $module = '', $recordID = 0)
58 {
59 $uploadFileEndpoint = $isAttachment ?
60 "{$this->_apiDomain}/crm/v2/{$module}/{$recordID}/Attachments"
61 : "{$this->_apiDomain}/crm/v2/files";
62 $payload = '';
63 if (is_array($files)) {
64 foreach ($files as $fileIndex => $fileName) {
65 if (file_exists("{$this->_basepath}{$fileName}")) {
66 $payload .= '--' . $this->_payloadBoundary;
67 $payload .= "\r\n";
68 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
69 '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n";
70 $payload .= "\r\n";
71 $payload .= file_get_contents("{$this->_basepath}{$fileName}");
72 $payload .= "\r\n";
73 }
74 }
75 } elseif (file_exists("{$this->_basepath}{$files}")) {
76 $payload .= '--' . $this->_payloadBoundary;
77 $payload .= "\r\n";
78 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
79 '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
80 $payload .= "\r\n";
81 $payload .= file_get_contents("{$this->_basepath}{$files}");
82 $payload .= "\r\n";
83 }
84 if (empty($payload)) {
85 return false;
86 }
87 $payload .= '--' . $this->_payloadBoundary . '--';
88 $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
89 if (!$isAttachment) {
90 $uploadedFiles = [];
91 if (!empty($uploadResponse->data) && \is_array($uploadResponse->data)) {
92 foreach ($uploadResponse->data as $singleFileResponse) {
93 if (!empty($singleFileResponse->code) && 'SUCCESS' === $singleFileResponse->code) {
94 $uploadedFiles[] = $singleFileResponse->details->id;
95 }
96 }
97 }
98 if (isset($uploadResponse->status) && 'error' === $uploadResponse->status) {
99 $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'error', $uploadResponse);
100 } else {
101 $this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'success', $uploadResponse);
102 }
103 return $uploadedFiles;
104 }
105 return $uploadResponse;
106 }
107 }
108