From c3ac6e0d0f6abf205f39929c5405f439ad7706ba Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sun, 22 Jan 2017 01:52:50 +0000 Subject: [PATCH] Bug 17944 - Add Koha::ItemType->can_be_deleted and use it from admin/itemtypes.pl Content-Type: text/plain; charset=utf-8 Removed the sql code from Itemtypes.pm and replaced it with DBIx database query in the itemtypes.pl administrative script Test plan: 1. In the staff interface, stage and manage MARC records for import 2. Try to delete an itemtype. If there are items of that itemtype in the database then a message telling you the number of items of that itemtype there are will be displayed. 3. Record that number 4. View the admin/itemtpes.pl script and confirm that there is sql code written in this file. 5. Apply this patch 6. View the admin/itemtypes.pl script and observe that there is no sql in this file. There is however DBIx code, for example $schema->resultset('Item')->search({ 'itype' => $itemtype_code} ); which is searching for items with the itype value matching $itemtype_code value. 7. In the staff interface try to delete the same itemtype 8. Record the number of items there are with that itemtype in the resulting message 9. The numbers recorded in steps 3 and 8 should match showing that the DBIx code is working as intended Signed-off-by: Jonathan Druart Signed-off-by: Marcel de Rooy --- Koha/ItemType.pm | 16 +++++++++++++ admin/itemtypes.pl | 26 ++++++++-------------- .../prog/en/modules/admin/itemtypes.tt | 2 +- t/db_dependent/Koha/ItemTypes.t | 24 +++++++++++++++++++- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 8f2279b..0b84e80 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 ? 1 : 0; +} + =head3 type =cut diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index c8c224e..756910d 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,27 +137,19 @@ 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::ItemTypes->find($itemtype_code); + my $overalltotal = $ItemType->can_be_deleted(); + if ($overalltotal == 0) { + push @messages, { type => 'error', code => 'cannot_be_deleted'}; $op = 'list'; } else { - my $itemtype = Koha::ItemTypes->find($itemtype_code); - $template->param( itemtype => $itemtype, ); + $template->param( itemtype => $ItemType, ); } } elsif ( $op eq 'delete_confirmed' ) { + my $itemtype = Koha::ItemTypes->find($itemtype_code); my $deleted = eval { $itemtype->delete }; if ( $@ or not $deleted ) { 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..082d733 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 => 26; 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,23 @@ 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, 0, '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, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' ); + +is ( $item->delete, 1, 'An item has been deleted' ); + +is ( $biblio->delete, 1, 'A biblioitem has been deleted' ); + +is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' ); + $schema->txn_rollback; -- 2.1.4