|
Lines 49-54
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); |
| 53 |
|
| 52 |
use Koha::Caches; |
54 |
use Koha::Caches; |
| 53 |
use Koha::ClassSources; |
55 |
use Koha::ClassSources; |
| 54 |
use Koha::Libraries; |
56 |
use Koha::Libraries; |
|
Lines 75-187
sub filter {
Link Here
|
| 75 |
my $opac = $interface eq 'opac' ? 1 : 0; |
77 |
my $opac = $interface eq 'opac' ? 1 : 0; |
| 76 |
|
78 |
|
| 77 |
my $marcflavour = C4::Context->preference('marcflavour'); |
79 |
my $marcflavour = C4::Context->preference('marcflavour'); |
| 78 |
my $av = _getAuthorisedValues4MARCSubfields($frameworkcode); |
80 |
my $coded_fields = _getCodedFields($frameworkcode); |
| 79 |
|
81 |
|
| 80 |
my $desc_field = $opac ? 'lib_opac' : 'lib'; |
82 |
my $desc_field = $opac ? 'lib_opac' : 'lib'; |
| 81 |
foreach my $tag ( keys %$av ) { |
83 |
for my $tag ( keys %$coded_fields ) { |
| 82 |
foreach my $field ( $record->field($tag) ) { |
84 |
for my $field ( $record->field($tag) ) { |
| 83 |
if ( $av->{$tag} ) { |
85 |
my @new_subfields = (); |
| 84 |
my @new_subfields = (); |
86 |
for my $subfield ( $field->subfields() ) { |
| 85 |
for my $subfield ( $field->subfields() ) { |
87 |
my ( $letter, $value ) = @$subfield; |
| 86 |
my ( $letter, $value ) = @$subfield; |
88 |
|
| 87 |
|
89 |
# Replace the field value with the authorised value |
| 88 |
# Replace the field value with the authorised value |
90 |
# *except* for MARC21 field 942$n (suppression in opac) |
| 89 |
# *except* for MARC21 field 942$n (suppression in opac) |
91 |
if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) |
| 90 |
if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) |
92 |
|| $marcflavour eq 'UNIMARC' ) |
| 91 |
|| $marcflavour eq 'UNIMARC' ) |
93 |
{ |
| 92 |
{ |
94 |
$value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $coded_fields, undef, $opac ) if $coded_fields->{$tag}->{$letter}; |
| 93 |
my $category = $av->{$tag}->{$letter}->{category}; |
|
|
| 94 |
$value = $av->{$tag}->{$letter}->{$category}->{$value} |
| 95 |
->{$desc_field} || $value; |
| 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 |
$frameworkcode //= ""; |
112 |
$frameworkcode //= ""; |
| 115 |
|
113 |
|
| 116 |
my $cache = Koha::Caches->get_instance(); |
114 |
my $cache = Koha::Caches->get_instance(); |
| 117 |
my $cache_key = "MarcAVStructure-$frameworkcode"; |
115 |
my $cache_key = "MarcCodedFields-$frameworkcode"; |
| 118 |
my $cached = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); |
116 |
my $cached = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); |
| 119 |
return $cached if $cached; |
117 |
return $cached if $cached; |
| 120 |
|
118 |
|
| 121 |
my $dbh = C4::Context->dbh; |
119 |
my $coded_fields = { |
| 122 |
my $sth = $dbh->prepare( |
120 |
map { |
| 123 |
"SELECT DISTINCT tagfield, tagsubfield, authorised_value |
121 |
$_->tagfield => { |
| 124 |
FROM marc_subfield_structure |
122 |
$_->tagsubfield => { |
| 125 |
WHERE authorised_value IS NOT NULL |
123 |
'authorised_value' => $_->authorised_value |
| 126 |
AND authorised_value!='' |
|
|
| 127 |
AND frameworkcode=?" |
| 128 |
); |
| 129 |
$sth->execute($frameworkcode); |
| 130 |
my $av_structure = {}; |
| 131 |
while ( my ( $tag, $letter, $category ) = $sth->fetchrow() ) { |
| 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 |
} |
124 |
} |
| 178 |
}; |
125 |
} |
| 179 |
$av_structure->{$tag}->{$letter}->{$category} = $authorised_values; |
126 |
} Koha::MarcSubfieldStructures->search( |
| 180 |
} |
127 |
{ |
| 181 |
} |
128 |
frameworkcode => $frameworkcode, |
|
|
129 |
authorised_value => { '>' => '' } |
| 130 |
}, |
| 131 |
{ |
| 132 |
columns => [ 'tagfield', 'tagsubfield', 'authorised_value' ], |
| 133 |
order_by => [ 'tagfield', 'tagsubfield' ] |
| 134 |
} |
| 135 |
)->as_list |
| 136 |
}; |
| 182 |
|
137 |
|
| 183 |
$cache->set_in_cache( $cache_key, $av_structure ); |
138 |
$cache->set_in_cache( $cache_key, $coded_fields ); |
| 184 |
return $av_structure; |
139 |
return $coded_fields; |
| 185 |
} |
140 |
} |
| 186 |
|
141 |
|
| 187 |
1; |
142 |
1; |
| 188 |
- |
|
|