From d54251c3cedc564cc37e7d20ab3fa5050725b59c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 25 May 2022 15:19:57 +0100 Subject: [PATCH] Bug 30848: Add an AuthorizedValues RecordProcessor filter This patch introduces a RecordProcessor filter for MARC::Record objects that replaces AV codes with AC descriptions in the MARC::Record passed to the processor. Target usage: my $biblio = Koha::Biblios->find( $biblio_id, { prefetch => [ metadata ] } ); my $record = $biblio->metadata->record; my $processor = Koha::RecordProcessor->new( { filters => ('ExpandAuthorizedValues'), options => { interface => 'opac', frameworkcode => $biblio->frameworkcode } } ); $processor->process( $record ); --- Koha/Filter/MARC/ExpandAuthorizedValues.pm | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 Koha/Filter/MARC/ExpandAuthorizedValues.pm diff --git a/Koha/Filter/MARC/ExpandAuthorizedValues.pm b/Koha/Filter/MARC/ExpandAuthorizedValues.pm new file mode 100644 index 0000000000..3306839728 --- /dev/null +++ b/Koha/Filter/MARC/ExpandAuthorizedValues.pm @@ -0,0 +1,133 @@ +package Koha::Filter::MARC::ExpandAuthorizedValues; + +# Copyright 2022 PTFS Europe +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +Koha::Filter::MARC::ExpandAuthorizedValues - Replaces AV codes with descriptions in MARC::Record objects. + +=head1 SYNOPSIS + + my $biblio = Koha::Biblios->find( + $biblio_id, + { prefetch => [ metadata ] } + ); + + my $record = $biblio->metadata->record; + + my $record_processor = Koha::RecordProcessor->new( + { + filters => ['ExpandAuthorizedValues'], + options => { + interface => 'opac', + } + } + ); + + $record_processor->process($record); + +=head1 DESCRIPTION + +Filter to replace Koha AV codes in MARC::Records with their Koha AV descriptions. + +=cut + +use Modern::Perl; + +use C4::Biblio qw( GetAuthorisedValueDesc GetMarcStructure ); + +use base qw(Koha::RecordProcessor::Base); +our $NAME = 'ExpandAuthorizedValues'; + +my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework. + +=head2 filter + +Embed items into the MARC::Record object. + +=cut + +sub filter { + my $self = shift; + my $record = shift; + + return unless defined $record and ref($record) eq 'MARC::Record'; + + my $params = $self->params; + my $interface = $params->{options}->{interface} // 'opac'; + my $opac = $interface eq 'opac' ? 1 : 0; + my $frameworkcode = $params->{options}->{frameworkcode} // q{}; + my $marcstructure = $params->{options}->{marcstructure} + // GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); + + my $marcflavour = C4::Context->preference('marcflavour'); + my $av = _getAuthorisedValues4MARCSubfields($frameworkcode); + foreach my $tag ( keys %$av ) { + foreach my $field ( $record->field($tag) ) { + if ( $av->{$tag} ) { + my @new_subfields = (); + for my $subfield ( $field->subfields() ) { + my ( $letter, $value ) = @$subfield; + + # Replace the field value with the authorised value + # *except* for MARC21 field 942$n (suppression in opac) + if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) + || $marcflavour eq 'UNIMARC' ) + { + $value = + GetAuthorisedValueDesc( $tag, $letter, $value, '', + $marcstructure, undef, $opac ) + if $av->{$tag}->{$letter}; + } + push( @new_subfields, $letter, $value ); + } + $field->replace_with( + MARC::Field->new( + $tag, $field->indicator(1), + $field->indicator(2), @new_subfields + ) + ); + } + } + } + + return $record; +} + +sub _getAuthorisedValues4MARCSubfields { + my ($frameworkcode) = @_; + unless ( $authval_per_framework{$frameworkcode} ) { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( + "SELECT DISTINCT tagfield, tagsubfield + FROM marc_subfield_structure + WHERE authorised_value IS NOT NULL + AND authorised_value!='' + AND frameworkcode=?" + ); + $sth->execute($frameworkcode); + my $av = {}; + while ( my ( $tag, $letter ) = $sth->fetchrow() ) { + $av->{$tag}->{$letter} = 1; + } + $authval_per_framework{$frameworkcode} = $av; + } + return $authval_per_framework{$frameworkcode}; +} + +1; -- 2.20.1