PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/WorkFlow/ConditionalLogic.php

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