From 9761aa9494eec75486151adb1b446ae6ef06fab0 Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Fri, 20 Sep 2024 16:47:18 +0200 Subject: [PATCH] Bug 37710: Remove the marc link when removing an item from a bundle A new functionality to automatically add a new MARC link has been added in Bug 29560 (Add option to create MARC links when adding items to bundles). There should also be a way to remove this link when removing the item from the bundle. Moreover I don't know if this should be a second bug but since the link is not removed, there can be multiple links if the item is added to a new bundle. TEST PLAN: 1 - Apply tests and prove t/db_dependent/Koha/Biblio.t -> should fail 2 - Create a bundle on an item by changing the 7th character of the leader to c (go to the marc, click on the button at the right of the field 000 and change the bibliographical level to collection). 3 - Add an item to the bundle (on the biblio, exemplars will have a button "manage bundle"->add_to_bundle, fill in a barcode and check "add MARC link") 4 - Check on the MARC that field 461 (UNIMARC) or 773 (MARC21) is propperly filled 5 - Remove the item from the bundle 6 - Check that the marc field is still there and remove it manually 7 - APPLY PATCH 8 - Repeat 2-5 -> the field should have been automatically removed 9 - Run tests -> should pass Note: as for link_marc_host, tests assume you are using MARC21 --- Koha/Biblio.pm | 34 ++++++++++++++++++++++++++++++++++ Koha/REST/V1/Items.pm | 1 + 2 files changed, 35 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 21f9b612..09d8a9b2 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1934,6 +1934,40 @@ sub generate_marc_host_field { return $link_field; } +=head3 unlink_marc_host + + $biblio->unlink_marc_host({ host => $biblio }); + $biblio->unlink_marc_host({ host => $biblionumber }); + +Remove the link to the parent created by link_marc_host. Expects either the biblio object or biblionumber of the host to link to. + +=cut + +sub unlink_marc_host { + my ( $self, $params ) = @_; + my $host_biblionumber; + if ( ref( $params->{host} ) eq 'Koha::Biblio' ) { + $host_biblionumber = $params->{host}->biblionumber; + } else { + $host_biblionumber = $params->{host}; + } + + my $record = $self->metadata->record; + my $field_number = C4::Context->preference('marcflavour') eq 'MARC21' ? '773' : '461'; + my @fields = $record->field($field_number); + + foreach my $field (@fields) { + my $subfield_biblionumber = $field->subfield('0') // q{}; + if ( $host_biblionumber eq $subfield_biblionumber ) { + $record->delete_field($field); + last; + } + } + + C4::Biblio::ModBiblio( $record, $self->biblionumber ); +} + + =head3 link_marc_host $biblio->link_marc_host({ field => $link_field}); diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index 48468b68..69219d1e 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -411,6 +411,7 @@ sub remove_from_bundle { unless $bundle_item; return try { + $bundle_item->biblio->unlink_marc_host( { host => $item->biblio } ); $bundle_item->remove_from_bundle; return $c->render_resource_deleted; } catch { -- 2.30.2