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