| @@ -1,18 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\WorkFlow; |
| 4 | 4 | |
| 5 | -final class ConditionalLogic { | |
| 5 | +final class ConditionalLogic | |
| 6 | +{ | |
| 6 | 7 | protected $_workflow_condition; |
| 7 | 8 | protected $_data; |
| 8 | 9 | |
| 9 | - public function __construct($workFlowCondition, $data) { | |
| 10 | + public function __construct($workFlowCondition, $data) | |
| 11 | + { | |
| 10 | 12 | $this->_workflow_condition = $workFlowCondition; |
| 11 | 13 | $this->_data = $data; |
| 12 | 14 | } |
| 13 | 15 | |
| 14 | - public function valueToCheck($logic) { | |
| 16 | + public function valueToCheck($logic) | |
| 17 | + { | |
| 15 | 18 | if ((isset($this->_data[$this->_workflow_condition->field]['mul']) && $this->_data[$this->_workflow_condition->field]['mul']) |
| 16 | 19 | || 'check' === $this->_data[$this->_workflow_condition->field]['type'] |
| 17 | 20 | ) { |
| 18 | 21 | $fieldValue = !empty($this->_data[$this->_workflow_condition->field]['value']) ? $this->_data[$this->_workflow_condition->field]['value'] : []; |
| @@ -18,8 +21,10 @@ | ||
| 18 | 21 | $fieldValue = !empty($this->_data[$this->_workflow_condition->field]['value']) ? $this->_data[$this->_workflow_condition->field]['value'] : []; |
| 19 | 22 | if (is_string($fieldValue)) { |
| 20 | 23 | if ('[' === $fieldValue[0] && ']' === $fieldValue[strlen($fieldValue) - 1]) { |
| 21 | 24 | $fieldValue = json_decode($fieldValue); |
| 25 | + } elseif (false !== strpos($fieldValue, BITFORMS_BF_SEPARATOR)) { | |
| 26 | + $fieldValue = explode(BITFORMS_BF_SEPARATOR, $fieldValue); | |
| 22 | 27 | } else { |
| 23 | 28 | $fieldValue = explode(',', $fieldValue); |
| 24 | 29 | } |
| 25 | 30 | } |
| @@ -36,18 +41,22 @@ | ||
| 36 | 41 | |
| 37 | 42 | return $default[$logic]; |
| 38 | 43 | } |
| 39 | 44 | |
| 40 | - public function isBetween() { | |
| 45 | + public function isBetween() | |
| 46 | + { | |
| 41 | 47 | $fldValue = $this->_data[$this->_workflow_condition->field]['value']; |
| 42 | 48 | $actionsValue = json_decode($this->_workflow_condition->val); |
| 43 | - if (is_object($actionsValue) && (!empty($actionsValue->min) && !empty($actionsValue->max))) { | |
| 44 | - return $fldValue >= $actionsValue->min && $fldValue <= $actionsValue->max; | |
| 49 | + if (is_object($actionsValue)) { | |
| 50 | + $min = empty($actionsValue->min) ? '' : $actionsValue->min; | |
| 51 | + $max = empty($actionsValue->max) ? '' : $actionsValue->max; | |
| 52 | + return $fldValue >= $min && $fldValue <= $max; | |
| 45 | 53 | } |
| 46 | 54 | return false; |
| 47 | 55 | } |
| 48 | 56 | |
| 49 | - public function isEqual() { | |
| 57 | + public function isEqual() | |
| 58 | + { | |
| 50 | 59 | $valueToCheckFun = self::valueToCheck('equal'); |
| 51 | 60 | |
| 52 | 61 | if (!is_array($valueToCheckFun)) { |
| 53 | 62 | return $valueToCheckFun; |
| @@ -70,9 +79,10 @@ | ||
| 70 | 79 | } |
| 71 | 80 | return false; |
| 72 | 81 | } |
| 73 | 82 | |
| 74 | - public function isNotEqual() { | |
| 83 | + public function isNotEqual() | |
| 84 | + { | |
| 75 | 85 | $valueToCheckFun = self::valueToCheck('not_equal'); |
| 76 | 86 | |
| 77 | 87 | if (!is_array($valueToCheckFun)) { |
| 78 | 88 | return $valueToCheckFun; |
| @@ -94,17 +104,20 @@ | ||
| 94 | 104 | } |
| 95 | 105 | return $valueToCheckLenght === $checker; |
| 96 | 106 | } |
| 97 | 107 | |
| 98 | - public function isNull() { | |
| 108 | + public function isNull() | |
| 109 | + { | |
| 99 | 110 | return empty($this->_data[$this->_workflow_condition->field]['value']); |
| 100 | 111 | } |
| 101 | 112 | |
| 102 | - public function isNotNull() { | |
| 113 | + public function isNotNull() | |
| 114 | + { | |
| 103 | 115 | return !empty($this->_data[$this->_workflow_condition->field]['value']); |
| 104 | 116 | } |
| 105 | 117 | |
| 106 | - public function isContain() { | |
| 118 | + public function isContain() | |
| 119 | + { | |
| 107 | 120 | $valueToCheckFun = self::valueToCheck('contain'); |
| 108 | 121 | |
| 109 | 122 | if (!is_array($valueToCheckFun)) { |
| 110 | 123 | return $valueToCheckFun; |
| @@ -124,9 +137,10 @@ | ||
| 124 | 137 | } |
| 125 | 138 | return false; |
| 126 | 139 | } |
| 127 | 140 | |
| 128 | - public function isContainAll() { | |
| 141 | + public function isContainAll() | |
| 142 | + { | |
| 129 | 143 | $valueToCheckFun = self::valueToCheck('contain_all'); |
| 130 | 144 | |
| 131 | 145 | if (!is_array($valueToCheckFun)) { |
| 132 | 146 | return $valueToCheckFun; |
| @@ -146,9 +160,10 @@ | ||
| 146 | 160 | } |
| 147 | 161 | return false; |
| 148 | 162 | } |
| 149 | 163 | |
| 150 | - public function isNotContain() { | |
| 164 | + public function isNotContain() | |
| 165 | + { | |
| 151 | 166 | $valueToCheckFun = self::valueToCheck('not_contain'); |
| 152 | 167 | |
| 153 | 168 | if (!is_array($valueToCheckFun)) { |
| 154 | 169 | return $valueToCheckFun; |
| @@ -168,9 +183,10 @@ | ||
| 168 | 183 | } |
| 169 | 184 | return false; |
| 170 | 185 | } |
| 171 | 186 | |
| 172 | - public function isGreaterThenValue() { | |
| 187 | + public function isGreaterThenValue() | |
| 188 | + { | |
| 173 | 189 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 174 | 190 | return false; |
| 175 | 191 | } |
| 176 | 192 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] > $this->_workflow_condition->val; |
| @@ -175,9 +191,10 @@ | ||
| 175 | 191 | } |
| 176 | 192 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] > $this->_workflow_condition->val; |
| 177 | 193 | } |
| 178 | 194 | |
| 179 | - public function isLessThenValue() { | |
| 195 | + public function isLessThenValue() | |
| 196 | + { | |
| 180 | 197 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 181 | 198 | return false; |
| 182 | 199 | } |
| 183 | 200 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] < $this->_workflow_condition->val; |
| @@ -182,9 +199,10 @@ | ||
| 182 | 199 | } |
| 183 | 200 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] < $this->_workflow_condition->val; |
| 184 | 201 | } |
| 185 | 202 | |
| 186 | - public function isGreatertOrEqual() { | |
| 203 | + public function isGreatertOrEqual() | |
| 204 | + { | |
| 187 | 205 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 188 | 206 | return false; |
| 189 | 207 | } |
| 190 | 208 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] >= $this->_workflow_condition->val; |
| @@ -189,9 +207,10 @@ | ||
| 189 | 207 | } |
| 190 | 208 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] >= $this->_workflow_condition->val; |
| 191 | 209 | } |
| 192 | 210 | |
| 193 | - public function isLessOrEqual() { | |
| 211 | + public function isLessOrEqual() | |
| 212 | + { | |
| 194 | 213 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 195 | 214 | return false; |
| 196 | 215 | } |
| 197 | 216 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] <= $this->_workflow_condition->val; |
| @@ -196,9 +215,10 @@ | ||
| 196 | 215 | } |
| 197 | 216 | return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] <= $this->_workflow_condition->val; |
| 198 | 217 | } |
| 199 | 218 | |
| 200 | - public function isStarttWithString() { | |
| 219 | + public function isStarttWithString() | |
| 220 | + { | |
| 201 | 221 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 202 | 222 | return false; |
| 203 | 223 | } |
| 204 | 224 | return isset($this->_data[$this->_workflow_condition->field]['value']) && 0 === stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val); |
| @@ -203,9 +223,10 @@ | ||
| 203 | 223 | } |
| 204 | 224 | return isset($this->_data[$this->_workflow_condition->field]['value']) && 0 === stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val); |
| 205 | 225 | } |
| 206 | 226 | |
| 207 | - public function isEndWithString() { | |
| 227 | + public function isEndWithString() | |
| 228 | + { | |
| 208 | 229 | if (!isset($this->_data[$this->_workflow_condition->field]['value'])) { |
| 209 | 230 | return false; |
| 210 | 231 | } |
| 211 | 232 | $fieldValue = $this->_data[$this->_workflow_condition->field]['value']; |
| @@ -215,9 +236,10 @@ | ||
| 215 | 236 | $fieldValueEnds = strtolower(substr($fieldValue, $fieldValueLength - $compareValueLength, $fieldValueLength)); |
| 216 | 237 | return $compareValue === $fieldValueEnds; |
| 217 | 238 | } |
| 218 | 239 | |
| 219 | - public function logicOnFieldActionValue($actionDetail, $fieldData, $fields) { | |
| 240 | + public function logicOnFieldActionValue($actionDetail, $fieldData, $fields) | |
| 241 | + { | |
| 220 | 242 | if (!empty($actionDetail->val) && !empty($fieldData[$actionDetail->field]['key'])) { |
| 221 | 243 | $actionValue = Helper::replaceFieldWithValue($actionDetail->val, $fieldData); |
| 222 | 244 | $fields->{$fieldData[$actionDetail->field]['key']}->val = Helper::evalMathExpression($actionValue); |
| 223 | 245 | $fieldData[$actionDetail->field]['value'] = $actionValue; |
| @@ -224,9 +246,10 @@ | ||
| 224 | 246 | } |
| 225 | 247 | return $fieldData; |
| 226 | 248 | } |
| 227 | 249 | |
| 228 | - public function logicOnFieldActionShow($actionDetail, $fieldData, $fields) { | |
| 250 | + public function logicOnFieldActionShow($actionDetail, $fieldData, $fields) | |
| 251 | + { | |
| 229 | 252 | $fields->{$fieldData[$actionDetail->field]['key']}->valid->hide = false; |
| 230 | 253 | if ('hidden' === $fields->{$fieldData[$actionDetail->field]['key']}->typ) { |
| 231 | 254 | $fields->{$fieldData[$actionDetail->field]['key']}->typ = 'text'; |
| 232 | 255 | } |
| @@ -232,9 +255,10 @@ | ||
| 232 | 255 | } |
| 233 | 256 | return $fieldData; |
| 234 | 257 | } |
| 235 | 258 | |
| 236 | - public function logicOnFieldActions($workFlowReturnable, $fieldData, $fields, $actionDetail) { | |
| 259 | + public function logicOnFieldActions($workFlowReturnable, $fieldData, $fields, $actionDetail) | |
| 260 | + { | |
| 237 | 261 | return [ |
| 238 | 262 | 'value' => self::logicOnFieldActionValue($actionDetail, $fieldData, $fields), |
| 239 | 263 | 'hide' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = true, |
| 240 | 264 | 'enable' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = false, |
| @@ -241,12 +265,13 @@ | ||
| 241 | 265 | 'disable' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = true, |
| 242 | 266 | 'readonly' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->readonly = true, |
| 243 | 267 | 'show' => self::logicOnFieldActionShow($actionDetail, $fieldData, $fields), |
| 244 | 268 | ]; |
| 245 | - return $workFlowReturnable['fields']; | |
| 269 | + // return $workFlowReturnable['fields']; | |
| 246 | 270 | } |
| 247 | 271 | |
| 248 | - public function getConditionStatus() { | |
| 272 | + public function getConditionStatus() | |
| 273 | + { | |
| 249 | 274 | $workflows = $this->_workflow_condition; |
| 250 | 275 | if (is_array($this->_workflow_condition)) { |
| 251 | 276 | foreach ($this->_workflow_condition as $sskey => $ssvalue) { |
| 252 | 277 | if (!is_string($ssvalue)) { |
| @@ -251,9 +276,8 @@ | ||
| 251 | 276 | foreach ($this->_workflow_condition as $sskey => $ssvalue) { |
| 252 | 277 | if (!is_string($ssvalue)) { |
| 253 | 278 | $this->_workflow_condition = $ssvalue; |
| 254 | 279 | $isCondition = $this->getConditionStatus(); |
| 255 | - | |
| 256 | 280 | if (0 === $sskey) { |
| 257 | 281 | $conditionStatus = $isCondition; |
| 258 | 282 | } |
| 259 | 283 | |
| @@ -274,15 +298,18 @@ | ||
| 274 | 298 | } |
| 275 | 299 | } |
| 276 | 300 | return (bool) $conditionStatus; |
| 277 | 301 | } else { |
| 278 | - $this->_workflow_condition->val = Helper::replaceFieldWithValue($this->_workflow_condition->val, $this->_data); | |
| 302 | + // A month value (2026-01) passes the math evaluator's checks and would come back as 2025. | |
| 303 | + $evalMathExpr = null === $this->dateFieldType(); | |
| 304 | + $this->_workflow_condition->val = Helper::replaceFieldWithValue($this->_workflow_condition->val, $this->_data, $evalMathExpr); | |
| 279 | 305 | |
| 280 | 306 | return $this->isLogicMatch($this->_workflow_condition->logic); |
| 281 | 307 | } |
| 282 | 308 | } |
| 283 | 309 | |
| 284 | - public function isLogicMatch($logic) { | |
| 310 | + public function isLogicMatch($logic) | |
| 311 | + { | |
| 285 | 312 | if (isset($this->_workflow_condition->smartKey)) { |
| 286 | 313 | $this->_workflow_condition->field = str_replace('()', '', $this->_workflow_condition->field); |
| 287 | 314 | } |
| 288 | 315 | |
| @@ -288,54 +315,81 @@ | ||
| 288 | 315 | |
| 289 | 316 | if (!isset($this->_data[$this->_workflow_condition->field])) { |
| 290 | 317 | return false; |
| 291 | 318 | } |
| 319 | + | |
| 320 | + $dateType = $this->dateFieldType(); | |
| 321 | + if (null !== $dateType) { | |
| 322 | + $fieldEntry = $this->_data[$this->_workflow_condition->field]; | |
| 323 | + $dateResult = DateConditionComparator::compare( | |
| 324 | + $logic, | |
| 325 | + isset($fieldEntry['value']) ? $fieldEntry['value'] : '', | |
| 326 | + isset($this->_workflow_condition->val) ? $this->_workflow_condition->val : '', | |
| 327 | + $dateType, | |
| 328 | + isset($fieldEntry['dateFormat']) ? $fieldEntry['dateFormat'] : '' | |
| 329 | + ); | |
| 330 | + if (null !== $dateResult) { | |
| 331 | + return $dateResult; | |
| 332 | + } | |
| 333 | + } | |
| 334 | + | |
| 292 | 335 | switch (strtolower($logic)) { |
| 293 | 336 | case 'equal': |
| 294 | 337 | return self::isEqual(); |
| 295 | - break; | |
| 338 | + | |
| 296 | 339 | case 'not_equal': |
| 297 | 340 | return self::isNotEqual(); |
| 298 | - break; | |
| 341 | + | |
| 299 | 342 | case 'null': |
| 300 | 343 | return self::isNull(); |
| 301 | - break; | |
| 344 | + | |
| 302 | 345 | case 'not_null': |
| 303 | 346 | return self::isNotNull(); |
| 304 | - break; | |
| 347 | + | |
| 305 | 348 | case 'contain': |
| 306 | 349 | return self::isContain(); |
| 307 | - break; | |
| 350 | + | |
| 308 | 351 | case 'contain_all': |
| 309 | 352 | return self::isContainAll(); |
| 310 | - break; | |
| 353 | + | |
| 311 | 354 | case 'not_contain': |
| 312 | 355 | return self::isNotContain(); |
| 313 | - break; | |
| 356 | + | |
| 314 | 357 | case 'greater': |
| 315 | 358 | return self::isGreaterThenValue(); |
| 316 | - break; | |
| 359 | + | |
| 317 | 360 | case 'less': |
| 318 | 361 | return self::isLessThenValue(); |
| 319 | - break; | |
| 362 | + | |
| 320 | 363 | case 'greater_or_equal': |
| 321 | 364 | return self::isGreatertOrEqual(); |
| 322 | - break; | |
| 365 | + | |
| 323 | 366 | case 'less_or_equal': |
| 324 | 367 | return self::isLessOrEqual(); |
| 325 | - break; | |
| 326 | 368 | |
| 327 | 369 | case 'start_with': |
| 328 | 370 | return self::isStarttWithString(); |
| 329 | - break; | |
| 371 | + | |
| 330 | 372 | case 'end_with': |
| 331 | 373 | return self::isEndWithString(); |
| 332 | - break; | |
| 374 | + | |
| 333 | 375 | case 'between': |
| 334 | 376 | return self::isBetween(); |
| 335 | - break; | |
| 336 | 377 | |
| 337 | 378 | default: |
| 338 | 379 | return false; |
| 339 | 380 | } |
| 381 | + } | |
| 382 | + | |
| 383 | + /** | |
| 384 | + * @return string|null the condition field's type when it is date-like | |
| 385 | + */ | |
| 386 | + private function dateFieldType() | |
| 387 | + { | |
| 388 | + $field = isset($this->_workflow_condition->field) ? $this->_workflow_condition->field : null; | |
| 389 | + if (!is_string($field) || !isset($this->_data[$field]['type'])) { | |
| 390 | + return null; | |
| 391 | + } | |
| 392 | + $type = $this->_data[$field]['type']; | |
| 393 | + return DateConditionComparator::isDateType($type) ? $type : null; | |
| 340 | 394 | } |
| 341 | 395 | } |