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

(-)a/Koha/Filter/MARC/ExpandCodedFields.pm (+140 lines)
Line 0 Link Here
1
package Koha::Filter::MARC::ExpandCodedFields;
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::ExpandCodedFields - 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 => ['ExpandCodedFields'],
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 );
53
54
use Koha::Caches;
55
56
use base qw(Koha::RecordProcessor::Base);
57
our $NAME = 'ExpandCodedFields';
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
76
    my $marcflavour  = C4::Context->preference('marcflavour');
77
    my $coded_fields = _getCodedFields($frameworkcode);
78
    for my $tag ( keys %$coded_fields ) {
79
        for my $field ( $record->field($tag) ) {
80
            my @new_subfields = ();
81
            for my $subfield ( $field->subfields() ) {
82
                my ( $letter, $value ) = @$subfield;
83
84
                # Replace the field value with the authorised value
85
                # *except* for MARC21 field 942$n (suppression in opac)
86
                if ( !( $tag eq '942' && $subfield->[0] eq 'n' )
87
                    || $marcflavour eq 'UNIMARC' )
88
                {
89
                    $value =
90
                      GetAuthorisedValueDesc( $tag, $letter, $value, '',
91
                        $coded_fields, undef, $opac )
92
                      if $coded_fields->{$tag}->{$letter};
93
                }
94
                push( @new_subfields, $letter, $value );
95
            }
96
            $field->replace_with(
97
                MARC::Field->new(
98
                    $tag,                 $field->indicator(1),
99
                    $field->indicator(2), @new_subfields
100
                )
101
            );
102
        }
103
    }
104
105
    return $record;
106
}
107
108
sub _getCodedFields {
109
    my ($frameworkcode) = @_;
110
    $frameworkcode //= "";
111
112
    my $cache     = Koha::Caches->get_instance();
113
    my $cache_key = "MarcCodedFields-$frameworkcode";
114
    my $cached    = $cache->get_from_cache( $cache_key, { unsafe => 1 } );
115
    return $cached if $cached;
116
117
    my $coded_fields = {
118
        map {
119
            $_->tagfield => {
120
                $_->tagsubfield => {
121
                    'authorised_value' => $_->authorised_value
122
                }
123
            }
124
        } Koha::MarcSubfieldStructures->search(
125
            {
126
                frameworkcode    => $frameworkcode,
127
                authorised_value => { '>' => '' }
128
            },
129
            {
130
                columns  => [ 'tagfield', 'tagsubfield', 'authorised_value' ],
131
                order_by => [ 'tagfield', 'tagsubfield' ]
132
            }
133
        )->as_list
134
    };
135
136
    $cache->set_in_cache( $cache_key, $coded_fields );
137
    return $coded_fields;
138
}
139
140
1;
(-)a/admin/biblio_framework.pl (+2 lines)
Lines 80-85 if ( $op eq 'add_form' ) { Link Here
80
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
80
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
81
    $cache->clear_from_cache("default_value_for_mod_marc-");
81
    $cache->clear_from_cache("default_value_for_mod_marc-");
82
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
82
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
83
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
83
    $op = 'list';
84
    $op = 'list';
84
} elsif ( $op eq 'delete_confirm' ) {
85
} elsif ( $op eq 'delete_confirm' ) {
85
    my $framework = Koha::BiblioFrameworks->find($frameworkcode);
86
    my $framework = Koha::BiblioFrameworks->find($frameworkcode);
Lines 111-116 if ( $op eq 'add_form' ) { Link Here
111
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
112
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
112
    $cache->clear_from_cache("default_value_for_mod_marc-");
113
    $cache->clear_from_cache("default_value_for_mod_marc-");
113
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
114
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
115
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
114
    $op = 'list';
116
    $op = 'list';
115
}
117
}
116
118
(-)a/admin/marc_subfields_structure.pl (+2 lines)
Lines 309-314 elsif ( $op eq 'add_validate' ) { Link Here
309
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
309
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
310
    $cache->clear_from_cache("default_value_for_mod_marc-");
310
    $cache->clear_from_cache("default_value_for_mod_marc-");
311
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
311
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
312
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
312
313
313
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
314
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
314
    exit;
315
    exit;
Lines 347-352 elsif ( $op eq 'delete_confirmed' ) { Link Here
347
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
348
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
348
    $cache->clear_from_cache("default_value_for_mod_marc-");
349
    $cache->clear_from_cache("default_value_for_mod_marc-");
349
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
350
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
351
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
350
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
352
    print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
351
    exit;
353
    exit;
352
354
(-)a/admin/marctagstructure.pl (+2 lines)
Lines 155-160 if ($op eq 'add_form') { Link Here
155
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
155
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
156
    $cache->clear_from_cache("default_value_for_mod_marc-");
156
    $cache->clear_from_cache("default_value_for_mod_marc-");
157
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
157
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
158
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
158
    print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
159
    print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
159
    exit;
160
    exit;
160
													# END $OP eq ADD_VALIDATE
161
													# END $OP eq ADD_VALIDATE
Lines 180-185 if ($op eq 'add_form') { Link Here
180
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
181
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
181
    $cache->clear_from_cache("default_value_for_mod_marc-");
182
    $cache->clear_from_cache("default_value_for_mod_marc-");
182
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
183
    $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
184
    $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
183
    $template->param( searchfield => $searchfield );
185
    $template->param( searchfield => $searchfield );
184
													# END $OP eq DELETE_CONFIRMED
186
													# END $OP eq DELETE_CONFIRMED
185
################## ITEMTYPE_CREATE ##################################
187
################## ITEMTYPE_CREATE ##################################
(-)a/t/db_dependent/Koha/Filter/ExpandCodedFields.t (-1 / +97 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
22
use t::lib::TestBuilder;
23
24
use C4::Biblio qw( GetMarcSubfieldStructure );
25
26
use Koha::Database;
27
28
use Koha::AuthorisedValueCategory;
29
use Koha::Caches;
30
31
use Koha::RecordProcessor;
32
33
my $schema  = Koha::Database->schema();
34
my $builder = t::lib::TestBuilder->new();
35
36
subtest 'ExpandCodedFields tests' => sub {
37
38
    plan tests => 10;
39
40
    $schema->storage->txn_begin();
41
42
    # Add a biblio
43
    my $biblio = $builder->build_sample_biblio;
44
    my $record = $biblio->metadata->record;
45
    $record->field('942')->update( n => 1 );
46
    $record->append_fields(
47
        MARC::Field->new( '590', '', '', a => 'CODE' ),
48
    );
49
50
    Koha::AuthorisedValueCategory->new({ category_name => 'TEST' })->store;
51
    Koha::AuthorisedValue->new(
52
        {
53
            category         => 'TEST',
54
            authorised_value => 'CODE',
55
            lib              => 'Description should show',
56
            lib_opac         => 'Description should show OPAC'
57
        }
58
    )->store;
59
    my $mss = Koha::MarcSubfieldStructures->find({tagfield => "590", tagsubfield => "a", frameworkcode => $biblio->frameworkcode });
60
    $mss->update({ authorised_value => "TEST" });
61
62
    my $cache = Koha::Caches->get_instance;
63
    $cache->clear_from_cache("MarcCodedFields-");
64
65
    C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
66
    $biblio = Koha::Biblios->find( $biblio->biblionumber);
67
    $record = $biblio->metadata->record;
68
69
    is( $record->field('590')->subfield('a'), 'CODE', 'Record prior to filtering contains AV Code' );
70
    is( $record->field('942')->subfield('n'), 1, 'Record suppression is numeric prior to filtering' );
71
72
    my $processor = Koha::RecordProcessor->new(
73
        {
74
            schema  => 'MARC',
75
            filters => ['ExpandCodedFields'],
76
        }
77
    );
78
    is( ref($processor), 'Koha::RecordProcessor', 'Created record processor with ExpandCodedFields filter' );
79
80
    my $result = $processor->process( $record );
81
    is( ref($result), 'MARC::Record', 'It returns a reference to a MARC::Record object' );
82
    is( $result->field('590')->subfield('a'), 'Description should show OPAC', 'Returned record contains AV OPAC description (interface defaults to opac)' );
83
    is( $record->field('590')->subfield('a'), 'Description should show OPAC', 'Original record now contains AV OPAC description (interface defaults to opac)' );
84
    is( $record->field('942')->subfield('n'), 1, 'Record suppression is still numeric after filtering' );
85
86
    # reset record for next test
87
    $record = $biblio->metadata->record;
88
    is( $record->field('590')->subfield('a'), 'CODE', 'Record reset contains AV Code' );
89
90
    # set interface
91
    $processor->options({ interface => 'intranet' });
92
    $result = $processor->process( $record );
93
    is( $record->field('590')->subfield('a'), 'Description should show', 'Original record now contains AV description (interface set to intranet)' );
94
    is( $record->field('942')->subfield('n'), 1, 'Item suppression field remains numeric after filtering' );
95
96
    $schema->storage->txn_rollback();
97
};

Return to bug 30848