Anzeige und Import von übersetzten Artikeltexten und Bemerkungen
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
index 13ae97c..0381b4b 100644 (file)
@@ -2,6 +2,8 @@ package SL::Controller::CsvImport::Base;
 
 use strict;
 
+use List::MoreUtils qw(pairwise);
+
 use SL::Helper::Csv;
 
 use parent qw(Rose::Object);
@@ -15,32 +17,59 @@ use Rose::Object::MakeMethods::Generic
 sub run {
   my ($self) = @_;
 
-  $::lxdebug->dump(0, "file", $self->file);
-  $::lxdebug->dump(0, "profile", $self->controller->profile);
   my $profile = $self->profile;
   $self->csv(SL::Helper::Csv->new(file                   => $self->file->file_name,
                                   encoding               => $self->controller->profile->get('charset'),
                                   class                  => $self->class,
                                   profile                => $profile,
                                   ignore_unknown_columns => 1,
+                                  strict_profile         => 1,
                                   map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
                                  ));
   $self->csv->parse;
 
   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
 
-  $::lxdebug->dump(0, "err", $self->csv->errors);
-
   return unless $self->csv->header;
 
   my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
+  $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
   $self->controller->headers($headers);
+  $self->controller->raw_data_headers({ used => { }, headers => [ ] });
 
-  $self->controller->data([ map { { object => $_, errors => [] } } $self->csv->get_objects ]);
+  # my @data;
+  # foreach my $object ($self->csv->get_objects)
+  my @objects  = $self->csv->get_objects;
+  my @raw_data = @{ $self->csv->get_data };
+  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [] } } @objects, @raw_data ]);
 
   $self->check_objects;
   $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
+  $self->fix_field_lenghts;
+}
+
+sub add_columns {
+  my ($self, @columns) = @_;
+
+  my $h = $self->controller->headers;
+
+  foreach my $column (grep { !$h->{used}->{$_} } @columns) {
+    $h->{used}->{$column} = 1;
+    push @{ $h->{methods} }, $column;
+    push @{ $h->{headers} }, $column;
+  }
+}
+
+sub add_raw_data_columns {
+  my ($self, @columns) = @_;
+
+  my $h = $self->controller->raw_data_headers;
+
+  foreach my $column (grep { !$h->{used}->{$_} } @columns) {
+    $h->{used}->{$column} = 1;
+    push @{ $h->{headers} }, $column;
+  }
 }
 
 sub init_profile {
@@ -48,8 +77,11 @@ sub init_profile {
 
   eval "require " . $self->class;
 
+  my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns });
   my %profile;
   for my $col ($self->class->meta->columns) {
+    next if $unwanted{$col};
+
     my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric')   ? "$col\_as_number"
       :        $col->isa('Rose::DB::Object::Metadata::Column::Date')      ? "$col\_as_date"
       :        $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
@@ -91,6 +123,8 @@ sub save_objects {
   my $data = $params{data} || $self->controller->data;
 
   foreach my $entry (@{ $data }) {
+    next if @{ $entry->{errors} };
+
     if (!$entry->{object}->save) {
       push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
     } else {
@@ -99,4 +133,18 @@ sub save_objects {
   }
 }
 
+sub field_lengths {
+  return ();
+}
+
+sub fix_field_lenghts {
+  my ($self) = @_;
+
+  my %field_lengths = $self->field_lengths;
+  foreach my $entry (@{ $self->controller->data }) {
+    next unless @{ $entry->{errors} };
+    map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
+  }
+}
+
 1;