From 8c5135dadbe8cd6d1df61e46026be8996f97f27c 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! Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart Signed-off-by: Katrin Fischer --- Koha/Biblio.pm | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index d4d54eed42..ada72137ff 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -130,6 +130,75 @@ sub record_schema { return $self->metadata->schema // C4::Context->preference("marcflavour"); } +=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.39.5