From 656efcb5364ae09cfffb02a8ad4f13ff8f5d7db7 Mon Sep 17 00:00:00 2001 From: Alex Sassmannshausen Date: Thu, 8 Sep 2016 12:39:24 +0200 Subject: [PATCH] Bug 5670: [Followup] Refactor .pl; error messages. * members/housebound.pl: Refactor & store messages for encountered errors. * koha-tmpl/intranet-tmpl/prog/en/modules/members/housebound.tt: Show messages. Signed-off-by: Claire Gravely Signed-off-by: Kyle M Hall --- .../prog/en/modules/members/housebound.tt | 24 +++++++ members/housebound.pl | 64 +++++++++++++------ 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/housebound.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/housebound.tt index fd0c618378..604523ad62 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/housebound.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/housebound.tt @@ -48,6 +48,30 @@ $(document).ready(function() {

Housebound details for [% patron.title %] [% patron.firstname %] [% patron.surname %] ([% patron.cardnumber %])

+ [% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_patron_load' %] + An error occurred whilst loading the patron details. + [% CASE 'error_on_profile_store' %] + An error occurred whilst updating this housebound profile. + [% CASE 'error_on_profile_create' %] + An error occurred whilst creating this housebound profile. + [% CASE 'error_on_visit_load' %] + An error occurred whilst loading the housebound visit. + [% CASE 'error_on_visit_delete' %] + An error occurred whilst deleting a housebound visit. + [% CASE 'error_on_visit_store' %] + An error occurred whilst updating a housebound visit. + [% CASE 'error_on_visit_create' %] + An error occurred whilst creating a new housebound visit. + [% CASE %] + [% m.code %] + [% END %] + Please try again later. +
+ [% END %] + [% IF ( method == 'update_or_create' ) %]

Manage housebound profile

diff --git a/members/housebound.pl b/members/housebound.pl index f5ced86b71..516effc841 100755 --- a/members/housebound.pl +++ b/members/housebound.pl @@ -47,17 +47,37 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -my $patron = Koha::Patrons->new->find($input->param('borrowernumber')); +my @messages; # For error messages. my $method = $input->param('method') // q{}; my $visit_id = $input->param('visit_id') // q{}; -my $branch = Koha::Libraries->new->find($patron->branchcode); -my $category = Koha::Patron::Categories->new->find($patron->categorycode); -my $houseboundprofile = $patron->housebound_profile; +# Get patron +my $patron = eval { + return Koha::Patrons->new->find($input->param('borrowernumber')); +}; +push @messages, { type => 'error', code => 'error_on_patron_load' } + if ( $@ or !$patron ); + +# Get supporting cast +my ( $branch, $category, $houseboundprofile, $visit ); +if ( $patron ) { + $branch = Koha::Libraries->new->find($patron->branchcode); + $category = Koha::Patron::Categories->new->find($patron->categorycode); + $houseboundprofile = $patron->housebound_profile; +} +if ( $visit_id ) { + $visit = eval { + return Koha::Patron::HouseboundVisits->find($visit_id); + }; + push @messages, { type => 'error', code => 'error_on_visit_load' } + if ( $@ or !$visit ); +} + +# Main processing my ( $houseboundvisits, $deliverers, $choosers ); my ( $houseboundvisit, $deliverer, $chooser ); -if ( $method eq 'updateconfirm' ) { +if ( $method eq 'updateconfirm' and $houseboundprofile ) { # We have received the input from the profile edit form. We must save the # changes, and return to simple display. $houseboundprofile->set({ @@ -69,8 +89,9 @@ if ( $method eq 'updateconfirm' ) { referral => $input->param('referral') // q{}, notes => $input->param('notes') // q{}, }); - die("Unable to store edited profile") - unless ( $houseboundprofile->store ); + my $success = eval { return $houseboundprofile->store }; + push @messages, { type => 'error', code => 'error_on_profile_store' } + if ( $@ or !$success ); $method = undef; } elsif ( $method eq 'createconfirm' ) { # We have received the input necessary to create a new profile. We must @@ -85,24 +106,24 @@ if ( $method eq 'updateconfirm' ) { referral => $input->param('referral') // q{}, notes => $input->param('notes') // q{}, }); - die("Unable to store new profile") - unless ( $houseboundprofile->store ); + my $success = eval { return $houseboundprofile->store }; + push @messages, { type => 'error', code => 'error_on_profile_create' } + if ( $@ or !$success ); $method = undef; } elsif ( $method eq 'visit_update_or_create' ) { # We want to edit, edit a visit, so we must pass its details. $deliverers = Koha::Patrons->new->housebound_deliverers; $choosers = Koha::Patrons->new->housebound_choosers; - $houseboundvisit = Koha::Patron::HouseboundVisits->find($visit_id) - if ( $visit_id ); -} elsif ( $method eq 'visit_delete' ) { + $houseboundvisit = $visit; +} elsif ( $method eq 'visit_delete' and $visit ) { # We want ot delete a specific visit. - my $visit = Koha::Patron::HouseboundVisits->find($visit_id); - die("Unable to delete visit") unless ( $visit->delete ); + my $success = eval { return $visit->delete }; + push @messages, { type => 'error', code => 'error_on_visit_delete' } + if ( $@ or !$success ); $method = undef; -} elsif ( $method eq 'editvisitconfirm' ) { +} elsif ( $method eq 'editvisitconfirm' and $visit ) { # We have received input for editing a visit. We must store and return to # simple display. - my $visit = Koha::Patron::HouseboundVisits->find($visit_id); $visit->set({ borrowernumber => $input->param('borrowernumber') // q{}, appointment_date => $input->param('date') // q{}, @@ -110,9 +131,11 @@ if ( $method eq 'updateconfirm' ) { chooser_brwnumber => $input->param('chooser') // q{}, deliverer_brwnumber => $input->param('deliverer') // q{}, }); - die("Unable to store edited visit") unless ( $visit->store ); + my $success = eval { return $visit->store }; + push @messages, { type => 'error', code => 'error_on_visit_store' } + if ( $@ or !$success ); $method = undef; -} elsif ( $method eq 'addvisitconfirm' ) { +} elsif ( $method eq 'addvisitconfirm' and !$visit ) { # We have received input for creating a visit. We must store and return # to simple display. my $visit = Koha::Patron::HouseboundVisit->new({ @@ -122,7 +145,9 @@ if ( $method eq 'updateconfirm' ) { chooser_brwnumber => $input->param('chooser') // q{}, deliverer_brwnumber => $input->param('deliverer') // q{}, }); - die("Unable to store new visit") unless ( $visit->store ); + my $success = eval { return $visit->store }; + push @messages, { type => 'error', code => 'error_on_visit_create' } + if ( $@ or !$success ); $method = undef; } @@ -135,6 +160,7 @@ $template->param( visit => $houseboundvisit, branch => $branch, category => $category, + messages => \@messages, method => $method, choosers => $choosers, deliverers => $deliverers, -- 2.39.5