View | Details | Raw Unified | Return to bug 30848
Collapse All | Expand All

(-)a/Koha/Filter/MARC/AuthorizedValues.pm (-1 / +133 lines)
Line 0 Link Here
0
- 
1
package Koha::Filter::MARC::AuthorizedValues;
2
3
# Copyright 2022 PTFS Europe
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
Koha::Filter::MARC::AuthorizedValues - Replaces AV codes with descriptions in MARC::Record objects.
23
24
=head1 SYNOPSIS
25
26
  my $biblio = Koha::Biblios->find(
27
      $biblio_id,
28
      { prefetch => [ metadata ] }
29
  );
30
31
  my $record = $biblio->metadata->record;
32
33
  my $record_processor = Koha::RecordProcessor->new(
34
    {
35
        filters => ['AuthorizedValues'],
36
        options => {
37
            interface => 'opac',
38
        }
39
    }
40
  );
41
42
  $record_processor->process($record);
43
44
=head1 DESCRIPTION
45
46
Filter to replace Koha AV codes in MARC::Records with their Koha AV descriptions.
47
48
=cut
49
50
use Modern::Perl;
51
52
use C4::Biblio qw( GetAuthorisedValueDesc GetMarcStructure );
53
54
use base qw(Koha::RecordProcessor::Base);
55
our $NAME = 'AuthorizedValues';
56
57
my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework.
58
59
=head2 filter
60
61
Embed items into the MARC::Record object.
62
63
=cut
64
65
sub filter {
66
    my $self   = shift;
67
    my $record = shift;
68
69
    return unless defined $record and ref($record) eq 'MARC::Record';
70
71
    my $params        = $self->params;
72
    my $interface     = $params->{options}->{interface} // 'opac';
73
    my $opac          = $interface eq 'opac' ? 1 : 0;
74
    my $frameworkcode = $params->{options}->{frameworkcode} // q{};
75
    my $marcstructure = $params->{options}->{marcstructure}
76
      // GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } );
77
78
    my $marcflavour = C4::Context->preference('marcflavour');
79
    my $av          = _getAuthorisedValues4MARCSubfields($frameworkcode);
80
    foreach my $tag ( keys %$av ) {
81
        foreach my $field ( $record->field($tag) ) {
82
            if ( $av->{$tag} ) {
83
                my @new_subfields = ();
84
                for my $subfield ( $field->subfields() ) {
85
                    my ( $letter, $value ) = @$subfield;
86
87
                    # Replace the field value with the authorised value
88
                    # *except* for MARC21 field 942$n (suppression in opac)
89
                    if ( !( $tag eq '942' && $subfield->[0] eq 'n' )
90
                        || $marcflavour eq 'UNIMARC' )
91
                    {
92
                        $value =
93
                          GetAuthorisedValueDesc( $tag, $letter, $value, '',
94
                            $marcstructure, undef, $opac )
95
                          if $av->{$tag}->{$letter};
96
                    }
97
                    push( @new_subfields, $letter, $value );
98
                }
99
                $field->replace_with(
100
                    MARC::Field->new(
101
                        $tag,                 $field->indicator(1),
102
                        $field->indicator(2), @new_subfields
103
                    )
104
                );
105
            }
106
        }
107
    }
108
109
    return $record;
110
}
111
112
sub _getAuthorisedValues4MARCSubfields {
113
    my ($frameworkcode) = @_;
114
    unless ( $authval_per_framework{$frameworkcode} ) {
115
        my $dbh = C4::Context->dbh;
116
        my $sth = $dbh->prepare(
117
            "SELECT DISTINCT tagfield, tagsubfield
118
             FROM marc_subfield_structure
119
             WHERE authorised_value IS NOT NULL
120
                AND authorised_value!=''
121
                AND frameworkcode=?"
122
        );
123
        $sth->execute($frameworkcode);
124
        my $av = {};
125
        while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
126
            $av->{$tag}->{$letter} = 1;
127
        }
128
        $authval_per_framework{$frameworkcode} = $av;
129
    }
130
    return $authval_per_framework{$frameworkcode};
131
}
132
133
1;

Return to bug 30848