From d3a1e630fcccfd475cd691e87ed5affa0934b97d Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sun, 22 Jan 2017 01:52:50 +0000 Subject: [PATCH] Bug 17944 - Removed SQl query from itemtypes.pl and replaced it with can_be_deleted method in Koha/ItemType.pm --- Koha/ItemType.pm | 16 +++++++++++++ admin/itemtypes.pl | 26 +++++++++------------- .../prog/en/modules/admin/itemtypes.tt | 2 +- t/db_dependent/Koha/ItemTypes.t | 18 ++++++++++++++- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 8f2279b..dd7d27c 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -23,6 +23,7 @@ use C4::Koha; use C4::Languages; use Koha::Database; use Koha::Localizations; +use Koha::Exceptions; use base qw(Koha::Object); @@ -89,6 +90,21 @@ sub translated_descriptions { } @translated_descriptions ]; } + +=head3 can_be_deleted +my $overalltotal = Koha::ItemType->can_be_deleted(); + +Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype + +=cut + +sub can_be_deleted { + my ($self) = @_; + my $nb_items = Koha::Items->search( { 'itype' => $self->itemtype} )->count; + my $nb_biblioitems = Koha::Biblioitems->search( { 'itemtype' => $self->itemtype} )->count; + return $nb_items + $nb_biblioitems == 0; +} + =head3 type =cut diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index c8c224e..7ee8237 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -31,8 +31,8 @@ use C4::Koha; use C4::Context; use C4::Auth; use C4::Output; - use Koha::ItemTypes; +use Koha::ItemType; use Koha::Localizations; my $input = new CGI; @@ -137,20 +137,13 @@ if ( $op eq 'add_form' ) { $searchfield = ''; $op = 'list'; -} elsif ( $op eq 'delete_confirm' ) { - - # Check both items and biblioitems - my ($total) = $dbh->selectrow_array( ' - SELECT COUNT(*) AS total FROM ( - SELECT itemtype AS t FROM biblioitems - UNION ALL - SELECT itype AS t FROM items - ) AS tmp - WHERE tmp.t=? - ', {}, $itemtype_code ); - - if ($total) { - push @messages, { type => 'error', code => 'cannot_be_deleted', total => $total }; + + } elsif ( $op eq 'delete_confirm' ) { + my $ItemType = Koha::ItemType->new(); + my $overalltotal = $ItemType->can_be_deleted(); + warn $overalltotal; + if (!$overalltotal) { + push @messages, { type => 'error', code => 'cannot_be_deleted'}; $op = 'list'; } else { my $itemtype = Koha::ItemTypes->find($itemtype_code); @@ -158,8 +151,9 @@ if ( $op eq 'add_form' ) { } } elsif ( $op eq 'delete_confirmed' ) { + my $itemtype = Koha::ItemTypes->find($itemtype_code); - my $deleted = eval { $itemtype->delete }; + my $deleted = eval{ $itemtype->delete };; if ( $@ or not $deleted ) { push @messages, { type => 'error', code => 'error_on_delete' }; } else { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 040b52e..0d07187 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -110,7 +110,7 @@ Item types administration [% CASE 'already_exists' %] This item type already exists. [% CASE 'cannot_be_deleted' %] - Cannot delete this item type.

This record is used [% m.total %] times. Deletion is not possible.

+ Cannot delete this item type.

This record is in use. Deletion is not possible.

[% CASE %] [% m.code %] [% END %] diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index e1ae330..8c093c8 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -19,10 +19,13 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 23; use Data::Dumper; use Koha::Database; use t::lib::Mocks; +use Koha::Items; +use Koha::Biblioitems; +use t::lib::TestBuilder; BEGIN { use_ok('Koha::ItemType'); @@ -126,4 +129,17 @@ is( 'item types should be sorted by translated description' ); +my $builder = t::lib::TestBuilder->new; +my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' }); + +is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted'); + +my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }}); + +is( $item_type->can_be_deleted, '', 'An item type that is used by an item cannot be deleted' ); + +my $biblio = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }}); + +is ( $item_type->can_be_deleted, '', 'An item type that is used by a biblioitem cannot be deleted' ); + $schema->txn_rollback; -- 2.1.4