From c9f93cca1184abb2293d76f540cca51f0418375e 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 => ('AuthorizedValues'), options => { interface => 'opac', frameworkcode => $biblio->frameworkcode } } ); $processor->process( $record ); --- Koha/Filter/MARC/AuthorizedValues.pm | 129 +++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Koha/Filter/MARC/AuthorizedValues.pm diff --git a/Koha/Filter/MARC/AuthorizedValues.pm b/Koha/Filter/MARC/AuthorizedValues.pm new file mode 100644 index 0000000000..2b9b5bb1b3 --- /dev/null +++ b/Koha/Filter/MARC/AuthorizedValues.pm @@ -0,0 +1,129 @@ +package Koha::Filter::MARC::AuthorizedValues; + +# 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::AuthorizedValues - 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 => ['AuthorizedValues'], + 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 = 'AuthorizedValues'; + +=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 $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, '', + $tagslib, 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