From e95294b56b3e6b51c96f35add267b4dc58fe47d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Mon, 21 Oct 2013 12:58:48 +0200 Subject: [PATCH] =?utf8?q?Csv-Helper=20l=C3=A4sst=20header=20als=20einfach?= =?utf8?q?es=20Arrayref=20bei=20Nicht-Multiplex-Daten=20zu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/CsvImport/Base.pm | 2 +- SL/Helper/Csv.pm | 35 ++++++++++++++++++++++----------- SL/Helper/Csv/Dispatcher.pm | 3 ++- t/helper/csv.t | 16 +++++++-------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index de12c1552..5da5fc290 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -51,7 +51,7 @@ sub run { return if ( !$self->csv->header || $self->csv->errors ); - my $headers = { headers => [ grep { $profile->{$_} } @{ $self->csv->header->[0] } ] }; + my $headers = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] }; $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ]; $headers->{used} = { map { ($_ => 1) } @{ $headers->{headers} } }; $self->controller->headers($headers); diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 85f175889..1cc0fae5c 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -162,7 +162,11 @@ sub _check_header { 0, ]) unless $h; - push @{ $header }, $h; + if ($self->is_multiplexed) { + push @{ $header }, $h; + } else { + $header = $h; + } } } @@ -171,14 +175,19 @@ sub _check_header { # data with a discouraged but valid byte order mark # if not removed, the first header field will not be recognized if ($header) { - my $h = $header->[0]; + my $h = ($self->is_multiplexed)? $header->[0] : $header; + if ($h && $h->[0] && $self->encoding =~ /utf-?8/i) { $h->[0] =~ s/^\x{FEFF}//; } } # check, if all header fields are parsed well - return unless $header && all { $_ } @$header; + if ($self->is_multiplexed) { + return unless $header && all { $_ } @$header; + } else { + return unless $header; + } # Special case: human stupidity # people insist that case sensitivity doesn't exist and try to enter all @@ -188,8 +197,9 @@ sub _check_header { if ($self->case_insensitive_header) { die 'case_insensitive_header is only possible with profile' unless $self->profile; if ($header) { - my $p_num = 0; - foreach my $h (@{ $header }) { + my $h_aref = ($self->is_multiplexed)? $header : [ $header ]; + my $p_num = 0; + foreach my $h (@{ $h_aref }) { my @names = ( keys %{ $self->profile->[$p_num]->{profile} || {} }, ); @@ -253,7 +263,7 @@ sub _header_by_row { if ($self->is_multiplexed) { return $self->_row_header->{$row->[0]} } else { - return $self->header->[0]; + return $self->header; } } @@ -322,7 +332,7 @@ SL::Helper::Csv - take care of csv file uploads sep_char => ',', # default ';' quote_char => '\'', # default '"' escape_char => '"', # default '"' - header => [ [qw(id text sellprice word)] ], # see later + header => [ qw(id text sellprice word) ], # see later profile => [ { profile => { sellprice => 'sellprice_as_number'}, class => 'SL::DB::Part' } ], ); @@ -427,10 +437,11 @@ Same as in L =item C
\@HEADERS -If given, it contains an ARRAYREF for each different class type (i.e. one -ARRAYREF if the data is only of one class type). These ARRAYREFS are the header -fields which are an array of columns. In this case the first lines are not used -as a header. Empty header fields will be ignored in objects. +If given, it contains an ARRAY of the header fields for not multiplexed data. +Or an ARRAYREF for each different class type for multiplexed data. These +ARRAYREFS are the header fields which are an array of columns. In this case +the first lines are not used as a header. Empty header fields will be ignored +in objects. If not given, headers are taken from the first n lines of data, where n is the number of different class types. @@ -438,7 +449,7 @@ number of different class types. Examples: classic data of one type: - [ [ 'name', 'street', 'zipcode', 'city' ] ] + [ 'name', 'street', 'zipcode', 'city' ] multiplexed data with two different types [ [ 'ordernumber', 'customer', 'transdate' ], [ 'partnumber', 'qty', 'sellprice' ] ] diff --git a/SL/Helper/Csv/Dispatcher.pm b/SL/Helper/Csv/Dispatcher.pm index a08a63397..3a725b5a0 100644 --- a/SL/Helper/Csv/Dispatcher.pm +++ b/SL/Helper/Csv/Dispatcher.pm @@ -109,8 +109,9 @@ sub parse_profile { my @specs; my $csv_profile = $self->_csv->profile; + my $h_aref = ($self->_csv->is_multiplexed)? $self->_csv->header : [ $self->_csv->header ]; my $i = 0; - foreach my $header (@{ $self->_csv->header }) { + foreach my $header (@{ $h_aref }) { my $spec = $self->_parse_profile(profile => $csv_profile->[$i]->{profile}, class => $csv_profile->[$i]->{class}, header => $header); diff --git a/t/helper/csv.t b/t/helper/csv.t index 334c6f350..809dfb11a 100644 --- a/t/helper/csv.t +++ b/t/helper/csv.t @@ -12,7 +12,7 @@ Support::TestSetup::login(); my $csv = SL::Helper::Csv->new( file => \"Kaffee\n", # " # make emacs happy - header => [[ 'description' ]], + header => [ 'description' ], profile => [{ class => 'SL::DB::Part', }], ); @@ -29,7 +29,7 @@ $::myconfig{dateformat} = 'dd.mm.yyyy'; $csv = SL::Helper::Csv->new( file => \"Kaffee;0.12;12,2;1,5234\n", # " # make emacs happy - header => [[ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ]], + header => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ], profile => [{profile => { listprice => 'listprice_as_number' }, class => 'SL::DB::Part',}], ); @@ -273,7 +273,7 @@ is_deeply( ($csv->errors)[0], [ 'description', undef, 'header field \'descriptio $csv = SL::Helper::Csv->new( file => \"Kaffee", # " # make emacs happy - header => [[ 'description' ]], + header => [ 'description' ], profile => [{class => 'SL::DB::Part'}], ); $csv->parse; @@ -296,7 +296,7 @@ is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive hea $csv = SL::Helper::Csv->new( file => \"Kaffee", # " # make emacs happy - header => [[ 'Description' ]], + header => [ 'Description' ], case_insensitive_header => 1, profile => [{ profile => { description => 'description' }, @@ -320,7 +320,7 @@ is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'utf8 BOM works (bug $csv = SL::Helper::Csv->new( file => \"Kaffee", # " # make emacs happy - header => [[ 'Description' ]], + header => [ 'Description' ], profile => [{class => 'SL::DB::Part'}], ); $csv->parse; @@ -330,7 +330,7 @@ is_deeply $csv->get_data, undef, 'case insensitive header without flag ignores'; $csv = SL::Helper::Csv->new( file => \"Kaffee", # " # make emacs happy - header => [[ 'foo' ]], + header => [ 'foo' ], profile => [{ profile => { foo => '' }, class => 'SL::DB::Part', @@ -345,7 +345,7 @@ ok $csv->get_objects->[0], 'empty path gets ignored in object creation'; $csv = SL::Helper::Csv->new( file => \"Kaffee", # " # make emacs happy - header => [[ 'foo' ]], + header => [ 'foo' ], strict_profile => 1, profile => [{ profile => { foo => '' }, @@ -359,7 +359,7 @@ ok $csv->get_objects->[0], 'empty path gets ignored in object creation (strict p $csv = SL::Helper::Csv->new( file => \"Phil", # " # make emacs happy - header => [[ 'CVAR_grOUnDHog' ]], + header => [ 'CVAR_grOUnDHog' ], strict_profile => 1, case_insensitive_header => 1, profile => [{ -- 2.20.1