| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\WorkFlow; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 6 |
use BitCode\BitForm\Core\Messages\SuccessMessageHandler; |
| 7 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 8 |
use BitCode\BitForm\Core\Util\SmartTags; |
| 9 |
|
| 10 |
final class Helper { |
| 11 |
public static function getFieldData($fields) { |
| 12 |
$fieldData = []; |
| 13 |
|
| 14 |
foreach ($fields as $fieldKey => $fieldDetail) { |
| 15 |
$fieldData[$fieldKey] = [ |
| 16 |
'key' => $fieldKey, |
| 17 |
'value' => empty($fieldDetail->val) ? '' : $fieldDetail->val, |
| 18 |
'type' => $fieldDetail->typ, |
| 19 |
]; |
| 20 |
if (isset($fieldDetail->mul)) { |
| 21 |
$fieldData[$fieldKey] = |
| 22 |
array_merge( |
| 23 |
$fieldData[$fieldKey], |
| 24 |
[ |
| 25 |
'mul' => true, |
| 26 |
] |
| 27 |
); |
| 28 |
} |
| 29 |
} |
| 30 |
return $fieldData; |
| 31 |
} |
| 32 |
|
| 33 |
public static function smartFldMargeFormFld($logics, $fieldData) { |
| 34 |
$fieldKeys = SmartTags::smartTagFieldKeys(); |
| 35 |
|
| 36 |
foreach ($logics as $logic) { |
| 37 |
$removeSpecialStr = ['${', '}', '()']; |
| 38 |
if (isset($logic->field) && in_array(str_replace($removeSpecialStr, '', $logic->field), $fieldKeys)) { |
| 39 |
$field = str_replace($removeSpecialStr, '', $logic->field); |
| 40 |
|
| 41 |
$fldKey = '${' . $field . '}'; |
| 42 |
$customValue = isset($logic->smartKey) ? $logic->smartKey : ''; |
| 43 |
$val = SmartTags::getSmartTagValue($field, false, $customValue); |
| 44 |
|
| 45 |
$fieldData[$fldKey] = [ |
| 46 |
'key' => $fldKey, |
| 47 |
'value' => empty($val) ? '' : $val, |
| 48 |
'type' => is_string($val) ? 'text' : 'array', |
| 49 |
]; |
| 50 |
} |
| 51 |
} |
| 52 |
return $fieldData; |
| 53 |
} |
| 54 |
|
| 55 |
public static function replaceFieldWithValue($stringToReplaceField, $fieldValues, $evalMathExpr = true) { |
| 56 |
$stringToReplaceField = FieldValueHandler::replaceFieldWithValue($stringToReplaceField, $fieldValues); |
| 57 |
if ($evalMathExpr) { |
| 58 |
return self::evalMathExpression($stringToReplaceField); |
| 59 |
} |
| 60 |
return $stringToReplaceField; |
| 61 |
} |
| 62 |
|
| 63 |
public static function setDefaultSubmitConfirmation($confirmationType, $fieldValue, $formId, $logID = 0) { |
| 64 |
$messageId = 0; |
| 65 |
$returnableData = null; |
| 66 |
return [ |
| 67 |
'msg_id' => $messageId, |
| 68 |
'confirmation' => $returnableData, |
| 69 |
]; |
| 70 |
$integrationHandler = new IntegrationHandler($formId); |
| 71 |
switch ($confirmationType) { |
| 72 |
case 'successMsg': |
| 73 |
$successMessageHandler |
| 74 |
= new SuccessMessageHandler($formId); |
| 75 |
$successMessage = $successMessageHandler->getAllMessage(); |
| 76 |
if (!is_wp_error($successMessage) && !empty($successMessage) && count($successMessage) > 0) { |
| 77 |
$returnableData = self::replaceFieldWithValue($successMessage[0]->message_content, $fieldValue); |
| 78 |
$messageId = $successMessage[0]->id; |
| 79 |
} |
| 80 |
break; |
| 81 |
case 'redirectPage': |
| 82 |
$redirectPage = $integrationHandler->getAllIntegration('form', 'redirectPage'); |
| 83 |
if (!is_wp_error($redirectPage) && !empty($redirectPage) && count($redirectPage) > 0) { |
| 84 |
$url = json_decode($redirectPage[0]->integration_details)->url; |
| 85 |
if (!empty($url)) { |
| 86 |
$url = self::replaceFieldWithValue($url, $fieldValue); |
| 87 |
} |
| 88 |
$returnableData = empty($url) ? '' : esc_url_raw($url); |
| 89 |
} |
| 90 |
break; |
| 91 |
case 'webHooks': |
| 92 |
$webHooks = $integrationHandler->getAllIntegration('form', 'webHooks'); |
| 93 |
if (!is_wp_error($webHooks) && !empty($webHooks) && 1 === count($webHooks)) { |
| 94 |
$returnableData = ["{\"id\":{$webHooks[0]->id}}"]; |
| 95 |
} |
| 96 |
break; |
| 97 |
default: |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
return [ |
| 102 |
'msg_id' => $messageId, |
| 103 |
'confirmation' => $returnableData, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
public static function calculte($firstOperand, $secondOperand, $operator) { |
| 108 |
switch ($operator) { |
| 109 |
case '+': |
| 110 |
return $firstOperand + $secondOperand; |
| 111 |
case '-': |
| 112 |
return $firstOperand - $secondOperand; |
| 113 |
case '*': |
| 114 |
return $firstOperand * $secondOperand; |
| 115 |
case '/': |
| 116 |
return $firstOperand / $secondOperand; |
| 117 |
case '^': |
| 118 |
return $firstOperand ** $secondOperand; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public static function filterMailContentType() { |
| 123 |
return 'text/html'; |
| 124 |
} |
| 125 |
|
| 126 |
public static function evalMathExpression($stringWithFieldValue) { |
| 127 |
$mathExpr = $stringWithFieldValue; |
| 128 |
if (empty($mathExpr)) { |
| 129 |
return $stringWithFieldValue; |
| 130 |
} |
| 131 |
preg_match_all('/[\+\-\*\/\s]+/', $mathExpr, $isMathExpr); |
| 132 |
if (empty($isMathExpr[0])) { |
| 133 |
return $stringWithFieldValue; |
| 134 |
} |
| 135 |
preg_match_all('/\w+/', $mathExpr, $exprValues); |
| 136 |
if (empty($exprValues[0])) { |
| 137 |
return $stringWithFieldValue; |
| 138 |
} |
| 139 |
foreach ($exprValues[0] as $opreands) { |
| 140 |
if (!is_numeric($opreands)) { |
| 141 |
return $stringWithFieldValue; |
| 142 |
} |
| 143 |
} |
| 144 |
$validOperator = ['+', '-', '*', '^', '/']; |
| 145 |
foreach ($isMathExpr[0] as $value) { |
| 146 |
if (!in_array(trim($value), $validOperator)) { |
| 147 |
return $stringWithFieldValue; |
| 148 |
} |
| 149 |
} |
| 150 |
$mathExpr = str_replace(' ', null, $mathExpr); |
| 151 |
$mathExpr = preg_replace('/\{|\[|\(/', '(', $mathExpr); |
| 152 |
$mathExpr = preg_replace('/\}|\]/', ')', $mathExpr); |
| 153 |
$calculated = self::infixToPostfixEvalute($mathExpr); |
| 154 |
if (!is_null($calculated)) { |
| 155 |
return (string) $calculated[0]; |
| 156 |
} |
| 157 |
|
| 158 |
return (string) $stringWithFieldValue; |
| 159 |
} |
| 160 |
|
| 161 |
public static function infixToPostfixEvalute($expression) { |
| 162 |
$operatorStack = []; |
| 163 |
$outputQueue = []; |
| 164 |
$numTemp = null; |
| 165 |
for ($strIndex = 0; $strIndex < strlen($expression); $strIndex++) { |
| 166 |
$token = $expression[$strIndex]; |
| 167 |
if ('+' === $token || '-' === $token || '*' === $token || '/' === $token || '^' === $token || '(' === $token || ')' === $token) { |
| 168 |
if (!is_null($numTemp)) { |
| 169 |
$outputQueue[] = $numTemp; |
| 170 |
$numTemp = null; |
| 171 |
} |
| 172 |
$stackSize = count($operatorStack); |
| 173 |
if ($stackSize) { |
| 174 |
$stackTop = $operatorStack[$stackSize - 1]; |
| 175 |
} |
| 176 |
if ('(' === $token) { |
| 177 |
$operatorStack[] = $token; |
| 178 |
} elseif (')' === $token) { |
| 179 |
while ('(' !== $operatorStack[count($operatorStack) - 1]) { |
| 180 |
$outputQueue[] = array_pop($operatorStack); |
| 181 |
if ('(' === $operatorStack[count($operatorStack) - 1]) { |
| 182 |
array_pop($operatorStack); |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
} elseif (isset($stackTop) && self::operatorPrecedence($token) > self::operatorPrecedence($stackTop)) { |
| 187 |
$operatorStack[] = $token; |
| 188 |
} elseif ('^' !== $token && $stackSize) { |
| 189 |
$operatorStack[$stackSize - 1] = $token; |
| 190 |
$outputQueue[] = $stackTop; |
| 191 |
} else { |
| 192 |
$operatorStack[] = $token; |
| 193 |
} |
| 194 |
continue; |
| 195 |
} |
| 196 |
$numTemp .= $token; |
| 197 |
if ($strIndex === strlen($expression) - 1 && !is_null($numTemp)) { |
| 198 |
$outputQueue[] = $numTemp; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (!is_null($operatorStack)) { |
| 203 |
$outputQueue = array_merge($outputQueue, array_reverse($operatorStack)); |
| 204 |
} |
| 205 |
$resultStack = []; |
| 206 |
foreach ($outputQueue as $value) { |
| 207 |
if (is_numeric($value)) { |
| 208 |
$resultStack[] = $value; |
| 209 |
continue; |
| 210 |
} |
| 211 |
$secondOperand = array_pop($resultStack); |
| 212 |
$firstOperand = array_pop($resultStack); |
| 213 |
$resultStack[] = self::calculate($firstOperand, $secondOperand, $value); |
| 214 |
} |
| 215 |
return $resultStack; |
| 216 |
} |
| 217 |
|
| 218 |
public static function operatorPrecedence($operator) { |
| 219 |
$precedence = [ |
| 220 |
'+' => 2, |
| 221 |
'-' => 2, |
| 222 |
'*' => 3, |
| 223 |
'/' => 3, |
| 224 |
'^' => 4, |
| 225 |
]; |
| 226 |
|
| 227 |
return isset($precedence[$operator]) ? $precedence[$operator] : 0; |
| 228 |
} |
| 229 |
|
| 230 |
public static function calculate($firstOperand, $secondOperand, $operator) { |
| 231 |
$calculated = [ |
| 232 |
'+' => $firstOperand + $secondOperand, |
| 233 |
'-' => $firstOperand - $secondOperand, |
| 234 |
'*' => $firstOperand * $secondOperand, |
| 235 |
'/' => $firstOperand / $secondOperand, |
| 236 |
'^' => $firstOperand ** $secondOperand, |
| 237 |
]; |
| 238 |
|
| 239 |
return isset($calculated[$operator]) ? $calculated[$operator] : ''; |
| 240 |
} |
| 241 |
} |
| 242 |
|