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 / ZohoDesk / 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/ZohoDesk/RecordApiHelper.php

176 lines 6.1 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 $fieldData = [];
64 $customFieldData = [];
65 foreach ($fieldMap as $fieldKey => $fieldPair) {
66 if (!empty($fieldPair->zohoFormField)) {
67 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
68 $fieldData[$fieldPair->zohoFormField] = $fieldPair->customValue;
69 } else {
70 if ('cf' === strtok($fieldPair->zohoFormField, '_')) {
71 $customFieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField];
72 } else {
73 $fieldData[$fieldPair->zohoFormField] = $fieldValues[$fieldPair->formField];
74 }
75 }
76 }
77
78 if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) {
79 $error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%s is required for zoho bigin', 'bit-form'), $fieldPair->zohoFormField));
80 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'field'], 'validation', $error);
81 return $error;
82 }
83 }
84
85 $contactData = [
86 'lastName' => $fieldData['lastName']
87 ];
88
89 if (array_key_exists('firstName', $fieldData)) {
90 $contactData['firstName'] = $fieldData['firstName'];
91 }
92
93 $contactId = '';
94
95 if (array_key_exists('email', $fieldData)) {
96 $contactData['email'] = $fieldData['email'];
97
98 $contactApiResponse = $this->searchContact($dataCenter, $contactData['email']);
99
100 if ($contactApiResponse) {
101 $contactId = $contactApiResponse->data[0]->id;
102 }
103 }
104
105 if ('' === $contactId) {
106 $contactApiResponse = $this->createContact($dataCenter, wp_json_encode($contactData));
107
108 if (isset($contactApiResponse->errorCode)) {
109 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'contact'], 'error', $contactApiResponse);
110 } else {
111 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'contact'], 'success', $contactApiResponse);
112 }
113
114 $contactId = $contactApiResponse->id;
115 }
116
117 $ticketData = $fieldData;
118
119 unset($ticketData['firstName'], $ticketData['lastName']);
120
121 $ticketData['contactId'] = $contactId;
122 $ticketData['departmentId'] = $department;
123 $ticketData['assigneeId'] = $actions->ticket_owner;
124
125 if (!empty($actions->product)) {
126 $ticketData['productId'] = $actions->product;
127 }
128
129 if ($customFieldData) {
130 $ticketData['cf'] = $customFieldData;
131 }
132
133 $ticketApiResponse = $this->insertRecord($dataCenter, wp_json_encode($ticketData));
134
135 if (isset($ticketApiResponse->errorCode)) {
136 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'ticket'], 'error', $ticketApiResponse);
137 } else {
138 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'ticket'], 'success', $ticketApiResponse);
139 }
140
141 if (!empty($actions->attachments)) {
142 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $this->_defaultHeader['orgId'], $formID, $entryID);
143 $fileFound = 0;
144 $responseType = 'success';
145 $attachmentApiResponses = [];
146 $attachments = explode(',', $actions->attachments);
147 foreach ($attachments as $fileField) {
148 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
149 $fileFound = 1;
150 if (is_array($fieldValues[$fileField])) {
151 foreach ($fieldValues[$fileField] as $singleFile) {
152 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $ticketApiResponse->id, $dataCenter);
153 if (isset($attachmentApiResponse->errorCode)) {
154 $responseType = 'error';
155 }
156 $attachmentApiResponses[] = $attachmentApiResponse;
157 }
158 } else {
159 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $ticketApiResponse->id, $dataCenter);
160 if (isset($attachmentApiResponse->errorCode)) {
161 $responseType = 'error';
162 }
163 $attachmentApiResponses[] = $attachmentApiResponse;
164 }
165 }
166 }
167
168 if ($fileFound) {
169 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'file', 'type_name' => 'ticket'], $responseType, $attachmentApiResponses);
170 }
171 }
172
173 return $ticketApiResponse;
174 }
175 }
176