PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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
← All changes | backend/src/Common/Helpers/EntryHelper.php +218 -0 0.6.1 → 1.5.1 View file →
@@ -1,0 +1,218 @@
1 +<?php
2 +
3 +namespace IvyForms\Common\Helpers;
4 +
5 +use IvyForms\Entity\Entry\Entry;
6 +use IvyForms\Services\Security\Common\IpDetectionService;
7 +
8 +class EntryHelper
9 +{
10 + /**
11 + * Get the user agent string.
12 + *
13 + * @return string
14 + */
15 + public static function getUserAgent(): string
16 + {
17 + $userAgent = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT');
18 + return $userAgent ?: '';
19 + }
20 +
21 + /**
22 + * Get the source URL.
23 + *
24 + * @return string|null
25 + */
26 + public static function getSourceURL(): ?string
27 + {
28 + $sourceURL = filter_input(INPUT_SERVER, 'HTTP_REFERER');
29 + return $sourceURL ?: '';
30 + }
31 +
32 + /**
33 + * Get the browser name from the user agent string.
34 + *
35 + * @param string $userAgent
36 + * @return string
37 + */
38 + public static function getBrowserName(string $userAgent): string
39 + {
40 + if (str_contains($userAgent, 'Firefox')) {
41 + return 'Mozilla Firefox';
42 + } elseif (str_contains($userAgent, 'Chrome') && !str_contains($userAgent, 'Edg')) {
43 + return 'Google Chrome';
44 + } elseif (str_contains($userAgent, 'Safari') && !str_contains($userAgent, 'Chrome')) {
45 + return 'Apple Safari';
46 + } elseif (str_contains($userAgent, 'Edg')) {
47 + return 'Microsoft Edge';
48 + } elseif (str_contains($userAgent, 'MSIE') || str_contains($userAgent, 'Trident')) {
49 + return 'Internet Explorer';
50 + }
51 + return 'Unknown';
52 + }
53 +
54 + /**
55 + * Build entry data array for form submissions.
56 + *
57 + * @param int $formId
58 + * @return array<string, mixed>
59 + */
60 + public static function buildEntryData(int $formId): array
61 + {
62 + return [
63 + 'formId' => $formId,
64 + 'userId' => get_current_user_id() ?: null,
65 + 'status' => Entry::DEFAULT_STATUS,
66 + 'ipAddress' => IpDetectionService::getUserIpAddress(),
67 + 'userAgent' => self::getUserAgent(),
68 + 'sourceURL' => self::getSourceURL(),
69 + ];
70 + }
71 +
72 + /**
73 + * Get the value of a parent subfield.
74 + *
75 + * @param int $fieldId
76 + * @param int|null $parentId
77 + * @param array<int|string, mixed> $submissionData
78 + * @param array<int, string|null> $parentSubfieldKeys
79 + * @return mixed|null
80 + */
81 + public static function getParentSubfieldValue(
82 + int $fieldId,
83 + ?int $parentId,
84 + array $submissionData,
85 + array $parentSubfieldKeys
86 + ) {
87 + if (!$parentId || !isset($parentSubfieldKeys[$fieldId])) {
88 + return null;
89 + }
90 +
91 + $subKey = $parentSubfieldKeys[$fieldId];
92 + $parentValue = self::decodeSubmissionValue($submissionData[$parentId] ?? []);
93 +
94 + if (!isset($parentValue[$subKey])) {
95 + return null;
96 + }
97 +
98 + $val = $parentValue[$subKey];
99 +
100 + return is_array($val) ? implode(', ', $val) : $val;
101 + }
102 +
103 + /**
104 + * Get the compound field value by combining child field values.
105 + *
106 + * @param int $fieldId
107 + * @param array<int, array<int>> $parentChildrenMap
108 + * @param array<int, string|null> $parentSubfieldKeys
109 + * @param array<int|string, mixed> $submissionData
110 + * @return string
111 + */
112 + public static function getCompoundFieldValue(
113 + int $fieldId,
114 + array $parentChildrenMap,
115 + array $parentSubfieldKeys,
116 + array $submissionData
117 + ): string {
118 + if (empty($parentChildrenMap[$fieldId])) {
119 + return '';
120 + }
121 +
122 + $parentValue = self::decodeSubmissionValue($submissionData[$fieldId] ?? []);
123 + $childValues = [];
124 +
125 + foreach ($parentChildrenMap[$fieldId] as $childId) {
126 + $subKey = $parentSubfieldKeys[$childId] ?? null;
127 + if ($subKey !== null && isset($parentValue[$subKey]) && $parentValue[$subKey] !== '') {
128 + $val = $parentValue[$subKey];
129 + $childValues[] = is_array($val) ? implode(', ', $val) : $val;
130 + }
131 + }
132 +
133 + return implode(' ', $childValues);
134 + }
135 +
136 + /**
137 + * Decode a submission value into an array.
138 + * Handles JSON strings (e.g., Likert submissions) and ensures an array is returned.
139 + *
140 + * @param mixed $value
141 + * @return array<string, mixed>
142 + */
143 + public static function decodeSubmissionValue($value): array
144 + {
145 + if (is_string($value)) {
146 + $decoded = json_decode($value, true);
147 + return is_array($decoded) ? $decoded : [];
148 + }
149 +
150 + return is_array($value) ? $value : [];
151 + }
152 +
153 + /**
154 + * Get the default field value from submission data.
155 + *
156 + * @param int $fieldId
157 + * @param array<int|string, mixed> $submissionData
158 + * @return mixed|string
159 + */
160 + public static function getDefaultFieldValue(int $fieldId, array $submissionData)
161 + {
162 + return $submissionData[$fieldId] ?? '';
163 + }
164 +
165 + /**
166 + * Build parent-children map from form fields.
167 + *
168 + * @param array<int, object> $formFields
169 + * @return array<int, array<int>>
170 + */
171 + public static function buildParentChildrenMap(array $formFields): array
172 + {
173 + $parentChildrenMap = [];
174 + foreach ($formFields as $field) {
175 + $parentId = method_exists($field, 'getParentId') ? $field->getParentId() : null;
176 + if ($parentId && $parentId > 0) {
177 + $parentChildrenMap[$parentId][] = $field->getId();
178 + }
179 + }
180 + return $parentChildrenMap;
181 + }
182 +
183 + /**
184 + * Build subfield key map for each parent.
185 + *
186 + * @param array<int, array<int>> $parentChildrenMap
187 + * @param array<mixed> $submissionData
188 + * @param array<int, object> $formFields
189 + * @return array<int, string|null>
190 + */
191 + public static function buildParentSubfieldKeys(
192 + array $parentChildrenMap,
193 + array $submissionData,
194 + array $formFields = []
195 + ): array {
196 + $fieldById = [];
197 + foreach ($formFields as $field) {
198 + if (method_exists($field, 'getId')) {
199 + $fieldById[$field->getId()] = $field;
200 + }
201 + }
202 +
203 + $parentSubfieldKeys = [];
204 + foreach ($parentChildrenMap as $parentId => $childIds) {
205 + $parentValue = self::decodeSubmissionValue($submissionData[$parentId] ?? []);
206 + $keys = array_keys($parentValue);
207 + $orderedChildIds = NameFieldKeyHelper::sortChildIdsByDisplayOrder($childIds, $fieldById);
208 + foreach ($orderedChildIds as $i => $childId) {
209 + $subKey = NameFieldKeyHelper::resolveStoredNameSubfieldKey($fieldById[$childId] ?? null);
210 + if ($subKey === null) {
211 + $subKey = $keys[$i] ?? null;
212 + }
213 + $parentSubfieldKeys[$childId] = $subKey;
214 + }
215 + }
216 + return $parentSubfieldKeys;
217 + }
218 +}