| 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 |
$newFieldProperty = []; |
| 79 |
|
| 80 |
// convert unchange property |
| 81 |
foreach ($convertFormate['unchangeProperty'] as $property) { |
| 82 |
if (property_exists($field, $property)) { |
| 83 |
$newFieldProperty[$property] = $field->{$property}; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// add new property |
| 88 |
foreach ($convertFormate['newProperty'] as $property => $value) { |
| 89 |
$properties = explode('->', $property); |
| 90 |
self::setNestedProperty($newFieldProperty, $properties, $value); |
| 91 |
} |
| 92 |
|
| 93 |
// modify changed property |
| 94 |
foreach ($convertFormate['changedProperty'] as $oldProperty => $newPropertyArr) { |
| 95 |
$properties = explode('->', $oldProperty); |
| 96 |
$value = self::getNestedPropertyValue($field, $properties); |
| 97 |
foreach ($newPropertyArr as $newProperty) { |
| 98 |
if (is_null($value) && isset($newProperty['val'])) { |
| 99 |
$value = $newProperty['val']; |
| 100 |
} |
| 101 |
if (isset($newProperty['isEqualTo']) && $value === $newProperty['isEqualTo']) { |
| 102 |
$value = $newProperty['val']; |
| 103 |
} elseif (isset($newProperty['isEqualTo'])) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
if (!is_null($value)) { |
| 107 |
$properties = explode('->', $newProperty['path']); |
| 108 |
self::setNestedProperty($newFieldProperty, $properties, $value); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
$newFieldProperty['fieldName'] .= "-$fieldKey"; |
| 113 |
|
| 114 |
if ('select' === $newFieldProperty['typ']) { |
| 115 |
$newFieldProperty['optionsList'][0]['List-1'] = self::optionListConvert($field->opt); |
| 116 |
if (isset($newFieldProperty['val']) && is_array($newFieldProperty['val'])) { |
| 117 |
$newFieldProperty['val'] = implode(BITFORMS_BF_SEPARATOR, $newFieldProperty['val']); |
| 118 |
} |
| 119 |
if (isset($field->opt[0]->prefix_img) && !is_null($field->opt[0]->prefix_img)) { |
| 120 |
$newFieldProperty['config']['optionIcon'] = true; |
| 121 |
} |
| 122 |
} |
| 123 |
$newFieldsArray[$fieldKey] = $newFieldProperty; |
| 124 |
} |
| 125 |
|
| 126 |
return $newFieldsArray; |
| 127 |
} |
| 128 |
|
| 129 |
public static function convertLayout($layoutObj) |
| 130 |
{ |
| 131 |
$newLayoutArray = []; |
| 132 |
|
| 133 |
foreach ($layoutObj as $breakdownSize => $layoutArr) { |
| 134 |
// v1 width variation |
| 135 |
$v1WidthVariation = [ |
| 136 |
'lg' => 6, |
| 137 |
'md' => 4, |
| 138 |
'sm' => 2, |
| 139 |
]; |
| 140 |
$v2Width = 60; |
| 141 |
$v2RowHeight = 43; |
| 142 |
|
| 143 |
$newLayoutArray[$breakdownSize] = []; |
| 144 |
foreach ($layoutArr as $index => $fieldLayout) { |
| 145 |
$fieldLayout->w = $fieldLayout->w * ($v2Width / $v1WidthVariation[$breakdownSize]); |
| 146 |
$fieldLayout->x = $fieldLayout->x * ($v2Width / $v1WidthVariation[$breakdownSize]); |
| 147 |
$fieldLayout->h = $fieldLayout->h * $v2RowHeight; |
| 148 |
$fieldLayout->y = $fieldLayout->y * $v2RowHeight; |
| 149 |
unset($fieldLayout->minH, $fieldLayout->maxH); |
| 150 |
$newLayoutArray[$breakdownSize][] = $fieldLayout; |
| 151 |
} |
| 152 |
} |
| 153 |
return $newLayoutArray; |
| 154 |
} |
| 155 |
|
| 156 |
public static function convertSuccessMessage($successMessageArr) |
| 157 |
{ |
| 158 |
$newSuccessMessageArr = []; |
| 159 |
|
| 160 |
// this config is set as snackbar in bottom right corner |
| 161 |
$defaultMessageConfig = [ |
| 162 |
'msgType' => 'snackbar', |
| 163 |
'position' => 'bottom-right', |
| 164 |
'animation'=> 'slide-up', |
| 165 |
'autoHide' => true, |
| 166 |
'duration' => 3, |
| 167 |
'styles' => [ |
| 168 |
'width' => '300px', |
| 169 |
'padding' => '10px 35px 10px 20px', |
| 170 |
'background' => '#383838', |
| 171 |
'color' => '#fff', |
| 172 |
'borderWidth' => '1px', |
| 173 |
'borderType' => 'none', |
| 174 |
'borderColor' => 'var(--bg-5)', |
| 175 |
'borderRadius'=> 'var(--g-bdr-rad)', |
| 176 |
'boxShadow' => [ |
| 177 |
[ |
| 178 |
'x' => '0px', |
| 179 |
'y' => '27px', |
| 180 |
'blur' => '30px', |
| 181 |
'spread'=> '', |
| 182 |
'color' => 'rgb(0 0 0 / 18%)', |
| 183 |
'inset' => '' |
| 184 |
] |
| 185 |
], |
| 186 |
'closeBackground'=> '#666', |
| 187 |
'closeHover' => 'var(--bg-10)', |
| 188 |
'closeIconColor' => '#fff', |
| 189 |
'closeIconHover' => 'var(--bg-50)' |
| 190 |
] |
| 191 |
]; |
| 192 |
foreach ($successMessageArr as $message) { |
| 193 |
$message->config = $defaultMessageConfig; |
| 194 |
$newSuccessMessageArr[] = $message; |
| 195 |
} |
| 196 |
return $newSuccessMessageArr; |
| 197 |
} |
| 198 |
|
| 199 |
public static function recursiveDeleteFiles($src) |
| 200 |
{ |
| 201 |
if (is_file($src)) { |
| 202 |
return unlink($src); |
| 203 |
} elseif (is_dir($src)) { |
| 204 |
$scan = glob(rtrim($src, '/') . '/*'); |
| 205 |
foreach ($scan as $path) { |
| 206 |
self::recursiveDeleteFiles($path); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public static function recursiveCopyFiles($src, $dst) |
| 212 |
{ |
| 213 |
if (is_file($src) && !is_dir($dst)) { |
| 214 |
return copy($src, $dst); |
| 215 |
} elseif (is_dir($src)) { |
| 216 |
$scan = glob(rtrim($src, '/') . '/*'); |
| 217 |
foreach ($scan as $path) { |
| 218 |
self::recursiveCopyFiles($path, $dst . '/' . basename($path)); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|