Importer update add support for meta data and multiple inserts for some entities like addresses and phones, updated other things

This commit is contained in:
Simon Pocrnjič
2025-10-09 22:28:48 +02:00
parent c8029c9eb0
commit 0598261cdc
27 changed files with 2517 additions and 375 deletions
@@ -385,6 +385,8 @@ public function bulkAddMappings(Request $request, ImportTemplate $template)
'default_field' => 'nullable|string', // if provided, used as the field name for all entries
'apply_mode' => 'nullable|string|in:insert,update,both,keyref',
'transform' => 'nullable|string|in:trim,upper,lower',
'options' => 'nullable|array',
'group' => 'nullable|string|max:50', // convenience: will be wrapped into options.group
])->validate();
// Accept commas, semicolons, and newlines; strip surrounding quotes/apostrophes and whitespace
@@ -408,9 +410,18 @@ public function bulkAddMappings(Request $request, ImportTemplate $template)
$entity = $data['entity'] ?? null;
$defaultField = $data['default_field'] ?? null; // allows forcing a specific field for all
// Build options payload once
$opts = [];
if (isset($data['options']) && is_array($data['options'])) {
$opts = $data['options'];
}
if (! empty($data['group'])) {
$opts['group'] = (string) $data['group'];
}
$created = 0;
$updated = 0;
DB::transaction(function () use ($template, $list, $apply, $transform, $entity, $defaultField, $basePosition, &$created, &$updated) {
DB::transaction(function () use ($template, $list, $apply, $transform, $entity, $defaultField, $basePosition, $opts, &$created, &$updated) {
foreach ($list as $idx => $source) {
$targetField = null;
if ($defaultField) {
@@ -429,7 +440,7 @@ public function bulkAddMappings(Request $request, ImportTemplate $template)
'entity' => $entity ?? $existing->entity,
'transform' => $transform ?? $existing->transform,
'apply_mode' => $apply ?? $existing->apply_mode ?? 'both',
'options' => $existing->options,
'options' => empty($opts) ? $existing->options : $opts,
// keep existing position
]);
$updated++;
@@ -441,7 +452,7 @@ public function bulkAddMappings(Request $request, ImportTemplate $template)
'target_field' => $targetField,
'transform' => $transform,
'apply_mode' => $apply,
'options' => null,
'options' => empty($opts) ? null : $opts,
'position' => $basePosition + $idx + 1,
]);
$created++;
@@ -546,6 +557,10 @@ public function applyToImport(Request $request, ImportTemplate $template, Import
$rows = $template->mappings()->orderBy('position')->get();
foreach ($rows as $row) {
$options = $row->options;
if (is_array($options) || $options instanceof \JsonSerializable || $options instanceof \stdClass) {
$options = json_encode($options);
}
\DB::table('import_mappings')->insert([
'import_id' => $import->id,
'entity' => $row->entity,
@@ -553,7 +568,7 @@ public function applyToImport(Request $request, ImportTemplate $template, Import
'target_field' => $row->target_field,
'transform' => $row->transform,
'apply_mode' => $row->apply_mode ?? 'both',
'options' => $row->options,
'options' => $options,
'position' => $row->position ?? null,
'created_at' => now(),
'updated_at' => now(),