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

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

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