PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.7.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.7.0
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
← All changes | includes/Admin/Form/AdminFormHandler.php +506 -304 2.02.7.0 View file →
@@ -18,27 +18,32 @@
18 18 use BitCode\BitForm\Core\Database\SuccessMessageModel;
19 19 use BitCode\BitForm\Core\Database\WorkFlowModel;
20 20 use BitCode\BitForm\Core\Integration\IntegrationHandler;
21 21 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
22 +use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
22 23 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
23 24 use BitCode\BitForm\Core\Util\FileHandler;
24 25 use BitCode\BitForm\Core\Util\FormDuplicateHelper;
25 26 use BitCode\BitForm\Core\Util\HttpHelper;
26 27 use BitCode\BitForm\Core\Util\IpTool;
28 +use BitCode\BitForm\Core\WorkFlow\WorkFlow;
27 29 use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler;
28 -use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
30 +use BitCode\BitFormPro\Admin\AppSetting\Pdf;
29 31 use WP_Error;
30 32
31 -class AdminFormHandler {
33 +class AdminFormHandler
34 +{
32 35 private static $formModel;
33 36 private static $ipTool;
34 37
35 - public function __construct() {
38 + public function __construct()
39 + {
36 40 static::$formModel = new FormModel();
37 41 static::$ipTool = new IpTool();
38 42 }
39 43
40 - public function getTemplate($Request, $post) {
44 + public function getTemplate($Request, $post)
45 + {
41 46 $templateName = null;
42 47 $newFormId = null;
43 48 if (isset($post->template)) {
44 49 $templateName = $post->template;
@@ -58,9 +63,10 @@
58 63 );
59 64 }
60 65 }
61 66
62 - public function saveStyleSheet($styles, $sheetName) {
67 + public function saveStyleSheet($styles, $sheetName)
68 + {
63 69 $this->createFormStylesDirIfNotExists();
64 70
65 71 if (
66 72 file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
@@ -73,9 +79,10 @@
73 79 }
74 80 return false;
75 81 }
76 82
77 - public function saveLayoutStyleSheet($layout, $sheetName, $lay_row_height = 43) {
83 + public function saveLayoutStyleSheet($layout, $sheetName, $lay_row_height = 43)
84 + {
78 85 $md_width = 600;
79 86 $sm_width = 400;
80 87 $lg_styles = '';
81 88 $md_styles = '';
@@ -155,9 +162,10 @@
155 162 }
156 163 return false;
157 164 }
158 165
159 - public function createFormStylesDirIfNotExists() {
166 + public function createFormStylesDirIfNotExists()
167 + {
160 168 if (!file_exists(BITFORMS_UPLOAD_DIR)) {
161 169 wp_mkdir_p(BITFORMS_UPLOAD_DIR);
162 170 }
163 171 if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) {
@@ -164,9 +172,19 @@
164 172 wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles');
165 173 }
166 174 }
167 175
168 - public function createNewForm($Request, $post) {
176 + private function pdfFontDownload($fontName)
177 + {
178 + if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
179 + $pdf = new Pdf();
180 + $pdf->fontsDownload($fontName);
181 + }
182 + return false;
183 + }
184 +
185 + public function createNewForm($Request, $post)
186 + {
169 187 // wp_send_json_success($post);
170 188 // exit;
171 189 if (!isset($post->form_id) || '' === $post->form_id) {
172 190 return new WP_Error('empty_form', __('Error Occurred, Please Reload', 'bit-form'));
@@ -197,9 +215,12 @@
197 215 $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
198 216 }
199 217
200 218 $fields = wp_unslash($post->fields);
219 + // $fields = $post->fields;
201 220 $layout = wp_unslash($post->layout);
221 + $nestedLayout = wp_unslash($post->nestedLayout);
222 + $formInfo = wp_unslash($post->formInfo);
202 223 $form_name = wp_unslash($post->form_name);
203 224 $formSettings = (object) wp_unslash($post->formSettings);
204 225 $atomic_class_map = isset($post->atomicClassMap) ? wp_unslash($post->atomicClassMap) : [];
205 226 $breakpointSize = wp_unslash($post->breakpointSize);
@@ -212,8 +233,9 @@
212 233 $reports = !empty($post->reports) ? $post->reports : (object) [];
213 234 $builderSettings = (object) isset($post->builderSettings) ? wp_unslash($post->builderSettings) : [];
214 235
215 236 $mailTem = $formSettings->mailTem;
237 + $pdfTem = $formSettings->pdfTem;
216 238 $integrations = $formSettings->integrations;
217 239
218 240 $user_details = static::$ipTool->getUserDetail();
219 241 if (empty($fields) || empty($layout)) {
@@ -228,10 +250,12 @@
228 250 __('Form Name Should Be Within 50 Characters', 'bit-form')
229 251 );
230 252 }
231 253 $form_content = [
232 - 'fields' => $fields,
233 - 'layout' => $layout,
254 + 'fields' => $fields,
255 + 'layout' => $layout,
256 + 'nestedLayout' => $nestedLayout,
257 + 'formInfo' => $formInfo,
234 258 ];
235 259
236 260 /* if (!empty($formSettings->submitBtn)) {
237 261 $form_content = array_merge($form_content, array('buttons' => $formSettings->submitBtn));
@@ -243,16 +267,18 @@
243 267 if (!empty($additional)) {
244 268 $form_content = array_merge($form_content, ['additional' => $additional]);
245 269 }
246 270 if (!empty($workFlows)) {
247 - $workFlowsStr = is_string($workFlows) ? $workFlows : json_encode($workFlows);
271 + $workflows = is_string($workFlows) ? json_decode($workFlows) : $workFlows;
248 272 $workFlowExist = [];
249 - if (\strpos($workFlowsStr, 'onload')) {
250 - $workFlowExist['onload'] = true;
273 + foreach ($workflows as $index => $workFlow) {
274 + if (in_array($workFlow->action_type, ['always', 'onload'])) {
275 + $workFlowExist['onload'] = true;
276 + }
277 + if ('oninput' === $workFlow->action_type || ('always' === $workFlow->action_type && 'cond' === $workFlow->action_behaviour)) {
278 + $workFlowExist['oninput'] = true;
279 + }
251 280 }
252 - if (\strpos($workFlowsStr, 'oninput')) {
253 - $workFlowExist['oninput'] = true;
254 - }
255 281 $form_content = array_merge($form_content, ['workFlowExist' => $workFlowExist]);
256 282 }
257 283 $form_content = \wp_json_encode($form_content);
258 284
@@ -311,11 +337,13 @@
311 337 'themeColors' => $themeColors,
312 338 'builderSettings' => $builderSettings
313 339 ];
314 340 $update_status = static::$formModel->update(
315 - ['builder_helper_state' => \wp_json_encode($builder_helper_state),
316 - 'atomic_class_map' => $atomic_class_map, ],
317 341 [
342 + 'builder_helper_state' => \wp_json_encode($builder_helper_state),
343 + 'atomic_class_map' => $atomic_class_map,
344 + ],
345 + [
318 346 'id' => $formID,
319 347 ]
320 348 );
321 349 if (is_wp_error($update_status)) {
@@ -337,8 +365,24 @@
337 365 }
338 366 }
339 367 }
340 368 // Email Template [end] */
369 + // if (!empty($pdfTem)) {
370 + // $pdfTemplateHandler = new PdfTemplateHandler($formID);
371 + // foreach ($pdfTem as $templateKey => $templateDetail) {
372 + // $savedID = $pdfTemplateHandler->save($templateDetail);
373 +
374 + // $pdfSetting = $templateDetail->setting;
375 +
376 + // if( isset($pdfSetting->font->name) && !empty($pdfSetting->font->name)) {
377 + // $this->pdfFontDownload($pdfSetting->font->name);
378 + // }
379 + // // if ($savedID) {
380 + // // $tempIdx = empty($templateDetail->id) ? $templateKey : \json_encode(['id' => $templateDetail->id]);
381 + // // $integartionIDForWorkflow['mailNotify'][$tempIdx] = $savedID;
382 + // // }
383 + // }
384 + // }
341 385 //Integration [start] */
342 386 $integrationHandler = new IntegrationHandler($formID, $user_details);
343 387 if (!empty($formSettings->confirmation->type)) {
344 388 $allIntegrations = $formSettings->confirmation->type;
@@ -434,20 +478,11 @@
434 478 return $updated_data;
435 479 }
436 480 }
437 481
438 - public function updateForm($Request, $post) {
439 - // wp_send_json_success($post);
440 - if ($post->formStyle) {
441 - $sheetName = 'bitform-' . $post->id . '.css';
442 - $this->saveStyleSheet($post->formStyle, $sheetName);
443 - }
444 -
445 - if ($post->layoutChanged) {
446 - $sheetName = 'bitform-layout-' . $post->id . '.css';
447 - $rowHeight = (int)$post->rowHeight;
448 - $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
449 - }
482 + public function updateForm($Request, $post)
483 + {
484 + // return ['checked dta'=>$post];
450 485 $formId = $post->id;
451 486 // get the builder_helper_state from the form
452 487 $builder_helper_state = self::$formModel->get(['builder_helper_state'], ['id' => $formId]);
453 488 $builder_helper_state = (!is_wp_error($builder_helper_state) && 1 === count($builder_helper_state) && isset($builder_helper_state[0]->builder_helper_state)) ? $builder_helper_state[0]->builder_helper_state : '{}';
@@ -476,8 +511,10 @@
476 511
477 512 if (property_exists($post, 'fields') && null !== $post->fields && property_exists($post, 'layout') && $post->layout) {
478 513 $fields = wp_unslash($post->fields);
479 514 $layout = wp_unslash($post->layout);
515 + $nestedLayout = wp_unslash($post->nestedLayouts);
516 + $formInfo = wp_unslash($post->formInfo);
480 517 $formID = wp_unslash($post->id);
481 518 $form_name = wp_unslash($post->form_name);
482 519 $formSettings = wp_unslash($post->formSettings);
483 520 $workFlows = wp_unslash($post->workFlows);
@@ -485,11 +522,13 @@
485 522 $reports = wp_unslash($post->currentReport);
486 523 }
487 524
488 525 $mailTem = $formSettings->mailTem;
526 + $pdfTem = $formSettings->pdfTem;
489 527 $integrations = $formSettings->integrations;
490 528
491 529 $user_details = static::$ipTool->getUserDetail();
530 +
492 531 if (empty($fields) || empty($layout) || is_null($formID)) {
493 532 return new WP_Error('empty_form', 'Can not update empty form.');
494 533 }
495 534
@@ -500,12 +539,16 @@
500 539 __('You are allowed to add maximum 2 workflows ', 'bit-form')
501 540 );
502 541 }
503 542 }
543 +
504 544 $form_content = [
505 - 'fields' => $fields,
506 - 'layout' => $layout,
545 + 'fields' => $fields,
546 + 'layout' => $layout,
547 + 'nestedLayout' => $nestedLayout,
548 + 'formInfo' => $formInfo,
507 549 ];
550 +
508 551 if (isset($post->report_id)) {
509 552 $form_content['report_id'] = wp_unslash($post->report_id);
510 553 }
511 554
@@ -547,9 +590,9 @@
547 590 }
548 591 unset($formSettings->confirmation->type->successMsg);
549 592 }
550 593 // return $formSettings;
551 - // Confiramtion Message [end] */
594 + // Confirmation Message [end] */
552 595 // Email Template [start]
553 596 if (!empty($mailTem)) {
554 597 $emailTemplateHandler
555 598 = new EmailTemplateHandler($formID, $user_details);
@@ -568,8 +611,25 @@
568 611 }
569 612 }
570 613 // return $formSettings;
571 614 // Email Template [end] */
615 + if (!empty($pdfTem)) {
616 + $pdfTemplateHandler = new PdfTemplateHandler($formID);
617 +
618 + foreach ($pdfTem as $templateKey => $templateDetail) {
619 + if (isset($templateDetail->setting->font->name)) {
620 + $this->pdfFontDownload($templateDetail->setting->font->name);
621 + }
622 +
623 + $savedID = $pdfTemplateHandler->saveOrUpdate($templateDetail);
624 + if (is_wp_error($savedID) && 'result_empty' === $savedID->get_error_code()) {
625 + $newData['pdfTemplate'] = 2;
626 + } else {
627 + $newData['pdfTemplate'] = 1;
628 + $integartionIDForWorkflow['pdfTem'][$templateKey] = $savedID;
629 + }
630 + }
631 + }
572 632 //Integration [start] */
573 633 $integrationHandler = new IntegrationHandler($formID, $user_details);
574 634 if (!empty($formSettings->confirmation->type)) {
575 635 $allIntegrations = $formSettings->confirmation->type;
@@ -632,14 +692,15 @@
632 692 $workFlowExist = [];
633 693 if (!empty($workFlows)) {
634 694 $workFlowHandler = new WorkFlowHandler($formID, $user_details);
635 695 // foreach ($workFlows as $workFlowIndex => $workFlow) {
696 + $workflows = is_string($workFlows) ? json_decode($workFlows) : $workFlows;
636 697
637 - foreach ($workFlows as $index => $workFlow) {
638 - if ('onload' === $workFlow->action_type) {
698 + foreach ($workflows as $index => $workFlow) {
699 + if (in_array($workFlow->action_type, ['always', 'onload'])) {
639 700 $workFlowExist['onload'] = true;
640 701 }
641 - if ('oninput' === $workFlow->action_type) {
702 + if ('oninput' === $workFlow->action_type || ('always' === $workFlow->action_type && 'cond' === $workFlow->action_behaviour)) {
642 703 $workFlowExist['oninput'] = true;
643 704 }
644 705 if (empty($workFlow->id)) {
645 706 $savedID = $workFlowHandler->saveworkFlow($workFlow, $integartionIDForWorkflow, $index);
@@ -648,9 +709,9 @@
648 709 } else {
649 710 $newData['workflow'] = 1;
650 711 }
651 712 } else {
652 - $newData = $workFlowHandler->updateworkFlow($workFlow->id, $workFlow, $integartionIDForWorkflow, $index, $newData);
713 + $newData = $workFlowHandler->updateworkFlow($workFlow->id, $workFlow, $integartionIDForWorkflow, $index, $newData);
653 714 }
654 715 }
655 716 }
656 717 $form_content = array_merge($form_content, ['workFlowExist' => $workFlowExist]);
@@ -701,8 +762,26 @@
701 762 );
702 763 }
703 764 }
704 765 //reports [end]
766 + //form abandonment [start]
767 + if (!empty($formSettings->formAbandonment)) {
768 + $formAbandonment = $formSettings->formAbandonment;
769 + // search for existing form abandonment in integration table
770 + $abandonmentInteg = $integrationHandler->getAllIntegration('formAbandonment', 'formAbandonment');
771 + if (!is_wp_error($abandonmentInteg) && !empty($abandonmentInteg)) {
772 + $abandonmentInteg = $abandonmentInteg[0];
773 + }
774 + if (isset($abandonmentInteg->id)) {
775 + $abandonmentIntegID = $abandonmentInteg->id;
776 + $abandonmentIntegDetails = json_encode($formAbandonment);
777 + $integrationHandler->updateIntegration($abandonmentIntegID, 'formAbandonment', 'formAbandonment', $abandonmentIntegDetails, 'formAbandonment');
778 + } else {
779 + $abandonmentIntegDetails = json_encode($formAbandonment);
780 + $integrationHandler->saveIntegration('formAbandonment', 'formAbandonment', $abandonmentIntegDetails, 'formAbandonment');
781 + }
782 + }
783 + //form abandonment [end]
705 784 $form_content = wp_json_encode($form_content);
706 785 $updateData = [
707 786 'form_name' => $form_name,
708 787 'form_content' => $form_content,
@@ -710,9 +789,8 @@
710 789 'user_ip' => $user_details['ip'],
711 790 'builder_helper_state' => \wp_json_encode((object)$builder_helper_state),
712 791 'generated_script_page_ids' => \wp_json_encode((object) []),
713 792 'user_device' => $user_details['device'],
714 - 'status' => 1,
715 793 'updated_at' => $user_details['time'],
716 794 ];
717 795 if (isset($atomic_class_map)) {
718 796 $updateData['atomic_class_map'] = $atomic_class_map;
@@ -741,8 +819,9 @@
741 819 unset($post->deletedFldKey);
742 820 }
743 821
744 822 $updated_data = $this->getAForm(['id' => $formID], null);
823 +
745 824 if (0 === $newData['settingConfiramation'] && 0 === $newData['mailTemplate'] && 0 === $newData['integation']) {
746 825 unset($updated_data['formSettings']);
747 826 }
748 827 if (0 === $newData['integation']) {
@@ -755,8 +834,13 @@
755 834 unset($updated_data['mailTem']);
756 835 } elseif (2 === $newData['mailTemplate']) {
757 836 $errorIN .= empty($errorIN) ? 'mailTemplate' : ', mailTemplate';
758 837 }
838 + // if (0 === $newData['pdfTemplate']) {
839 + // unset($updated_data['pdfTem']);
840 + // } elseif (2 === $newData['pdfTemplate']) {
841 + // $errorIN .= empty($errorIN) ? 'pdfTemplate' : ', pdfTemplate';
842 + // }
759 843 if (0 === $newData['workflow']) {
760 844 unset($updated_data['workFlows']);
761 845 } elseif (2 === $newData['workflow']) {
762 846 $errorIN .= empty($errorIN) ? 'workflow' : ', workflow';
@@ -775,9 +859,10 @@
775 859 return $updated_data;
776 860 }
777 861 }
778 862
779 - public function setEmptyMetaValue($fieldkeys) {
863 + public function setEmptyMetaValue($fieldkeys)
864 + {
780 865 $sqlEscaped = array_map(function ($fld) {
781 866 return "'" . esc_sql($fld) . "'";
782 867 }, $fieldkeys);
783 868 $deletedFldKey = implode(',', $sqlEscaped);
@@ -786,95 +871,99 @@
786 871 $sql = $wpdb->prepare("UPDATE $tablename SET meta_value = %s WHERE meta_key IN ( " . $deletedFldKey . ')', '');
787 872 $wpdb->query($sql);
788 873 }
789 874
790 - public function changeFormStatus($Request, $post) {
791 - if (isset($Request['status']) && $Request['id']) {
792 - $status = wp_unslash($Request['status']);
793 - $id = wp_unslash($Request['id']);
794 - } else {
795 - $status = wp_unslash($post->status);
796 - $id = wp_unslash($post->id);
797 - }
798 - $user_details = static::$ipTool->getUserDetail();
799 - // return $post->fields;
800 - $status = 'true' === $status || true === $status ? true : false;
801 - if (!is_bool($status) || is_null($id)) {
802 - // echo $status;
803 - return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
804 - }
805 - $update_status = static::$formModel->update(
806 - [
807 - 'user_id' => $user_details['id'],
808 - 'user_ip' => $user_details['ip'],
809 - 'user_device' => $user_details['device'],
810 - 'status' => $status ? 1 : 0,
811 - 'updated_at' => $user_details['time'],
812 - ],
813 - [
814 - 'id' => $id,
815 - ]
816 - );
817 - if (is_wp_error($update_status)) {
818 - return $update_status;
819 - } elseif (!$update_status) {
820 - return false;
821 - } else {
822 - return __('Form status changed successfully', 'bit-form');
823 - }
875 + public function changeFormStatus($Request, $post)
876 + {
877 + if (isset($Request['status']) && $Request['id']) {
878 + $status = wp_unslash($Request['status']);
879 + $id = wp_unslash($Request['id']);
880 + } else {
881 + $status = wp_unslash($post->status);
882 + $id = wp_unslash($post->id);
824 883 }
884 + $user_details = static::$ipTool->getUserDetail();
885 + // return $post->fields;
886 + $status = 'true' === $status || true === $status ? true : false;
887 + if (!is_bool($status) || is_null($id)) {
888 + // echo $status;
889 + return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
890 + }
891 + $update_status = static::$formModel->update(
892 + [
893 + 'user_id' => $user_details['id'],
894 + 'user_ip' => $user_details['ip'],
895 + 'user_device' => $user_details['device'],
896 + 'status' => $status ? 1 : 0,
897 + 'updated_at' => $user_details['time'],
898 + ],
899 + [
900 + 'id' => $id,
901 + ]
902 + );
903 + if (is_wp_error($update_status)) {
904 + return $update_status;
905 + } elseif (!$update_status) {
906 + return false;
907 + } else {
908 + return __('Form status changed successfully', 'bit-form');
909 + }
910 + }
825 911
826 - public function changeBulkFormStatus($Request, $post) {
827 - if (isset($Request['status']) && $Request['formID']) {
828 - $status = wp_unslash($Request['status']);
829 - $formID = wp_unslash($Request['formID']);
830 - } else {
831 - $status = wp_unslash($post->status);
832 - $formID = wp_unslash($post->formID);
833 - }
834 - $user_details = static::$ipTool->getUserDetail();
835 - // return $post->fields;
836 - $status = 'true' === $status || true === $status ? true : false;
837 - if (!is_bool($status) || is_null($formID) || !is_array($formID)) {
838 - // echo $status;
839 - return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
840 - }
841 - $update_status = static::$formModel->bulkUpdate(
842 - [
843 - 'user_id' => $user_details['id'],
844 - 'user_ip' => $user_details['ip'],
845 - 'user_device' => $user_details['device'],
846 - 'status' => $status ? 1 : 0,
847 - 'updated_at' => $user_details['time'],
848 - ],
849 - [
850 - 'id' => $formID,
851 - ]
852 - );
853 - if (is_wp_error($update_status)) {
854 - return $update_status;
855 - } elseif (!$update_status) {
856 - return false;
857 - } else {
858 - return __('Form status changed successfully', 'bit-form');
859 - }
912 + public function changeBulkFormStatus($Request, $post)
913 + {
914 + if (isset($Request['status']) && $Request['formID']) {
915 + $status = wp_unslash($Request['status']);
916 + $formID = wp_unslash($Request['formID']);
917 + } else {
918 + $status = wp_unslash($post->status);
919 + $formID = wp_unslash($post->formID);
860 920 }
921 + $user_details = static::$ipTool->getUserDetail();
922 + // return $post->fields;
923 + $status = 'true' === $status || true === $status ? true : false;
924 + if (!is_bool($status) || is_null($formID) || !is_array($formID)) {
925 + // echo $status;
926 + return new WP_Error('status_change', __('Form status change failed.', 'bit-form'));
927 + }
928 + $update_status = static::$formModel->bulkUpdate(
929 + [
930 + 'user_id' => $user_details['id'],
931 + 'user_ip' => $user_details['ip'],
932 + 'user_device' => $user_details['device'],
933 + 'status' => $status ? 1 : 0,
934 + 'updated_at' => $user_details['time'],
935 + ],
936 + [
937 + 'id' => $formID,
938 + ]
939 + );
940 + if (is_wp_error($update_status)) {
941 + return $update_status;
942 + } elseif (!$update_status) {
943 + return false;
944 + } else {
945 + return __('Form status changed successfully', 'bit-form');
946 + }
947 + }
861 948
862 - public function getAllForm() {
863 - global $wpdb;
949 + public function getAllForm()
950 + {
951 + global $wpdb;
864 952
865 - $allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries FROM `{$wpdb->prefix}bitforms_form` as forms LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id GROUP BY forms.id");
953 + $allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries FROM `{$wpdb->prefix}bitforms_form` as forms LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id GROUP BY forms.id");
866 954
867 - if (is_wp_error($allForms)) {
868 - return $allForms;
869 - } elseif (!$allForms) {
870 - return [];
871 - } else {
872 - return $allForms;
873 - }
955 + if (is_wp_error($allForms)) {
956 + return $allForms;
957 + } elseif (!$allForms) {
958 + return [];
959 + } else {
960 + return $allForms;
874 961 }
962 + }
875 963
876 - public function getAForm($Request, $post) {
964 + public function getAForm($Request, $post)
965 + {
877 966 if (isset($Request['id'])) {
878 967 $formID = wp_unslash($Request['id']);
879 968 } else {
880 969 $formID = wp_unslash($post->id);
@@ -891,8 +980,10 @@
891 980 $helper_states = $formManager->getFormHelperStates();
892 981 if ($formManager->isExist()) {
893 982 $form_content_arr = [
894 983 'layout' => $form_content->layout,
984 + 'nestedLayout' => !empty($form_content->nestedLayout) ? $form_content->nestedLayout : (object) [],
985 + 'formInfo' => !empty($form_content->formInfo) ? $form_content->formInfo : (object) ['formName' => $formManager->getFormName()],
895 986 'fields' => $form_content->fields,
896 987 'form_name' => $formManager->getFormName(),
897 988 'workFlowExist' => $form_content->workFlowExist,
898 989 'report_id' => isset($form_content->report_id) ? $form_content->report_id : null
@@ -925,8 +1016,24 @@
925 1016 'body' => $emailTemplatevalue->body,
926 1017 ];
927 1018 }
928 1019 }
1020 + // get all pdf template
1021 + $pdfTemplateHandler = new PdfTemplateHandler($formID);
1022 + $pdfTemplates = $pdfTemplateHandler->getAll();
1023 +
1024 + if (!is_wp_error($pdfTemplates)) {
1025 + foreach ($pdfTemplates as $value) {
1026 + $pdfTem[] =
1027 + [
1028 + 'id' => $value->id,
1029 + 'title' => $value->title,
1030 + 'setting' => json_decode($value->setting),
1031 + 'body' => $value->body,
1032 + ];
1033 + }
1034 + }
1035 +
929 1036 $integrationHandler = new IntegrationHandler($formID);
930 1037 $formIntegrations = $integrationHandler->getAllIntegration('form');
931 1038 if (!is_wp_error($formIntegrations)) {
932 1039 foreach ($formIntegrations as $integrationkey => $integrationValue) {
@@ -954,10 +1061,10 @@
954 1061 ];
955 1062 $integrations[] = array_merge(
956 1063 $integrationData,
957 1064 is_string($integrationValue->integration_details) ?
958 - (array) json_decode($integrationValue->integration_details) :
959 - $integrationValue->integration_details
1065 + (array) json_decode($integrationValue->integration_details) :
1066 + $integrationValue->integration_details
960 1067 );
961 1068 }
962 1069 }
963 1070 }
@@ -991,8 +1098,9 @@
991 1098 'integrations' => []
992 1099 ]
993 1100 );
994 1101 }
1102 +
995 1103 if (!empty($mailTem)) {
996 1104 $settingsContent = array_merge(
997 1105 $settingsContent,
998 1106 [
@@ -1006,8 +1114,24 @@
1006 1114 'mailTem' => []
1007 1115 ]
1008 1116 );
1009 1117 }
1118 +
1119 + if (!empty($pdfTem)) {
1120 + $settingsContent = array_merge(
1121 + $settingsContent,
1122 + [
1123 + 'pdfTem' => $pdfTem,
1124 + ]
1125 + );
1126 + } else {
1127 + $settingsContent = array_merge(
1128 + $settingsContent,
1129 + [
1130 + 'pdfTem' => [],
1131 + ]
1132 + );
1133 + }
1010 1134 $formSettings = [
1011 1135 'formSettings' => $settingsContent,
1012 1136 ];
1013 1137 $data = [
@@ -1095,9 +1219,10 @@
1095 1219 return $data;
1096 1220 }
1097 1221 }
1098 1222
1099 - public function deleteAForm($Request, $post) {
1223 + public function deleteAForm($Request, $post)
1224 + {
1100 1225 if (isset($Request['id'])) {
1101 1226 $formID = wp_unslash($Request['id']);
1102 1227 } else {
1103 1228 $formID = wp_unslash($post->id);
@@ -1163,9 +1288,10 @@
1163 1288 }
1164 1289 return is_wp_error($delete_status) ? $delete_status : __('Form deleted successfully.', 'bit-form');
1165 1290 }
1166 1291
1167 - public function deleteBlukForm($Request, $post) {
1292 + public function deleteBlukForm($Request, $post)
1293 + {
1168 1294 if (isset($Request['formID'])) {
1169 1295 $formID = wp_unslash($Request['formID']);
1170 1296 } else {
1171 1297 $formID = wp_unslash($post->formID);
@@ -1234,9 +1360,10 @@
1234 1360
1235 1361 return is_wp_error($delete_status) ? $delete_status : __('Forms deleted successfully.', 'bit-form');
1236 1362 }
1237 1363
1238 - public function genarateNewLayoutNField($layout, $fields, $oldId, $newId) {
1364 + public function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
1365 + {
1239 1366 $newField = (object) [];
1240 1367 foreach ($layout->lg as $ind => $itm) {
1241 1368 $fld_tmp = $fields->{$layout->lg[$ind]->i};
1242 1369 $layout->lg[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->lg[$ind]->i);
@@ -1246,9 +1373,10 @@
1246 1373 }
1247 1374 return ['layout' => $layout, 'fields' => $newField];
1248 1375 }
1249 1376
1250 - public function duplicateAForm($Request, $post) {
1377 + public function duplicateAForm($Request, $post)
1378 + {
1251 1379 $oldFormId = intval($post->id);
1252 1380 $newFormId = intval($post->newFormId);
1253 1381 if ($oldFormId < 1) {
1254 1382 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
@@ -1290,9 +1418,10 @@
1290 1418 }
1291 1419 return $duplicateResponse;
1292 1420 }
1293 1421
1294 - public function importAForm($post) {
1422 + public function importAForm($post)
1423 + {
1295 1424 $oldFormId = !empty($post->formDetail->form_id) ? $post->formDetail->form_id : null;
1296 1425 $newFormId = intval($post->newFormId);
1297 1426 if (!$oldFormId || empty($post->formDetail->fields) || empty($post->formDetail->layout)) {
1298 1427 return new WP_Error('invalid_form', __('Please import a valid json.', 'bit-form'));
@@ -1299,9 +1428,11 @@
1299 1428 }
1300 1429 $importtedForm = (array)$post->formDetail;
1301 1430 $importtedForm['form_content']['fields'] = $importtedForm['fields'];
1302 1431 $importtedForm['form_content']['layout'] = $importtedForm['layout'];
1303 - unset($importtedForm['fields'], $importtedForm['layout']);
1432 + $importtedForm['form_content']['nestedLayout'] = $importtedForm['nestedLayout'];
1433 + $importtedForm['form_content']['formInfo'] = $importtedForm['formInfo'];
1434 + unset($importtedForm['fields'], $importtedForm['layout'], $importtedForm['nestedLayout'], $importtedForm['formInfo']);
1304 1435 $formData = FormDuplicateHelper::createReplica((array)$importtedForm, $oldFormId, $newFormId);
1305 1436
1306 1437 if (!empty($importtedForm['rowHeight'])) {
1307 1438 $formData->rowHeight = $importtedForm['rowHeight'];
@@ -1358,9 +1489,10 @@
1358 1489 // echo $formJson;
1359 1490 // die();
1360 1491 // }
1361 1492
1362 - public function exportAForm($Request, $post) {
1493 + public function exportAForm($Request, $post)
1494 + {
1363 1495 $oldFormId = intval($post->id);
1364 1496 if ($oldFormId < 1) {
1365 1497 return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1366 1498 }
@@ -1389,9 +1521,10 @@
1389 1521 }
1390 1522 return $formData;
1391 1523 }
1392 1524
1393 - public function getFormEntryLabelAndCount($Request, $post) {
1525 + public function getFormEntryLabelAndCount($Request, $post)
1526 + {
1394 1527 if (isset($Request['id'])) {
1395 1528 $id = wp_unslash($Request['id']);
1396 1529 } else {
1397 1530 $id = wp_unslash($post->id);
@@ -1446,9 +1579,10 @@
1446 1579 $response['fieldDetails'] = $labels;
1447 1580 return $response;
1448 1581 }
1449 1582
1450 - public function getFormEntry($Request, $post) {
1583 + public function getFormEntry($Request, $post)
1584 + {
1451 1585 if (isset($Request['id'])) {
1452 1586 $id = wp_unslash($Request['id']);
1453 1587 $offset = isset($Request['offset']) ?
1454 1588 wp_unslash($Request['offset']) : 0;
@@ -1505,182 +1639,186 @@
1505 1639 $formEntries = $customFieldHandler->updatedEntries($formEntries, $fieldDetails);
1506 1640 return $formEntries;
1507 1641 }
1508 1642
1509 - public function getExportEntry($post) {
1510 - $formId = wp_unslash($post->formId);
1511 - $fileFormate = isset($post->fileFormate) ? wp_unslash($post->fileFormate) : null;
1512 - $limit = isset($post->limit) ? wp_unslash($post->limit) : null;
1513 - $sortBy = isset($post->sort) ? wp_unslash($post->sort) : 'ASC';
1514 - $sortByField = isset($post->sortField) ? wp_unslash($post->sortField) : 'bitforms_form_entry_id';
1515 - $formFields = json_decode($post->selectedField);
1516 - if (is_null($formId)) {
1517 - return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1518 - }
1519 - $formManager = new AdminFormManager($formId);
1520 - $fieldLabels = $formManager->getFieldLabel(true);
1521 - if (!$formManager->isExist()) {
1522 - return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
1523 - }
1643 + public function getExportEntry($post)
1644 + {
1645 + $formId = wp_unslash($post->formId);
1646 + $fileFormate = isset($post->fileFormate) ? wp_unslash($post->fileFormate) : null;
1647 + $limit = isset($post->limit) ? wp_unslash($post->limit) : null;
1648 + $sortBy = isset($post->sort) ? wp_unslash($post->sort) : 'ASC';
1649 + $sortByField = isset($post->sortField) ? wp_unslash($post->sortField) : 'bitforms_form_entry_id';
1650 + $formFields = json_decode($post->selectedField);
1651 + if (is_null($formId)) {
1652 + return new WP_Error('empty_form', __('Form id is empty.', 'bit-form'));
1653 + }
1654 + $formManager = new AdminFormManager($formId);
1655 + $fieldLabels = $formManager->getFieldLabel(true);
1656 + if (!$formManager->isExist()) {
1657 + return new WP_Error('empty_form', __('Form does not exists', 'bit-form'));
1658 + }
1524 1659
1525 - $formEntry = new FormEntryModel();
1526 - $entries = $formEntry->get(
1527 - 'id',
1528 - [
1529 - 'form_id' => $formId,
1530 - ],
1531 - null,
1532 - null,
1533 - 'created_at',
1534 - 'DESC'
1535 - );
1536 - if (is_wp_error($entries)) {
1537 - if ('result_empty' === $entries->get_error_code()) {
1538 - return [
1539 - 'count' => 0,
1540 - 'entries' => [],
1541 - ];
1542 - }
1543 - return $entries;
1660 + $formEntry = new FormEntryModel();
1661 + $entries = $formEntry->get(
1662 + 'id',
1663 + [
1664 + 'form_id' => $formId,
1665 + ],
1666 + null,
1667 + null,
1668 + 'created_at',
1669 + 'DESC'
1670 + );
1671 + if (is_wp_error($entries)) {
1672 + if ('result_empty' === $entries->get_error_code()) {
1673 + return [
1674 + 'count' => 0,
1675 + 'entries' => [],
1676 + ];
1544 1677 }
1545 - $entryMeta = new FormEntryMetaModel();
1546 - $filter = [];
1547 - $formEntries = $entryMeta->getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit, $sortBy, $sortByField);
1548 - return $formEntries;
1678 + return $entries;
1549 1679 }
1680 + $entryMeta = new FormEntryMetaModel();
1681 + $filter = [];
1682 + $formEntries = $entryMeta->getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit, $sortBy, $sortByField);
1683 + return $formEntries;
1684 + }
1550 1685
1551 - public function deleteBlukFormEntries($Request, $post) {
1552 - if (isset($Request['formID'])) {
1553 - $formID = wp_unslash($Request['formID']);
1554 - $entries = wp_unslash($Request['entries']);
1686 + public function deleteBlukFormEntries($Request, $post)
1687 + {
1688 + if (isset($Request['formID'])) {
1689 + $formID = wp_unslash($Request['formID']);
1690 + $entries = wp_unslash($Request['entries']);
1691 + } else {
1692 + $formID = wp_unslash($post->formID);
1693 + $entries = wp_unslash($post->entries);
1694 + }
1695 + if (is_null($formID) || !is_array($entries) || 0 === count($entries)) {
1696 + return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bit-form'));
1697 + }
1698 + $formManager = new AdminFormManager($formID);
1699 + if (!$formManager->isExist()) {
1700 + return new WP_Error('empty_form', __('Form does not exist.', 'bit-form'));
1701 + }
1702 + $workFlowRunHelper = new WorkFlow($formID);
1703 + $workFlowreturnedOnDelete = $workFlowRunHelper->executeOnDelete(
1704 + $formManager,
1705 + $formID,
1706 + $entries
1707 + );
1708 + if (isset($workFlowreturnedOnDelete['entries'])) {
1709 + if (0 === count($workFlowreturnedOnDelete['entries'])) {
1710 + return ['message' => __('Entry Deletetion prevented by workflow', 'bit-form')];
1711 + } elseif (count($workFlowreturnedOnDelete['entries']) === count($entries)) {
1712 + $message = __('Entry Deleted successfully', 'bit-form');
1555 1713 } else {
1556 - $formID = wp_unslash($post->formID);
1557 - $entries = wp_unslash($post->entries);
1714 + $result['prevented'] = array_diff($entries, $workFlowreturnedOnDelete['entries']);
1715 + $entries = $workFlowreturnedOnDelete['entries'];
1716 + $message = __('Entry Deleted successfully, Some prevented by workflow', 'bit-form');
1558 1717 }
1559 - if (is_null($formID) || !is_array($entries) || 0 === count($entries)) {
1560 - return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bit-form'));
1561 - }
1562 - $formManager = new AdminFormManager($formID);
1563 - if (!$formManager->isExist()) {
1564 - return new WP_Error('empty_form', __('Form does not exist.', 'bit-form'));
1565 - }
1566 - $workFlowRunHelper = new WorkFlowRunHelper($formID);
1567 - $workFlowreturnedOnDelete = $workFlowRunHelper->executeOnDelete(
1568 - $formManager,
1569 - $formID,
1570 - $entries
1571 - );
1572 - if (isset($workFlowreturnedOnDelete['entries'])) {
1573 - if (0 === count($workFlowreturnedOnDelete['entries'])) {
1574 - return ['message' => __('Entry Deletetion prevented by workflow', 'bit-form')];
1575 - } elseif (count($workFlowreturnedOnDelete['entries']) === count($entries)) {
1576 - $message = __('Entry Deleted successfully', 'bit-form');
1577 - } else {
1578 - $result['prevented'] = array_diff($entries, $workFlowreturnedOnDelete['entries']);
1579 - $entries = $workFlowreturnedOnDelete['entries'];
1580 - $message = __('Entry Deleted successfully, Some prevented by workflow', 'bit-form');
1718 + } else {
1719 + $message = __('Entry Deleted successfully', 'bit-form');
1720 + }
1721 + global $wpdb;
1722 + $prefix = $wpdb->prefix;
1723 + $formEntryModel = new FormEntryModel();
1724 + $delete_status = $formEntryModel->bulkDelete(
1725 + [
1726 + "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1727 + "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1728 + ]
1729 + );
1730 + if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1731 + $fileHandler = new FileHandler();
1732 + foreach ($entries as $enrtyKey => $entryID) {
1733 + $fileEntries = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID;
1734 + if (file_exists($fileEntries)) {
1735 + $fileHandler->rmrf($fileEntries);
1581 1736 }
1582 - } else {
1583 - $message = __('Entry Deleted successfully', 'bit-form');
1584 1737 }
1585 - global $wpdb;
1586 - $prefix = $wpdb->prefix;
1587 - $formEntryModel = new FormEntryModel();
1588 - $delete_status = $formEntryModel->bulkDelete(
1738 + }
1739 + $count = $formEntryModel->count(
1740 + [
1741 + 'form_id' => $formID,
1742 + ]
1743 + );
1744 + $formManager->resetSubmissionCount(intval($count[0]->count));
1745 + if (is_wp_error($delete_status)) {
1746 + return new WP_Error('entry_not_exists', __('Form entry deletion failed.', 'bit-form'));
1747 + }
1748 + $result['message'] = $message;
1749 + return $result;
1750 + }
1751 +
1752 + public function duplicateFormEntry($Request, $post)
1753 + {
1754 + if (isset($Request['formID'])) {
1755 + $formID = wp_unslash($Request['formID']);
1756 + $entries = wp_unslash($Request['entries']);
1757 + } else {
1758 + $formID = wp_unslash($post->formID);
1759 + $entries = wp_unslash($post->entries);
1760 + }
1761 + if (is_null($formID) || !is_array($entries) || empty($entries)) {
1762 + return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bit-form'));
1763 + }
1764 +
1765 + $formManager = new AdminFormManager($formID);
1766 + if (!$formManager->isExist()) {
1767 + return new WP_Error('empty_form', __('Form does not exist', 'bit-form'));
1768 + }
1769 + $formEntryModel = new FormEntryModel();
1770 + $entryMeta = new FormEntryMetaModel();
1771 + $user_details = static::$ipTool->getUserDetail();
1772 + $total_entries = count($entries);
1773 + $duplicate_count = 0;
1774 + $test = '';
1775 + $fileHandler = new FileHandler();
1776 + $result = [];
1777 + foreach ($entries as $entryIndex => $entryID) {
1778 + $duplicatedEntryId = $formEntryModel->insert(
1589 1779 [
1590 - "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1591 - "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1780 + 'form_id' => $formID,
1781 + 'user_id' => $user_details['id'],
1782 + 'user_ip' => $user_details['ip'],
1783 + 'user_device' => $user_details['device'],
1784 + 'referer' => 'duplicate of #' . $entryID,
1785 + 'status' => 1,
1786 + 'created_at' => $user_details['time'],
1592 1787 ]
1593 1788 );
1594 - if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1595 - $fileHandler = new FileHandler();
1596 - foreach ($entries as $enrtyKey => $entryID) {
1597 - $fileEntries = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID;
1598 - if (file_exists($fileEntries)) {
1599 - $fileHandler->rmrf($fileEntries);
1600 - }
1601 - }
1602 - }
1603 - $count = $formEntryModel->count(
1604 - [
1605 - 'form_id' => $formID,
1606 - ]
1607 - );
1608 - $formManager->resetSubmissionCount(intval($count[0]->count));
1609 - if (is_wp_error($delete_status)) {
1610 - return new WP_Error('entry_not_exists', __('Form entry deletion failed.', 'bit-form'));
1611 - }
1612 - $result['message'] = $message;
1613 - return $result;
1614 - }
1615 -
1616 - public function duplicateFormEntry($Request, $post) {
1617 - if (isset($Request['formID'])) {
1618 - $formID = wp_unslash($Request['formID']);
1619 - $entries = wp_unslash($Request['entries']);
1620 - } else {
1621 - $formID = wp_unslash($post->formID);
1622 - $entries = wp_unslash($post->entries);
1623 - }
1624 - if (is_null($formID) || !is_array($entries) || empty($entries)) {
1625 - return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bit-form'));
1626 - }
1627 -
1628 - $formManager = new AdminFormManager($formID);
1629 - if (!$formManager->isExist()) {
1630 - return new WP_Error('empty_form', __('Form does not exist', 'bit-form'));
1631 - }
1632 - $formEntryModel = new FormEntryModel();
1633 - $entryMeta = new FormEntryMetaModel();
1634 - $user_details = static::$ipTool->getUserDetail();
1635 - $total_entries = count($entries);
1636 - $duplicate_count = 0;
1637 - $test = '';
1638 - $fileHandler = new FileHandler();
1639 - $result = [];
1640 - foreach ($entries as $entryIndex => $entryID) {
1641 - $duplicatedEntryId = $formEntryModel->insert(
1789 + if ($duplicatedEntryId) {
1790 + $duplicate_status = $entryMeta->duplicateEntryMeta(
1642 1791 [
1643 - 'form_id' => $formID,
1644 - 'user_id' => $user_details['id'],
1645 - 'user_ip' => $user_details['ip'],
1646 - 'user_device' => $user_details['device'],
1647 - 'referer' => 'duplicate of #' . $entryID,
1648 - 'status' => 1,
1649 - 'created_at' => $user_details['time'],
1792 + 'duplicateID' => $duplicatedEntryId,
1793 + 'entryID' => $entryID,
1650 1794 ]
1651 1795 );
1652 - if ($duplicatedEntryId) {
1653 - $duplicate_status = $entryMeta->duplicateEntryMeta(
1654 - [
1655 - 'duplicateID' => $duplicatedEntryId,
1656 - 'entryID' => $entryID,
1657 - ]
1658 - );
1659 - if ($duplicate_status) {
1660 - $result['details'][$entryID] = $duplicatedEntryId;
1661 - $duplicate_count = $duplicate_count + 1;
1662 - if (file_exists(BITFORMS_UPLOAD_DIR . "/$formID/$entryID")) {
1663 - $fileHandler->cpyr(
1664 - BITFORMS_UPLOAD_DIR . "/$formID/$entryID",
1665 - BITFORMS_UPLOAD_DIR . "/$formID/$duplicatedEntryId"
1666 - );
1667 - }
1796 + if ($duplicate_status) {
1797 + $result['details'][$entryID] = $duplicatedEntryId;
1798 + $duplicate_count = $duplicate_count + 1;
1799 + if (file_exists(BITFORMS_UPLOAD_DIR . "/$formID/$entryID")) {
1800 + $fileHandler->cpyr(
1801 + BITFORMS_UPLOAD_DIR . "/$formID/$entryID",
1802 + BITFORMS_UPLOAD_DIR . "/$formID/$duplicatedEntryId"
1803 + );
1668 1804 }
1669 1805 }
1670 1806 }
1807 + }
1671 1808
1672 - $count = $formEntryModel->count(
1673 - [
1674 - 'form_id' => $formID,
1675 - ]
1676 - );
1677 - $formManager->resetSubmissionCount(intval($count[0]->count));
1678 - $result['message'] = 1 === count($entries) ? __('Entry Duplicated successfully', 'bit-form') : __('Entries Duplicated successfully', 'bit-form');
1679 - return ($total_entries === $duplicate_count) ? $result : false;
1680 - }
1809 + $count = $formEntryModel->count(
1810 + [
1811 + 'form_id' => $formID,
1812 + ]
1813 + );
1814 + $formManager->resetSubmissionCount(intval($count[0]->count));
1815 + $result['message'] = 1 === count($entries) ? __('Entry Duplicated successfully', 'bit-form') : __('Entries Duplicated successfully', 'bit-form');
1816 + return ($total_entries === $duplicate_count) ? $result : false;
1817 + }
1681 1818
1682 - public function editFormEntry($Request, $post) {
1819 + public function editFormEntry($Request, $post)
1820 + {
1683 1821 if (isset($Request['formID'])) {
1684 1822 $formID = wp_unslash($Request['formID']);
1685 1823 $entryID = wp_unslash($Request['entryID']);
1686 1824 } else {
@@ -1732,9 +1870,9 @@
1732 1870 $form_fields->{$key}->val = $entries[$key];
1733 1871 $form_fields->{$key}->name = $key;
1734 1872 }
1735 1873 }
1736 - $workFlowRunHelper = new WorkFlowRunHelper($formID);
1874 + $workFlowRunHelper = new WorkFlow($formID);
1737 1875 $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
1738 1876 'edit',
1739 1877 $form_fields
1740 1878 );
@@ -1755,16 +1893,18 @@
1755 1893 ];
1756 1894 return $formData;
1757 1895 }
1758 1896
1759 - public function updateFormEntry($Request, $post) {
1897 + public function updateFormEntry($Request, $post)
1898 + {
1760 1899 if (isset($Request['formID'], $Request['entryID'])) {
1761 1900 $formID = wp_unslash($Request['formID']);
1762 1901 $entryID = wp_unslash($Request['entryID']);
1763 1902 unset($Request['action'], $Request['formID'], $Request['entryID']);
1764 -
1765 - $updatedValue = wp_unslash($post);
1766 1903 }
1904 + $formManager = new AdminFormManager($formID);
1905 + $formManager->fieldNameReplaceOfPost();
1906 + $updatedValue = wp_unslash($_POST);
1767 1907
1768 1908 if (is_null($updatedValue) || !is_array($updatedValue)) {
1769 1909 return new WP_Error('empty_data', __('Failed to update, Data is empty.', 'bit-form'));
1770 1910 }
@@ -1772,9 +1912,8 @@
1772 1912 if (is_null($formID) || is_null($entryID)) {
1773 1913 return new WP_Error('empty_data', __('Invalid Form ID or entries ID.', 'bit-form'));
1774 1914 }
1775 1915
1776 - $formManager = new AdminFormManager($formID);
1777 1916 if (!$formManager->isExist()) {
1778 1917 return new WP_Error('empty_data', __('Form does not exist.', 'bit-form'));
1779 1918 }
1780 1919 return $formManager->updateFormEntry($updatedValue, $formID, $entryID);
@@ -1779,9 +1918,10 @@
1779 1918 }
1780 1919 return $formManager->updateFormEntry($updatedValue, $formID, $entryID);
1781 1920 }
1782 1921
1783 - public function getLogHistory($Request, $post) {
1922 + public function getLogHistory($Request, $post)
1923 + {
1784 1924 if (isset($Request['formID'])) {
1785 1925 $formID = wp_unslash($Request['formID']);
1786 1926 $entryID = wp_unslash($Request['entryID']);
1787 1927 } else {
@@ -1802,9 +1942,10 @@
1802 1942 $log_history = $formLogModel->geLogHistory($formID, $entryID);
1803 1943 return $log_history;
1804 1944 }
1805 1945
1806 - public function importDataStore($Request, $post) {
1946 + public function importDataStore($Request, $post)
1947 + {
1807 1948 if (isset($Request['formID'])) {
1808 1949 $formID = wp_unslash($Request['formID']);
1809 1950 } else {
1810 1951 $formID = wp_unslash($post->formID);
@@ -1813,9 +1954,10 @@
1813 1954 return new WP_Error('empty_form', __('Invalid Form id or entries id.', 'bit-form'));
1814 1955 }
1815 1956 }
1816 1957
1817 - public function getAllWPPages($Request, $post) {
1958 + public function getAllWPPages($Request, $post)
1959 + {
1818 1960 $pages = get_pages(['post_status' => 'publish', 'sort_column' => 'post_date', 'sort_order' => 'desc']);
1819 1961 $allPages = [];
1820 1962 foreach ($pages as $pageKey => $pageDetails) {
1821 1963 $allPages[$pageKey]['title'] = $pageDetails->post_title;
@@ -1823,9 +1965,10 @@
1823 1965 }
1824 1966 return $allPages;
1825 1967 }
1826 1968
1827 - public function deleteAIntegration($Request, $post) {
1969 + public function deleteAIntegration($Request, $post)
1970 + {
1828 1971 if (isset($Request['formID']) && $Request['id']) {
1829 1972 $formID = json_decode(wp_unslash($Request['formID']));
1830 1973 $id = wp_unslash($Request['id']);
1831 1974 } else {
@@ -1846,9 +1989,10 @@
1846 1989 'message' => __('Integration deleted', 'bit-form'),
1847 1990 ];
1848 1991 }
1849 1992
1850 - public function deleteSuccessMessage($Request, $post) {
1993 + public function deleteSuccessMessage($Request, $post)
1994 + {
1851 1995 if (isset($Request['formID']) && $Request['id']) {
1852 1996 $formID = json_decode(wp_unslash($Request['formID']));
1853 1997 $id = wp_unslash($Request['id']);
1854 1998 } else {
@@ -1868,9 +2012,10 @@
1868 2012 'message' => __('Message deleted', 'bit-form'),
1869 2013 ];
1870 2014 }
1871 2015
1872 - public function deleteAWorkflow($Request, $post) {
2016 + public function deleteAWorkflow($Request, $post)
2017 + {
1873 2018 if (isset($Request['formID']) && $Request['id']) {
1874 2019 $formID = json_decode(wp_unslash($Request['formID']));
1875 2020 $id = wp_unslash($Request['id']);
1876 2021 } else {
@@ -1889,9 +2034,10 @@
1889 2034 'message' => __('workflow deleted', 'bit-form'),
1890 2035 ];
1891 2036 }
1892 2037
1893 - public function deleteAMailTemplate($Request, $post) {
2038 + public function deleteAMailTemplate($Request, $post)
2039 + {
1894 2040 if (isset($Request['formID']) && $Request['id']) {
1895 2041 $formID = json_decode(wp_unslash($Request['formID']));
1896 2042 $id = wp_unslash($Request['id']);
1897 2043 } else {
@@ -1910,9 +2056,10 @@
1910 2056 'message' => __('Email Template deleted', 'bit-form'),
1911 2057 ];
1912 2058 }
1913 2059
1914 - public function duplicateAMailTemplate($Request, $post) {
2060 + public function duplicateAMailTemplate($Request, $post)
2061 + {
1915 2062 if (isset($Request['formID']) && $Request['id']) {
1916 2063 $formID = json_decode(wp_unslash($Request['formID']));
1917 2064 $id = wp_unslash($Request['id']);
1918 2065 } else {
@@ -1931,9 +2078,10 @@
1931 2078 'message' => __('Email Template duplicated', 'bit-form'),
1932 2079 ];
1933 2080 }
1934 2081
1935 - public function setAllFormsReport($Request, $post) {
2082 + public function setAllFormsReport($Request, $post)
2083 + {
1936 2084 $reports = empty($post->reports) ? null : wp_unslash($post->reports);
1937 2085 if (empty($reports)) {
1938 2086 return new WP_Error('empty_report_prefs', 'Report data is Empty, nothing to save or update.');
1939 2087 }
@@ -2009,9 +2157,10 @@
2009 2157 'reports' => empty($reports) ? [] : $reports,
2010 2158 ];
2011 2159 }
2012 2160
2013 - public function savegReCaptcha($Request, $post) {
2161 + public function savegReCaptcha($Request, $post)
2162 + {
2014 2163 $reCaptcha = $post->reCaptcha;
2015 2164
2016 2165 if (is_null($reCaptcha)) {
2017 2166 return new WP_Error('empty_gcaptchdetails', __('g-ReCAPTCHA details is empty', 'bit-form'));
@@ -2045,9 +2194,10 @@
2045 2194 }
2046 2195 return $response;
2047 2196 }
2048 2197
2049 - public function savePaymentSetting($Request, $post) {
2198 + public function savePaymentSetting($Request, $post)
2199 + {
2050 2200 if (isset($post->paySetting)) {
2051 2201 $paySetting = $post->paySetting;
2052 2202 }
2053 2203 if (is_null($paySetting)) {
@@ -2055,12 +2205,21 @@
2055 2205 }
2056 2206 if (is_string($paySetting)) {
2057 2207 $paySetting = json_decode($paySetting);
2058 2208 }
2209 + $canSave = null;
2210 + // $checkedCanSave = [
2211 + // 'PayPal' => $this->addWebhookToPaypal($paySetting),
2212 + // 'Razorpay' => true,
2213 + // 'Stripe' => $this->addWebhookToStripe($paySetting),
2214 + // ];
2215 + // $canSave = $checkedCanSave[$paySetting->type];
2059 2216 if ('PayPal' === $paySetting->type) {
2060 2217 $canSave = $this->addWebhookToPaypal($paySetting);
2061 2218 } elseif ('Razorpay' === $paySetting->type) {
2062 2219 $canSave = true;
2220 + } elseif ('Stripe' === $paySetting->type) {
2221 + $canSave = $this->addWebhookToStripe($paySetting);
2063 2222 }
2064 2223 if (is_null($canSave)) {
2065 2224 return ['message' => __('Please re-check your Client ID & Secret', 'bit-form')];
2066 2225 }
@@ -2087,9 +2246,10 @@
2087 2246 }
2088 2247 return $response;
2089 2248 }
2090 2249
2091 - private function addWebhookToPaypal($paySetting) {
2250 + private function addWebhookToPaypal($paySetting)
2251 + {
2092 2252 $transaction_mode = $paySetting->mode;
2093 2253 $clientId = $paySetting->clientID;
2094 2254 $clientSecret = $paySetting->clientSecret;
2095 2255 $base_url = 'sandbox' === $transaction_mode ? 'https://api-m.sandbox.paypal.com' : 'https://api.paypal.com';
@@ -2115,9 +2275,49 @@
2115 2275 }
2116 2276 return true;
2117 2277 }
2118 2278
2119 - public function builerHelperState($formId) {
2279 + private function addWebhookToStripe($paySetting)
2280 + {
2281 + // $transaction_mode = $paySetting->mode;
2282 + // $publishableKey = $paySetting->publishableKey;
2283 + $clientSecret = $paySetting->clientSecret;
2284 +
2285 + $create_webhook_url = 'https://api.stripe.com/v1/webhook_endpoints';
2286 + $headers = [
2287 + 'Content-Type' => 'application/x-www-form-urlencoded',
2288 + 'Authorization' => "Bearer $clientSecret",
2289 + ];
2290 +
2291 + $webhook_url = get_rest_url() . 'bitform/v1/payments/stripe';
2292 +
2293 + $apiResponse = HttpHelper::get($create_webhook_url, '', $headers);
2294 + $webhookList = $apiResponse->data;
2295 + $webhook_already_exist = false;
2296 +
2297 + foreach ($webhookList as $webhook) {
2298 + if ($webhook->url === $webhook_url) {
2299 + $webhook_already_exist = true;
2300 + break;
2301 + }
2302 + }
2303 +
2304 + $data = [
2305 + 'enabled_events[]' => 'payment_intent.succeeded',
2306 + 'url' => $webhook_url,
2307 + ];
2308 +
2309 + if (!$webhook_already_exist) {
2310 + $webhook_response = HttpHelper::post($create_webhook_url, $data, $headers);
2311 + if (is_wp_error($webhook_response) || (isset($webhook_response->error) && $webhook_response->error)) {
2312 + return null;
2313 + }
2314 + }
2315 + return true;
2316 + }
2317 +
2318 + public function builerHelperState($formId)
2319 + {
2120 2320 $data = static::$formModel->get(
2121 2321 ['id', 'builder_helper_state'],
2122 2322 ['id' => $formId]
2123 2323 );
@@ -2132,9 +2332,10 @@
2132 2332
2133 2333 return $data;
2134 2334 }
2135 2335
2136 - public function updateGeneratedScriptPageIds() {
2336 + public function updateGeneratedScriptPageIds()
2337 + {
2137 2338 $updateData = [
2138 2339 'generated_script_page_ids' => \wp_json_encode((object) []),
2139 2340 ];
2140 2341 $update_status = static::$formModel->update(
@@ -2151,9 +2352,10 @@
2151 2352 return true;
2152 2353 }
2153 2354 }
2154 2355
2155 - public function updateSuccessMessageClassName($str, $msgIdx, $msgId) {
2356 + public function updateSuccessMessageClassName($str, $msgIdx, $msgId)
2357 + {
2156 2358 $TEMP_CONF_ID = '_tmp_' . $msgIdx . '_conf_id';
2157 2359 return str_replace($TEMP_CONF_ID, $msgId, $str);
2158 2360 }
2159 2361 }