| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
use IvyForms\Common\Exceptions\ValidationException; |
| 7 |
use IvyForms\Entity\FieldOptions\FieldOptions; |
| 8 |
use IvyForms\Factory\FieldOptions\FieldOptionsFactory; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* General helper utilities related to Field processing. |
| 16 |
*/ |
| 17 |
class Helper |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Resolve parentId using fieldIndex -> parentId map if needed. |
| 21 |
* |
| 22 |
* @param array<string,mixed> $data Passed by reference; parentId modified in place. |
| 23 |
* @param array<int,int> $parentMap Map of fieldIndex => newly created parent field id. |
| 24 |
*/ |
| 25 |
public static function resolveParentId(array &$data, array $parentMap): void |
| 26 |
{ |
| 27 |
if (!array_key_exists('parentId', $data)) { |
| 28 |
return; |
| 29 |
} |
| 30 |
$pid = $data['parentId']; |
| 31 |
$needsResolve = ($pid === 0 || $pid === '0' || $pid === null); |
| 32 |
if ($needsResolve && isset($data['fieldIndex']) && isset($parentMap[$data['fieldIndex']])) { |
| 33 |
$data['parentId'] = $parentMap[$data['fieldIndex']]; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Partition field options into new, update, and submitted IDs. |
| 39 |
* |
| 40 |
* @param array<string, mixed> $fieldData |
| 41 |
* @param array<int> $existingOptionIds |
| 42 |
* @return array{ |
| 43 |
* newOptions: array<int, FieldOptions>, |
| 44 |
* updateOptions: array<int, FieldOptions>, |
| 45 |
* submittedOptionIds: array<int, int> |
| 46 |
* } |
| 47 |
* @throws ValidationException |
| 48 |
*/ |
| 49 |
public static function partitionFieldOptions(array $fieldData, array $existingOptionIds): array |
| 50 |
{ |
| 51 |
$newOptions = []; |
| 52 |
$updateOptions = []; |
| 53 |
$submittedOptionIds = []; |
| 54 |
foreach ($fieldData['fieldOptions'] as $optionData) { |
| 55 |
$option = FieldOptionsFactory::create($optionData); |
| 56 |
if ( |
| 57 |
isset($optionData['id']) && $optionData['id'] > 0 |
| 58 |
&& in_array($optionData['id'], $existingOptionIds, true) |
| 59 |
) { |
| 60 |
$updateOptions[] = $option; |
| 61 |
$submittedOptionIds[] = $optionData['id']; |
| 62 |
continue; |
| 63 |
} |
| 64 |
$newOptions[] = $option; |
| 65 |
} |
| 66 |
return [ |
| 67 |
'newOptions' => $newOptions, |
| 68 |
'updateOptions' => $updateOptions, |
| 69 |
'submittedOptionIds' => $submittedOptionIds, |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Batch process field options: add new, update existing, delete orphaned. |
| 75 |
* |
| 76 |
* @param int $fieldId |
| 77 |
* @param array<string, mixed> $fieldData |
| 78 |
* @param object $fieldOptionsService |
| 79 |
* @param object $fieldOptionsRepository |
| 80 |
* @return void |
| 81 |
* @throws ValidationException |
| 82 |
*/ |
| 83 |
public static function batchProcessFieldOptions( |
| 84 |
int $fieldId, |
| 85 |
array $fieldData, |
| 86 |
object $fieldOptionsService, |
| 87 |
object $fieldOptionsRepository |
| 88 |
): void { |
| 89 |
$existingOptions = $fieldOptionsService->getByFieldId($fieldId); |
| 90 |
$existingOptionIds = array_map(fn($opt) => $opt->getId(), $existingOptions); |
| 91 |
$partition = self::partitionFieldOptions($fieldData, $existingOptionIds); |
| 92 |
$newOptionIds = $fieldOptionsService->addOptions($partition['newOptions']); |
| 93 |
$fieldOptionsService->updateOptions($partition['updateOptions']); |
| 94 |
$submittedOptionIds = array_merge($partition['submittedOptionIds'], $newOptionIds); |
| 95 |
$orphanedOptionIds = array_diff($existingOptionIds, $submittedOptionIds); |
| 96 |
if (!empty($orphanedOptionIds)) { |
| 97 |
$fieldOptionsRepository->deleteByOptionIds($orphanedOptionIds); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|