From 0f0f8e2346d9a1167eac45b7ffb12310173a164d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 22 Jul 2022 17:42:35 -0300 Subject: [PATCH] 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 | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 3c8e5cb4b2..f3025ed706 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -88,6 +88,72 @@ 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, + expand_coded_fields => 1|0 ] + } + ); + +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 = { + interface => 'opac', + frameworkcode => $self->frameworkcode, + }; + + if ($params->{embed_items}) { + push @filters, 'EmbedItems'; + if ($params->{opac}) { + $options->{items} = $self->items->filter_by_visible_in_opac( + { + ( $params->{patron} ? patron => $params->{patron} : ()) + } + ); + } + else { + $options->{items} = $self->items; + } + } + + if ( $params->{opac} ) { + push @filters, 'ViewPolicy'; + } + + if ( $params->{expand_coded_fields} ) { + push @filters, 'ExpandCodedFields'; + } + + my $rp = Koha::RecordProcessor->new( + { + filters => \@filters, + options => $options + } + ); + + $rp->process( $record ); + } + + return $record; +} + =head3 orders my $orders = $biblio->orders(); -- 2.20.1