PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.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 / Common / Helpers / EntryHelper.php

EntryHelper.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Common/Helpers/EntryHelper.php

182 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $subKey = $parentSubfieldKeys[$fieldId];
89 if (isset($submissionData[$parentId][$subKey])) {
90 return $submissionData[$parentId][$subKey];
91 }
92 }
93 return null;
94 }
95
96 /**
97 * Get the compound field value by combining child field values.
98 *
99 * @param int $fieldId
100 * @param array<int, array<int>> $parentChildrenMap
101 * @param array<int, string|null> $parentSubfieldKeys
102 * @param array<int|string, mixed> $submissionData
103 * @return string
104 */
105 public static function getCompoundFieldValue(
106 int $fieldId,
107 array $parentChildrenMap,
108 array $parentSubfieldKeys,
109 array $submissionData
110 ): string {
111 $childValues = [];
112 if (!empty($parentChildrenMap[$fieldId])) {
113 foreach ($parentChildrenMap[$fieldId] as $childId) {
114 $subKey = $parentSubfieldKeys[$childId] ?? null;
115 if ($subKey && isset($submissionData[$fieldId][$subKey])) {
116 $val = $submissionData[$fieldId][$subKey];
117 if ($val !== '') {
118 $childValues[] = $val;
119 }
120 }
121 }
122 }
123 return implode(' ', $childValues);
124 }
125
126 /**
127 * Get the default field value from submission data.
128 *
129 * @param int $fieldId
130 * @param array<int|string, mixed> $submissionData
131 * @return mixed|string
132 */
133 public static function getDefaultFieldValue(int $fieldId, array $submissionData)
134 {
135 return $submissionData[$fieldId] ?? '';
136 }
137
138 /**
139 * Build parent-children map from form fields.
140 *
141 * @param array<int, object> $formFields
142 * @return array<int, array<int>>
143 */
144 public static function buildParentChildrenMap(array $formFields): array
145 {
146 $parentChildrenMap = [];
147 foreach ($formFields as $field) {
148 $parentId = method_exists($field, 'getParentId') ? $field->getParentId() : null;
149 if ($parentId && $parentId > 0) {
150 $parentChildrenMap[$parentId][] = $field->getId();
151 }
152 }
153 return $parentChildrenMap;
154 }
155
156 /**
157 * Build subfield key map for each parent.
158 *
159 * @param array<int, array<int>> $parentChildrenMap
160 * @param array<mixed> $submissionData
161 * @return array<int, string|null>
162 */
163 public static function buildParentSubfieldKeys(array $parentChildrenMap, array $submissionData): array
164 {
165 $parentSubfieldKeys = [];
166 foreach ($parentChildrenMap as $parentId => $childIds) {
167 $parentValue = $submissionData[$parentId] ?? [];
168 // Ensure $parentValue is always an array before calling array_keys().
169 // Scalar values (e.g. a plain string stored at the parent field index)
170 // must be treated as an empty subfield map — no subfield keys to extract.
171 if (!is_array($parentValue)) {
172 $parentValue = [];
173 }
174 $keys = array_keys($parentValue);
175 foreach ($childIds as $i => $childId) {
176 $parentSubfieldKeys[$childId] = $keys[$i] ?? null;
177 }
178 }
179 return $parentSubfieldKeys;
180 }
181 }
182