Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
index 4de38ed..e7889d7 100644 (file)
@@ -15,12 +15,14 @@ use parent qw(Rose::Object);
 
 use Rose::Object::MakeMethods::Generic
 (
- scalar                  => [ qw(controller file csv save_with_cascade) ],
+ scalar                  => [ qw(controller file csv test_run save_with_cascade) ],
  'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ],
 );
 
 sub run {
-  my ($self) = @_;
+  my ($self, %params) = @_;
+
+  $self->test_run($params{test_run});
 
   $self->controller->track_progress(phase => 'parsing csv', progress => 0);
 
@@ -120,6 +122,11 @@ sub add_cvar_raw_data_columns {
   map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns };
 }
 
+sub init_all_cvar_configs {
+  # Must be overridden by derived specialized importer classes.
+  return [];
+}
+
 sub init_cvar_columns {
   my ($self) = @_;
 
@@ -189,8 +196,6 @@ sub check_vc {
 sub handle_cvars {
   my ($self, $entry) = @_;
 
-  return unless $self->can('all_cvar_configs');
-
   my %type_to_column = ( text      => 'text_value',
                          textfield => 'text_value',
                          select    => 'text_value',
@@ -229,13 +234,9 @@ sub init_profile {
     $profile{$col} = $name;
   }
 
-  if ($self->can('all_cvar_configs')) {
-    for (@{ $self->all_cvar_configs }) {
-      $profile{ 'cvar_' . $_->name } = '';
-    }
-  }
+  $profile{ 'cvar_' . $_->name } = '' for @{ $self->all_cvar_configs };
 
-  $self->profile(\%profile);
+  \%profile;
 }
 
 sub add_displayable_columns {
@@ -264,8 +265,6 @@ sub setup_displayable_columns {
 sub add_cvar_columns_to_displayable_columns {
   my ($self) = @_;
 
-  return unless $self->can('all_cvar_configs');
-
   $self->add_displayable_columns(map { { name        => 'cvar_' . $_->name,
                                          description => $::locale->text('#1 (custom variable)', $_->description) } }
                                      @{ $self->all_cvar_configs });
@@ -383,7 +382,7 @@ sub save_objects {
   return unless $data->[0];
   return unless $data->[0]{object};
 
-  $self->controller->track_progress(phase => 'saving objects', progress => 0); # scale from 45..95%;
+  $self->controller->track_progress(phase => 'saving data', progress => 0); # scale from 45..95%;
 
   my $dbh = $data->[0]{object}->db;
 
@@ -423,5 +422,20 @@ sub fix_field_lengths {
   }
 }
 
-1;
+sub clean_fields {
+  my ($self, $illegal_chars, $object, @fields) = @_;
+
+  my @cleaned_fields;
+  foreach my $field (grep { $object->can($_) } @fields) {
+    my $value = $object->$field;
+
+    next unless defined($value) && ($value =~ s/$illegal_chars/ /g);
+
+    $object->$field($value);
+    push @cleaned_fields, $field;
+  }
+
+  return @cleaned_fields;
+}
 
+1;