Bug 10855: Remove additional field values when subscription is removed

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Julian Maurice 2015-09-03 14:05:07 +02:00 committed by Tomas Cohen Arazi
parent 4748b17887
commit 10c5109221
3 changed files with 49 additions and 1 deletions

View File

@ -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");
}

View File

@ -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.

View File

@ -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;