PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.2.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.2.3
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 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / WorkFlow / ConditionalLogic.php

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

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