| 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\Common\Exceptions\ValidationException; |
| 11 |
use IvyForms\Entity\FieldOptions\FieldOptions; |
| 12 |
use IvyForms\Factory\FieldOptions\FieldOptionsFactory; |
| 13 |
use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface; |
| 14 |
use IvyForms\Services\Media\ImageService; |
| 15 |
|
| 16 |
/** |
| 17 |
* General helper utilities related to Field processing. |
| 18 |
*/ |
| 19 |
class FieldHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Resolve parentId using fieldIndex -> parentId map if needed. |
| 23 |
* |
| 24 |
* @param array<string,mixed> $data Passed by reference; parentId modified in place. |
| 25 |
* @param array<int,int> $parentMap Map of fieldIndex => newly created parent field id. |
| 26 |
*/ |
| 27 |
public static function resolveParentId(array &$data, array $parentMap): void |
| 28 |
{ |
| 29 |
if (!array_key_exists('parentId', $data)) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$parentId = $data['parentId']; |
| 34 |
|
| 35 |
//parentId is 0 or null → use current field's fieldIndex |
| 36 |
if (($parentId === 0 || $parentId === '0' || $parentId === null) && isset($data['fieldIndex'])) { |
| 37 |
$parentIndex = $data['fieldIndex']; |
| 38 |
if (isset($parentMap[$parentIndex])) { |
| 39 |
$data['parentId'] = $parentMap[$parentIndex]; |
| 40 |
} |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
//parentId is numeric → use it as parent's fieldIndex |
| 45 |
if (is_numeric($parentId)) { |
| 46 |
if (isset($parentMap[$parentId])) { |
| 47 |
$data['parentId'] = $parentMap[$parentId]; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Remap parentId for duplicated field using old-to-new field ID map. |
| 54 |
* |
| 55 |
* @param array<string,mixed> $fieldData Passed by reference; parentId modified in place. |
| 56 |
* @param array<int,int> $fieldIdMap Map of old field ID => new field ID. |
| 57 |
*/ |
| 58 |
public static function remapParentIdForDuplication(array &$fieldData, array $fieldIdMap): void |
| 59 |
{ |
| 60 |
if ($fieldData['parentId'] !== null && isset($fieldIdMap[$fieldData['parentId']])) { |
| 61 |
$fieldData['parentId'] = $fieldIdMap[$fieldData['parentId']]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Partition field options into new, update, and submitted IDs. |
| 67 |
* |
| 68 |
* @param array<string, mixed> $fieldData |
| 69 |
* @param array<int> $existingOptionIds |
| 70 |
* @return array{ |
| 71 |
* newOptions: array<int, FieldOptions>, |
| 72 |
* updateOptions: array<int, FieldOptions>, |
| 73 |
* submittedOptionIds: array<int, int> |
| 74 |
* } |
| 75 |
* @throws ValidationException |
| 76 |
*/ |
| 77 |
public static function partitionFieldOptions(array $fieldData, array $existingOptionIds): array |
| 78 |
{ |
| 79 |
$newOptions = []; |
| 80 |
$updateOptions = []; |
| 81 |
$submittedOptionIds = []; |
| 82 |
foreach ($fieldData['fieldOptions'] as $optionData) { |
| 83 |
$option = FieldOptionsFactory::create($optionData); |
| 84 |
if ( |
| 85 |
isset($optionData['id']) && $optionData['id'] > 0 |
| 86 |
&& in_array($optionData['id'], $existingOptionIds, true) |
| 87 |
) { |
| 88 |
$updateOptions[] = $option; |
| 89 |
$submittedOptionIds[] = $optionData['id']; |
| 90 |
continue; |
| 91 |
} |
| 92 |
$newOptions[] = $option; |
| 93 |
} |
| 94 |
return [ |
| 95 |
'newOptions' => $newOptions, |
| 96 |
'updateOptions' => $updateOptions, |
| 97 |
'submittedOptionIds' => $submittedOptionIds, |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if a field type has options (radio, checkbox, select, multi-select). |
| 103 |
* |
| 104 |
* @param string $fieldType |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public static function fieldTypeHasOptions(string $fieldType): bool |
| 108 |
{ |
| 109 |
return in_array($fieldType, ['radio', 'checkbox', 'select', 'multi-select'], true); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Duplicate field options from one field to another. |
| 114 |
* |
| 115 |
* @param int $oldFieldId |
| 116 |
* @param int $newFieldId |
| 117 |
* @param FieldOptionsRepositoryInterface $repository |
| 118 |
* @return void |
| 119 |
* @throws ValidationException |
| 120 |
*/ |
| 121 |
public static function duplicateFieldOptions( |
| 122 |
int $oldFieldId, |
| 123 |
int $newFieldId, |
| 124 |
FieldOptionsRepositoryInterface $repository |
| 125 |
): void { |
| 126 |
$options = $repository->getByFieldId($oldFieldId); |
| 127 |
foreach ($options as $option) { |
| 128 |
$optionData = $option->toArray(); |
| 129 |
unset($optionData['id']); |
| 130 |
$optionData['fieldId'] = $newFieldId; |
| 131 |
$repository->add(FieldOptionsFactory::create($optionData)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Process htmlContent to upload base64 images |
| 137 |
* |
| 138 |
* @param array<string, mixed> $fieldData Passed by reference; htmlContent modified in place |
| 139 |
* @param ImageService $imageService |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public static function processFieldHtmlContent(array &$fieldData, ImageService $imageService): void |
| 143 |
{ |
| 144 |
if (!empty($fieldData['htmlContent'])) { |
| 145 |
$processedContent = $imageService->processImagesInContent( |
| 146 |
$fieldData['htmlContent'], |
| 147 |
'field' |
| 148 |
); |
| 149 |
$fieldData['htmlContent'] = $processedContent; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|