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

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

249 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoBigin Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoBigin;
9
10 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
11 use BitCode\BitForm\Core\Util\DateTimeHelper;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13 use WP_Error;
14
15 /**
16 * Provide functionality for Record insert,upsert
17 */
18 class RecordApiHelper
19 {
20 private $_defaultHeader;
21 private $_apiDomain;
22 private $_tokenDetails;
23
24 private $_integID;
25
26 private $_logID;
27
28 private $_logResponse;
29
30 public function __construct($tokenDetails, $integID, $logID)
31 {
32 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
33 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
34 $this->_tokenDetails = $tokenDetails;
35 $this->_integID = $integID;
36 $this->_logID = $logID;
37 $this->_logResponse = new UtilApiResponse();
38 }
39
40 public function insertRecord($module, $data)
41 {
42 $insertRecordEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}";
43 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
44 }
45
46 private function insertNote($module, $recordId, $data)
47 {
48 $insertRecordEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordId}/Notes";
49 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
50 }
51
52 public function executeRecordApi($formID, $entryID, $defaultConf, $module, $fieldValues, $fieldMap, $actions, $required)
53 {
54 $entryDetails = [
55 'formId' => $formID,
56 'entryId' => $entryID,
57 'fieldValues' => $fieldValues
58 ];
59
60 $fieldData = [];
61 foreach ($fieldMap as $fieldKey => $fieldPair) {
62 if (!empty($fieldPair->zohoFormField)) {
63 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
64 $fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldPair->customValue, $defaultConf->moduleData->{$module}->fields->{$fieldPair->zohoFormField});
65 } else {
66 $fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldValues[$fieldPair->formField], $defaultConf->moduleData->{$module}->fields->{$fieldPair->zohoFormField});
67 }
68
69 if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) {
70 /* translators: %1$s: field name, %2$s: Zoho Bigin module name. */
71 $error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%1$s is required for zoho bigin, %2$s module', 'bit-form'), $fieldPair->zohoFormField, $module));
72 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'record', 'type_name' => 'field'], 'validation', $error, $entryDetails);
73 return $error;
74 }
75 }
76 }
77
78 $requestParams['data'][] = (object) $fieldData;
79 $requestParams['trigger'] = [];
80
81 if (!empty($actions->workflow)) {
82 $requestParams['trigger'][] = 'workflow';
83 }
84 if (!empty($actions->approval)) {
85 $requestParams['trigger'][] = 'approval';
86 }
87
88 $recordApiResponse = $this->insertRecord($module, wp_json_encode($requestParams));
89 if ((isset($recordApiResponse->status) && 'error' === $recordApiResponse->status) || 'error' === $recordApiResponse->data[0]->status) {
90 return $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'record', 'type_name' => $module], 'error', $recordApiResponse, $entryDetails);
91 } else {
92 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'record', 'type_name' => $module], 'success', $recordApiResponse, $entryDetails);
93 }
94 $recordID = 0;
95 if (!empty($recordApiResponse->data[0]->details->id)) {
96 $recordID = $recordApiResponse->data[0]->details->id;
97 if (isset($actions->note)) {
98 $note_title = $actions->note->title ? $actions->note->title : '';
99 $note_content = $actions->note->content ? $actions->note->content : '';
100 $note = (object) [
101 'Note_Title' => $note_title,
102 'Note_Content' => $note_content,
103 'Parent_Id' => $recordID,
104 'se_module' => $module
105 ];
106 $requestParams['data'][] = $note;
107
108 $noteApiResponse = $this->insertNote($module, $recordID, wp_json_encode($requestParams));
109 if (isset($noteApiResponse->status) && 'error' === $noteApiResponse->status) {
110 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'note', 'type_name' => $module], 'error', $noteApiResponse, $entryDetails);
111 } else {
112 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'note', 'type_name' => $module], 'success', $noteApiResponse, $entryDetails);
113 }
114 }
115 }
116
117 // Attachments
118 if (isset($actions->attachments)) {
119 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID);
120 $attachments = explode(',', $actions->attachments);
121 $fileFound = 0;
122 $responseType = 'success';
123 $attachmentApiResponses = [];
124 foreach ($attachments as $fileField) {
125 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
126 $fileFound = 1;
127 if (is_array($fieldValues[$fileField])) {
128 foreach ($fieldValues[$fileField] as $singleFile) {
129 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, $module, $recordID);
130 if (isset($attachmentApiResponse->status) && 'error' === $attachmentApiResponse->status) {
131 $responseType = 'error';
132 }
133 $attachmentApiResponses[] = $attachmentApiResponse;
134 }
135 } else {
136 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], $module, $recordID);
137 if (isset($attachmentApiResponse->status) && 'error' === $attachmentApiResponse->status) {
138 $responseType = 'error';
139 }
140 $attachmentApiResponses[] = $attachmentApiResponse;
141 }
142 }
143 }
144 if ($fileFound) {
145 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'file', 'type_name' => $module], $responseType, $attachmentApiResponses, $entryDetails);
146 }
147 }
148
149 if (isset($actions->photo)) {
150 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID);
151 if (isset($fieldValues[$actions->photo])) {
152 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$actions->photo], $module, $recordID, true);
153 if (isset($attachmentApiResponse->status) && 'error' === $attachmentApiResponse->status) {
154 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'photo', 'type_name' => $module], 'error', $attachmentApiResponse, $entryDetails);
155 } else {
156 $this->_logResponse->apiResponse($this->_logID, $this->_integID, ['type' => 'photo', 'type_name' => $module], 'success', $attachmentApiResponse, $entryDetails);
157 }
158 }
159 }
160
161 return $recordApiResponse;
162 }
163
164 public function formatFieldValue($value, $formatSpecs)
165 {
166 if (empty($value)) {
167 return '';
168 }
169
170 switch ($formatSpecs->data_type) {
171 case 'AutoNumber':
172 $apiFormat = 'integer';
173 break;
174
175 case 'Text':
176 case 'Pick list':
177 case 'Lookup':
178 case 'Email':
179 case 'Website':
180 case 'Currency':
181 case 'TextArea':
182 $apiFormat = 'string';
183 break;
184
185 case 'Date':
186 $apiFormat = 'date';
187 break;
188
189 case 'DateTime':
190 $apiFormat = 'datetime';
191 break;
192
193 case 'Double':
194 $apiFormat = 'double';
195 break;
196
197 case 'Boolean':
198 $apiFormat = 'boolean';
199 break;
200
201 default:
202 $apiFormat = $formatSpecs->data_type;
203 break;
204 }
205
206 $formatedValue = '';
207 $fieldFormat = gettype($value);
208 if ($fieldFormat === $apiFormat && 'datetime' !== $formatSpecs->data_type) {
209 $formatedValue = $value;
210 } else {
211 if ('string' === $apiFormat && 'datetime' !== $formatSpecs->data_type) {
212 $formatedValue = !is_string($value) ? wp_json_encode($value) : $value;
213 } elseif ('datetime' === $apiFormat) {
214 $dateTimeHelper = new DateTimeHelper();
215 $formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d\TH:i', DateTimeHelper::wp_timezone(), 'Y-m-d H:i:s', null);
216 } elseif ('date' === $apiFormat) {
217 $dateTimeHelper = new DateTimeHelper();
218 $formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d', DateTimeHelper::wp_timezone(), 'm/d/Y', null);
219 } else {
220 $stringyfiedValue = !is_string($value) ? wp_json_encode($value) : $value;
221
222 switch ($apiFormat) {
223 case 'double':
224 $formatedValue = (float) $stringyfiedValue;
225 break;
226
227 case 'boolean':
228 $formatedValue = (bool) $stringyfiedValue;
229 break;
230
231 case 'integer':
232 $formatedValue = (int) $stringyfiedValue;
233 break;
234
235 default:
236 $formatedValue = $stringyfiedValue;
237 break;
238 }
239 }
240 }
241 $formatedValueLenght = 'array' === $apiFormat || 'object' === $apiFormat ? (is_countable($formatedValue) ? \count($formatedValue) : @count($formatedValue)) : \strlen($formatedValue);
242 if ($formatedValueLenght > $formatSpecs->length) {
243 $formatedValue = 'array' === $apiFormat || 'object' === $apiFormat ? array_slice($formatedValue, 0, $formatSpecs->length) : substr($formatedValue, 0, $formatSpecs->length);
244 }
245
246 return $formatedValue;
247 }
248 }
249