From 6486c07c1d2de531d88f0c26bd6485338e0d575c Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 3 Sep 2015 14:05:07 +0200 Subject: [PATCH] Bug 10855: Remove additional field values when subscription is removed --- C4/Serials.pm | 5 +++++ Koha/AdditionalField.pm | 31 +++++++++++++++++++++++++++++++ t/db_dependent/AdditionalField.t | 14 +++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index 9441dfe..435e17f 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -1844,6 +1844,11 @@ sub DelSubscription { $dbh->do("DELETE FROM subscriptionhistory WHERE subscriptionid=$subscriptionid"); $dbh->do("DELETE FROM serial WHERE subscriptionid=$subscriptionid"); + my $afs = Koha::AdditionalField->all({tablename => 'subscription'}); + foreach my $af (@$afs) { + $af->delete_values({record_id => $subscriptionid}); + } + logaction( "SERIAL", "DELETE", $subscriptionid, "" ) if C4::Context->preference("SubscriptionLog"); } diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index aa00bb6..563a7f3 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -134,6 +134,29 @@ sub fetch_values { } } +sub delete_values { + my ($self, $args) = @_; + + my $record_id = $args->{record_id}; + + my $dbh = C4::Context->dbh; + + my @where_strs = ('field_id = ?'); + my @where_args = ($self->{id}); + + if ($record_id) { + push @where_strs, 'record_id = ?'; + push @where_args, $record_id; + } + + my $query = q{ + DELETE FROM additional_field_values + WHERE + } . join (' AND ', @where_strs); + + $dbh->do($query, undef, @where_args); +} + sub all { my ( $class, $args ) = @_; die "BAD CALL: Don't use fetch_all_values as an instance method" @@ -331,6 +354,14 @@ The record_id argument is optional. } } +=head2 delete_values + +Delete values from the database for a given record_id. +The record_id argument is optional. + + my $af = Koha::AdditionalField({ id => $id })->fetch; + $af->delete_values({record_id => $record_id}); + =head2 all Retrieve all additional fields in the database given some parameters. diff --git a/t/db_dependent/AdditionalField.t b/t/db_dependent/AdditionalField.t index ed28580..91d6cd3 100644 --- a/t/db_dependent/AdditionalField.t +++ b/t/db_dependent/AdditionalField.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 37; +use Test::More tests => 40; use C4::Bookseller qw( AddBookseller ); use C4::Context; @@ -288,5 +288,17 @@ is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscri $exists = grep /not_existent_id/, @$matching_record_ids; is ( $exists, 0, "get_matching_record_ids: field common: common% does not inexistent id" ); +# delete_values +$af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch; + +$af1->fetch_values; +is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" ); +$af1->delete_values({record_id => $subscriptionid1}); +$af1->fetch_values; +is_deeply ( $af1->values, {$subscriptionid2 => qq|value_for_af1_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" ); +$af1->delete_values; +$af1->fetch_values; +is_deeply ( $af1->values, {}, "fetch_values: no values" ); + $dbh->rollback; -- 1.9.1