PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.3.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.3.2
V3.3.2 3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 All 139 releases
bit-form / includes / Core / WorkFlow / ConditionalLogic.php

ConditionalLogic.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.3.2, at includes/Core/WorkFlow/ConditionalLogic.php

396 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\WorkFlow;
4
5 final class ConditionalLogic
6 {
7 protected $_workflow_condition;
8 protected $_data;
9
10 public function __construct($workFlowCondition, $data)
11 {
12 $this->_workflow_condition = $workFlowCondition;
13 $this->_data = $data;
14 }
15
16 public function valueToCheck($logic)
17 {
18 if ((isset($this->_data[$this->_workflow_condition->field]['mul']) && $this->_data[$this->_workflow_condition->field]['mul'])
19 || 'check' === $this->_data[$this->_workflow_condition->field]['type']
20 ) {
21 $fieldValue = !empty($this->_data[$this->_workflow_condition->field]['value']) ? $this->_data[$this->_workflow_condition->field]['value'] : [];
22 if (is_string($fieldValue)) {
23 if ('[' === $fieldValue[0] && ']' === $fieldValue[strlen($fieldValue) - 1]) {
24 $fieldValue = json_decode($fieldValue);
25 } elseif (false !== strpos($fieldValue, BITFORMS_BF_SEPARATOR)) {
26 $fieldValue = explode(BITFORMS_BF_SEPARATOR, $fieldValue);
27 } else {
28 $fieldValue = explode(',', $fieldValue);
29 }
30 }
31 return [\explode(',', $this->_workflow_condition->val), $fieldValue];
32 }
33 $default = [
34 'between' => $this->_data[$this->_workflow_condition->field]['value'] === $this->_workflow_condition->val,
35 'equal' => $this->_data[$this->_workflow_condition->field]['value'] === $this->_workflow_condition->val,
36 'not_equal' => $this->_data[$this->_workflow_condition->field]['value'] !== $this->_workflow_condition->val,
37 'contain' => isset($this->_data[$this->_workflow_condition->field]['value']) && false !== stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val),
38 'not_contain' => false === stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val),
39 'contain_all' => isset($this->_data[$this->_workflow_condition->field]['value']) && false !== stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val),
40 ];
41
42 return $default[$logic];
43 }
44
45 public function isBetween()
46 {
47 $fldValue = $this->_data[$this->_workflow_condition->field]['value'];
48 $actionsValue = json_decode($this->_workflow_condition->val);
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;
53 }
54 return false;
55 }
56
57 public function isEqual()
58 {
59 $valueToCheckFun = self::valueToCheck('equal');
60
61 if (!is_array($valueToCheckFun)) {
62 return $valueToCheckFun;
63 }
64
65 $valueToCheck = $valueToCheckFun[0];
66 $fieldValue = $valueToCheckFun[1];
67
68 if (count($valueToCheck) !== count($fieldValue)) {
69 return false;
70 }
71 $checker = 0;
72 foreach ($valueToCheck as $value) {
73 if (!empty($fieldValue) && \in_array($value, $fieldValue)) {
74 $checker = $checker + 1;
75 }
76 }
77 if ($checker === count($valueToCheck) && count($valueToCheck) === count($fieldValue)) {
78 return true;
79 }
80 return false;
81 }
82
83 public function isNotEqual()
84 {
85 $valueToCheckFun = self::valueToCheck('not_equal');
86
87 if (!is_array($valueToCheckFun)) {
88 return $valueToCheckFun;
89 }
90
91 $valueToCheck = $valueToCheckFun[0];
92
93 $fieldValue = $valueToCheckFun[1];
94
95 $valueToCheckLenght = count($valueToCheck);
96 if ($valueToCheckLenght !== count($fieldValue)) {
97 return true;
98 }
99 $checker = 0;
100 foreach ($valueToCheck as $value) {
101 if (!in_array($value, $fieldValue)) {
102 $checker += 1;
103 }
104 }
105 return $valueToCheckLenght === $checker;
106 }
107
108 public function isNull()
109 {
110 return empty($this->_data[$this->_workflow_condition->field]['value']);
111 }
112
113 public function isNotNull()
114 {
115 return !empty($this->_data[$this->_workflow_condition->field]['value']);
116 }
117
118 public function isContain()
119 {
120 $valueToCheckFun = self::valueToCheck('contain');
121
122 if (!is_array($valueToCheckFun)) {
123 return $valueToCheckFun;
124 }
125
126 $valueToCheck = $valueToCheckFun[0];
127 $fieldValue = $valueToCheckFun[1];
128
129 $checker = 0;
130 foreach ($valueToCheck as $value) {
131 if (\in_array($value, $fieldValue)) {
132 $checker = $checker + 1;
133 }
134 }
135 if ($checker > 0) {
136 return true;
137 }
138 return false;
139 }
140
141 public function isContainAll()
142 {
143 $valueToCheckFun = self::valueToCheck('contain_all');
144
145 if (!is_array($valueToCheckFun)) {
146 return $valueToCheckFun;
147 }
148
149 $valueToCheck = $valueToCheckFun[0];
150 $fieldValue = $valueToCheckFun[1];
151
152 $checker = 0;
153 foreach ($valueToCheck as $value) {
154 if (\in_array($value, $fieldValue)) {
155 $checker = $checker + 1;
156 }
157 }
158 if ($checker >= count($valueToCheck)) {
159 return true;
160 }
161 return false;
162 }
163
164 public function isNotContain()
165 {
166 $valueToCheckFun = self::valueToCheck('not_contain');
167
168 if (!is_array($valueToCheckFun)) {
169 return $valueToCheckFun;
170 }
171
172 $valueToCheck = $valueToCheckFun[0];
173 $fieldValue = $valueToCheckFun[1];
174
175 $checker = 0;
176 foreach ($valueToCheck as $value) {
177 if (!in_array($value, $fieldValue)) {
178 $checker = $checker + 1;
179 }
180 }
181 if ($checker === count($valueToCheck)) {
182 return true;
183 }
184 return false;
185 }
186
187 public function isGreaterThenValue()
188 {
189 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
190 return false;
191 }
192 return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] > $this->_workflow_condition->val;
193 }
194
195 public function isLessThenValue()
196 {
197 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
198 return false;
199 }
200 return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] < $this->_workflow_condition->val;
201 }
202
203 public function isGreatertOrEqual()
204 {
205 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
206 return false;
207 }
208 return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] >= $this->_workflow_condition->val;
209 }
210
211 public function isLessOrEqual()
212 {
213 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
214 return false;
215 }
216 return isset($this->_data[$this->_workflow_condition->field]['value']) && $this->_data[$this->_workflow_condition->field]['value'] <= $this->_workflow_condition->val;
217 }
218
219 public function isStarttWithString()
220 {
221 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
222 return false;
223 }
224 return isset($this->_data[$this->_workflow_condition->field]['value']) && 0 === stripos($this->_data[$this->_workflow_condition->field]['value'], $this->_workflow_condition->val);
225 }
226
227 public function isEndWithString()
228 {
229 if (!isset($this->_data[$this->_workflow_condition->field]['value'])) {
230 return false;
231 }
232 $fieldValue = $this->_data[$this->_workflow_condition->field]['value'];
233 $fieldValueLength = strlen($this->_data[$this->_workflow_condition->field]['value']);
234 $compareValue = strtolower($this->_workflow_condition->val);
235 $compareValueLength = strlen($this->_workflow_condition->val);
236 $fieldValueEnds = strtolower(substr($fieldValue, $fieldValueLength - $compareValueLength, $fieldValueLength));
237 return $compareValue === $fieldValueEnds;
238 }
239
240 public function logicOnFieldActionValue($actionDetail, $fieldData, $fields)
241 {
242 if (!empty($actionDetail->val) && !empty($fieldData[$actionDetail->field]['key'])) {
243 $actionValue = Helper::replaceFieldWithValue($actionDetail->val, $fieldData);
244 $fields->{$fieldData[$actionDetail->field]['key']}->val = Helper::evalMathExpression($actionValue);
245 $fieldData[$actionDetail->field]['value'] = $actionValue;
246 }
247 return $fieldData;
248 }
249
250 public function logicOnFieldActionShow($actionDetail, $fieldData, $fields)
251 {
252 $fields->{$fieldData[$actionDetail->field]['key']}->valid->hide = false;
253 if ('hidden' === $fields->{$fieldData[$actionDetail->field]['key']}->typ) {
254 $fields->{$fieldData[$actionDetail->field]['key']}->typ = 'text';
255 }
256 return $fieldData;
257 }
258
259 public function logicOnFieldActions($workFlowReturnable, $fieldData, $fields, $actionDetail)
260 {
261 return [
262 'value' => self::logicOnFieldActionValue($actionDetail, $fieldData, $fields),
263 'hide' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = true,
264 'enable' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = false,
265 'disable' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = true,
266 'readonly' => $fields->{$fieldData[$actionDetail->field]['key']}->valid->readonly = true,
267 'show' => self::logicOnFieldActionShow($actionDetail, $fieldData, $fields),
268 ];
269 // return $workFlowReturnable['fields'];
270 }
271
272 public function getConditionStatus()
273 {
274 $workflows = $this->_workflow_condition;
275 if (is_array($this->_workflow_condition)) {
276 foreach ($this->_workflow_condition as $sskey => $ssvalue) {
277 if (!is_string($ssvalue)) {
278 $this->_workflow_condition = $ssvalue;
279 $isCondition = $this->getConditionStatus();
280 if (0 === $sskey) {
281 $conditionStatus = $isCondition;
282 }
283
284 if ($sskey - 1 >= 0 && is_string($workflows[$sskey - 1])) {
285 switch (strtolower($workflows[$sskey - 1])) {
286 case 'or':
287 $conditionStatus = $conditionStatus || $isCondition;
288 break;
289
290 case 'and':
291 $conditionStatus = $conditionStatus && $isCondition;
292 break;
293
294 default:
295 break;
296 }
297 }
298 }
299 }
300 return (bool) $conditionStatus;
301 } else {
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);
305
306 return $this->isLogicMatch($this->_workflow_condition->logic);
307 }
308 }
309
310 public function isLogicMatch($logic)
311 {
312 if (isset($this->_workflow_condition->smartKey)) {
313 $this->_workflow_condition->field = str_replace('()', '', $this->_workflow_condition->field);
314 }
315
316 if (!isset($this->_data[$this->_workflow_condition->field])) {
317 return false;
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
335 switch (strtolower($logic)) {
336 case 'equal':
337 return self::isEqual();
338
339 case 'not_equal':
340 return self::isNotEqual();
341
342 case 'null':
343 return self::isNull();
344
345 case 'not_null':
346 return self::isNotNull();
347
348 case 'contain':
349 return self::isContain();
350
351 case 'contain_all':
352 return self::isContainAll();
353
354 case 'not_contain':
355 return self::isNotContain();
356
357 case 'greater':
358 return self::isGreaterThenValue();
359
360 case 'less':
361 return self::isLessThenValue();
362
363 case 'greater_or_equal':
364 return self::isGreatertOrEqual();
365
366 case 'less_or_equal':
367 return self::isLessOrEqual();
368
369 case 'start_with':
370 return self::isStarttWithString();
371
372 case 'end_with':
373 return self::isEndWithString();
374
375 case 'between':
376 return self::isBetween();
377
378 default:
379 return false;
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;
394 }
395 }
396