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

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