| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Entity\Entry\Entry; |
| 11 |
|
| 12 |
class EntryHelper |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Get the client IP address. |
| 16 |
* |
| 17 |
* @return string|null |
| 18 |
*/ |
| 19 |
public static function getClientIpAddress(): ?string |
| 20 |
{ |
| 21 |
$ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); |
| 22 |
return $ip ?: null; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the user agent string. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public static function getUserAgent(): string |
| 31 |
{ |
| 32 |
$userAgent = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'); |
| 33 |
return $userAgent ?: ''; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the source URL. |
| 38 |
* |
| 39 |
* @return string|null |
| 40 |
*/ |
| 41 |
public static function getSourceURL(): ?string |
| 42 |
{ |
| 43 |
$sourceURL = filter_input(INPUT_SERVER, 'HTTP_REFERER'); |
| 44 |
return $sourceURL ?: ''; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the browser name from the user agent string. |
| 49 |
* |
| 50 |
* @param string $userAgent |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public static function getBrowserName(string $userAgent): string |
| 54 |
{ |
| 55 |
if (str_contains($userAgent, 'Firefox')) { |
| 56 |
return 'Mozilla Firefox'; |
| 57 |
} elseif (str_contains($userAgent, 'Chrome') && !str_contains($userAgent, 'Edg')) { |
| 58 |
return 'Google Chrome'; |
| 59 |
} elseif (str_contains($userAgent, 'Safari') && !str_contains($userAgent, 'Chrome')) { |
| 60 |
return 'Apple Safari'; |
| 61 |
} elseif (str_contains($userAgent, 'Edg')) { |
| 62 |
return 'Microsoft Edge'; |
| 63 |
} elseif (str_contains($userAgent, 'MSIE') || str_contains($userAgent, 'Trident')) { |
| 64 |
return 'Internet Explorer'; |
| 65 |
} |
| 66 |
return 'Unknown'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Build entry data array for form submissions. |
| 71 |
* |
| 72 |
* @param int $formId |
| 73 |
* @return array<string, mixed> |
| 74 |
*/ |
| 75 |
public static function buildEntryData(int $formId): array |
| 76 |
{ |
| 77 |
return [ |
| 78 |
'formId' => $formId, |
| 79 |
'userId' => get_current_user_id() ?: null, |
| 80 |
'status' => Entry::DEFAULT_STATUS, |
| 81 |
'ipAddress' => self::getClientIpAddress(), |
| 82 |
'userAgent' => self::getUserAgent(), |
| 83 |
'sourceURL' => self::getSourceURL(), |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get the value of a parent subfield. |
| 89 |
* |
| 90 |
* @param int $fieldId |
| 91 |
* @param int|null $parentId |
| 92 |
* @param array<int|string, mixed> $submissionData |
| 93 |
* @param array<int, string|null> $parentSubfieldKeys |
| 94 |
* @return mixed|null |
| 95 |
*/ |
| 96 |
public static function getParentSubfieldValue( |
| 97 |
int $fieldId, |
| 98 |
?int $parentId, |
| 99 |
array $submissionData, |
| 100 |
array $parentSubfieldKeys |
| 101 |
) { |
| 102 |
if ($parentId && isset($parentSubfieldKeys[$fieldId])) { |
| 103 |
$subKey = $parentSubfieldKeys[$fieldId]; |
| 104 |
if (isset($submissionData[$parentId][$subKey])) { |
| 105 |
return $submissionData[$parentId][$subKey]; |
| 106 |
} |
| 107 |
} |
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the compound field value by combining child field values. |
| 113 |
* |
| 114 |
* @param int $fieldId |
| 115 |
* @param array<int, array<int>> $parentChildrenMap |
| 116 |
* @param array<int, string|null> $parentSubfieldKeys |
| 117 |
* @param array<int|string, mixed> $submissionData |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public static function getCompoundFieldValue( |
| 121 |
int $fieldId, |
| 122 |
array $parentChildrenMap, |
| 123 |
array $parentSubfieldKeys, |
| 124 |
array $submissionData |
| 125 |
): string { |
| 126 |
$childValues = []; |
| 127 |
if (!empty($parentChildrenMap[$fieldId])) { |
| 128 |
foreach ($parentChildrenMap[$fieldId] as $childId) { |
| 129 |
$subKey = $parentSubfieldKeys[$childId] ?? null; |
| 130 |
if ($subKey && isset($submissionData[$fieldId][$subKey])) { |
| 131 |
$val = $submissionData[$fieldId][$subKey]; |
| 132 |
if ($val !== '') { |
| 133 |
$childValues[] = $val; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
return implode(' ', $childValues); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the default field value from submission data. |
| 143 |
* |
| 144 |
* @param int $fieldId |
| 145 |
* @param array<int|string, mixed> $submissionData |
| 146 |
* @return mixed|string |
| 147 |
*/ |
| 148 |
public static function getDefaultFieldValue(int $fieldId, array $submissionData) |
| 149 |
{ |
| 150 |
return $submissionData[$fieldId] ?? ''; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Build parent-children map from form fields. |
| 155 |
* |
| 156 |
* @param array<int, object> $formFields |
| 157 |
* @return array<int, array<int>> |
| 158 |
*/ |
| 159 |
public static function buildParentChildrenMap(array $formFields): array |
| 160 |
{ |
| 161 |
$parentChildrenMap = []; |
| 162 |
foreach ($formFields as $field) { |
| 163 |
$parentId = method_exists($field, 'getParentId') ? $field->getParentId() : null; |
| 164 |
if ($parentId && $parentId > 0) { |
| 165 |
$parentChildrenMap[$parentId][] = $field->getId(); |
| 166 |
} |
| 167 |
} |
| 168 |
return $parentChildrenMap; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Build subfield key map for each parent. |
| 173 |
* |
| 174 |
* @param array<int, array<int>> $parentChildrenMap |
| 175 |
* @param array<mixed> $submissionData |
| 176 |
* @return array<int, string|null> |
| 177 |
*/ |
| 178 |
public static function buildParentSubfieldKeys(array $parentChildrenMap, array $submissionData): array |
| 179 |
{ |
| 180 |
$parentSubfieldKeys = []; |
| 181 |
foreach ($parentChildrenMap as $parentId => $childIds) { |
| 182 |
$parentValue = $submissionData[$parentId] ?? []; |
| 183 |
$keys = array_keys($parentValue); |
| 184 |
foreach ($childIds as $i => $childId) { |
| 185 |
$parentSubfieldKeys[$childId] = $keys[$i] ?? null; |
| 186 |
} |
| 187 |
} |
| 188 |
return $parentSubfieldKeys; |
| 189 |
} |
| 190 |
} |
| 191 |
|