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

(-)a/Koha/Filter/MARC/ExpandAuthorizedValues.pm (-22 / +76 lines)
Lines 49-61 Filter to replace Koha AV codes in MARC::Records with their Koha AV descriptions Link Here
49
49
50
use Modern::Perl;
50
use Modern::Perl;
51
51
52
use C4::Biblio qw( GetAuthorisedValueDesc GetMarcStructure );
52
use Koha::Caches;
53
use Koha::ClassSources;
54
use Koha::Libraries;
55
use Koha::ItemTypes;
53
56
54
use base qw(Koha::RecordProcessor::Base);
57
use base qw(Koha::RecordProcessor::Base);
55
our $NAME = 'ExpandAuthorizedValues';
58
our $NAME = 'ExpandAuthorizedValues';
56
59
57
my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework.
58
59
=head2 filter
60
=head2 filter
60
61
61
Embed items into the MARC::Record object.
62
Embed items into the MARC::Record object.
Lines 69-82 sub filter { Link Here
69
    return unless defined $record and ref($record) eq 'MARC::Record';
70
    return unless defined $record and ref($record) eq 'MARC::Record';
70
71
71
    my $params        = $self->params;
72
    my $params        = $self->params;
72
    my $interface     = $params->{options}->{interface} // 'opac';
73
    my $interface     = $params->{options}->{interface}     // 'opac';
73
    my $opac          = $interface eq 'opac' ? 1 : 0;
74
    my $frameworkcode = $params->{options}->{frameworkcode} // q{};
74
    my $frameworkcode = $params->{options}->{frameworkcode} // q{};
75
    my $marcstructure = $params->{options}->{marcstructure}
75
    my $opac          = $interface eq 'opac' ? 1 : 0;
76
      // GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } );
77
76
78
    my $marcflavour = C4::Context->preference('marcflavour');
77
    my $marcflavour = C4::Context->preference('marcflavour');
79
    my $av          = _getAuthorisedValues4MARCSubfields($frameworkcode);
78
    my $av          = _getAuthorisedValues4MARCSubfields($frameworkcode);
79
80
    my $desc_field = $opac ? 'lib_opac' : 'lib';
80
    foreach my $tag ( keys %$av ) {
81
    foreach my $tag ( keys %$av ) {
81
        foreach my $field ( $record->field($tag) ) {
82
        foreach my $field ( $record->field($tag) ) {
82
            if ( $av->{$tag} ) {
83
            if ( $av->{$tag} ) {
Lines 89-98 sub filter { Link Here
89
                    if ( !( $tag eq '942' && $subfield->[0] eq 'n' )
90
                    if ( !( $tag eq '942' && $subfield->[0] eq 'n' )
90
                        || $marcflavour eq 'UNIMARC' )
91
                        || $marcflavour eq 'UNIMARC' )
91
                    {
92
                    {
92
                        $value =
93
                        my $category = $av->{$tag}->{$letter}->{category};
93
                          GetAuthorisedValueDesc( $tag, $letter, $value, '',
94
                        $value = $av->{$tag}->{$letter}->{$category}->{$value}
94
                            $marcstructure, undef, $opac )
95
                          ->{$desc_field} || $value;
95
                          if $av->{$tag}->{$letter};
96
                    }
96
                    }
97
                    push( @new_subfields, $letter, $value );
97
                    push( @new_subfields, $letter, $value );
98
                }
98
                }
Lines 111-133 sub filter { Link Here
111
111
112
sub _getAuthorisedValues4MARCSubfields {
112
sub _getAuthorisedValues4MARCSubfields {
113
    my ($frameworkcode) = @_;
113
    my ($frameworkcode) = @_;
114
    unless ( $authval_per_framework{$frameworkcode} ) {
114
    $frameworkcode //= "";
115
        my $dbh = C4::Context->dbh;
115
116
        my $sth = $dbh->prepare(
116
    my $cache     = Koha::Caches->get_instance();
117
            "SELECT DISTINCT tagfield, tagsubfield
117
    my $cache_key = "MarcAVStructure-$frameworkcode";
118
    my $cached    = $cache->get_from_cache( $cache_key, { unsafe => 1 } );
119
    return $cached if $cached;
120
121
    my $dbh = C4::Context->dbh;
122
    my $sth = $dbh->prepare(
123
        "SELECT DISTINCT tagfield, tagsubfield, authorised_value
118
             FROM marc_subfield_structure
124
             FROM marc_subfield_structure
119
             WHERE authorised_value IS NOT NULL
125
             WHERE authorised_value IS NOT NULL
120
                AND authorised_value!=''
126
                AND authorised_value!=''
121
                AND frameworkcode=?"
127
                AND frameworkcode=?"
122
        );
128
    );
123
        $sth->execute($frameworkcode);
129
    $sth->execute($frameworkcode);
124
        my $av = {};
130
    my $av_structure = {};
125
        while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
131
    while ( my ( $tag, $letter, $category ) = $sth->fetchrow() ) {
126
            $av->{$tag}->{$letter} = 1;
132
        $av_structure->{$tag}->{$letter}->{category} = $category;
133
        if ( $category eq 'branches' ) {
134
            my $libraries = {
135
                map {
136
                    $_->branchcode => {
137
                        lib_opac => $_->branchname,
138
                        lib => $_->branchname
139
                    }
140
                } Koha::Libraries->search()->as_list
141
            };
142
            $av_structure->{$tag}->{$letter}->{$category} = $libraries;
143
        }
144
        elsif ( $category eq 'itemtypes' ) {
145
            my $itemtypes = {
146
                map {
147
                    $_->itemtype => {
148
                        lib_opac => $_->translated_description,
149
                        lib      => $_->translated_description
150
                    }
151
                } Koha::ItemTypes->search()->as_list
152
            };
153
            $av_structure->{$tag}->{$letter}->{$category} = $itemtypes;
154
        }
155
        elsif ( $category eq 'cn_source' ) {
156
            my $cn_sources = {
157
                map {
158
                    $_->cn_source => {
159
                        lib_opac => $_->description,
160
                        lib => $_->description
161
                    }
162
                } Koha::ClassSources->search()->as_list
163
            };
164
            $av_structure->{$tag}->{$letter}->{$category} = $cn_sources;
165
        }
166
        else {
167
            my $authorised_values = {
168
                map {
169
                    $_->{authorised_value} =>
170
                      { lib => $_->lib, lib_opac => $_->lib_opac }
171
                } @{ Koha::AuthorisedValues->search(
172
                        { category => $category },
173
                        {
174
                            columns => [ 'authorised_value', 'lib_opac', 'lib' ]
175
                        }
176
                    )->unblessed
177
                }
178
            };
179
            $av_structure->{$tag}->{$letter}->{$category} = $authorised_values;
127
        }
180
        }
128
        $authval_per_framework{$frameworkcode} = $av;
129
    }
181
    }
130
    return $authval_per_framework{$frameworkcode};
182
183
    $cache->set_in_cache( $cache_key, $av_structure );
184
    return $av_structure;
131
}
185
}
132
186
133
1;
187
1;
(-)a/t/db_dependent/Koha/Filter/ExpandAuthorizedValues.t (-1 / +1 lines)
Lines 62-67 subtest 'ExpandAuthorizedValues tests' => sub { Link Here
62
    my $cache = Koha::Caches->get_instance;
62
    my $cache = Koha::Caches->get_instance;
63
    $cache->clear_from_cache("MarcStructure-0-");
63
    $cache->clear_from_cache("MarcStructure-0-");
64
    $cache->clear_from_cache("MarcStructure-1-");
64
    $cache->clear_from_cache("MarcStructure-1-");
65
    $cache->clear_from_cache("MarcAVStructure-1-");
65
    $cache->clear_from_cache("default_value_for_mod_marc-");
66
    $cache->clear_from_cache("default_value_for_mod_marc-");
66
    $cache->clear_from_cache("MarcSubfieldStructure-");
67
    $cache->clear_from_cache("MarcSubfieldStructure-");
67
68
68
- 

Return to bug 30848