Last active
December 12, 2021 00:08
-
-
Save alfredo-wpmudev/8b9b251f560eea6c52716e5df01e55f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Working in progress | |
$arguments =[ | |
'formID' => 1046668, | |
'variableValue' => '{hidden-1}', | |
'fixedValue' => 25, | |
'showField' => '{text-2}', | |
'triggerField' => '{text-1},{text-3}', | |
'logic' => 'if({text-1}>=5 && {text-1}<=9){{hidden-1}=fixedValue*0.9}else if({text-1}>=10 && {text-1}<=16){{hidden-1}=fixedValue*0.8}else{{hidden-1}=fixedValue}' | |
]; | |
$js_var =[]; | |
$patron = '/{(\w+)-(\d+)}/'; | |
$declaration_replacement = '${1}-$2'; | |
$replacement = '${1}$2'; | |
$math_pathern = '/([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+)/'; | |
$conditional = ''; | |
$js_code =''; | |
$js_code .='if(jQuery("#forminator-module-'.$arguments['formID'].'")){ '; | |
if($arguments['variableValue']){ | |
$hidden = preg_replace($patron, $declaration_replacement, $arguments['variableValue']); | |
$js_declaration_variableValue = 'var variableField = jQuery("#'.$hidden.'"); '; | |
$js_code .=$js_declaration_variableValue; | |
$js_var[$arguments['variableValue']] = 'variableField'; | |
} | |
if($arguments['fixedValue']){ | |
$js_declaration_fixedValue = 'var fixedValue = '.$arguments['fixedValue'].'; '; | |
$js_code .=$js_declaration_fixedValue; | |
$js_var[$arguments['fixedValue']] = 'fixedValue'; | |
} | |
if($arguments['showField']){ | |
$showField = preg_replace($patron, $declaration_replacement, $arguments['showField']); | |
$js_declaration_showField = ' var showField = jQuery("#forminator-field-'.$showField.'"); | |
showField.prop("disabled", true);'; | |
$js_code .=$js_declaration_showField; | |
$js_var[$arguments['showField']] = 'showField'; | |
} | |
if($arguments['triggerField']){ | |
$arguments['triggerField'] = str_replace(' ', '', $arguments['triggerField']); | |
$manyTriggers = strpos($arguments['triggerField'], ","); | |
if($manyTriggers === false){ | |
$triggerField = preg_replace($patron, $declaration_replacement, $arguments['triggerField']); | |
$sufix = preg_replace($patron, $replacement, $arguments['triggerField']); | |
$js_declaration_triggerField = ' var triggerField'.$sufix.' = jQuery("#forminator-field-'.$triggerField.'");'; | |
$js_code .=$js_declaration_triggerField; | |
$js_var[$arguments['triggerField']] = 'triggerField'.$sufix; | |
$js_var['triggers'] []= 'triggerField'.$sufix; | |
}else{ | |
$triggerFieldArray = explode (",", $arguments['triggerField']); | |
foreach ($triggerFieldArray as $trigger) { | |
$triggerField = preg_replace($patron, $declaration_replacement, $trigger); | |
$sufix = preg_replace($patron, $replacement, $trigger); | |
$js_declaration_triggerField = ' var triggerField'.$sufix.' = jQuery("#forminator-field-'.$triggerField.'");'; | |
$js_code .=$js_declaration_triggerField; | |
$js_var[$trigger] = 'triggerField'.$sufix; | |
$js_var['triggers'] []= 'triggerField'.$sufix; | |
} | |
} | |
} | |
if($arguments['logic']){ | |
$conditional = str_replace(' ', '', $arguments['logic']); | |
$matches = array(); | |
$math = array(); | |
preg_match_all($patron, $conditional, $matches); | |
echo '<br>'; | |
foreach ($matches[0] as $match) { | |
$conditional = str_replace($match, $js_var[$match].".val()",$conditional); | |
} | |
$conditional = preg_replace($math_pathern, "$0", $conditional); | |
$conditional = str_replace('variableField.val()=', 'variableField.val(',$conditional); | |
$conditional = str_replace('}', ');}',$conditional); | |
$conditional = str_replace('elseif', 'else if',$conditional); | |
$conditional = str_replace('&&', ' && ',$conditional); | |
$conditional = str_replace('||', ' || ',$conditional); | |
$js_function ='function applyDiscountForm'.$arguments['formID'].'(){'; | |
$js_function .= $conditional; | |
$js_function .= 'showField.val(variableField.val());' ; | |
$js_function .='}'; | |
$js_code .=$js_function ; | |
} | |
foreach ($js_var['triggers'] as $trigger) { | |
$js_code .=$trigger.'.change(applyDiscountForm'.$arguments['formID'].');' ; | |
} | |
$js_code .='}'; | |
echo $js_code; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment