@@ -, +, @@ --- Koha/Exceptions.pm | 4 + .../Restriction/Type.pm} | 49 +++---- .../Restriction/Types.pm} | 8 +- Koha/Schema/Result/RestrictionType.pm | 4 +- admin/restrictions.pl | 40 ++++-- circ/circulation.pl | 4 +- .../prog/en/modules/admin/restrictions.tt | 10 ++ members/memberentry.pl | 4 +- members/moremember.pl | 4 +- t/db_dependent/Koha/Patron/Restriction/Type.t | 124 ++++++++++++++++++ .../Patron/Restriction/Types.t} | 13 +- t/db_dependent/RestrictionTypes.t | 120 ----------------- 12 files changed, 210 insertions(+), 174 deletions(-) rename Koha/{RestrictionType.pm => Patron/Restriction/Type.pm} (56%) rename Koha/{RestrictionTypes.pm => Patron/Restriction/Types.pm} (85%) create mode 100755 t/db_dependent/Koha/Patron/Restriction/Type.t rename t/db_dependent/{RestrictionType.t => Koha/Patron/Restriction/Types.t} (80%) delete mode 100755 t/db_dependent/RestrictionTypes.t --- a/Koha/Exceptions.pm +++ a/Koha/Exceptions.pm @@ -32,6 +32,10 @@ use Exception::Class ( isa => 'Koha::Exception', description => 'The default value cannot be deleted' }, + 'Koha::Exceptions::CannotDeleteSystem' => { + isa => 'Koha::Exception', + description => 'The system value cannot be deleted' + }, 'Koha::Exceptions::MissingParameter' => { isa => 'Koha::Exception', description => 'A required parameter is missing' --- a/Koha/RestrictionType.pm +++ a/Koha/RestrictionType.pm @@ -1,4 +1,4 @@ -package Koha::RestrictionType; +package Koha::Patron::Restriction::Type; # This file is part of Koha. # @@ -19,12 +19,14 @@ use Modern::Perl; use base qw(Koha::Object); -use Koha::RestrictionTypes; +use Koha::Exceptions; + +use Koha::Patron::Restriction::Types; use C4::Context; =head1 NAME -Koha::RestrictionType - Koha RestrictionType Object class +Koha::Patron::Restriction::Type - Koha RestrictionType Object class =head1 API @@ -39,28 +41,29 @@ Overloaded delete method that does extra clean up: =cut sub delete { - my ( $self ) = @_; + my ($self) = @_; - # Find out what the default is - my $default = Koha::RestrictionTypes->find({ is_default => 1 })->code; - # Ensure we're not trying to delete a is_system type (this includes - # the default type) - return 0 if $self->is_system == 1; - # We can't use Koha objects here because Koha::Patron::Debarments - # is not a Koha object. So we'll do it old skool - my $rows = C4::Context->dbh->do( - "UPDATE borrower_debarments SET type = ? WHERE type = ?", - undef, - ($default, $self->code) - ); + # Ensure we can't delete the current default + Koha::Exceptions::CannotDeleteDefault->throw if $self->is_default; + + # Ensure we can't delete system values + Koha::Exceptions::CannotDeleteSystem->throw if $self->is_system; - # Now do the delete if the update was successful - if ($rows) { - my $deleted = $self->SUPER::delete($self); - return $deleted - } + return $self->_result->result_source->schema->txn_do( + sub { + # Update all linked restrictions to the default + my $default = + Koha::Patron::Restriction::Types->find( { is_default => 1 } )->code; + + # We can't use Koha objects here because Koha::Patron::Debarments + # is not a Koha object. So we'll do it old skool + my $rows = C4::Context->dbh->do( + "UPDATE borrower_debarments SET type = ? WHERE type = ?", + undef, ( $default, $self->code ) ); - return 0; + return $self->SUPER::delete; + } + ); } =head3 make_default @@ -75,7 +78,7 @@ sub make_default { $self->_result->result_source->schema->txn_do( sub { my $types = - Koha::RestrictionTypes->search( { code => { '!=' => $self->code } } ); + Koha::Patron::Restriction::Types->search( { code => { '!=' => $self->code } } ); $types->update( { is_default => 0 } ); $self->set( { is_default => 1 } ); $self->SUPER::store; --- a/Koha/RestrictionTypes.pm +++ a/Koha/RestrictionTypes.pm @@ -1,4 +1,4 @@ -package Koha::RestrictionTypes; +package Koha::Patron::Restriction::Types; # This file is part of Koha. # @@ -18,13 +18,13 @@ package Koha::RestrictionTypes; use Modern::Perl; use Koha::Database; -use Koha::RestrictionType; +use Koha::Patron::Restriction::Type; use base qw(Koha::Objects); =head1 NAME -Koha::RestrictionTypes - Koha Restriction Types Object set class +Koha::Patron::Restriction::Types - Koha Restriction Types Object set class =head1 API @@ -63,7 +63,7 @@ sub _type { =cut sub object_class { - return 'Koha::RestrictionType'; + return 'Koha::Patron::Restriction::Type'; } 1; --- a/Koha/Schema/Result/RestrictionType.pm +++ a/Koha/Schema/Result/RestrictionType.pm @@ -98,10 +98,10 @@ __PACKAGE__->add_columns( ); sub koha_object_class { - 'Koha::RestrictionType'; + 'Koha::Patron::Restriction::Type'; } sub koha_objects_class { - 'Koha::RestrictionTypes'; + 'Koha::Patron::Restriction::Types'; } 1; --- a/admin/restrictions.pl +++ a/admin/restrictions.pl @@ -20,16 +20,17 @@ use Modern::Perl; use CGI qw ( -utf8 ); +use Try::Tiny qw( try catch ); + use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); -use Koha::RestrictionTypes; +use Koha::Patron::Restriction::Types; my $input = CGI->new; my $op = $input->param('op') // 'list'; my $code = uc $input->param('code'); my @messages = (); - my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { template_name => "admin/restrictions.tt", @@ -43,11 +44,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( if ( $op eq 'add_form') { # Get all existing restrictions, so we can do client-side validation $template->param( - existing => scalar Koha::RestrictionTypes->search() + existing => scalar Koha::Patron::Restriction::Types->search() ); if ($code) { $template->param( - restriction => scalar Koha::RestrictionTypes->find($code) + restriction => scalar Koha::Patron::Restriction::Types->find($code) ); } } elsif ( $op eq 'add_validate' ) { @@ -57,7 +58,7 @@ if ( $op eq 'add_form') { if ($is_a_modif) { # Check whether another restriction already has this display text - my $dupe = Koha::RestrictionTypes->find({ + my $dupe = Koha::Patron::Restriction::Types->find({ display_text => $display_text }); if ($dupe) { @@ -65,36 +66,51 @@ if ( $op eq 'add_form') { type => 'error', code => 'duplicate_display_text' }; } else { - my $restriction = Koha::RestrictionTypes->find($code); + my $restriction = Koha::Patron::Restriction::Types->find($code); $restriction->display_text($display_text); $restriction->store; + push @messages, { type => 'message', code => 'update_success' }; } } else { # Check whether another restriction already has this code - my $dupe = Koha::RestrictionTypes->find($code); + my $dupe = Koha::Patron::Restriction::Types->find($code); if ($dupe) { push @messages, { type => 'error', code => 'duplicate_code' }; } else { - my $restriction = Koha::RestrictionType->new({ + my $restriction = Koha::Patron::Restriction::Type->new({ code => $code, display_text => $display_text }); $restriction->store; + push @messages, { type => 'message', code => 'add_success' }; } } $op = 'list'; } elsif ( $op eq 'make_default' ) { - my $restriction = Koha::RestrictionTypes->find($code); + my $restriction = Koha::Patron::Restriction::Types->find($code); $restriction->make_default; $op = 'list'; } elsif ( $op eq 'delete_confirm' ) { $template->param( - restriction => scalar Koha::RestrictionTypes->find($code) + restriction => scalar Koha::Patron::Restriction::Types->find($code) ); } elsif ( $op eq 'delete_confirmed' ) { - Koha::RestrictionTypes->find($code)->delete; + try { + Koha::Patron::Restriction::Types->find($code)->delete; + push @messages, { type => 'message', code => 'delete_success' }; + } + catch { + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::CannotDeleteDefault') ) { + push @messages, { type => 'error', code => 'delete_default' }; + } + elsif ( $_->isa('Koha::Exceptions::CannotDeleteSystem') ) { + push @messages, { type => 'error', code => 'delete_system' }; + } + } + } $op = 'list'; } @@ -104,7 +120,7 @@ $template->param( ); if ( $op eq 'list' ) { - my $restrictions = Koha::RestrictionTypes->search(); + my $restrictions = Koha::Patron::Restriction::Types->search(); $template->param( restrictions => $restrictions, ) --- a/circ/circulation.pl +++ a/circ/circulation.pl @@ -46,7 +46,7 @@ use Koha::CsvProfiles; use Koha::Patrons; use Koha::Patron::Debarments qw( GetDebarments ); use Koha::DateUtils qw( dt_from_string ); -use Koha::RestrictionTypes; +use Koha::Patron::Restriction::Types; use Koha::Plugins; use Koha::Database; use Koha::BiblioFrameworks; @@ -628,7 +628,7 @@ $template->param( PatronAutoComplete => C4::Context->preference("PatronAutoComplete"), debarments => scalar GetDebarments({ borrowernumber => $borrowernumber }), todaysdate => dt_from_string()->set(hour => 23)->set(minute => 59), - restriction_types => scalar Koha::RestrictionTypes->keyed_on_code(), + restriction_types => scalar Koha::Patron::Restriction::Types->keyed_on_code(), has_modifications => $has_modifications, override_high_holds => $override_high_holds, nopermission => scalar $query->param('nopermission'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/restrictions.tt @@ -71,10 +71,20 @@ [% FOR m IN messages %]