PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.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
bit-form / includes / Core / Util / FormDuplicateHelper.php

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

153 lines 6.4 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\Util;
4
5 /**
6 * Helps to relpicate form information for form duplication or export
7 *
8 */
9 final class FormDuplicateHelper {
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 // $new_lay_fields = static::genarateNewLayoutNField($data['form_content']['layout'], $data['form_content']['fields'], $oldFormId, $newFormId);
21 $formName = $data['form_name'];
22 $formData = (object)[];
23 $formData->form_id = $newFormId;
24 $formData->fields = (object) $data['form_content']['fields'];
25 $formData->layout = $data['form_content']['layout'];
26 $formData->form_name = $formName;
27
28 if (isset($data['formSettings'])) {
29 $formSettings = (array) (!is_string($data['formSettings']) ? $data['formSettings'] : json_decode($data['formSettings']));
30
31 if (isset($formSettings['confirmation'])) {
32 $confirmation = !is_string($formSettings['confirmation']) ? json_encode($formSettings['confirmation']) : $formSettings['confirmation'];
33 $formSettings['confirmation'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $confirmation));
34 }
35
36 if (isset($formSettings['integrations'])) {
37 $integrations = !is_string($formSettings['integrations']) ? json_encode($formSettings['integrations']) : $formSettings['integrations'];
38 $integrations = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $integrations);
39 $formSettings['integrations'] = json_decode(\preg_replace('/(\"formField\"\s*\:\s*\"bf?)(\d+)([- \d]+)/', '${1}' . $newFormId . '$3', $integrations));
40 }
41
42 if (isset($formSettings['mailTem'])) {
43 $mailTem = !is_string($formSettings['mailTem']) ? json_encode($formSettings['mailTem']) : $formSettings['mailTem'];
44 $formSettings['mailTem'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $mailTem));
45 $formData->formSettings = (object)$formSettings;
46 }
47 }
48
49 if (isset($data['breakpointSize'])) {
50 $formData->breakpointSize = $data['breakpointSize'];
51 }
52 if (isset($data['style'])) {
53 // $styleTemp = !is_string($data['style']) ? json_encode($data['style']) : $data['style'];
54 // $formData->style = json_decode(\preg_replace('/(\.?b)(\d+)(-\d+)/', '${1}' . $newFormId . '$3', $styleTemp));
55 $formData->style = $data['style'];
56 }
57 if (isset($data['themeVars'])) {
58 $formData->themeVars = $data['themeVars'];
59 }
60 if (isset($data['staticStyles'])) {
61 $formData->staticStyles = $data['staticStyles'];
62 }
63 if (isset($data['themeColors'])) {
64 $formData->themeColors = $data['themeColors'];
65 }
66 if (isset($data['builderSettings'])) {
67 $formData->builderSettings = $data['builderSettings'];
68 }
69
70 if (isset($data['workFlows'])) {
71 $workFlows = !is_string($data['workFlows']) ? \json_encode($data['workFlows']) : $data['workFlows'];
72 $workFlows = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $workFlows);
73 $workFlows = json_decode(\preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}' . $newFormId . '$3', $workFlows));
74 $formData->workFlows = $workFlows;
75 }
76
77 if (isset($data['reports'])) {
78 $reports = !is_string($data['reports']) ? \json_encode($data['reports']) : $data['reports'];
79 $reports = json_decode(\preg_replace("/(\"bf?)(\d+)([- \d]+\")/", '${1}' . $newFormId . '$3', $reports));
80 $formData->reports = $reports;
81 }
82
83 if (isset($data['additional'])) {
84 $formData->additional = $data['additional'];
85 }
86
87 return $formData;
88 }
89
90 public static function genarateNewLayoutNField($layout, $fields, $oldId, $newId) {
91 $newField = (object) [];
92 foreach ((array)$layout->lg as $ind => $itm) {
93 $fld_tmp = $fields->{$layout->lg[$ind]->i};
94 $layout->lg[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->lg[$ind]->i);
95 $layout->md[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->md[$ind]->i);
96 $layout->sm[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->sm[$ind]->i);
97 $newField->{$layout->lg[$ind]->i} = json_decode(\preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", wp_json_encode($fld_tmp)));
98 }
99 return ['layout' => $layout, 'fields' => $newField];
100 }
101
102 public static function calcRowHeight($formStyle, $formID) {
103 $rowHeight = 0;
104 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\.\#]*font-size\s*:\s*(\d+)[px|em|rem|!important]*;/', $formStyle, $fontSize);
105 if ($fontSize) {
106 $lineHeight = 1;
107 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*line-height\s*:\s*([.\d]+)[!important]*;/', $formStyle, $lineHeightMatched);
108 if ($lineHeightMatched) {
109 $lineHeight = $lineHeightMatched[1];
110 }
111 $rowHeight = $fontSize[1] * $lineHeight;
112 }
113
114 preg_match('/fld-wrp-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*padding\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $padding);
115 if ($padding) {
116 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $padding[1])), 'Y');
117 }
118
119 preg_match('/input.fld-' . $formID . '\s*,\s*textarea.fld-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*margin\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $margin);
120 if ($margin) {
121 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $margin[1])), 'Y');
122 }
123 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);
124 if ($height) {
125 $rowHeight = $rowHeight + $height[1];
126 } else {
127 $rowHeight = $rowHeight + 40;
128 }
129
130 return $rowHeight / 2;
131 }
132
133 public static function cssPropSumByAxis($value, $axis) {
134 if (1 === count($value)) {
135 $value = array_fill(0, 4, $value[0]);
136 }
137
138 if (2 === count($value)) {
139 $value[] = $value[0];
140 $value[] = $value[1];
141 }
142
143 if (3 === count($value)) {
144 $value[] = $value[1];
145 }
146 if ('X' === $axis) {
147 return intval($value[1]) + intval($value[3]);
148 } else {
149 return intval($value[0]) + intval($value[2]);
150 }
151 }
152 }
153