Anzeige und Import von übersetzten Artikeltexten und Bemerkungen
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
index 1753922..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);
@@ -21,6 +23,7 @@ sub run {
                                   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;
@@ -31,22 +34,54 @@ sub run {
 
   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 {
   my ($self) = @_;
 
   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"