PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 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 2.9.1, at includes/Core/Integration/ZohoMail/RecordApiHelper.php

111 lines 4.6 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\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\FieldValueHandler;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for Record insert,upsert
16 */
17 class RecordApiHelper
18 {
19 private $_defaultHeader;
20 private $_tokenDetails;
21
22 private $_logID;
23
24 private $_integrationID;
25
26 private $_logResponse;
27
28 public function __construct($tokenDetails, $integId, $logID)
29 {
30 $this->_tokenDetails = $tokenDetails;
31 $this->_integrationID = $integId;
32 $this->_logID = $logID;
33 $this->_logResponse = new UtilApiResponse();
34 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
35 $this->_defaultHeader['accountId'] = $tokenDetails->accountId;
36 $this->_defaultHeader['Content-Type'] = 'application/json';
37 }
38
39 public function insertRecord($dataCenter, $data)
40 {
41 $insertRecordEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$this->_defaultHeader['accountId']}/messages";
42 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
43 }
44
45 public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID)
46 {
47 $mailInfo = [
48 'fromAddress' => $integrationDetails->tokenDetails->accountEmail,
49 'toAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->to, $fieldValues)),
50 'ccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->cc, $fieldValues)),
51 'bccAddress' => implode(',', FieldValueHandler::validateMailArry($integrationDetails->bcc, $fieldValues)),
52 'subject' => FieldValueHandler::replaceFieldWithValue($integrationDetails->subject, $fieldValues),
53 'content' => FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues)
54 ];
55
56 if ('draft' === $integrationDetails->mailType) {
57 $mailInfo['mode'] = 'draft';
58 }
59
60 if (!empty($integrationDetails->actions->attachments)) {
61 $mailAttachments = [];
62 $fileFound = 0;
63 $responseType = 'success';
64 $attachmentApiResponses = [];
65 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID);
66 $attachments = explode(',', $integrationDetails->actions->attachments);
67 foreach ($attachments as $fileField) {
68 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
69 $fileFound = 1;
70 if (is_array($fieldValues[$fileField])) {
71 foreach ($fieldValues[$fileField] as $singleFile) {
72 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter);
73 if (isset($attachmentApiResponse->data)) {
74 array_push($mailAttachments, $attachmentApiResponse->data);
75 }
76 if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) {
77 $responseType = 'error';
78 }
79 $attachmentApiResponses[] = $attachmentApiResponse;
80 }
81 } else {
82 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $integrationDetails->tokenDetails->accountId, $integrationDetails->dataCenter);
83 if (isset($attachmentApiResponse->data)) {
84 array_push($mailAttachments, $attachmentApiResponse->data);
85 }
86 if (!isset($attachmentApiResponse->status) && !isset($attachmentApiResponse->status->code) && 200 !== $attachmentApiResponse->status->code) {
87 $responseType = 'error';
88 }
89
90 $attachmentApiResponses[] = $attachmentApiResponse;
91 }
92 }
93 }
94
95 $mailInfo['attachments'] = $mailAttachments;
96 if ($fileFound) {
97 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => 'mail'], $responseType, $attachmentApiResponses);
98 }
99 }
100
101 $recordApiResponse = $this->insertRecord($integrationDetails->dataCenter, wp_json_encode($mailInfo));
102 if (200 === $recordApiResponse->status->code) {
103 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'success', $recordApiResponse);
104 } else {
105 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $integrationDetails->mailType, 'type_name' => 'mail'], 'error', $recordApiResponse);
106 }
107
108 return $recordApiResponse;
109 }
110 }
111