1 package SL::Controller::CsvImport::Base;
7 use parent qw(Rose::Object);
9 use Rose::Object::MakeMethods::Generic
11 scalar => [ qw(controller file csv) ],
12 'scalar --get_set_init' => [ qw(profile existing_objects class manager_class) ],
18 $::lxdebug->dump(0, "file", $self->file);
19 $::lxdebug->dump(0, "profile", $self->controller->profile);
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,
25 ignore_unknown_columns => 1,
26 map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
30 $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
32 $::lxdebug->dump(0, "err", $self->csv->errors);
34 return unless $self->csv->header;
36 my $headers = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
37 $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
38 $self->controller->headers($headers);
40 $self->controller->data([ map { { object => $_, errors => [] } } $self->csv->get_objects ]);
43 $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
49 eval "require " . $self->class;
52 for my $col ($self->class->meta->columns) {
53 my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric') ? "$col\_as_number"
54 : $col->isa('Rose::DB::Object::Metadata::Column::Date') ? "$col\_as_date"
55 : $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
58 $profile{$col} = $name;
61 $self->profile(\%profile);
64 sub init_existing_objects {
67 eval "require " . $self->class;
68 $self->existing_objects($self->manager_class->get_all);
75 sub init_manager_class {
78 $self->class =~ m/^SL::DB::(.+)/;
79 $self->manager_class("SL::DB::Manager::" . $1);
85 sub check_duplicates {
89 my ($self, %params) = @_;
91 my $data = $params{data} || $self->controller->data;
93 foreach my $entry (@{ $data }) {
94 next if @{ $entry->{errors} };
96 if (!$entry->{object}->save) {
97 push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
99 $self->controller->num_imported($self->controller->num_imported + 1);