PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / IntegrationHandler.php

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

697 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration;
4
5 use BitCode\BitForm\Admin\Form\AdminFormManager;
6 use BitCode\BitForm\Admin\Form\Helpers;
7 use BitCode\BitForm\Core\Database\FormEntryLogModel;
8 use BitCode\BitForm\Core\Database\FormEntryModel;
9 use BitCode\BitForm\Core\Database\IntegrationModel;
10 use BitCode\BitForm\Core\Form\FormManager;
11 use BitCode\BitForm\Core\Util\FieldValueHandler;
12 use BitCode\BitForm\Core\Util\Log;
13 use BitCode\BitForm\Core\Util\MailNotifier;
14 use BitCode\BitForm\Frontend\Form\FrontendFormManager;
15
16 final class IntegrationHandler
17 {
18 private $_formID;
19 private static $_integrationModel;
20 private $_user_details;
21
22 /**
23 * Constructor of Integration Handler
24 *
25 * @param Integer $formID If Integration is accessible globally then
26 * $formID will be 0 and category app
27 * @param Array $user_details Details of user accessing data
28 * @return void
29 */
30 public function __construct($formID, $user_details = null)
31 {
32 $this->_formID = $formID;
33 static::$_integrationModel = new IntegrationModel();
34 $this->_user_details = $user_details;
35 }
36
37 public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null)
38 {
39 $conditions = [
40 'form_id' => $this->_formID,
41 'id' => $integrationID,
42 ];
43 if (!is_null($integrationType)) {
44 $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
45 }
46 if (!is_null($integrationCategory)) {
47 $conditions = array_merge($conditions, ['category' => $integrationCategory]);
48 }
49 return static::$_integrationModel->get(
50 [
51 'id',
52 'integration_name',
53 'integration_type',
54 'integration_details',
55 'status',
56 'form_id',
57 ],
58 $conditions
59 );
60 }
61
62 public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null)
63 {
64 $conditions = [
65 'form_id' => $this->_formID,
66 ];
67 if (!is_null($integrationType)) {
68 $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
69 }
70
71 if (!is_null($integrationCategory)) {
72 $conditions = array_merge($conditions, ['category' => $integrationCategory]);
73 }
74 if (!is_null($status)) {
75 $conditions = array_merge($conditions, ['status' => 1]);
76 }
77 if (!is_null($id)) {
78 $conditions = array_merge($conditions, ['id' => $id]);
79 }
80 return static::$_integrationModel->get(
81 [
82 'id',
83 'form_id',
84 'integration_name',
85 'integration_type',
86 'integration_details',
87 'status',
88 ],
89 $conditions
90 );
91 }
92
93 public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null)
94 {
95 $conditions = [];
96 if (!is_null($integrationType)) {
97 $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
98 }
99
100 if (!is_null($integrationCategory)) {
101 $conditions = array_merge($conditions, ['category' => $integrationCategory]);
102 }
103 if (!is_null($status)) {
104 $conditions = array_merge($conditions, ['status' => 1]);
105 }
106 if (!is_null($id)) {
107 $conditions = array_merge($conditions, ['id' => $id]);
108 }
109 return static::$_integrationModel->get(
110 [
111 'id',
112 'form_id',
113 'integration_name',
114 'integration_type',
115 'integration_details',
116 'status',
117 ],
118 $conditions
119 );
120 }
121
122 public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
123 {
124 if (null === $status) {
125 $status = 1;
126 }
127 return static::$_integrationModel->insert(
128 [
129 'integration_name' => $integrationName,
130 'integration_type' => $integrationType,
131 'integration_details' => $integrationDetails,
132 'category' => $integrationCategory,
133 'form_id' => $this->_formID,
134 'user_id' => $this->_user_details['id'],
135 'user_ip' => $this->_user_details['ip'],
136 'status' => $status,
137 'created_at' => $this->_user_details['time'],
138 'updated_at' => $this->_user_details['time'],
139 ]
140 );
141 }
142
143 public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
144 {
145 if (null === $status) {
146 $status = 1;
147 }
148 return static::$_integrationModel->update(
149 [
150 'integration_name' => $integrationName,
151 'integration_type' => $integrationType,
152 'integration_details' => $integrationDetails,
153 'category' => $integrationCategory,
154 'form_id' => $this->_formID,
155 'user_id' => $this->_user_details['id'],
156 'user_ip' => $this->_user_details['ip'],
157 'status' => $status,
158 'updated_at' => $this->_user_details['time'],
159 ],
160 [
161 'id' => $integrationID,
162 ]
163 );
164 }
165
166 public function updateIntegrationStatus($integrationID, $status)
167 {
168 $data = ['status' => (int) $status];
169 if (!empty($this->_user_details['time'])) {
170 $data['updated_at'] = $this->_user_details['time'];
171 }
172 return static::$_integrationModel->update(
173 $data,
174 [
175 'id' => $integrationID,
176 'form_id' => $this->_formID,
177 ]
178 );
179 }
180
181 public function duplicateAllInAForm($oldFormId)
182 {
183 $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at'];
184 $integDupData = [
185 'integration_name',
186 'integration_type',
187 'integration_details',
188 'category',
189 $this->_formID,
190 $this->_user_details['id'],
191 $this->_user_details['ip'],
192 'status',
193 $this->_user_details['time'],
194 $this->_user_details['time'],
195 ];
196 return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]);
197 }
198
199 public function deleteIntegration($integrationID)
200 {
201 return static::$_integrationModel->delete(
202 [
203 'id' => $integrationID,
204 'form_id' => $this->_formID,
205 ]
206 );
207 }
208
209 public static function replaceFieldWithValue($dataToReplaceField, $fieldValues)
210 {
211 if (empty($dataToReplaceField)) {
212 return false;
213 }
214 if (is_string($dataToReplaceField)) {
215 $dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues);
216 } elseif (is_array($dataToReplaceField)) {
217 foreach ($dataToReplaceField as $field => $value) {
218 if (is_array($value) && 1 === count($value)) {
219 $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues);
220 } elseif (is_array($value)) {
221 $dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues);
222 } else {
223 $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues);
224 }
225 }
226 }
227 return $dataToReplaceField;
228 }
229
230 private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
231 {
232 if (empty($stringToReplaceField)) {
233 return $stringToReplaceField;
234 }
235 $fieldPattern = '/\${\w[^ ${}]*}/';
236 preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
237 $uniqueFieldsInStr = array_unique($matchedField[0]);
238
239 foreach ($uniqueFieldsInStr as $value) {
240 $fieldName = substr($value, 2, strlen($value) - 3);
241
242 $repeaterArr = explode('.', $fieldName);
243 if (isset($fieldValues[$fieldName])) {
244 $stringToReplaceField = is_string($fieldValues[$fieldName])
245 ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField)
246 : wp_json_encode($fieldValues[$fieldName]);
247 } elseif (2 === count($repeaterArr) && isset($fieldValues[$repeaterArr[0]])) {
248 $repeaterValues = [];
249 $repeaterFieldValues = $fieldValues[$repeaterArr[0]];
250 foreach ($repeaterFieldValues as $value) {
251 if (isset($value[$repeaterArr[1]])) {
252 $repeaterValues[] = $value[$repeaterArr[1]];
253 }
254 }
255
256 $stringToReplaceField = $repeaterValues;
257 } else {
258 $stringToReplaceField = FieldValueHandler::replaceSmartTagWithValue($value);
259 }
260 }
261 return $stringToReplaceField;
262 }
263
264 public static function updatedHiddenFieldValue($formID)
265 {
266 $hiddenFields = [];
267 $frontendFormManger = FrontendFormManager::getInstance($formID);
268
269 if ($frontendFormManger->isHoneypotActive()) {
270 $str = '_bitform' . '_' . $formID . '_' . time() . '_';
271 $honpotToken = Helpers::honeypotEncryptedToken($str);
272 $hiddenFields[] = [
273 'name' => 'b_h_t',
274 'value' => $honpotToken
275 ];
276 }
277 $csrfTokens = Helpers::csrfEecrypted();
278 $hiddenFields[] = [
279 'name' => 't_identity',
280 'value' => $csrfTokens['t_identity'],
281 ];
282 $hiddenFields[] = [
283 'name' => 'csrf',
284 'value' => $csrfTokens['csrf']
285 ];
286 return $hiddenFields;
287 }
288
289 public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true)
290 {
291 $responseData = [];
292 $entryId = $workFlowReturnedData['triggerData']['entryID'];
293 $responseData['entry_id'] = $entryId;
294 if (isset($workFlowReturnedData['message'])) {
295 $formID = $workFlowReturnedData['triggerData']['formID'];
296 $responseData['message'] = $workFlowReturnedData['message'];
297 $responseData['hidden_fields'] = self::updatedHiddenFieldValue($formID);
298 $responseData['msg_id'] = isset($workFlowReturnedData['msg_id']) ? $workFlowReturnedData['msg_id'] : 0;
299 if (isset($workFlowReturnedData['msg_duration'])) {
300 $responseData['msg_duration'] = $workFlowReturnedData['msg_duration'];
301 }
302 if (isset($workFlowReturnedData['afterSubmit'])) {
303 $responseData['afterSubmit'] = $workFlowReturnedData['afterSubmit'];
304 }
305 }
306 if (isset($workFlowReturnedData['redirectPage'])) {
307 $responseData['redirectPage'] = $workFlowReturnedData['redirectPage'];
308 }
309 if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) {
310 $responseData['updatedData'] = $workFlowReturnedData['updatedData'];
311 }
312 if (isset($workFlowReturnedData['new_nonce'])) {
313 $responseData['new_nonce'] = $workFlowReturnedData['new_nonce'];
314 }
315 if (!isset($workFlowReturnedData['triggerData'])) {
316 return $responseData;
317 }
318
319 $triggerData = $workFlowReturnedData['triggerData'];
320
321 $trnasientData = get_transient("bitform_trigger_transient_{$entryId}");
322 $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData;
323
324 if (!empty($trnasientData['fields'])) {
325 $fieldData = isset($workFlowReturnedData['fields']) ? $workFlowReturnedData['fields'] : $workFlowReturnedData['updatedData'];
326 $workFlowReturnedData['fields'] = array_merge($fieldData, $trnasientData['fields']);
327 }
328
329 if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) {
330 if (!headers_sent()) {
331 header('Connection: close');
332 $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html';
333 header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset'));
334 status_header(200);
335 if (!$isFormRequest) {
336 $response = [
337 'status' => 200,
338 'code' => 4000,
339 'message' => 'Data Added Successfully!!',
340 'success' => true
341 ];
342 } else {
343 $response = [
344 'success' => true,
345 'data' => $responseData,
346 ];
347 }
348 }
349 ob_start();
350 echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : esc_html($workFlowReturnedData['message']);
351 ob_end_flush();
352 flush();
353 session_write_close();
354
355 if (isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
356 $entryModel = new FormEntryModel();
357 $updatedStatus = $entryModel->update(
358 [
359 'status' => 2,
360 ],
361 [
362 'form_id' => $triggerData['formID'],
363 'id' => $triggerData['entryID'],
364 ]
365 );
366
367 $triggerData['fields'] = $workFlowReturnedData['fields'];
368 if (!is_wp_error($updatedStatus)) {
369 if ($workFlowReturnedData['dflt_template']) {
370 // $triggerData['fields'] = $workFlowReturnedData['fields'];
371 do_action('bitform_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData);
372 } elseif (isset($triggerData['dblOptin'])) {
373 foreach ($triggerData['dblOptin'] as $value) {
374 try {
375 MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']);
376 } catch (\Throwable $mailError) {
377 // response already flushed — a mail failure here must not kill the rest of the run
378 Log::debug_log('[+] Double opt-in mail failed: ' . $mailError->getMessage());
379 }
380 }
381 }
382 }
383 if (function_exists('fastcgi_finish_request')) {
384 fastcgi_finish_request();
385 }
386
387 if (defined('REST_REQUEST') || wp_doing_ajax()) {
388 die;
389 }
390 return $workFlowReturnedData;
391 }
392 if (isset($triggerData['mail']) && !empty($triggerData['mail'])) {
393 $formManager = new AdminFormManager($triggerData['formID']);
394 $formContent = $formManager->getFormContent();
395 $submitted_fields = $formContent->fields;
396 $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']);
397 foreach ($triggerData['mail'] as $value) {
398 try {
399 MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID'], false, $triggerData['logID']);
400 } catch (\Throwable $mailError) {
401 // response already flushed — a mail failure here must not skip the integrations below
402 Log::debug_log('[+] Mail notification failed: ' . $mailError->getMessage());
403 }
404 }
405 }
406 do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
407
408 if (function_exists('fastcgi_finish_request')) {
409 fastcgi_finish_request();
410 }
411
412 if (defined('REST_REQUEST') || wp_doing_ajax()) {
413 die;
414 }
415 return $workFlowReturnedData;
416 }
417
418 $entryID = $triggerData['entryID'];
419 if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && empty($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) {
420 $eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
421 $scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
422 $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}");
423 } elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
424 $triggerData['fields'] = $workFlowReturnedData['fields'];
425 if (!$workFlowReturnedData['integrationRun'] && isset($workFlowReturnedData['integrationDetails'])) {
426 $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails'];
427 $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template'];
428 $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun'];
429 }
430
431 // Generate one-time trigger token for security
432 $triggerToken = wp_hash($triggerData['entryID'] . $triggerData['logID'] . time() . wp_rand(), 'nonce');
433 $triggerData['trigger_token'] = $triggerToken;
434
435 set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS);
436 $queuedAt = current_time('mysql');
437 $entryLog = new FormEntryLogModel();
438 $queueuEntry = $entryLog->log_history_insert(
439 [
440 'log_id' => $triggerData['logID'],
441 'integration_id' => 0,
442 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]),
443 'response_type' => 'queued',
444 // 'trigger' is a durable copy of the transient payload: object-cache hosts can
445 // evict the transient (and it expires after 1h), which would otherwise lose the run
446 'response_obj' => wp_json_encode(['status' => 'queued', 'queued_at' => $queuedAt, 'trigger' => $triggerData]),
447 'created_at' => $queuedAt,
448 ]
449 );
450 if (\is_int($queueuEntry)) {
451 // Safety net: the browser-fired bitforms_trigger_workflow request can be lost
452 // (navigation abort, WAF, network drop). This event re-runs the queued workflow;
453 // it no-ops if the browser trigger already claimed the run.
454 wp_schedule_single_event(
455 time() + 2 * MINUTE_IN_SECONDS,
456 'bitforms_reclaim_workflow',
457 [(int) $entryID, (int) $triggerData['formID'], (int) $triggerData['logID'], $queueuEntry]
458 );
459 if (!wp_next_scheduled('bitforms_reclaim_sweep')) {
460 wp_schedule_event(time() + HOUR_IN_SECONDS, 'hourly', 'bitforms_reclaim_sweep');
461 }
462 }
463 $responseData['cronNotOk'] = [
464 $triggerData['entryID'],
465 $triggerData['logID'],
466 $queueuEntry,
467 $triggerToken, // Add one-time token as 4th element
468 ];
469 }
470 return $responseData;
471 }
472
473 public static function repeaterFieldValueFormatter($fieldValues)
474 {
475 $formattedArray = [];
476
477 foreach ($fieldValues as $key => $value) {
478 if (is_array($value)) {
479 foreach ($value as $index => $v) {
480 if (is_array($v)) {
481 foreach ($v as $k1 => $v1) {
482 $formattedArray[$k1][] = $v1;
483 }
484 } else {
485 $formattedArray[$key][] = $v;
486 }
487 }
488 } else {
489 $formattedArray[$key] = $value;
490 }
491 }
492
493 return $formattedArray;
494 }
495
496 /**
497 *
498 * @param mixed $fieldValues
499 * @param mixed $returnRepeaterValue default is array
500 * @param mixed $formID form ID required for string formatting
501 * @return array and repeater field value as array or string
502 */
503 public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '', $formID = null)
504 {
505 // get the repeater field and store it in an array
506 $repeaterField = [];
507 foreach ($fieldValues as $key => $value) {
508 if (!preg_match('/\./', $key)) {
509 continue;
510 }
511 $repeaterField[] = $key;
512 }
513
514 // put the value of the child key to the parent repeater field\
515 foreach ($repeaterField as $key) {
516 list($parentKey, $childKey) = explode('.', $key);
517 $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key;
518
519 if (!is_array($repeatValues)) {
520 $fieldValues[$key] = $fieldValues[$childKey];
521 continue;
522 }
523
524 foreach ($repeatValues as $value) {
525 if (isset($value[$childKey])) {
526 if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) {
527 $fieldValues[$key] = [];
528 }
529 if (!empty($value[$childKey])) {
530 $fieldValues[$key][] = $value[$childKey];
531 }
532 }
533 }
534 }
535
536 // convert the array to string repeater field value
537 if ('string' === $returnRepeaterValue) {
538 $form = new FormManager($formID);
539 foreach ($repeaterField as $key) {
540 if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) {
541 $fieldValues[$key] = implode(', ', self::flattenArray($fieldValues[$key]));
542 }
543 }
544
545 foreach ($fieldValues as $key=>$value) {
546 if (!$form->isRepeaterField($key)) {
547 continue;
548 }
549 $repeaterValues = $fieldValues[$key];
550
551 $newFieldValues = self::constructRepeaterValue($repeaterValues, $formID);
552
553 $fieldValues[$key] = is_array($newFieldValues) ? json_encode(array_values($newFieldValues), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $fieldValues[$key];
554 }
555 }
556
557 return $fieldValues;
558 }
559
560 private static function flattenArray($array)
561 {
562 return array_map(function ($val) {
563 if (is_array($val)) {
564 return count($val) > 1 ? ('[' . implode(', ') . ']') : implode(', ', $val);
565 } else {
566 return $val;
567 }
568 }, $array);
569 }
570
571 /**
572 * Replace file field value with web url
573 * @param mixed $fieldValues
574 * @param number $formId
575 * @param number $entryId
576 * @return mixed $fieldValues
577 */
578 public static function handleFileUrl($fieldValues, $formId, $entryId)
579 {
580 if (!$formId || !$entryId) {
581 return $fieldValues;
582 }
583 $form = FormManager::getInstance($formId);
584 $formFields = $form->getFields();
585
586 $fldsIncludeFile = ['file-up', 'advanced-file-up', 'repeater', 'signature'];
587 foreach ($fieldValues as $key =>$value) {
588 $fldTyp = $formFields[$key]['type'] ?? null;
589 if (!in_array($fldTyp, $fldsIncludeFile)) {
590 continue;
591 }
592
593 if ('repeater' === $fldTyp && is_array($value)) {
594 foreach ($value as $rIndex => $rValue) {
595 foreach ($rValue as $subKey=>$subValue) {
596 if (!in_array($formFields[$subKey]['type'], $fldsIncludeFile)) {
597 continue;
598 }
599 // for file fields inside repeater
600 if (is_array($subValue)) {
601 foreach ($subValue as $fileIndex=>$fileValue) {
602 $fieldValues[$key][$rIndex][$subKey][$fileIndex] = self::constructFileUrl($formId, $entryId, $fileValue);
603 }
604 } else {
605 // for signature field inside repeater;
606 $fieldValues[$key][$rIndex][$subKey] = self::constructFileUrl($formId, $entryId, $subValue);
607 }
608 }
609 }
610 }
611
612 if (in_array($fldTyp, ['file-up', 'advanced-file-up', 'signature'], true)) {
613 if (is_array($value)) {
614 foreach ($value as $fldIndex=>$fldValue) {
615 $fieldValues[$key][$fldIndex] = self::constructFileUrl($formId, $entryId, $fldValue);
616 }
617 } else {
618 $fieldValues[$key] = self::constructFileUrl($formId, $entryId, $value);
619 }
620 }
621 }
622
623 return $fieldValues;
624 }
625
626 public static function assignRepeaterFieldValue($fieldValues, $formId)
627 {
628 if (!$formId) {
629 return $fieldValues;
630 }
631 $form = new FormManager($formId);
632 $formFields = $form->getFields();
633
634 foreach ($fieldValues as $key=>$value) {
635 if (!$form->isRepeaterField($key)) {
636 continue;
637 }
638
639 if ('repeater' === $formFields[$key]['type']) {
640 if (is_array($value)) {
641 foreach ($value as $rIndex=>$rValue) {
642 foreach ($rValue as $subKey =>$subValue) {
643 $fieldValues[$subKey] = $subValue;
644 }
645 }
646 }
647 }
648 }
649
650 return $fieldValues;
651 }
652
653 public static function constructRepeaterValue($repeaterValues, $formId)
654 {
655 if (!$formId || !is_array($repeaterValues)) {
656 return $repeaterValues;
657 }
658 $form = FormManager::getInstance($formId);
659 $formFields = $form->getFields();
660 $newRptrValue = [];
661 foreach ($repeaterValues as $key=>$value) {
662 $newRepeatedValue = [];
663 foreach ($value as $rKey =>$rValue) {
664 $fldLbl = isset($formFields[$rKey]) ? $formFields[$rKey]['label'] : $rKey;
665 $newRepeatedValue[$fldLbl] = $rValue;
666 }
667 $newRptrValue[$key] = $newRepeatedValue;
668 }
669
670 return $newRptrValue;
671 }
672
673 private static function constructFileUrl($formId, $entryId, $fileName)
674 {
675 $webPath = Helpers::getWebPathWithEncryptedEntryId($formId, $entryId);
676
677 return rtrim($webPath, '/') . '/' . ltrim($fileName, '/');
678 }
679
680 // private static function constructFileUrl($formId, $entryId, $fileName)
681 // {
682 // $restBaseUrl = get_rest_url();
683 // if (!$restBaseUrl) {
684 // return $fileName;
685 // }
686 // //http://bitlocal.local/wp-json/bitform/v1/bitform-file-download/?formID=1&entryID=16&fileID=bit-form-1005.pdf
687 // $queryParam = build_query([
688 // 'formID' => $formId,
689 // 'entryID' => $entryId,
690 // 'fileID' => $fileName
691 // ]);
692
693 // $path = $restBaseUrl . 'bitform' . '/' . 'v1' . '/' . 'bitform-file-download' . '/' . '?' . $queryParam;
694 // return rtrim($path, '/');
695 // }
696 }
697