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 C4::Biblio qw(GetAuthorisedValueDesc); |
|
|
53 |
|
54 |
use Koha::Caches; |
55 |
use Koha::ClassSources; |
56 |
use Koha::Libraries; |
57 |
use Koha::ItemTypes; |
53 |
|
58 |
|
54 |
use base qw(Koha::RecordProcessor::Base); |
59 |
use base qw(Koha::RecordProcessor::Base); |
55 |
our $NAME = 'ExpandAuthorizedValues'; |
60 |
our $NAME = 'ExpandAuthorizedValues'; |
56 |
|
61 |
|
57 |
my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework. |
|
|
58 |
|
59 |
=head2 filter |
62 |
=head2 filter |
60 |
|
63 |
|
61 |
Embed items into the MARC::Record object. |
64 |
Embed items into the MARC::Record object. |
Lines 69-133
sub filter {
Link Here
|
69 |
return unless defined $record and ref($record) eq 'MARC::Record'; |
72 |
return unless defined $record and ref($record) eq 'MARC::Record'; |
70 |
|
73 |
|
71 |
my $params = $self->params; |
74 |
my $params = $self->params; |
72 |
my $interface = $params->{options}->{interface} // 'opac'; |
75 |
my $interface = $params->{options}->{interface} // 'opac'; |
73 |
my $opac = $interface eq 'opac' ? 1 : 0; |
|
|
74 |
my $frameworkcode = $params->{options}->{frameworkcode} // q{}; |
76 |
my $frameworkcode = $params->{options}->{frameworkcode} // q{}; |
75 |
my $marcstructure = $params->{options}->{marcstructure} |
77 |
my $opac = $interface eq 'opac' ? 1 : 0; |
76 |
// GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); |
|
|
77 |
|
78 |
|
78 |
my $marcflavour = C4::Context->preference('marcflavour'); |
79 |
my $marcflavour = C4::Context->preference('marcflavour'); |
79 |
my $av = _getAuthorisedValues4MARCSubfields($frameworkcode); |
80 |
my $coded_fields = _getCodedFields($frameworkcode); |
80 |
foreach my $tag ( keys %$av ) { |
81 |
|
81 |
foreach my $field ( $record->field($tag) ) { |
82 |
my $desc_field = $opac ? 'lib_opac' : 'lib'; |
82 |
if ( $av->{$tag} ) { |
83 |
for my $tag ( keys %$coded_fields ) { |
83 |
my @new_subfields = (); |
84 |
for my $field ( $record->field($tag) ) { |
84 |
for my $subfield ( $field->subfields() ) { |
85 |
my @new_subfields = (); |
85 |
my ( $letter, $value ) = @$subfield; |
86 |
for my $subfield ( $field->subfields() ) { |
86 |
|
87 |
my ( $letter, $value ) = @$subfield; |
87 |
# Replace the field value with the authorised value |
88 |
|
88 |
# *except* for MARC21 field 942$n (suppression in opac) |
89 |
# Replace the field value with the authorised value |
89 |
if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) |
90 |
# *except* for MARC21 field 942$n (suppression in opac) |
90 |
|| $marcflavour eq 'UNIMARC' ) |
91 |
if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) |
91 |
{ |
92 |
|| $marcflavour eq 'UNIMARC' ) |
92 |
$value = |
93 |
{ |
93 |
GetAuthorisedValueDesc( $tag, $letter, $value, '', |
94 |
$value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $coded_fields, undef, $opac ) if $coded_fields->{$tag}->{$letter}; |
94 |
$marcstructure, undef, $opac ) |
|
|
95 |
if $av->{$tag}->{$letter}; |
96 |
} |
97 |
push( @new_subfields, $letter, $value ); |
98 |
} |
95 |
} |
99 |
$field->replace_with( |
96 |
push( @new_subfields, $letter, $value ); |
100 |
MARC::Field->new( |
|
|
101 |
$tag, $field->indicator(1), |
102 |
$field->indicator(2), @new_subfields |
103 |
) |
104 |
); |
105 |
} |
97 |
} |
|
|
98 |
$field->replace_with( |
99 |
MARC::Field->new( |
100 |
$tag, $field->indicator(1), |
101 |
$field->indicator(2), @new_subfields |
102 |
) |
103 |
); |
106 |
} |
104 |
} |
107 |
} |
105 |
} |
108 |
|
106 |
|
109 |
return $record; |
107 |
return $record; |
110 |
} |
108 |
} |
111 |
|
109 |
|
112 |
sub _getAuthorisedValues4MARCSubfields { |
110 |
sub _getCodedFields { |
113 |
my ($frameworkcode) = @_; |
111 |
my ($frameworkcode) = @_; |
114 |
unless ( $authval_per_framework{$frameworkcode} ) { |
112 |
$frameworkcode //= ""; |
115 |
my $dbh = C4::Context->dbh; |
113 |
|
116 |
my $sth = $dbh->prepare( |
114 |
my $cache = Koha::Caches->get_instance(); |
117 |
"SELECT DISTINCT tagfield, tagsubfield |
115 |
my $cache_key = "MarcCodedFields-$frameworkcode"; |
118 |
FROM marc_subfield_structure |
116 |
my $cached = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); |
119 |
WHERE authorised_value IS NOT NULL |
117 |
return $cached if $cached; |
120 |
AND authorised_value!='' |
118 |
|
121 |
AND frameworkcode=?" |
119 |
my $coded_fields = { |
122 |
); |
120 |
map { |
123 |
$sth->execute($frameworkcode); |
121 |
$_->tagfield => { |
124 |
my $av = {}; |
122 |
$_->tagsubfield => { |
125 |
while ( my ( $tag, $letter ) = $sth->fetchrow() ) { |
123 |
'authorised_value' => $_->authorised_value |
126 |
$av->{$tag}->{$letter} = 1; |
124 |
} |
127 |
} |
125 |
} |
128 |
$authval_per_framework{$frameworkcode} = $av; |
126 |
} Koha::MarcSubfieldStructures->search( |
129 |
} |
127 |
{ |
130 |
return $authval_per_framework{$frameworkcode}; |
128 |
frameworkcode => $frameworkcode, |
|
|
129 |
authorised_value => { '>' => '' } |
130 |
}, |
131 |
{ |
132 |
columns => [ 'tagfield', 'tagsubfield', 'authorised_value' ], |
133 |
order_by => [ 'tagfield', 'tagsubfield' ] |
134 |
} |
135 |
)->as_list |
136 |
}; |
137 |
|
138 |
$cache->set_in_cache( $cache_key, $coded_fields ); |
139 |
return $coded_fields; |
131 |
} |
140 |
} |
132 |
|
141 |
|
133 |
1; |
142 |
1; |