Artikelimport bisher
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
1 package SL::Controller::CsvImport::Base;
2
3 use strict;
4
5 use List::MoreUtils qw(pairwise);
6
7 use SL::Helper::Csv;
8
9 use parent qw(Rose::Object);
10
11 use Rose::Object::MakeMethods::Generic
12 (
13  scalar                  => [ qw(controller file csv) ],
14  'scalar --get_set_init' => [ qw(profile existing_objects class manager_class) ],
15 );
16
17 sub run {
18   my ($self) = @_;
19
20   my $profile = $self->profile;
21   $self->csv(SL::Helper::Csv->new(file                   => $self->file->file_name,
22                                   encoding               => $self->controller->profile->get('charset'),
23                                   class                  => $self->class,
24                                   profile                => $profile,
25                                   ignore_unknown_columns => 1,
26                                   map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
27                                  ));
28   $self->csv->parse;
29
30   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
31
32   return unless $self->csv->header;
33
34   my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
35   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
36   $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
37   $self->controller->headers($headers);
38
39   # my @data;
40   # foreach my $object ($self->csv->get_objects)
41   my @objects  = $self->csv->get_objects;
42   my @raw_data = @{ $self->csv->get_data };
43   $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [] } } @objects, @raw_data ]);
44
45   $self->check_objects;
46   $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
47   $self->fix_field_lenghts;
48 }
49
50 sub add_columns {
51   my ($self, @columns) = @_;
52
53   my $h = $self->controller->headers;
54
55   foreach my $column (grep { !$h->{used}->{$_} } @columns) {
56     $h->{used}->{$column} = 1;
57     push @{ $h->{methods} }, $column;
58     push @{ $h->{headers} }, $column;
59   }
60 }
61
62 sub init_profile {
63   my ($self) = @_;
64
65   eval "require " . $self->class;
66
67   my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns });
68   my %profile;
69   for my $col ($self->class->meta->columns) {
70     next if $unwanted{$col};
71
72     my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric')   ? "$col\_as_number"
73       :        $col->isa('Rose::DB::Object::Metadata::Column::Date')      ? "$col\_as_date"
74       :        $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
75       :                                                                     $col->name;
76
77     $profile{$col} = $name;
78   }
79
80   $self->profile(\%profile);
81 }
82
83 sub init_existing_objects {
84   my ($self) = @_;
85
86   eval "require " . $self->class;
87   $self->existing_objects($self->manager_class->get_all);
88 }
89
90 sub init_class {
91   die "class not set";
92 }
93
94 sub init_manager_class {
95   my ($self) = @_;
96
97   $self->class =~ m/^SL::DB::(.+)/;
98   $self->manager_class("SL::DB::Manager::" . $1);
99 }
100
101 sub check_objects {
102 }
103
104 sub check_duplicates {
105 }
106
107 sub save_objects {
108   my ($self, %params) = @_;
109
110   my $data = $params{data} || $self->controller->data;
111
112   foreach my $entry (@{ $data }) {
113     next if @{ $entry->{errors} };
114
115     if (!$entry->{object}->save) {
116       push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
117     } else {
118       $self->controller->num_imported($self->controller->num_imported + 1);
119     }
120   }
121 }
122
123 sub field_lengths {
124   return ();
125 }
126
127 sub fix_field_lenghts {
128   my ($self) = @_;
129
130   my %field_lengths = $self->field_lengths;
131   foreach my $entry (@{ $self->controller->data }) {
132     next unless @{ $entry->{errors} };
133     map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
134   }
135 }
136
137 1;