PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.5.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.5.2
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 / Util / FormDuplicateHelper.php

FormDuplicateHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.5.2, at includes/Core/Util/FormDuplicateHelper.php

136 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace BitCode\BitForm\Core\Util;
3
4 /**
5 * Helps to relpicate form information for form duplication or export
6 *
7 */
8 final class FormDuplicateHelper
9 {
10 /**
11 * Create replica
12 *
13 * @param Array $data Data to replicate
14 * @param Integer $oldFormId Id of the form which will be replicated
15 * @param Integer|String $newFormId Id of the new form
16 *
17 * @return $formData Replicated data
18 */
19 public static function createReplica($data, $oldFormId, $newFormId)
20 {
21 $new_lay_fields = static::genarateNewLayoutNField($data['form_content']['layout'], $data['form_content']['fields'], $oldFormId, $newFormId);
22 $formName = $data['form_name'];
23 $formData = (object)[];
24 $formData->form_id = $newFormId;
25 $formData->fields = (object) $new_lay_fields['fields'];
26 $formData->layout = $new_lay_fields['layout'];
27 $formData->form_name = $formName;
28
29 if (isset($data['formSettings'])) {
30 $formSettings = (array) (!is_string($data['formSettings']) ? $data['formSettings'] : json_decode($data['formSettings']));
31
32 if (isset($formSettings['confirmation'])) {
33 $confirmation = !is_string($formSettings['confirmation']) ? json_encode($formSettings['confirmation']) : $formSettings['confirmation'];
34 $formSettings['confirmation'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}'.$newFormId.'$3', $confirmation));
35 }
36
37 if (isset($formSettings['integrations'])) {
38 $integrations = !is_string($formSettings['integrations']) ? json_encode($formSettings['integrations']) : $formSettings['integrations'];
39 $integrations = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}'.$newFormId.'$3', $integrations);
40 $formSettings['integrations'] = json_decode(\preg_replace('/(\"formField\"\s*\:\s*\"bf?)(\d+)([- \d]+)/', '${1}'.$newFormId.'$3', $integrations));
41 }
42
43 if (isset($formSettings['mailTem'])) {
44 $mailTem = !is_string($formSettings['mailTem']) ? json_encode($formSettings['mailTem']) : $formSettings['mailTem'];
45 $formSettings['mailTem'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}'.$newFormId.'$3', $mailTem));
46 $formData->formSettings = (object)$formSettings;
47 }
48 }
49
50 if (isset($data['workFlows'])) {
51 $workFlows = !is_string($data['workFlows']) ? \json_encode($data['workFlows']) : $data['workFlows'];
52 $workFlows = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}'.$newFormId.'$3', $workFlows);
53 $workFlows = json_decode(\preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}'.$newFormId.'$3', $workFlows));
54 $formData->workFlows = $workFlows;
55 }
56
57 if (isset($data['reports'])) {
58 $reports = !is_string($data['reports']) ? \json_encode($data['reports']) : $data['reports'];
59 $reports = json_decode(\preg_replace("/(\"bf?)(\d+)([- \d]+\")/", '${1}'.$newFormId.'$3', $reports));
60 $formData->reports = $reports;
61 }
62
63 if (isset($data['additional'])) {
64 $formData->additional = $data['additional'];
65 }
66
67 return $formData;
68 }
69
70 public static function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
71 {
72 $newField = (object) [];
73 foreach ((array)$layout->lg as $ind => $itm) {
74 $fld_tmp = $fields->{$layout->lg[$ind]->i};
75 $layout->lg[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->lg[$ind]->i);
76 $layout->md[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->md[$ind]->i);
77 $layout->sm[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->sm[$ind]->i);
78 $newField->{$layout->lg[$ind]->i} = json_decode(str_replace("bf$oldId-", "bf$newId-", wp_json_encode($fld_tmp)));
79 }
80 return ['layout' => $layout, 'fields' => $newField];
81 }
82
83 public static function calcRowHeight($formStyle, $formID)
84 {
85 $rowHeight = 0;
86 preg_match('/fld-lbl-'.$formID.'\s*{[\w\-\(\)\!\;\,\s\:\.\#]*font-size\s*:\s*(\d+)[px|em|rem|!important]*;/', $formStyle, $fontSize);
87 if ($fontSize) {
88 $lineHeight = 1;
89 preg_match('/fld-lbl-'.$formID.'\s*{[\w\-\(\)\!\;\,\s\:\#]*line-height\s*:\s*([.\d]+)[!important]*;/', $formStyle, $lineHeightMatched);
90 if ($lineHeightMatched) {
91 $lineHeight = $lineHeightMatched[1];
92 }
93 $rowHeight = $fontSize[1] * $lineHeight;
94 }
95
96 preg_match('/fld-wrp-'.$formID.'\s*{[\w\-\(\)\!\;\,\s\:\#]*padding\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $padding);
97 if ($padding) {
98 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(" ", preg_replace('/px|em|rem|!important/', "", $padding[1])), 'Y');
99 }
100
101 preg_match('/input.fld-'. $formID .'\s*,\s*textarea.fld-'. $formID .'\s*{[\w\-\(\)\!\;\,\s\:\#]*margin\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $margin);
102 if ($margin) {
103 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(" ", preg_replace('/px|em|rem|!important/', "", $margin[1])), 'Y');
104 }
105 preg_match('/input.fld-'. $formID .'\s*,\s*textarea.fld-'. $formID .'\s*{[\w\-\(\)\!\;\,\s\:\#]*[^line\-height]height\s*:\s*([\d\s]+)[px|em|rem|!important]*;/', $formStyle, $height);
106 if ($height) {
107 $rowHeight = $rowHeight + $height[1];
108 } else {
109 $rowHeight = $rowHeight + 40;
110 }
111
112 return $rowHeight / 2;
113 }
114
115 public static function cssPropSumByAxis($value, $axis)
116 {
117 if (count($value) === 1) {
118 $value = array_fill(0, 4, $value[0]);
119 }
120
121 if (count($value) === 2) {
122 $value[] = $value[0];
123 $value[] = $value[1];
124 }
125
126 if (count($value) === 3) {
127 $value[] = $value[1];
128 }
129 if ($axis === 'X') {
130 return intval($value[1]) + intval($value[3]);
131 } else {
132 return intval($value[0]) + intval($value[2]);
133 }
134 }
135 }
136