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:
@@ -18,7 +18,7 @@ public function run(): void
|
||||
'description' => 'Basic person import: name, email, phone, address',
|
||||
'source_type' => 'csv',
|
||||
'default_record_type' => 'person',
|
||||
'sample_headers' => ['first_name','last_name','email','phone','address','city','postal_code','country'],
|
||||
'sample_headers' => ['first_name', 'last_name', 'email', 'phone', 'address', 'city', 'postal_code', 'country'],
|
||||
'is_active' => true,
|
||||
'meta' => [
|
||||
'delimiter' => ',',
|
||||
@@ -47,5 +47,113 @@ public function run(): void
|
||||
'position' => $map['position'],
|
||||
]);
|
||||
}
|
||||
|
||||
// Multi-contacts template demonstrating group support for emails/phones/addresses
|
||||
$multi = ImportTemplate::query()->firstOrCreate([
|
||||
'name' => 'Person CSV (multi contacts)',
|
||||
], [
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'description' => 'Person import with multiple emails/phones/addresses per row using group 1 and 2',
|
||||
'source_type' => 'csv',
|
||||
'default_record_type' => 'person',
|
||||
'sample_headers' => [
|
||||
'first_name', 'last_name', 'Email 1', 'Email 2', 'Phone 1', 'Phone 2',
|
||||
'Address 1', 'City 1', 'Postal 1', 'Country 1',
|
||||
'Address 2', 'City 2', 'Postal 2', 'Country 2',
|
||||
],
|
||||
'is_active' => true,
|
||||
'meta' => [
|
||||
'delimiter' => ',',
|
||||
'enclosure' => '"',
|
||||
'escape' => '\\',
|
||||
],
|
||||
]);
|
||||
|
||||
$multiMappings = [
|
||||
// Person identity
|
||||
['source_column' => 'first_name', 'target_field' => 'person.first_name', 'position' => 1],
|
||||
['source_column' => 'last_name', 'target_field' => 'person.last_name', 'position' => 2],
|
||||
|
||||
// Emails (groups 1, 2)
|
||||
['source_column' => 'Email 1', 'target_field' => 'email.value', 'position' => 3, 'options' => ['group' => '1']],
|
||||
['source_column' => 'Email 2', 'target_field' => 'email.value', 'position' => 4, 'options' => ['group' => '2']],
|
||||
|
||||
// Phones (groups 1, 2)
|
||||
['source_column' => 'Phone 1', 'target_field' => 'phone.nu', 'position' => 5, 'options' => ['group' => '1']],
|
||||
['source_column' => 'Phone 2', 'target_field' => 'phone.nu', 'position' => 6, 'options' => ['group' => '2']],
|
||||
|
||||
// Address group 1
|
||||
['source_column' => 'Address 1', 'target_field' => 'address.address', 'position' => 7, 'options' => ['group' => '1']],
|
||||
['source_column' => 'City 1', 'target_field' => 'address.city', 'position' => 8, 'options' => ['group' => '1']],
|
||||
['source_column' => 'Postal 1', 'target_field' => 'address.postal_code', 'position' => 9, 'options' => ['group' => '1']],
|
||||
['source_column' => 'Country 1', 'target_field' => 'address.country', 'position' => 10, 'options' => ['group' => '1']],
|
||||
|
||||
// Address group 2
|
||||
['source_column' => 'Address 2', 'target_field' => 'address.address', 'position' => 11, 'options' => ['group' => '2']],
|
||||
['source_column' => 'City 2', 'target_field' => 'address.city', 'position' => 12, 'options' => ['group' => '2']],
|
||||
['source_column' => 'Postal 2', 'target_field' => 'address.postal_code', 'position' => 13, 'options' => ['group' => '2']],
|
||||
['source_column' => 'Country 2', 'target_field' => 'address.country', 'position' => 14, 'options' => ['group' => '2']],
|
||||
];
|
||||
|
||||
foreach ($multiMappings as $map) {
|
||||
ImportTemplateMapping::firstOrCreate([
|
||||
'import_template_id' => $multi->id,
|
||||
'source_column' => $map['source_column'],
|
||||
], [
|
||||
'target_field' => $map['target_field'],
|
||||
'position' => $map['position'],
|
||||
'options' => $map['options'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
// Contracts with Meta (groups + keys) demo
|
||||
$contractsMeta = ImportTemplate::query()->firstOrCreate([
|
||||
'name' => 'Contracts CSV (meta groups demo)',
|
||||
], [
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'description' => 'Contracts import demonstrating contract.meta mappings using groups and keys.',
|
||||
'source_type' => 'csv',
|
||||
'default_record_type' => 'contract',
|
||||
'sample_headers' => [
|
||||
'Reference', 'Start', 'End',
|
||||
'Note', 'Category', 'Custom ID',
|
||||
'Legal Note', 'Legal ID',
|
||||
],
|
||||
'is_active' => true,
|
||||
'meta' => [
|
||||
'delimiter' => ',',
|
||||
'enclosure' => '"',
|
||||
'escape' => '\\',
|
||||
],
|
||||
]);
|
||||
|
||||
$contractsMetaMappings = [
|
||||
// Core contract fields
|
||||
['source_column' => 'Reference', 'target_field' => 'contract.reference', 'position' => 1],
|
||||
['source_column' => 'Start', 'target_field' => 'contract.start_date', 'position' => 2],
|
||||
['source_column' => 'End', 'target_field' => 'contract.end_date', 'position' => 3],
|
||||
|
||||
// Meta group 1 using explicit options.key
|
||||
['source_column' => 'Note', 'target_field' => 'contract.meta', 'position' => 4, 'options' => ['group' => '1', 'key' => 'note']],
|
||||
['source_column' => 'Category', 'target_field' => 'contract.meta', 'position' => 5, 'options' => ['group' => '1', 'key' => 'category']],
|
||||
|
||||
// Meta group 1 using bracket key syntax (no options.key needed)
|
||||
['source_column' => 'Custom ID', 'target_field' => 'contract.meta[custom_id]', 'position' => 6, 'options' => ['group' => '1']],
|
||||
|
||||
// Meta group 2 examples
|
||||
['source_column' => 'Legal Note', 'target_field' => 'contract.meta', 'position' => 7, 'options' => ['group' => '2', 'key' => 'note']],
|
||||
['source_column' => 'Legal ID', 'target_field' => 'contract.meta[legal_id]', 'position' => 8, 'options' => ['group' => '2']],
|
||||
];
|
||||
|
||||
foreach ($contractsMetaMappings as $map) {
|
||||
ImportTemplateMapping::firstOrCreate([
|
||||
'import_template_id' => $contractsMeta->id,
|
||||
'source_column' => $map['source_column'],
|
||||
], [
|
||||
'target_field' => $map['target_field'],
|
||||
'position' => $map['position'],
|
||||
'options' => $map['options'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user