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