From 8d77b091e58bfd76986fa594582b554ec0c5f41b Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Thu, 26 Jan 2017 14:40:12 +1300 Subject: [PATCH] [SIGNED-OFF] Bug 17944 - Removed the sql code from Itemtypes.pm 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: Owen Leonard --- Koha/ItemTypes.pm | 17 ----------------- admin/itemtypes.pl | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/Koha/ItemTypes.pm b/Koha/ItemTypes.pm index 9eb26df..92fea9c 100644 --- a/Koha/ItemTypes.pm +++ b/Koha/ItemTypes.pm @@ -39,23 +39,6 @@ Koha::ItemTypes - Koha ItemType Object set class =cut -sub check_items_and_biblioitems { - my ($itemtype_code) = @_; - - my $dbh = C4::Context->dbh; - - # 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 ); - return $total; -} - sub _type { return 'Itemtype'; } diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index 81bb391..fd2bded 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -137,12 +137,20 @@ if ( $op eq 'add_form' ) { $searchfield = ''; $op = 'list'; -} elsif ( $op eq 'delete_confirm' ) { - my $total = Koha::ItemTypes->check_items_and_biblioitems($itemtype_code); + } elsif ( $op eq 'delete_confirm' ) { - if ($total) { - push @messages, { type => 'error', code => 'cannot_be_deleted', total => $total }; + my $context = C4::Context->new; + my $schema = Koha::Database->new()->schema(); + my $itemtotal = $schema->resultset('Item')->search({ 'itype' => $itemtype_code} ); + my $bibliototal = $schema->resultset('Biblioitem')->search({ 'itemtype' => $itemtype_code} ); + + $itemtotal->count; + $bibliototal->count; + my $overalltotal = $itemtotal + $bibliototal; + + if ($overalltotal) { + push @messages, { type => 'error', code => 'cannot_be_deleted', total => $overalltotal }; $op = 'list'; } else { my $itemtype = Koha::ItemTypes->find($itemtype_code); @@ -150,6 +158,7 @@ if ( $op eq 'add_form' ) { } } elsif ( $op eq 'delete_confirmed' ) { + my $itemtype = Koha::ItemTypes->find($itemtype_code); my $deleted = eval { $itemtype->delete }; if ( $@ or not $deleted ) { -- 2.1.4