PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 / WorkFlowHandler.php

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

467 lines 18.6 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 use BitCode\BitForm\Core\Database\WorkFlowModel;
6
7 final class WorkFlowHandler
8 {
9 private static $_formID;
10 private static $_workFlowModel;
11 private $_user_details;
12
13 public function __construct($formID, array $user_details = null)
14 {
15 static::$_formID = $formID;
16 static::$_workFlowModel = new WorkFlowModel();
17 $this->_user_details = $user_details;
18 }
19
20 public function getAllworkFlowV1()
21 {
22 $workFlows = static::$_workFlowModel->get(
23 [
24 'id',
25 'workflow_name',
26 'workflow_type',
27 'workflow_run',
28 'workflow_behaviour',
29 'workflow_condition',
30 'workflow_action'
31 ],
32 [
33 'form_id' => static::$_formID,
34 ],
35 null,
36 null,
37 'id',
38 'desc'
39 );
40 $workFlowsFormated = [];
41 if (empty($workFlows) || is_wp_error($workFlows)) {
42 return [];
43 }
44 foreach ($workFlows as $key => $value) {
45 $allAction = json_decode($value->workflow_action);
46 $workFlow['id'] = $value->id;
47 $workFlow['title'] = $value->workflow_name;
48 $workFlow['action_type'] = $value->workflow_type;
49 $workFlow['action_run'] = $value->workflow_run;
50 $workFlow['action_behaviour'] = $value->workflow_behaviour;
51 $workFlow['logics'] = json_decode($value->workflow_condition);
52 $workFlow['actions'] = $allAction->action;
53 $workFlow['successAction'] = $allAction->successAction;
54 if (isset($allAction->validateMsg)) {
55 $workFlow['validateMsg'] = $allAction->validateMsg;
56 }
57 if (isset($allAction->avoid_delete)) {
58 $workFlow['avoid_delete'] = $allAction->avoid_delete;
59 }
60 $workFlowsFormated[$key] = $workFlow;
61 }
62
63 return $workFlowsFormated;
64 }
65
66 public function getAllworkFlow()
67 {
68 $workFlows = static::$_workFlowModel->get(
69 [
70 'id',
71 'workflow_name',
72 'workflow_type',
73 'workflow_run',
74 'workflow_behaviour',
75 'workflow_condition',
76 'workflow_info',
77 'workflow_category',
78 'workflow_order',
79 'workflow_status',
80 ],
81 [
82 'form_id' => static::$_formID,
83 ],
84 null,
85 null,
86 'workflow_order',
87 'ASC'
88 );
89 $workFlowsFormated = [];
90 if (empty($workFlows) || is_wp_error($workFlows)) {
91 return [];
92 }
93 foreach ($workFlows as $workFlowKey => $value) {
94 $workFlow['id'] = $value->id;
95 $workFlow['title'] = $value->workflow_name;
96 $workFlow['action_type'] = $value->workflow_type;
97 $workFlow['action_run'] = $value->workflow_run;
98 $workFlow['action_behaviour'] = $value->workflow_behaviour;
99 $workFlow['conditions'] = json_decode($value->workflow_condition ?? '');
100 $workFlow['info'] = json_decode($value->workflow_info ?? '');
101 $workFlow['workflow_category'] = $value->workflow_category;
102 $workFlow['status'] = (int) $value->workflow_status;
103 foreach ((array) $workFlow['conditions'] as $conIndex => $condition) {
104 if (property_exists($condition, 'logics')) {
105 $workFlow['conditions'][$conIndex]->logics = $condition->logics;
106 }
107 if (property_exists($condition, 'actions')) {
108 $workFlow['conditions'][$conIndex]->actions = $condition->actions;
109 }
110 }
111
112 $workFlowsFormated[$workFlowKey] = $workFlow;
113 }
114
115 return $workFlowsFormated;
116 }
117
118 public function updateworkFlow($workFlowID, $workFlowDetails, $actionIntegrationDetails, $workFlowOrder, $errorCode = [])
119 {
120 $conditions = $workFlowDetails->conditions;
121 foreach ($conditions as $conIndex => $condition) {
122 $actions = $condition->actions;
123 if (!empty($actions->success)) {
124 foreach ($actions->success as $actionKey => $actionValue) {
125 $detailIds = $actionValue->details->id;
126 if (!empty($detailIds)) {
127 if (is_array($detailIds)) {
128 foreach ($detailIds as $key => $id) {
129 $actionIntegrationID = \json_decode($id);
130 if (isset($actionIntegrationID->index)) {
131 $value = wp_json_encode(['id' => (string) $actionIntegrationDetails[$actionValue->type][$actionIntegrationID->index]]);
132 $errorCode['workflow'] = 1;
133 } else {
134 $value = $id;
135 }
136 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id[$key] = $value;
137 }
138 } else {
139 $actionIntegrationID = \json_decode($detailIds);
140 if (isset($actionIntegrationDetails[$actionValue->type])) {
141 if (isset($actionIntegrationID->index)) {
142 $value = wp_json_encode(['id' => (string) $actionIntegrationDetails[$actionValue->type][$actionIntegrationID->index]]);
143 $errorCode['workflow'] = 1;
144 } else {
145 $value = $actionValue->details->id;
146 }
147
148 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id = $value;
149 }
150 }
151 }
152 // pdfId update
153 if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfId)) {
154 $pdfDetailId = $actionValue->details->pdfId;
155 $actionIntegrationID = \json_decode($pdfDetailId);
156 if (isset($actionIntegrationID->index)) {
157 $value = wp_json_encode(['id' => (string) $actionIntegrationDetails['pdfTem'][$actionIntegrationID->index]]);
158 $errorCode['workflow'] = 1;
159 } else {
160 $value = $actionValue->details->pdfId;
161 }
162
163 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfId = $value;
164 }
165 // pdfIds update (multi-PDF attachments)
166 if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfIds) && is_array($actionValue->details->pdfIds)) {
167 foreach ($actionValue->details->pdfIds as $pdfKey => $pdfDetailId) {
168 $actionIntegrationID = \json_decode($pdfDetailId);
169 if (isset($actionIntegrationID->index)) {
170 $value = wp_json_encode(['id' => (string) $actionIntegrationDetails['pdfTem'][$actionIntegrationID->index]]);
171 $errorCode['workflow'] = 1;
172 } else {
173 $value = $pdfDetailId;
174 }
175 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfIds[$pdfKey] = $value;
176 }
177 }
178 }
179 }
180 if (!empty($actions->failure)) {
181 $validateMsgID = \json_decode($actions->failure);
182 $failure = isset($validateMsgID->index) ?
183 wp_json_encode(
184 [
185 'id' => $actionIntegrationDetails['successMsg'][$validateMsgID->index],
186 ]
187 )
188 : $actions->failure;
189
190 $conditions[$conIndex]->actions->failure = $failure;
191 }
192
193 if (!empty($workFlowDetails->avoid_delete)) {
194 $conditions[$conIndex]->actions['avoid_delete'] = $workFlowDetails->avoid_delete;
195 }
196 }
197
198 $status = static::$_workFlowModel->update(
199 [
200 'workflow_name' => $workFlowDetails->title,
201 'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type,
202 'workflow_run' => $workFlowDetails->action_run,
203 'workflow_behaviour' => $workFlowDetails->action_behaviour,
204 'workflow_condition' => wp_json_encode($conditions),
205 'workflow_info' => wp_json_encode($this->maybeRemapCLRef($workFlowDetails->info ?? null, $actionIntegrationDetails)),
206 'workflow_category' => $this->deriveWorkflowCategory($workFlowDetails->info ?? null),
207 // "workflow_action" => wp_json_encode($workFlowActions),
208 'workflow_order' => $workFlowOrder,
209 'workflow_status' => $workFlowDetails->status,
210 'user_id' => $this->_user_details['id'],
211 'user_ip' => $this->_user_details['ip'],
212 'user_location' => '',
213 'user_device' => $this->_user_details['device'],
214 'updated_at' => $this->_user_details['time'],
215 ],
216 [
217 'id' => $workFlowID,
218 'form_id' => static::$_formID,
219 ]
220 );
221
222 if (is_wp_error($status) && 'result_empty' !== $status->get_error_code()) {
223 $errorCode['workflow'] = 2;
224 }
225 return $errorCode;
226 }
227
228 public function saveworkFlow($workFlowDetails, $actionIntegrationDetails, $workFlowOrder)
229 {
230 $conditions = $workFlowDetails->conditions;
231 foreach ($conditions as $conIndex => $condition) {
232 $actions = $condition->actions;
233 if (property_exists($actions, 'success')) {
234 foreach ($actions->success as $actionKey => $actionValue) {
235 $detailIds = $actionValue->details->id;
236 if (!empty($detailIds)) {
237 if (is_array($detailIds)) {
238 foreach ($detailIds as $key => $id) {
239 $actionIntegrationID = \json_decode($id);
240
241 $value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionValue->type);
242
243 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id[$key] = false === $value ? $id : wp_json_encode(['id' => "$value"]);
244 }
245 } else {
246 $actionIntegrationID = \json_decode($detailIds);
247 $value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionValue->type);
248 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id = false === $value ? $actionValue->details->id : wp_json_encode(['id' => "$value"]);
249 }
250 }
251
252 // pdfId update
253 if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfId)) {
254 $pdfDetailId = $actionValue->details->pdfId;
255 $actionIntegrationID = \json_decode($pdfDetailId);
256 $value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, 'pdfTem');
257
258 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfId = false === $value ? $pdfDetailId : wp_json_encode(['id' => "$value"]);
259 }
260
261 // pdfIds update (multi-PDF attachments)
262 if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfIds) && is_array($actionValue->details->pdfIds)) {
263 foreach ($actionValue->details->pdfIds as $pdfKey => $pdfDetailId) {
264 $actionIntegrationID = \json_decode($pdfDetailId);
265 $value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, 'pdfTem');
266
267 $workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfIds[$pdfKey] = false === $value ? $pdfDetailId : wp_json_encode(['id' => "$value"]);
268 }
269 }
270 }
271 }
272
273 if (!empty($actions->failure)) {
274 $validateMsgID = \json_decode($actions->failure);
275 $failure = isset($validateMsgID->index) ?
276 wp_json_encode(
277 [
278 'id' => $actionIntegrationDetails['successMsg'][$validateMsgID->index],
279 ]
280 )
281 : $actions->failure;
282
283 $conditions[$conIndex]->actions->failure = $failure;
284 }
285
286 if (!empty($actions->avoid_delete)) {
287 $conditions[$conIndex]->actions->avoid_delete = $workFlowDetails->avoid_delete;
288 }
289 }
290
291 return static::$_workFlowModel->insert(
292 [
293 'workflow_name' => $workFlowDetails->title,
294 'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type,
295 'workflow_run' => $workFlowDetails->action_run,
296 'workflow_behaviour' => $workFlowDetails->action_behaviour,
297 'workflow_condition' => wp_json_encode($conditions),
298 'workflow_info' => wp_json_encode($this->maybeRemapCLRef($workFlowDetails->info ?? null, $actionIntegrationDetails)),
299 'workflow_category' => $this->deriveWorkflowCategory($workFlowDetails->info ?? null),
300 'workflow_order' => $workFlowOrder,
301 'workflow_status' => isset($workFlowDetails->status) ? $workFlowDetails->status : 1,
302 'form_id' => static::$_formID,
303 'user_id' => $this->_user_details['id'],
304 'user_ip' => $this->_user_details['ip'],
305 'user_location' => '',
306 'user_device' => $this->_user_details['device'],
307 'created_at' => $this->_user_details['time'],
308 'updated_at' => $this->_user_details['time'],
309 ]
310 );
311 }
312
313 public function oldupdateworkFlow($workFlowID, $workFlowDetails, $actionIntegrationDetails)
314 {
315 // $workFlowActions['action'] = $workFlowDetails->actions;
316 // foreach ($workFlowDetails->successAction as $successActionkey => $successActionValue) {
317 // if (!empty($successActionValue->details->id)) {
318 // if (is_array($successActionValue->details->id)) {
319 // foreach ($successActionValue->details->id as $key => $value) {
320 // $actionIntegrationID = \json_decode($value);
321 // $workFlowDetails->successAction[$successActionkey]->details->id[$key]
322 // = isset($actionIntegrationID->index) ?
323 // wp_json_encode(
324 // [
325 // 'id'=>
326 // $actionIntegrationDetails[$successActionValue->type][$actionIntegrationID->index]
327 // ]
328 // )
329 // : $value;
330 // }
331 // } else {
332 // $actionIntegrationID = \json_decode($successActionValue->details->id);
333 // $workFlowDetails->successAction[$successActionkey]->details->id
334 // = isset($actionIntegrationID->index) ?
335 // wp_json_encode(
336 // [
337 // 'id'=>
338 // $actionIntegrationDetails[$successActionValue->type][$actionIntegrationID->index]
339 // ]
340 // )
341 // : $successActionValue->details->id;
342 // }
343 // }
344 // }
345 // $workFlowActions['success'] = $workFlowDetails->successAction;
346 // if (!empty($workFlowDetails->validateMsg)) {
347 // $validateMsgID = \json_decode($workFlowDetails->validateMsg);
348 // $workFlowActions['validateMsg'] = isset($validateMsgID->index) ?
349 // wp_json_encode(
350 // [
351 // 'id'=>
352 // $actionIntegrationDetails['successMsg'][$validateMsgID->index]
353 // ]
354 // )
355 // : $workFlowDetails->validateMsg;
356 // }
357 // if (!empty($workFlowDetails->avoid_delete)) {
358 // $workFlowActions['avoid_delete'] = $workFlowDetails->avoid_delete;
359 // }
360 return static::$_workFlowModel->update(
361 [
362 'workflow_name' => $workFlowDetails->title,
363 'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type,
364 'workflow_run' => $workFlowDetails->action_run,
365 'workflow_behaviour' => $workFlowDetails->action_behaviour,
366 'workflow_condition' => wp_json_encode($workFlowDetails->conditions),
367 // "workflow_action" => null,
368 'workflow_status' => $workFlowDetails->status,
369 'user_id' => $this->_user_details['id'],
370 'user_ip' => $this->_user_details['ip'],
371 'user_location' => '',
372 'user_device' => $this->_user_details['device'],
373 'updated_at' => $this->_user_details['time'],
374 ],
375 [
376 'id' => $workFlowID,
377 'form_id' => static::$_formID,
378 ]
379 );
380 }
381
382 public function duplicateWorkFlow($currentFormID)
383 {
384 $workFlowCols = [
385 'workflow_name', 'workflow_type', 'workflow_run', 'workflow_behaviour',
386 'workflow_condition', 'workflow_info', 'workflow_category', 'workflow_action', 'workflow_status', 'form_id', 'user_id',
387 'user_ip', 'user_location', 'user_device', 'created_at', 'updated_at',
388 ];
389 $dupData = [
390 'workflow_name',
391 'workflow_type',
392 'workflow_run',
393 'workflow_behaviour',
394 'workflow_condition',
395 'workflow_info',
396 'workflow_category',
397 'workflow_action',
398 'workflow_status',
399 static::$_formID,
400 $this->_user_details['id'],
401 $this->_user_details['ip'],
402 '',
403 $this->_user_details['device'],
404 $this->_user_details['time'],
405 $this->_user_details['time'],
406 ];
407 return static::$_workFlowModel->duplicate($workFlowCols, $dupData, ['form_id' => $currentFormID]);
408 }
409
410 public function deleteworkFlow($workFlowID)
411 {
412 return static::$_workFlowModel->delete(
413 [
414 'id' => $workFlowID,
415 'form_id' => static::$_formID,
416 ]
417 );
418 }
419
420 /**
421 * Reconcile a brand-new feature record's inline-CL temp ref (info.targetId) to the real id assigned
422 * on save. New confirmation messages / redirects / integrations bind their CL row to a client ref;
423 * the corresponding save records $actionIntegrationDetails['<action>CLRef'][ref] => realId, applied
424 * here before persisting. Works for action = confirmation | redirect | email | integration.
425 */
426 public function maybeRemapCLRef($info, $actionIntegrationDetails)
427 {
428 if (!empty($info) && isset($info->action, $info->targetId)) {
429 $mapKey = $info->action . 'CLRef';
430 if (isset($actionIntegrationDetails[$mapKey][$info->targetId])) {
431 $info->targetId = (string) $actionIntegrationDetails[$mapKey][$info->targetId];
432 }
433 }
434 return $info;
435 }
436
437 /**
438 * Derive the storage category used to split Free vs Pro processing.
439 * classic = legacy combined workflows (Free), basic = field show/hide (Free),
440 * advanced = Pro multi-condition routing. Source of truth is info.type sent by the UI.
441 */
442 public function deriveWorkflowCategory($info)
443 {
444 if (!empty($info) && isset($info->type)) {
445 if ('advanced' === $info->type) {
446 return 'advanced';
447 }
448 if ('basic' === $info->type) {
449 return 'basic';
450 }
451 }
452 return 'classic';
453 }
454
455 public function maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionType)
456 {
457 if (isset($actionIntegrationDetails[$actionType])) {
458 if (isset($actionIntegrationID->index)) {
459 return $actionIntegrationDetails[$actionType][$actionIntegrationID->index];
460 } elseif (isset($actionIntegrationID->id, $actionIntegrationDetails[$actionType][wp_json_encode($actionIntegrationID)])) {
461 return $actionIntegrationDetails[$actionType][wp_json_encode($actionIntegrationID)];
462 }
463 }
464 return false;
465 }
466 }
467