From 37bc29829fa9ef78948e3ece2c5e60696b4bb5c1 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 14 Nov 2017 12:57:28 +0000 Subject: [PATCH] Refactored csv import flash errors --- lib/Pear/LocalLoop/Controller/Admin/Import.pm | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/Import.pm b/lib/Pear/LocalLoop/Controller/Admin/Import.pm index c2b3ef1..c1c2fe1 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Import.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Import.pm @@ -69,7 +69,7 @@ sub post_add { }; if ( defined $error ) { - $c->flash( error => $error, csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( $error ); $c->redirect_to( '/admin/import/add' ); return; } @@ -78,7 +78,7 @@ sub post_add { my @required = grep {/^user$|^value$|^date$|^organisation$/} @csv_headers; unless ( scalar( @required ) == 4 ) { - $c->flash( error => 'Required columns not available', csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( 'Required columns not available' ); $c->redirect_to( '/admin/import/add' ); return; } @@ -86,7 +86,7 @@ sub post_add { my $csv_output = $csv->getline_hr_all( $fh ); unless ( scalar( @$csv_output ) ) { - $c->flash( error => "No data found", csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( "No data found" ); $c->redirect_to( '/admin/import/add' ); return; } @@ -94,7 +94,7 @@ sub post_add { for my $data ( @$csv_output ) { for my $key ( qw/ user value organisation / ) { unless ( defined $data->{$key} ) { - $c->flash( error => "Undefined [$key] data found", csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( "Undefined [$key] data found" ); $c->redirect_to( '/admin/import/add' ); return; } @@ -103,7 +103,7 @@ sub post_add { my $dtp = DateTime::Format::Strptime->new( pattern => $date_format ); my $dt_obj = $dtp->parse_datetime($data->{date}); unless ( defined $dt_obj ) { - $c->flash( error => "Undefined or incorrect format for [date] data found", csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( "Undefined or incorrect format for [date] data found" ); $c->redirect_to( '/admin/import/add' ); return; } @@ -126,7 +126,7 @@ sub post_add { ); unless ( defined $value_set ) { - $c->flash( error => 'Error creating new Value Set', csv_data => $csv_data, date_format => $date_format ); + $c->_csv_flash_error( 'Error creating new Value Set' ); $c->redirect_to( '/admin/import/add' ); return; } @@ -135,6 +135,17 @@ sub post_add { $c->redirect_to( '/admin/import/' . $value_set->id ); } +sub _csv_flash_error { + my ( $c, $error ) = @_; + $error //= "An error occurred"; + + $c->flash( + error => $error, + csv_data => $c->param('csv'), + date_format => $c->param('date_format'), + ); +} + sub get_user { my $c = shift; my $set_id = $c->param('set_id'); @@ -182,6 +193,8 @@ sub get_user { ); } + + sub get_org { my $c = shift;