PluginProbe
The Innovative Form Builder – IvyForms / 1.4.1
The Innovative Form Builder – IvyForms v1.4.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Confirmation / ConfirmationResolver.php

ConfirmationResolver.php in The Innovative Form Builder – IvyForms 1.4.1, at backend/src/Services/Confirmation/ConfirmationResolver.php

107 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Confirmation;
4
5 use IvyForms\Entity\Confirmation\Confirmation;
6
7 /**
8 * Resolves which confirmation applies after submission (conditional logic + ordering).
9 *
10 * Resolution is two-phase:
11 * 1. Walk enabled confirmations by position and return the first whose smart logic matches.
12 * Rows with smart logic disabled are skipped here (matches() returns false).
13 * 2. If none match, resolveFallback() picks default → first non-conditional → first enabled.
14 */
15 class ConfirmationResolver
16 {
17 /**
18 * @param array<Confirmation> $confirmations
19 * @param array<string, mixed> $submissionData
20 * @param array<int, mixed> $formFields
21 */
22 public function resolve(
23 array $confirmations,
24 array $submissionData,
25 array $formFields,
26 int $formId
27 ): ?Confirmation {
28 $enabled = array_values(array_filter(
29 $confirmations,
30 static fn (Confirmation $confirmation): bool => $confirmation->isEnabled()
31 ));
32
33 if ($enabled === []) {
34 return null;
35 }
36
37 usort(
38 $enabled,
39 static function (Confirmation $left, Confirmation $right): int {
40 $position = $left->getPosition() <=> $right->getPosition();
41 if ($position !== 0) {
42 return $position;
43 }
44
45 return $left->getId() <=> $right->getId();
46 }
47 );
48
49 foreach ($enabled as $confirmation) {
50 if ($this->matches($confirmation, $submissionData, $formFields, $formId)) {
51 return $confirmation;
52 }
53 }
54
55 return $this->resolveFallback($enabled);
56 }
57
58 /**
59 * @param array<Confirmation> $enabled
60 */
61 private function resolveFallback(array $enabled): ?Confirmation
62 {
63 foreach ($enabled as $confirmation) {
64 if ($confirmation->isDefault()) {
65 return $confirmation;
66 }
67 }
68
69 foreach ($enabled as $confirmation) {
70 $smartLogic = $confirmation->getSmartLogic();
71 if (empty($smartLogic['enabled'])) {
72 return $confirmation;
73 }
74 }
75
76 return $enabled[0] ?? null;
77 }
78
79 /**
80 * @param array<string, mixed> $submissionData
81 * @param array<int, mixed> $formFields
82 */
83 private function matches(
84 Confirmation $confirmation,
85 array $submissionData,
86 array $formFields,
87 int $formId
88 ): bool {
89 $smartLogic = $confirmation->getSmartLogic();
90 if (empty($smartLogic['enabled'])) {
91 // Non-conditional rows are only considered in resolveFallback() so
92 // conditional confirmations are evaluated first by position.
93 return false;
94 }
95
96 return (bool) apply_filters(
97 'ivyforms/confirmation/should_use',
98 true,
99 $smartLogic,
100 $submissionData,
101 $formFields,
102 $confirmation,
103 $formId
104 );
105 }
106 }
107