Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision e95294b5

Von Bernd Bleßmann vor fast 11 Jahren hinzugefügt

  • ID e95294b56b3e6b51c96f35add267b4dc58fe47d7
  • Vorgänger 01a4e61c
  • Nachfolger 3250ebcf

Csv-Helper lässt header als einfaches Arrayref bei Nicht-Multiplex-Daten zu.

Unterschiede anzeigen:

SL/Controller/CsvImport/Base.pm
51 51

  
52 52
  return if ( !$self->csv->header || $self->csv->errors );
53 53

  
54
  my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header->[0] } ] };
54
  my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
55 55
  $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
56 56
  $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
57 57
  $self->controller->headers($headers);
SL/Helper/Csv.pm
162 162
        0,
163 163
      ]) unless $h;
164 164

  
165
      push @{ $header }, $h;
165
      if ($self->is_multiplexed) {
166
        push @{ $header }, $h;
167
      } else {
168
        $header = $h;
169
      }
166 170
    }
167 171
  }
168 172

  
......
171 175
  # data with a discouraged but valid byte order mark
172 176
  # if not removed, the first header field will not be recognized
173 177
  if ($header) {
174
    my $h = $header->[0];
178
    my $h = ($self->is_multiplexed)? $header->[0] : $header;
179

  
175 180
    if ($h && $h->[0] && $self->encoding =~ /utf-?8/i) {
176 181
      $h->[0] =~ s/^\x{FEFF}//;
177 182
    }
178 183
  }
179 184

  
180 185
  # check, if all header fields are parsed well
181
  return unless $header && all { $_ } @$header;
186
  if ($self->is_multiplexed) {
187
    return unless $header && all { $_ } @$header;
188
  } else {
189
    return unless $header;
190
  }
182 191

  
183 192
  # Special case: human stupidity
184 193
  # people insist that case sensitivity doesn't exist and try to enter all
......
188 197
  if ($self->case_insensitive_header) {
189 198
    die 'case_insensitive_header is only possible with profile' unless $self->profile;
190 199
    if ($header) {
191
      my $p_num = 0;
192
      foreach my $h (@{ $header }) {
200
      my $h_aref = ($self->is_multiplexed)? $header : [ $header ];
201
      my $p_num  = 0;
202
      foreach my $h (@{ $h_aref }) {
193 203
        my @names = (
194 204
          keys %{ $self->profile->[$p_num]->{profile} || {} },
195 205
        );
......
253 263
  if ($self->is_multiplexed) {
254 264
    return $self->_row_header->{$row->[0]}
255 265
  } else {
256
    return $self->header->[0];
266
    return $self->header;
257 267
  }
258 268
}
259 269

  
......
322 332
    sep_char    => ',',     # default ';'
323 333
    quote_char  => '\'',    # default '"'
324 334
    escape_char => '"',     # default '"'
325
    header      => [ [qw(id text sellprice word)] ], # see later
335
    header      => [ qw(id text sellprice word) ], # see later
326 336
    profile     => [ { profile => { sellprice => 'sellprice_as_number'},
327 337
                       class   => 'SL::DB::Part' } ],
328 338
  );
......
427 437

  
428 438
=item C<header> \@HEADERS
429 439

  
430
If given, it contains an ARRAYREF for each different class type (i.e. one
431
ARRAYREF if the data is only of one class type). These ARRAYREFS are the header
432
fields which are an array of columns. In this case the first lines are not used
433
as a header. Empty header fields will be ignored in objects.
440
If given, it contains an ARRAY of the header fields for not multiplexed data.
441
Or an ARRAYREF for each different class type for multiplexed data. These
442
ARRAYREFS are the header fields which are an array of columns. In this case
443
the first lines are not used as a header. Empty header fields will be ignored
444
in objects.
434 445

  
435 446
If not given, headers are taken from the first n lines of data, where n is the
436 447
number of different class types.
......
438 449
Examples:
439 450

  
440 451
  classic data of one type:
441
  [ [ 'name', 'street', 'zipcode', 'city' ] ]
452
  [ 'name', 'street', 'zipcode', 'city' ]
442 453

  
443 454
  multiplexed data with two different types
444 455
  [ [ 'ordernumber', 'customer', 'transdate' ], [ 'partnumber', 'qty', 'sellprice' ] ]
SL/Helper/Csv/Dispatcher.pm
109 109
  my @specs;
110 110

  
111 111
  my $csv_profile = $self->_csv->profile;
112
  my $h_aref = ($self->_csv->is_multiplexed)? $self->_csv->header : [ $self->_csv->header ];
112 113
  my $i = 0;
113
  foreach my $header (@{ $self->_csv->header }) {
114
  foreach my $header (@{ $h_aref }) {
114 115
    my $spec = $self->_parse_profile(profile => $csv_profile->[$i]->{profile},
115 116
                                     class   => $csv_profile->[$i]->{class},
116 117
                                     header  => $header);
t/helper/csv.t
12 12

  
13 13
my $csv = SL::Helper::Csv->new(
14 14
  file    => \"Kaffee\n",       # " # make emacs happy
15
  header  => [[ 'description' ]],
15
  header  => [ 'description' ],
16 16
  profile => [{ class  => 'SL::DB::Part', }],
17 17
);
18 18

  
......
29 29

  
30 30
$csv = SL::Helper::Csv->new(
31 31
  file    => \"Kaffee;0.12;12,2;1,5234\n",            # " # make emacs happy
32
  header  => [[ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ]],
32
  header  => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ],
33 33
  profile => [{profile => { listprice => 'listprice_as_number' },
34 34
               class   => 'SL::DB::Part',}],
35 35
);
......
273 273

  
274 274
$csv = SL::Helper::Csv->new(
275 275
  file   => \"Kaffee",       # " # make emacs happy
276
  header =>  [[ 'description' ]],
276
  header =>  [ 'description' ],
277 277
  profile => [{class  => 'SL::DB::Part'}],
278 278
);
279 279
$csv->parse;
......
296 296

  
297 297
$csv = SL::Helper::Csv->new(
298 298
  file   => \"Kaffee",          # " # make emacs happy
299
  header =>  [[ 'Description' ]],
299
  header =>  [ 'Description' ],
300 300
  case_insensitive_header => 1,
301 301
  profile => [{
302 302
    profile => { description => 'description' },
......
320 320

  
321 321
$csv = SL::Helper::Csv->new(
322 322
  file   => \"Kaffee",            # " # make emacs happy
323
  header => [[ 'Description' ]],
323
  header => [ 'Description' ],
324 324
  profile => [{class  => 'SL::DB::Part'}],
325 325
);
326 326
$csv->parse;
......
330 330

  
331 331
$csv = SL::Helper::Csv->new(
332 332
  file   => \"Kaffee",            # " # make emacs happy
333
  header => [[ 'foo' ]],
333
  header => [ 'foo' ],
334 334
  profile => [{
335 335
    profile => { foo => '' },
336 336
    class  => 'SL::DB::Part',
......
345 345

  
346 346
$csv = SL::Helper::Csv->new(
347 347
  file   => \"Kaffee",            # " # make emacs happy
348
  header => [[ 'foo' ]],
348
  header => [ 'foo' ],
349 349
  strict_profile => 1,
350 350
  profile => [{
351 351
    profile => { foo => '' },
......
359 359

  
360 360
$csv = SL::Helper::Csv->new(
361 361
  file   => \"Phil",            # " # make emacs happy
362
  header => [[ 'CVAR_grOUnDHog' ]],
362
  header => [ 'CVAR_grOUnDHog' ],
363 363
  strict_profile => 1,
364 364
  case_insensitive_header => 1,
365 365
  profile => [{

Auch abrufbar als: Unified diff