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