| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Migration; |
| 4 |
|
| 5 |
class MigrationHelper |
| 6 |
{ |
| 7 |
public static function setNestedProperty(&$rootObj, $properties, $value) |
| 8 |
{ |
| 9 |
$firstProperty = array_shift($properties); |
| 10 |
$current = &$rootObj; |
| 11 |
|
| 12 |
if (is_object($current)) { |
| 13 |
if (!property_exists($current, $firstProperty)) { |
| 14 |
$current->$firstProperty = (object)[]; |
| 15 |
} |
| 16 |
if (count($properties) > 0) { |
| 17 |
self::setNestedProperty($current->$firstProperty, $properties, $value); |
| 18 |
} else { |
| 19 |
$current->$firstProperty = $value; |
| 20 |
} |
| 21 |
} elseif (is_array($current)) { |
| 22 |
if (!array_key_exists($firstProperty, $current)) { |
| 23 |
$current[$firstProperty] = []; |
| 24 |
} |
| 25 |
if (count($properties) > 0) { |
| 26 |
self::setNestedProperty($current[$firstProperty], $properties, $value); |
| 27 |
} else { |
| 28 |
$current[$firstProperty] = $value; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public static function getNestedPropertyValue($rootObj, $properties) |
| 34 |
{ |
| 35 |
$firstProperty = array_shift($properties); |
| 36 |
$current = $rootObj; |
| 37 |
if (is_object($current) && count($properties) > 0) { |
| 38 |
return self::getNestedPropertyValue($current->$firstProperty, $properties); |
| 39 |
} elseif (is_object($current)) { |
| 40 |
return property_exists($current, $firstProperty) ? $current->$firstProperty : null; |
| 41 |
} elseif (is_array($current) && count($properties) > 0) { |
| 42 |
return self::getNestedPropertyValue($current[$firstProperty], $properties); |
| 43 |
} elseif (is_array($current)) { |
| 44 |
return array_key_exists($firstProperty, $current) ? $current[$firstProperty] : null; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function optionListConvert($optionList) |
| 49 |
{ |
| 50 |
$newOptionList = []; |
| 51 |
foreach ($optionList as $option) { |
| 52 |
$newOpt = (object)[]; |
| 53 |
if (isset($option->label)) { |
| 54 |
$newOpt->lbl = $option->label; |
| 55 |
} |
| 56 |
if (isset($option->value)) { |
| 57 |
$newOpt->val = $option->value; |
| 58 |
} |
| 59 |
if (isset($option->code)) { |
| 60 |
$newOpt->i = $option->code; |
| 61 |
} |
| 62 |
if (isset($option->prefix_img)) { |
| 63 |
$newOpt->img = str_replace('/v1', '', BITFORMS_ASSET_URI . $option->prefix_img); |
| 64 |
} |
| 65 |
$newOptionList[] = $newOpt; |
| 66 |
} |
| 67 |
return $newOptionList; |
| 68 |
} |
| 69 |
|
| 70 |
public static function convertFields($fieldsArray) |
| 71 |
{ |
| 72 |
$conversionFormate = new FieldsConversionFormate(); |
| 73 |
$newFieldsArray = []; |
| 74 |
|
| 75 |
foreach ($fieldsArray as $fieldKey => $field) { |
| 76 |
// print_r(gettype($field)); |
| 77 |
$convertFormate = $conversionFormate->getFormate($field->typ); |
| 78 |
// Unknown field type: keep the field as-is instead of fataling the migration |
| 79 |
if (empty($convertFormate)) { |
| 80 |
$newFieldsArray[$fieldKey] = $field; |
| 81 |
continue; |
| 82 |
} |
| 83 |
$newFieldProperty = []; |
| 84 |
|
| 85 |
// convert unchange property |
| 86 |
foreach ($convertFormate['unchangeProperty'] as $property) { |
| 87 |
if (property_exists($field, $property)) { |
| 88 |
$newFieldProperty[$property] = $field->{$property}; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// add new property |
| 93 |
foreach ($convertFormate['newProperty'] as $property => $value) { |
| 94 |
$properties = explode('->', $property); |
| 95 |
self::setNestedProperty($newFieldProperty, $properties, $value); |
| 96 |
} |
| 97 |
|
| 98 |
// modify changed property |
| 99 |
foreach ($convertFormate['changedProperty'] as $oldProperty => $newPropertyArr) { |
| 100 |
$properties = explode('->', $oldProperty); |
| 101 |
$value = self::getNestedPropertyValue($field, $properties); |
| 102 |
foreach ($newPropertyArr as $newProperty) { |
| 103 |
if (is_null($value) && isset($newProperty['val'])) { |
| 104 |
$value = $newProperty['val']; |
| 105 |
} |
| 106 |
if (isset($newProperty['isEqualTo']) && $value === $newProperty['isEqualTo']) { |
| 107 |
$value = $newProperty['val']; |
| 108 |
} elseif (isset($newProperty['isEqualTo'])) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
if (!is_null($value)) { |
| 112 |
$properties = explode('->', $newProperty['path']); |
| 113 |
self::setNestedProperty($newFieldProperty, $properties, $value); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
$newFieldProperty['fieldName'] .= "-$fieldKey"; |
| 118 |
|
| 119 |
if ('select' === $newFieldProperty['typ']) { |
| 120 |
$newFieldProperty['optionsList'][0]['List-1'] = self::optionListConvert($field->opt); |
| 121 |
if (isset($newFieldProperty['val']) && is_array($newFieldProperty['val'])) { |
| 122 |
$newFieldProperty['val'] = implode(BITFORMS_BF_SEPARATOR, $newFieldProperty['val']); |
| 123 |
} |
| 124 |
if (isset($field->opt[0]->prefix_img) && !is_null($field->opt[0]->prefix_img)) { |
| 125 |
$newFieldProperty['config']['optionIcon'] = true; |
| 126 |
} |
| 127 |
} |
| 128 |
$newFieldsArray[$fieldKey] = $newFieldProperty; |
| 129 |
} |
| 130 |
|
| 131 |
return $newFieldsArray; |
| 132 |
} |
| 133 |
|
| 134 |
public static function convertLayout($layoutObj) |
| 135 |
{ |
| 136 |
$newLayoutArray = []; |
| 137 |
|
| 138 |
foreach ($layoutObj as $breakdownSize => $layoutArr) { |
| 139 |
// v1 width variation |
| 140 |
$v1WidthVariation = [ |
| 141 |
'lg' => 6, |
| 142 |
'md' => 4, |
| 143 |
'sm' => 2, |
| 144 |
]; |
| 145 |
$v2Width = 60; |
| 146 |
$v2RowHeight = 43; |
| 147 |
|
| 148 |
$newLayoutArray[$breakdownSize] = []; |
| 149 |
foreach ($layoutArr as $index => $fieldLayout) { |
| 150 |
$fieldLayout->w = $fieldLayout->w * ($v2Width / $v1WidthVariation[$breakdownSize]); |
| 151 |
$fieldLayout->x = $fieldLayout->x * ($v2Width / $v1WidthVariation[$breakdownSize]); |
| 152 |
$fieldLayout->h = $fieldLayout->h * $v2RowHeight; |
| 153 |
$fieldLayout->y = $fieldLayout->y * $v2RowHeight; |
| 154 |
unset($fieldLayout->minH, $fieldLayout->maxH); |
| 155 |
$newLayoutArray[$breakdownSize][] = $fieldLayout; |
| 156 |
} |
| 157 |
} |
| 158 |
return $newLayoutArray; |
| 159 |
} |
| 160 |
|
| 161 |
public static function convertSuccessMessage($successMessageArr) |
| 162 |
{ |
| 163 |
$newSuccessMessageArr = []; |
| 164 |
|
| 165 |
// this config is set as snackbar in bottom right corner |
| 166 |
$defaultMessageConfig = [ |
| 167 |
'msgType' => 'snackbar', |
| 168 |
'position' => 'bottom-right', |
| 169 |
'animation'=> 'slide-up', |
| 170 |
'autoHide' => true, |
| 171 |
'duration' => 3, |
| 172 |
'styles' => [ |
| 173 |
'width' => '300px', |
| 174 |
'padding' => '10px 35px 10px 20px', |
| 175 |
'background' => '#383838', |
| 176 |
'color' => '#fff', |
| 177 |
'borderWidth' => '1px', |
| 178 |
'borderType' => 'none', |
| 179 |
'borderColor' => 'var(--bg-5)', |
| 180 |
'borderRadius'=> 'var(--g-bdr-rad)', |
| 181 |
'boxShadow' => [ |
| 182 |
[ |
| 183 |
'x' => '0px', |
| 184 |
'y' => '27px', |
| 185 |
'blur' => '30px', |
| 186 |
'spread'=> '', |
| 187 |
'color' => 'rgb(0 0 0 / 18%)', |
| 188 |
'inset' => '' |
| 189 |
] |
| 190 |
], |
| 191 |
'closeBackground'=> '#666', |
| 192 |
'closeHover' => 'var(--bg-10)', |
| 193 |
'closeIconColor' => '#fff', |
| 194 |
'closeIconHover' => 'var(--bg-50)' |
| 195 |
] |
| 196 |
]; |
| 197 |
foreach ($successMessageArr as $message) { |
| 198 |
$message->config = $defaultMessageConfig; |
| 199 |
$newSuccessMessageArr[] = $message; |
| 200 |
} |
| 201 |
return $newSuccessMessageArr; |
| 202 |
} |
| 203 |
|
| 204 |
public static function convertWebHooksToIntegrations($formSettingsV1) |
| 205 |
{ |
| 206 |
$webHooks = $formSettingsV1->confirmation->type->webHooks; |
| 207 |
$integrations = $formSettingsV1->integrations; |
| 208 |
foreach ($webHooks as $webHook) { |
| 209 |
$webHook->type = 'Web Hooks'; |
| 210 |
$webHook->name = $webHook->title; |
| 211 |
$integrations[] = $webHook; |
| 212 |
} |
| 213 |
return $integrations; |
| 214 |
} |
| 215 |
|
| 216 |
public static function duplicateV1StyleFiles() |
| 217 |
{ |
| 218 |
$source = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'; |
| 219 |
$destination = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'; |
| 220 |
if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1')) { |
| 221 |
wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'); |
| 222 |
} |
| 223 |
self::recursiveCopyFiles($source, $destination); |
| 224 |
self::recursiveDeleteFiles($source); |
| 225 |
} |
| 226 |
|
| 227 |
public static function getBitformDefaultStyleClass() |
| 228 |
{ |
| 229 |
return self::arrayToCssString(BitformDefaultStyle::commonStyleClasses()); |
| 230 |
} |
| 231 |
|
| 232 |
public static function recursiveDeleteFiles($src) |
| 233 |
{ |
| 234 |
if (is_file($src)) { |
| 235 |
return wp_delete_file($src); |
| 236 |
} elseif (is_dir($src)) { |
| 237 |
$scan = glob(rtrim($src, '/') . '/*'); |
| 238 |
foreach ($scan as $path) { |
| 239 |
self::recursiveDeleteFiles($path); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public static function recursiveCopyFiles($src, $dst) |
| 245 |
{ |
| 246 |
if (is_file($src) && !is_dir($dst)) { |
| 247 |
return copy($src, $dst); |
| 248 |
} elseif (is_dir($src)) { |
| 249 |
$scan = glob(rtrim($src, '/') . '/*'); |
| 250 |
foreach ($scan as $path) { |
| 251 |
self::recursiveCopyFiles($path, $dst . '/' . basename($path)); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public static function arrayToCssString($array) |
| 257 |
{ |
| 258 |
$cssString = ''; |
| 259 |
|
| 260 |
foreach ($array as $selector => $properties) { |
| 261 |
$cssString .= $selector . ' {'; |
| 262 |
|
| 263 |
foreach ($properties as $property => $value) { |
| 264 |
$cssString .= $property . ': ' . $value . '; '; |
| 265 |
} |
| 266 |
|
| 267 |
// Remove the trailing space and add closing brace |
| 268 |
$cssString = rtrim($cssString, ' ') . '}'; |
| 269 |
} |
| 270 |
|
| 271 |
return $cssString; |
| 272 |
} |
| 273 |
|
| 274 |
public static function formatFormData($formData) |
| 275 |
{ |
| 276 |
$newFormData = json_decode(wp_json_encode($formData)); |
| 277 |
|
| 278 |
$formContent = json_decode(wp_json_encode($formData->form_content)); |
| 279 |
|
| 280 |
$newFormData->fields = $formContent->fields; |
| 281 |
$newFormData->layout = $formContent->layout; |
| 282 |
$newFormData->nestedLayouts = $formContent->nestedLayout; |
| 283 |
$newFormData->formInfo = $formContent->formInfo; |
| 284 |
|
| 285 |
if (is_array($formData->reports) && count($formData->reports) > 0) { |
| 286 |
$newFormData->currentReport = $formData->reports[0]; |
| 287 |
$newFormData->report_id = $formData->reports[0]->id; |
| 288 |
} |
| 289 |
|
| 290 |
return $newFormData; |
| 291 |
} |
| 292 |
} |
| 293 |
|