Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 67643d03

Von Sven Schöling vor fast 12 Jahren hinzugefügt

  • ID 67643d0386e7ad051009af639cada8a5ad60d1ea
  • Vorgänger 1e62938e
  • Nachfolger a5a42ed1

Csv Import in Datenbank zwischenspeichern und rudimentärer Report.

Unterschiede anzeigen:

SL/Controller/CsvImport.pm
4 4

  
5 5
use SL::DB::Buchungsgruppe;
6 6
use SL::DB::CsvImportProfile;
7
use SL::DB::CsvImportReport;
7 8
use SL::DB::Unit;
8 9
use SL::Helper::Flash;
9 10
use SL::SessionFile;
......
26 27

  
27 28
__PACKAGE__->run_before('check_auth');
28 29
__PACKAGE__->run_before('ensure_form_structure');
29
__PACKAGE__->run_before('check_type');
30
__PACKAGE__->run_before('check_type', except => [ qw(report) ]);
30 31
__PACKAGE__->run_before('load_all_profiles');
31 32

  
32 33
#
......
182 183
  $self->num_imported(0);
183 184
  $worker->save_objects if !$params{test};
184 185

  
186
  $self->save_report;
187

  
185 188
  $self->num_importable(scalar grep { !$_ } map { scalar @{ $_->{errors} } } @{ $self->data || [] });
186 189
  $self->import_status($params{test} ? 'tested' : 'imported');
187 190

  
......
257 260
         );
258 261
}
259 262

  
263
sub save_report {
264
  my ($self, $report_id) = @_;
265

  
266
  my $clone_profile = $self->profile->clone_and_reset_deep;
267
  $clone_profile->save; # weird bug. if this isn't saved before adding it to the report, it will default back to the last profile.
268

  
269
  my $report = SL::DB::CsvImportReport->new(
270
    session_id => $::auth->create_or_refresh_session,
271
    profile    => $clone_profile,
272
    type       => $self->type,
273
    file       => '',
274
  )->save(cascade => 1);
275

  
276
  my $dbh = $::form->get_standard_dbh;
277
  $dbh->begin_work;
278

  
279
  my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
280

  
281
  my $sth = $dbh->prepare($query);
282

  
283
  # save headers
284
  my @headers = (
285
    @{ $self->info_headers->{headers} || [] },
286
    @{ $self->headers->{headers} || [] },
287
    @{ $self->raw_data_headers->{headers} || [] },
288
  );
289
  my @info_methods = keys %{ $self->info_headers->{methods} || {} };
290
  my @methods      = @{ $self->headers->{methods} || [] };
291
  my @raw_methods  = keys %{ $self->raw_data_headers->{used} || {} };
292

  
293
  $sth->execute($report->id, $_, 0, $headers[$_]) for 0 .. $#headers;
294

  
295
  # col offsets
296
  my $o1 =       @info_methods;
297
  my $o2 = $o1 + @methods;
298

  
299
  for my $row (0 .. $#{ $self->data }) {
300
    my $data_row = $self->{data}[$row];
301

  
302
    $sth->execute($report->id,       $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
303
    $sth->execute($report->id, $o1 + $_, $row + 1, $data_row->{object}->${ \ $methods[$_] })    for 0 .. $#methods;
304
    $sth->execute($report->id, $o2 + $_, $row + 1, $data_row->{raw_data}{ $raw_methods[$_] })   for 0 .. $#raw_methods;
305
  }
306

  
307
  $dbh->commit;
308
}
309

  
310
sub action_report {
311
  my ($self) = @_;
312

  
313
  $self->{report} = SL::DB::Manager::CsvImportReport->find_by(id => $::form->{id});
314

  
315
  $self->render('csv_import/report');
316
}
317

  
260 318
sub csv_file_name {
261 319
  my ($self) = @_;
262 320
  return "csv-import-" . $self->type . ".csv";

Auch abrufbar als: Unified diff