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

(-)a/Koha/Filter/MARC/AuthorizedValues.pm (-1 / +129 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
=head2 filter
58
59
Embed items into the MARC::Record object.
60
61
=cut
62
63
sub filter {
64
    my $self   = shift;
65
    my $record = shift;
66
67
    return unless defined $record and ref($record) eq 'MARC::Record';
68
69
    my $params        = $self->params;
70
    my $interface     = $params->{options}->{interface}     // 'opac';
71
    my $frameworkcode = $params->{options}->{frameworkcode} // q{};
72
    my $marcstructure = $params->{options}->{marcstructure}
73
      // GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } );
74
75
    my $marcflavour = C4::Context->preference('marcflavour');
76
    my $av          = _getAuthorisedValues4MARCSubfields($frameworkcode);
77
    foreach my $tag ( keys %$av ) {
78
        foreach my $field ( $record->field($tag) ) {
79
            if ( $av->{$tag} ) {
80
                my @new_subfields = ();
81
                for my $subfield ( $field->subfields() ) {
82
                    my ( $letter, $value ) = @$subfield;
83
                    # Replace the field value with the authorised value 
84
                    # *except* for MARC21 field 942$n (suppression in opac)
85
                    if ( !( $tag eq '942' && $subfield->[0] eq 'n' )
86
                        || $marcflavour eq 'UNIMARC' )
87
                    {
88
                        $value =
89
                          GetAuthorisedValueDesc( $tag, $letter, $value, '',
90
                            $tagslib, undef, $opac )
91
                          if $av->{$tag}->{$letter};
92
                    }
93
                    push( @new_subfields, $letter, $value );
94
                }
95
                $field->replace_with(
96
                    MARC::Field->new(
97
                        $tag,                 $field->indicator(1),
98
                        $field->indicator(2), @new_subfields
99
                    )
100
                );
101
            }
102
        }
103
    }
104
105
    return $record;
106
}
107
108
sub _getAuthorisedValues4MARCSubfields {
109
    my ($frameworkcode) = @_;
110
    unless ( $authval_per_framework{$frameworkcode} ) {
111
        my $dbh = C4::Context->dbh;
112
        my $sth = $dbh->prepare(
113
            "SELECT DISTINCT tagfield, tagsubfield
114
             FROM marc_subfield_structure
115
             WHERE authorised_value IS NOT NULL
116
                AND authorised_value!=''
117
                AND frameworkcode=?"
118
        );
119
        $sth->execute($frameworkcode);
120
        my $av = {};
121
        while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
122
            $av->{$tag}->{$letter} = 1;
123
        }
124
        $authval_per_framework{$frameworkcode} = $av;
125
    }
126
    return $authval_per_framework{$frameworkcode};
127
}
128
129
1;

Return to bug 30848