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

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

305 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoCrm Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoCRM;
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 protected $_defaultHeader;
21 protected $_apiDomain;
22 protected $_tokenDetails;
23 protected $_logResponse;
24
25 public function __construct($tokenDetails)
26 {
27 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
28 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
29 $this->_tokenDetails = $tokenDetails;
30 $this->_logResponse = new UtilApiResponse();
31 }
32
33 public function serachRecord($module, $searchCriteria)
34 {
35 $searchRecordEndpoint = "{$this->_apiDomain}/crm/v2/{$module}/search";
36 return HttpHelper::get($searchRecordEndpoint, ['criteria' => "({$searchCriteria})"], $this->_defaultHeader);
37 }
38
39 public function upsertRecord($module, $data)
40 {
41 $insertRecordEndpoint = "{$this->_apiDomain}/crm/v2/{$module}/upsert";
42 $data = \is_string($data) ? $data : wp_json_encode($data);
43 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
44 }
45
46 public function insertRecord($module, $data)
47 {
48 $insertRecordEndpoint = "{$this->_apiDomain}/crm/v2/{$module}";
49 $data = \is_string($data) ? $data : wp_json_encode($data);
50 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
51 }
52
53 public function executeRecordApi($formID, $entryID, $integId, $logID, $defaultConf, $module, $layout, $fieldValues, $fieldMap, $actions, $required, $fileMap = [], $isRelated = false)
54 {
55 $entryDetails = [
56 'formId' => $formID,
57 'entryId' => $entryID,
58 'fieldValues' => $fieldValues
59 ];
60 $fieldData = [];
61 $filesApiHelper = new FilesApiHelper($this->_tokenDetails, $formID, $entryID, $integId, $logID);
62 foreach ($fieldMap as $fieldKey => $fieldPair) {
63 if (!empty($fieldPair->zohoFormField)) {
64 if (empty($defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField})) {
65 continue;
66 }
67 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
68 $fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldPair->customValue, $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField});
69 } else {
70 $fieldData[$fieldPair->zohoFormField] = $this->formatFieldValue($fieldValues[$fieldPair->formField], $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField});
71 }
72 if (empty($fieldData[$fieldPair->zohoFormField]) && \in_array($fieldPair->zohoFormField, $required)) {
73 /* translators: %1$s: field name, %2$s: module name */
74 $error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%1$s is required for zoho crm, %2$s module', 'bit-form'), $fieldPair->zohoFormField, $module));
75 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => 'field'], 'validation', $error, $entryDetails);
76 return $error;
77 }
78
79 if (!empty($fieldData[$fieldPair->zohoFormField])) {
80 $requiredLength = $defaultConf->layouts->{$module}->{$layout}->fields->{$fieldPair->zohoFormField}->length;
81 $currentLength = is_array($fieldData[$fieldPair->zohoFormField]) || is_object($fieldData[$fieldPair->zohoFormField]) ?
82 @count($fieldData[$fieldPair->zohoFormField])
83 : strlen($fieldData[$fieldPair->zohoFormField]);
84 if ($currentLength > $requiredLength) {
85 /* translators: %1$s: field name, %2$s: maximum length, %3$s: given length */
86 $error = new WP_Error('REQ_FIELD_LENGTH_EXCEEDED', wp_sprintf(__('zoho crm field %1$s\'s maximum length is %2$s, Given %3$s', 'bit-form'), $fieldPair->zohoFormField, $requiredLength, $currentLength));
87 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => 'field'], 'validation', $error, $entryDetails);
88 return $error;
89 }
90 }
91 }
92 }
93
94 if (count($fileMap)) {
95 foreach ($fileMap as $fileKey => $filePair) {
96 if (!empty($filePair->zohoFormField)) {
97 if ('fileupload' === $defaultConf->layouts->{$module}->{$layout}->fileUploadFields->{$filePair->zohoFormField}->data_type && !empty($fieldValues[$filePair->formField])) {
98 $files = $fieldValues[$filePair->formField];
99 $fileLength = $defaultConf->layouts->{$module}->{$layout}->fileUploadFields->{$filePair->zohoFormField}->length;
100 if (\is_array($files) && count($files) !== $fileLength) {
101 $files = array_slice($fieldValues[$filePair->formField], 0, $fileLength);
102 }
103 $uploadsIDs = $filesApiHelper->uploadFiles($files);
104 if ($uploadsIDs) {
105 $fieldData[$filePair->zohoFormField] = $uploadsIDs;
106 }
107 }
108 }
109 }
110 }
111 if (!empty($defaultConf->layouts->{$module}->{$layout}->id)) {
112 $fieldData['Layout']['id'] = $defaultConf->layouts->{$module}->{$layout}->id;
113 }
114 if (!empty($actions->gclid) && isset($fieldValues['GCLID'])) {
115 $fieldData['$gclid'] = $fieldValues['GCLID'];
116 }
117 if (!empty($actions->rec_owner)) {
118 $fieldData['Owner']['id'] = $actions->rec_owner;
119 }
120 $requestParams['data'][] = (object) $fieldData;
121 $requestParams['trigger'] = [];
122 if (!empty($actions->workflow)) {
123 $requestParams['trigger'][] = 'workflow';
124 }
125 if (!empty($actions->approval)) {
126 $requestParams['trigger'][] = 'approval';
127 }
128 if (!empty($actions->blueprint)) {
129 $requestParams['trigger'][] = 'blueprint';
130 }
131 if (!empty($actions->assignment_rules)) {
132 $requestParams['lar_id'] = $actions->assignment_rules;
133 }
134 $recordApiResponse = '';
135 if (!empty($actions->upsert) && !empty($actions->upsert->crmField)) {
136 $requestParams['duplicate_check_fields'] = [];
137 if (!empty($actions->upsert)) {
138 $duplicateCheckFields = [];
139 $searchCriteria = '';
140 foreach ($actions->upsert->crmField as $fieldInfo) {
141 if (!empty($fieldInfo->name) && $fieldData[$fieldInfo->name]) {
142 $duplicateCheckFields[] = $fieldInfo->name;
143 if (empty($searchCriteria)) {
144 $searchCriteria .= "({$fieldInfo->name}:equals:{$fieldData[$fieldInfo->name]})";
145 } else {
146 $searchCriteria .= "and({$fieldInfo->name}:equals:{$fieldData[$fieldInfo->name]})";
147 }
148 }
149 }
150 if (isset($actions->upsert->overwrite) && !$actions->upsert->overwrite && !empty($searchCriteria)) {
151 $searchRecordApiResponse = $this->serachRecord($module, $searchCriteria);
152 if (!empty($searchRecordApiResponse) && !empty($searchRecordApiResponse->data)) {
153 $previousData = $searchRecordApiResponse->data[0];
154 foreach ($fieldData as $apiName => $currentValue) {
155 if (!empty($previousData->{$apiName})) {
156 $fieldData[$apiName] = $previousData->{$apiName};
157 }
158 }
159 $requestParams['data'][] = (object) $fieldData;
160 }
161 }
162 $requestParams['duplicate_check_fields'] = $duplicateCheckFields;
163 }
164 $recordApiResponse = $this->upsertRecord($module, (object) $requestParams);
165 } elseif ($isRelated) {
166 $recordApiResponse = $this->insertRecord($module, (object) $requestParams);
167 } else {
168 $recordApiResponse = $this->upsertRecord($module, (object) $requestParams);
169 }
170 if (isset($recordApiResponse->status) && 'error' === $recordApiResponse->status) {
171 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => $module], 'error', $recordApiResponse, $entryDetails);
172 } else {
173 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'record', 'type_name' => $module], 'success', $recordApiResponse, $entryDetails);
174 }
175 if (
176 !empty($recordApiResponse->data)
177 && !empty($recordApiResponse->data[0]->code)
178 && 'SUCCESS' === $recordApiResponse->data[0]->code
179 && !empty($recordApiResponse->data[0]->details->id)
180 ) {
181 if (!empty($actions->tag_rec)) {
182 $tags = '';
183 $tag_rec = \explode(',', $actions->tag_rec);
184 foreach ($tag_rec as $tag) {
185 if (is_string($tag) && '${' === substr($tag, 0, 2) && '}' === $tag[strlen($tag) - 1]) {
186 $tags .= (!empty($tags) ? ',' : '') . $fieldValues[substr($tag, 2, strlen($tag) - 3)];
187 } else {
188 $tags .= (!empty($tags) ? ',' : '') . $tag;
189 }
190 }
191 $tagApiHelper = new TagApiHelper($this->_tokenDetails, $module);
192 $addTagResponse = $tagApiHelper->addTagsSingleRecord($recordApiResponse->data[0]->details->id, $tags);
193 if (isset($addTagResponse->status) && 'error' === $addTagResponse->status) {
194 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'tag', 'type_name' => $module], 'error', $addTagResponse, $entryDetails);
195 } else {
196 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'tag', 'type_name' => $module], 'success', $addTagResponse, $entryDetails);
197 }
198 }
199 if (!empty($actions->attachment)) {
200 $validAttachments = [];
201 $fileFound = 0;
202 $responseType = 'success';
203 $attachmentApiResponses = [];
204 $attachment = explode(',', $actions->attachment);
205 foreach ($attachment as $fileField) {
206 if (isset($fieldValues[$fileField]) && !empty($fieldValues[$fileField])) {
207 $fileFound = 1;
208 if (is_array($fieldValues[$fileField])) {
209 foreach ($fieldValues[$fileField] as $singleFile) {
210 $attachmentApiResponse = $filesApiHelper->uploadFiles($singleFile, true, $module, $recordApiResponse->data[0]->details->id);
211 if (isset($attachmentApiResponse->status) && 'error' === $attachmentApiResponse->status) {
212 $responseType = 'error';
213 }
214 }
215 } else {
216 $attachmentApiResponse = $filesApiHelper->uploadFiles($fieldValues[$fileField], true, $module, $recordApiResponse->data[0]->details->id);
217 if (isset($attachmentApiResponse->status) && 'error' === $attachmentApiResponse->status) {
218 $responseType = 'error';
219 }
220 }
221 }
222 }
223 if ($fileFound) {
224 $this->_logResponse->apiResponse($logID, $integId, ['type' => 'attachment', 'type_name' => $module], $responseType, $attachmentApiResponses, $entryDetails);
225 }
226 }
227 }
228 return $recordApiResponse;
229 }
230
231 public function formatFieldValue($value, $formatSpecs)
232 {
233 if (empty($value)) {
234 return '';
235 }
236
237 switch ($formatSpecs->json_type) {
238 case 'jsonarray':
239 $apiFormat = 'array';
240 break;
241 case 'jsonobject':
242 $apiFormat = 'object';
243 break;
244
245 default:
246 $apiFormat = $formatSpecs->json_type;
247 break;
248 }
249
250 $formatedValue = '';
251 $fieldFormat = gettype($value);
252 if ($fieldFormat === $apiFormat && 'datetime' !== $formatSpecs->data_type) {
253 $formatedValue = $value;
254 } else {
255 if ('array' === $apiFormat || 'object' === $apiFormat) {
256 if ('string' === $fieldFormat) {
257 if (-1 === strpos($value, ',')) {
258 $formatedValue = json_decode($value);
259 } else {
260 $formatedValue = explode(',', $value);
261 }
262 $formatedValue = is_null($formatedValue) && !is_null($value) ? [$value] : $formatedValue;
263 } else {
264 $formatedValue = $value;
265 }
266
267 if ('object' === $apiFormat) {
268 $formatedValue = (object) $formatedValue;
269 }
270 } elseif ('string' === $apiFormat && 'datetime' !== $formatSpecs->data_type) {
271 $formatedValue = !is_string($value) ? wp_json_encode($value) : $value;
272 } elseif ('datetime' === $formatSpecs->data_type) {
273 $dateTimeHelper = new DateTimeHelper();
274 $formatedValue = $dateTimeHelper->getFormated($value, 'Y-m-d\TH:i', wp_timezone(), 'Y-m-d\TH:i:sP', null);
275 } else {
276 $stringyfiedValue = !is_string($value) ? wp_json_encode($value) : $value;
277
278 switch ($apiFormat) {
279 case 'double':
280 $formatedValue = (float) $stringyfiedValue;
281 break;
282
283 case 'boolean':
284 $formatedValue = (bool) $stringyfiedValue;
285 break;
286
287 case 'integer':
288 $formatedValue = (int) $stringyfiedValue;
289 break;
290
291 default:
292 $formatedValue = $stringyfiedValue;
293 break;
294 }
295 }
296 }
297 $formatedValueLenght = 'array' === $apiFormat || 'object' === $apiFormat ? (is_countable($formatedValue) ? \count($formatedValue) : @count($formatedValue)) : \strlen($formatedValue);
298 if ($formatedValueLenght > $formatSpecs->length) {
299 $formatedValue = 'array' === $apiFormat || 'object' === $apiFormat ? array_slice($formatedValue, 0, $formatSpecs->length) : substr($formatedValue, 0, $formatSpecs->length);
300 }
301
302 return $formatedValue;
303 }
304 }
305