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
V3.3.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 All 139 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 2.0, at includes/Core/Integration/ZohoBigin/RecordApiHelper.php

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