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

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