From f2657859f97ec92b930baa40ec8233856ec6a4a4 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 22 Jul 2022 17:42:35 -0300 Subject: [PATCH] [POC] Bug 31224: Add Koha::Biblio->metadata_record This patch introduces a higher-level method for retrieving a 'prepared' record for using in specific contexts. In particular, I only focused on embedding item information and OPAC view filtering. But we could add a way to get the record through the ViewPolicy also for staff. The virtue of this patch is that it explains better than my words why I think having the *embed_items* and *opac* parameters in Koha::Biblio::Metadata->record is not ideal. And makes the other implementation feel like is done at the wrong level. I know it's been done like that to act as a drop-in replacement fro GetMarcBiblio, which was a good first step. But we need to revisit how it is used, in general, to come up with a more refined and useful design. So we do not port the same design problems from C4::* into Koha::*. Try to think how it would be used in contexts like opac-MARCdetail.pl or any other OPAC controller script. Thanks! --- Koha/Biblio.pm | 55 +++++++++++++++++++++++++++++++++++++++++ Koha/Biblio/Metadata.pm | 11 +++------ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 0af49b939ec..5e1810092cf 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -88,6 +88,61 @@ sub metadata { return Koha::Biblio::Metadata->_new_from_dbic($metadata); } +=head3 metadata_record + + my $record = $biblio->metadata_record( + { + [ embed_items => 1|0, + opac => 1|0, + patron => $patron, ] + } + ); + +Returns the metadata serialized as appropriate for the metadata object +type. Currently only I objects are returned. + +=cut + +sub metadata_record { + my ($self, $params) = @_; + + my $patron = $params->{patron}; + + my $record = $self->metadata->record; + + if ( $params->{embed_items} or $params->{opac} ) { + # There's need for a RecordProcessor, let's do it! + my @filters; + my $options = {}; + + if ($params->{embed_items}) { + push @filters, 'EmbedItems'; + $options->{items} = $self->items->filter_by_visible_in_opac( + { + ( $params->{patron} ? patron => $params->{patron} : ()) + } + ); + } + + if ( $params->{opac} ) { + push @filters, 'ViewPolicy'; + $options->{interface} = 'opac'; + $options->{frameworkcode} = $self->frameworkcode; + } + + my $rp = Koha::RecordProcessor->new( + { + filters => \@filters, + options => $options + } + ); + + $rp->process( $record ); + } + + return $record; +} + =head3 orders my $orders = $biblio->orders(); diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index ad451797e51..b6608919f0a 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -165,14 +165,11 @@ sub _embed_items { if ( $opac ) { $items = $items->filter_by_visible_in_opac({ patron => $patron }); } - my @itemnumbers = $items->get_column('itemnumber'); - my @item_fields; - for my $itemnumber ( @itemnumbers ) { - my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); - push @item_fields, $item_marc->field($itemtag); - } - $record->append_fields(@item_fields); + # convert items to MARC::Field objects + my @item_fields = map { $_->as_marc_field } @{$items->as_list}; + + $record->append_fields(@item_fields); } else { Koha::Exceptions::Metadata->throw( -- 2.34.1