CsvImport: Feldlängen aus den Metadaten auslesen, nicht fest codieren.
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Sat, 24 May 2014 23:19:49 +0000 (01:19 +0200)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Sat, 24 May 2014 23:19:49 +0000 (01:19 +0200)
SL/Controller/CsvImport/Base.pm
SL/Controller/CsvImport/BaseMulti.pm
SL/Controller/CsvImport/Contact.pm
SL/Controller/CsvImport/CustomerVendor.pm
SL/Controller/CsvImport/Shipto.pm

index 8c6fa73..681ec24 100644 (file)
@@ -465,7 +465,9 @@ sub save_objects {
 }
 
 sub field_lengths {
-  return ();
+  my ($self) = @_;
+
+  return map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{$self->class->meta->columns};
 }
 
 sub fix_field_lengths {
index 31a3c3b..7e35d97 100644 (file)
@@ -250,6 +250,32 @@ sub add_cvar_columns_to_displayable_columns {
                                      @{ $self->cvar_configs_by->{row_ident}->{$row_ident} });
 }
 
+sub field_lengths {
+  my ($self) = @_;
+
+  my %field_lengths_by_ri = ();
+
+  foreach my $p (@{ $self->profile }) {
+    my %field_lengths = map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{ $p->{class}->meta->columns };
+    $field_lengths_by_ri{ $p->{row_ident} } = \%field_lengths;
+  }
+
+  return %field_lengths_by_ri;
+}
+
+sub fix_field_lengths {
+  my ($self) = @_;
+
+  my %field_lengths_by_ri = $self->field_lengths;
+  foreach my $entry (@{ $self->controller->data }) {
+    next unless @{ $entry->{errors} };
+    my %field_lengths = %{ $field_lengths_by_ri{ $entry->{raw_data}->{datatype} } };
+    map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
+  }
+
+  return;
+}
+
 sub is_multiplexed { 1 }
 
 1;
index 2eb877e..e653d7c 100644 (file)
@@ -126,16 +126,6 @@ sub get_duplicate_check_fields {
   };
 }
 
-sub field_lengths {
-  return ( cp_title     => 75,
-           cp_givenname => 75,
-           cp_name      => 75,
-           cp_phone1    => 75,
-           cp_phone2    => 75,
-           cp_gender    =>  1,
-         );
-}
-
 sub setup_displayable_columns {
   my ($self) = @_;
 
index 946df06..7d37904 100644 (file)
@@ -214,26 +214,6 @@ sub save_objects {
   $self->SUPER::save_objects(data => $without_number);
 }
 
-sub field_lengths {
-  return ( name           => 75,
-           department_1   => 75,
-           department_2   => 75,
-           street         => 75,
-           zipcode        => 10,
-           city           => 75,
-           country        => 75,
-           contact        => 75,
-           fax            => 30,
-           account_number => 15,
-           bank_code      => 10,
-           language       => 5,
-           username       => 50,
-           ustid          => 14,
-           iban           => 100,
-           bic            => 100,
-         );
-}
-
 sub init_profile {
   my ($self) = @_;
 
index c20b574..1d2fdd7 100644 (file)
@@ -88,20 +88,6 @@ sub get_duplicate_check_fields {
   };
 }
 
-sub field_lengths {
-  return ( shiptoname         => 75,
-           shiptodepartment_1 => 75,
-           shiptodepartment_2 => 75,
-           shiptostreet       => 75,
-           shiptozipcode      => 75,
-           shiptocity         => 75,
-           shiptocountry      => 75,
-           shiptocontact      => 75,
-           shiptophone        => 30,
-           shiptofax          => 30,
-         );
-}
-
 sub init_profile {
   my ($self) = @_;