| 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 |
* @param array<int, object> $formFields |
| 162 |
* @return array<int, string|null> |
| 163 |
*/ |
| 164 |
public static function buildParentSubfieldKeys( |
| 165 |
array $parentChildrenMap, |
| 166 |
array $submissionData, |
| 167 |
array $formFields = [] |
| 168 |
): array { |
| 169 |
$fieldById = []; |
| 170 |
foreach ($formFields as $field) { |
| 171 |
if (method_exists($field, 'getId')) { |
| 172 |
$fieldById[$field->getId()] = $field; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
$parentSubfieldKeys = []; |
| 177 |
foreach ($parentChildrenMap as $parentId => $childIds) { |
| 178 |
$parentValue = $submissionData[$parentId] ?? []; |
| 179 |
// Ensure $parentValue is always an array before calling array_keys(). |
| 180 |
// Scalar values (e.g. a plain string stored at the parent field index) |
| 181 |
// must be treated as an empty subfield map — no subfield keys to extract. |
| 182 |
if (!is_array($parentValue)) { |
| 183 |
$parentValue = []; |
| 184 |
} |
| 185 |
$keys = array_keys($parentValue); |
| 186 |
$orderedChildIds = NameFieldKeyHelper::sortChildIdsByDisplayOrder($childIds, $fieldById); |
| 187 |
foreach ($orderedChildIds as $i => $childId) { |
| 188 |
$subKey = NameFieldKeyHelper::resolveStoredNameSubfieldKey($fieldById[$childId] ?? null); |
| 189 |
if ($subKey === null) { |
| 190 |
$subKey = $keys[$i] ?? null; |
| 191 |
} |
| 192 |
$parentSubfieldKeys[$childId] = $subKey; |
| 193 |
} |
| 194 |
} |
| 195 |
return $parentSubfieldKeys; |
| 196 |
} |
| 197 |
} |
| 198 |
|