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
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.0, at includes/Core/Integration/ZohoMail/RecordApiHelper.php

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