BUG6436 Don't allow deletion of an itemtype used in items.itype
authorFridolyn SOMERS <fridolyn.somers@progilone.fr>
Tue, 31 May 2011 13:16:15 +0000 (15:16 +0200)
committerChris Cormack <chrisc@catalyst.net.nz>
Fri, 3 Jun 2011 02:24:54 +0000 (14:24 +1200)
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
admin/itemtypes.pl

index 1a33a21..3219da3 100755 (executable)
@@ -188,15 +188,17 @@ elsif ( $op eq 'add_validate' ) {
     # called by default form, used to confirm deletion of data in DB
 }
 elsif ( $op eq 'delete_confirm' ) {
-    # Check both categoryitem and biblioitems, see Bug 199
-    my $total = 0;
-    for my $table ('biblioitems') {
-        my $sth =
-          $dbh->prepare(
-            "select count(*) as total from $table where itemtype=?");
-        $sth->execute($itemtype);
-        $total += $sth->fetchrow_hashref->{total};
-    }
+    # Check both items and biblioitems
+    my $sth = $dbh->prepare('
+        SELECT COUNT(*) AS total FROM (
+            SELECT itemtype AS t FROM biblioitems
+            UNION
+            SELECT itype AS t FROM items
+        ) AS tmp
+        WHERE tmp.t=?
+    ');
+    $sth->execute($itemtype);
+    my $total = $sth->fetchrow_hashref->{'total'};
 
     my $sth =
       $dbh->prepare(