From 3f154ff531b1a4fae8fa6713968f75d624ac433f Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Thu, 30 Oct 2025 09:53:52 +0000 Subject: [PATCH] Bug 17387: add restore functionality to Old::Biblio/Item TEST PLAN: Item/Biblio Undelete Feature SETUP: - Apply patches and update database - Give test user "Restore deleted records" permission under Cataloging permissions - Delete at least one bibliographic record with items attached - Delete at least one standalone item TESTING: Navigate to Cataloging: - Go to Cataloging home page - Click "Restore deleted records" link (should be visible with permission) Test Restore Records Page: - Verify two tables appear: "Deleted bibliographic records" and "Deleted items" - Verify deleted records appear in tables with biblio ID, title, barcode, deleted date Test Date Filtering: - In left sidebar, enter date range in "Deleted from" and "Deleted to" fields - Click "Apply" button - tables should filter by date range - Click "Clear" button - filter should reset Test Item Restore: - Find a deleted item in the items table (with existing biblio) - Click "Restore" button in Actions column - Verify success message appears and item disappears from table - Search catalog for the restored item - confirm it's back Test Biblio Warning Modal: - Find a deleted item whose biblio is also deleted - Click "Restore" button on the item - Modal appears warning "bibliographic record is deleted" - Click "Restore biblio" - biblio restores, modal closes - Click "Restore" on the item again - item now restores successfully - Verify both records are back in catalog Test Biblio with Items Restore: - Find deleted biblio with multiple deleted items - Click "Restore" button for the biblio - Modal shows checkboxes for each deleted item - Select some items (not all), click "Restore with selected items" - Verify biblio and selected items restore - Unselected items remain in deleted items table Permission Testing: - Remove "Restore deleted records" permission from user - Verify "Restore deleted records" link no longer appears in Cataloging home --- Koha/Old/Biblio.pm | 42 ++++++++++++++++++++++++++++++++++++++++++ Koha/Old/Item.pm | 26 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/Koha/Old/Biblio.pm b/Koha/Old/Biblio.pm index 1b3f46b59e8..7d4200cb743 100644 --- a/Koha/Old/Biblio.pm +++ b/Koha/Old/Biblio.pm @@ -19,6 +19,10 @@ use Modern::Perl; use base qw(Koha::Object); +use Koha::Database; +use Koha::Biblio; +use Koha::Biblioitem; +use Koha::Biblio::Metadata; use Koha::Old::Biblio::Metadatas; use Koha::Old::Biblioitems; @@ -130,6 +134,44 @@ sub to_api_mapping { }; } +=head3 restore + + my $biblio = $deleted_biblio->restore; + +Restores the deleted biblio record back to the biblio table along with +its biblioitems and metadata. This removes the record from the deleted tables +and re-inserts it into the active tables. + +Returns the newly restored Koha::Biblio object. + +=cut + +sub restore { + my ($self) = @_; + + my $biblio_data = $self->unblessed; + my $biblioitem = $self->biblioitem; + my $biblioitem_data = $biblioitem->unblessed; + my $metadata = $self->metadata; + my $metadata_data = $metadata->unblessed; + + my $new_biblio = Koha::Biblio->new($biblio_data)->store; + + $biblioitem_data->{biblionumber} = $new_biblio->biblionumber; + $biblioitem_data->{biblioitemnumber} = $new_biblio->biblionumber; + Koha::Biblioitem->new($biblioitem_data)->store; + + delete $metadata_data->{id}; + $metadata_data->{biblionumber} = $new_biblio->biblionumber; + Koha::Biblio::Metadata->new($metadata_data)->store; + + $metadata->delete; + $biblioitem->delete; + $self->delete; + + return $new_biblio; +} + =head2 Internal methods =head3 _type diff --git a/Koha/Old/Item.pm b/Koha/Old/Item.pm index d95201024e5..edaec7a3686 100644 --- a/Koha/Old/Item.pm +++ b/Koha/Old/Item.pm @@ -19,6 +19,8 @@ use Modern::Perl; use base qw(Koha::Object); +use Koha::Item; + =head1 NAME Koha::Old::Item - Koha Old::Item Object class @@ -29,6 +31,30 @@ Koha::Old::Item - Koha Old::Item Object class =cut +=head3 restore + + my $item = $deleted_item->restore; + +Restores the deleted item record back to the items table. This removes +the record from the deleteditems table and re-inserts it into the items table. + +Returns the newly restored Koha::Item object. + +=cut + +sub restore { + my ($self) = @_; + + my $item_data = $self->unblessed; + delete $item_data->{deleted_on}; + + my $new_item = Koha::Item->new($item_data)->store; + + $self->delete; + + return $new_item; +} + =head2 Internal methods =head3 _type -- 2.39.5