PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.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 / ZohoDesk / RecordApiHelper.php

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

182 lines 6.3 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\ZohoDesk;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\HttpHelper;
12 use WP_Error;
13
14 /**
15 * Provide functionality for Record insert,upsert
16 */
17 class RecordApiHelper
18 {
19 private $_defaultHeader;
20 private $_apiDomain;
21 private $_tokenDetails;
22
23 private $_logID;
24
25 private $_integrationID;
26
27 private $_logResponse;
28
29 public function __construct($tokenDetails, $orgId, $integId, $logID)
30 {
31 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
32 $this->_defaultHeader['orgId'] = $orgId;
33 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
34 $this->_tokenDetails = $tokenDetails;
35 $this->_integrationID = $integId;
36 $this->_logID = $logID;
37 $this->_logResponse = new UtilApiResponse();
38 }
39
40 public function insertRecord($dataCenter, $data)
41 {
42 $insertRecordEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/tickets";
43
44 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
45 }
46
47 public function createContact($dataCenter, $data)
48 {
49 $getContactEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/contacts";
50
51 return HttpHelper::post($getContactEndpoint, $data, $this->_defaultHeader);
52 }
53
54 public function searchContact($dataCenter, $email)
55 {
56 $searchContactEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/contacts/search?limit=1&email={$email}";
57
58 return HttpHelper::get($searchContactEndpoint, null, $this->_defaultHeader);
59 }
60
61 public function executeRecordApi($department, $dataCenter, $fieldValues, $fieldMap, $required, $actions, $entryID, $formID)
62 {
63 $entryDetails = [
64 'formId' => $formID,
65 'entryId' => $entryID,
66 'fieldValues' => $fieldValues
67 ];
68 $fieldData = [];
69 $customFieldData = [];
70 foreach ($fieldMap as $fieldKey => $fieldPair) {
71 if (!empty($fieldPair->zohoFormField)) {
72 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
73 $fieldData[$fieldPair->zohoFormField] = $fieldPair->customValue;
74 } else {
75 if ('cf' === strtok($fieldPair->zohoFormField, '_')) {
76 $customFieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField];
77 } else {
78 $fieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField];
79 }
80 }
81 }
82
83 if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) {
84 /* translators: %s: field name */
85 $error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for zoho bigin', 'bit-form'), $fieldPair->zohoFormField));
86 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'field'], 'validation', $error, $entryDetails);
87 return $error;
88 }
89 }
90
91 $contactData = [
92 'lastName' => $fieldData['lastName']
93 ];
94
95 if (array_key_exists('firstName', $fieldData)) {
96 $contactData['firstName'] = $fieldData['firstName'];
97 }
98
99 $contactId = '';
100
101 if (array_key_exists('email', $fieldData)) {
102 $contactData['email'] = $fieldData['email'];
103
104 $contactApiResponse = $this->searchContact($dataCenter, $contactData['email']);
105
106 if ($contactApiResponse) {
107 $contactId = $contactApiResponse->data[0]->id;
108 }
109 }
110
111 if ('' === $contactId) {
112 $contactApiResponse = $this->createContact($dataCenter, wp_json_encode($contactData));
113
114 if (isset($contactApiResponse->errorCode)) {
115 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'contact'], 'error', $contactApiResponse, $entryDetails);
116 } else {
117 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'contact'], 'success', $contactApiResponse, $entryDetails);
118 }
119
120 $contactId = $contactApiResponse->id;
121 }
122
123 $ticketData = $fieldData;
124
125 unset($ticketData['firstName'], $ticketData['lastName']);
126
127 $ticketData['contactId'] = $contactId;
128 $ticketData['departmentId'] = $department;
129 $ticketData['assigneeId'] = $actions->ticket_owner;
130
131 if (!empty($actions->product)) {
132 $ticketData['productId'] = $actions->product;
133 }
134
135 if ($customFieldData) {
136 $ticketData['cf'] = $customFieldData;
137 }
138
139 $ticketApiResponse = $this->insertRecord($dataCenter, wp_json_encode($ticketData));
140
141 if (isset($ticketApiResponse->errorCode)) {
142 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'ticket'], 'error', $ticketApiResponse, $entryDetails);
143 } else {
144 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'ticket'], 'success', $ticketApiResponse, $entryDetails);
145 }
146
147 if (!empty($actions->attachments)) {
148 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $this->_defaultHeader['orgId'], $formID, $entryID);
149 $fileFound = 0;
150 $responseType = 'success';
151 $attachmentApiResponses = [];
152 $attachments = explode(',', $actions->attachments);
153 foreach ($attachments as $fileField) {
154 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
155 $fileFound = 1;
156 if (is_array($fieldValues[$fileField])) {
157 foreach ($fieldValues[$fileField] as $singleFile) {
158 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $ticketApiResponse->id, $dataCenter);
159 if (isset($attachmentApiResponse->errorCode)) {
160 $responseType = 'error';
161 }
162 $attachmentApiResponses[] = $attachmentApiResponse;
163 }
164 } else {
165 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $ticketApiResponse->id, $dataCenter);
166 if (isset($attachmentApiResponse->errorCode)) {
167 $responseType = 'error';
168 }
169 $attachmentApiResponses[] = $attachmentApiResponse;
170 }
171 }
172 }
173
174 if ($fileFound) {
175 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => 'ticket'], $responseType, $attachmentApiResponses, $entryDetails);
176 }
177 }
178
179 return $ticketApiResponse;
180 }
181 }
182