PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.3
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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / ZohoMail / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.3, at includes/Core/Integration/ZohoMail/RecordApiHelper.php

123 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoRecruit Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoMail;
9
10 use BitCode\BitForm\Admin\Form\Helpers;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\FieldValueHandler;
13 use BitCode\BitForm\Core\Util\HttpHelper;
14
15 /**
16 * Provide functionality for Record insert,upsert
17 */
18 class RecordApiHelper
19 {
20 private $_defaultHeader;
21 private $_tokenDetails;
22
23 private $_logID;
24
25 private $_integrationID;
26
27 private $_logResponse;
28
29 public function __construct($tokenDetails, $integId, $logID)
30 {
31 $this->_tokenDetails = $tokenDetails;
32 $this->_integrationID = $integId;
33 $this->_logID = $logID;
34 $this->_logResponse = new UtilApiResponse();
35 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
36 $this->_defaultHeader['accountId'] = $tokenDetails->accountId;
37 $this->_defaultHeader['Content-Type'] = 'application/json';
38 }
39
40 public function insertRecord($dataCenter, $data)
41 {
42 $insertRecordEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$this->_defaultHeader['accountId']}/messages";
43 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
44 }
45
46 public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID)
47 {
48 $webUrl = BITFORMS_UPLOAD_BASE_URL . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . Helpers::getEncryptedEntryId($entryID) . DIRECTORY_SEPARATOR;
49 $mailContent = FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues, $formID);
50 $mailContent = FieldValueHandler::changeImagePathInHTMLString($mailContent, $webUrl);
51 $mailContent = FieldValueHandler::changeHrefPathInHTMLString($mailContent, $webUrl);
52
53 $mailInfo = [
54 'fromAddress' => $integrationDetails->tokenDetails->accountEmail,
55 'toAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->to, $fieldValues)),
56 'ccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->cc, $fieldValues)),
57 'bccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->bcc, $fieldValues)),
58 'subject' => FieldValueHandler::replaceFieldWithValue($integrationDetails->subject, $fieldValues),
59 'content' => $mailContent
60 ];
61
62 if ('draft' === $integrationDetails->mailType) {
63 $mailInfo['mode'] = 'draft';
64 }
65
66 $entryDetails = [
67 'formId' => $formID,
68 'entryId' => $entryID,
69 'fieldValues' => $fieldValues
70 ];
71
72 if (!empty($integrationDetails->actions->attachments)) {
73 $mailAttachments = [];
74 $fileFound = 0;
75 $responseType = 'success';
76 $attachmentApiResponses = [];
77 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID);
78 $attachments = explode(',', $integrationDetails->actions->attachments);
79 foreach ($attachments as $fileField) {
80 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
81 $fileFound = 1;
82 if (is_array($fieldValues[$fileField])) {
83 foreach ($fieldValues[$fileField] as $singleFile) {
84 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter);
85 if (isset($attachmentApiResponse->data)) {
86 array_push($mailAttachments, $attachmentApiResponse->data);
87 }
88 if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) {
89 $responseType = 'error';
90 }
91 $attachmentApiResponses[] = $attachmentApiResponse;
92 }
93 } else {
94 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter);
95 if (isset($attachmentApiResponse->data)) {
96 array_push($mailAttachments, $attachmentApiResponse->data);
97 }
98 if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) {
99 $responseType = 'error';
100 }
101
102 $attachmentApiResponses[] = $attachmentApiResponse;
103 }
104 }
105 }
106
107 $mailInfo['attachments'] = $mailAttachments;
108 if ($fileFound) {
109 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => 'mail'], $responseType, $attachmentApiResponses, $entryDetails);
110 }
111 }
112
113 $recordApiResponse = $this->insertRecord($integrationDetails->dataCenter, wp_json_encode($mailInfo));
114 if (200 === $recordApiResponse->status->code) {
115 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'success', $recordApiResponse, $entryDetails);
116 } else {
117 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'error', $recordApiResponse, $entryDetails);
118 }
119
120 return $recordApiResponse;
121 }
122 }
123