|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Authority::BiblioIndicators; |
|
|
2 |
|
| 3 |
# Copyright 2018 Rijksmuseum |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Koha::Caches; |
| 23 |
|
| 24 |
our $cached_indicators; |
| 25 |
|
| 26 |
sub new { return bless ref($_[1]) ? $_[1] : {}, shift; } |
| 27 |
|
| 28 |
=head1 NAME |
| 29 |
|
| 30 |
Koha::Authority::BiblioIndicators - Obtain biblio indicators, controlled by authority record |
| 31 |
|
| 32 |
=head1 API |
| 33 |
|
| 34 |
=head2 METHODS |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
=head3 get |
| 39 |
|
| 40 |
$self->get({ |
| 41 |
auth_record => $record, |
| 42 |
report_tag => $authtype->auth_tag_to_report, |
| 43 |
biblio_tag => $tag, |
| 44 |
flavour => $flavour, |
| 45 |
}); |
| 46 |
|
| 47 |
=cut |
| 48 |
|
| 49 |
sub get { |
| 50 |
my ( $self, $params ) = @_; |
| 51 |
my $flavour = $params->{flavour} // q{}; |
| 52 |
my $tag = $params->{biblio_tag} // q{}; |
| 53 |
my $record = $params->{auth_record}; |
| 54 |
my $report_tag = $params->{report_tag} // q{}; |
| 55 |
|
| 56 |
_load_config() if !defined $cached_indicators; |
| 57 |
|
| 58 |
my $result = {}; |
| 59 |
return $result if !exists $cached_indicators->{$flavour}; |
| 60 |
my $rule = $cached_indicators->{$flavour}->{$tag} // |
| 61 |
$cached_indicators->{$flavour}->{'*'} // |
| 62 |
{}; |
| 63 |
my $report_fld = $record ? $record->field( $report_tag ) : undef; |
| 64 |
|
| 65 |
foreach my $ind ( 'ind1', 'ind2' ) { |
| 66 |
if( exists $rule->{$ind} ) { |
| 67 |
if( !$rule->{$ind} ) { |
| 68 |
$result->{$ind} = $rule->{$ind}; # undef or empty string |
| 69 |
} elsif( $rule->{$ind} eq 'auth1' ) { |
| 70 |
$result->{$ind} = $report_fld->indicator(1) if $report_fld; |
| 71 |
} elsif( $rule->{$ind} eq 'auth2' ) { |
| 72 |
$result->{$ind} = $report_fld->indicator(2) if $report_fld; |
| 73 |
} elsif( $rule->{$ind} eq 'thesaurus' ) { |
| 74 |
} else { |
| 75 |
$result->{$ind} = substr( $rule->{$ind}, 0, 1); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
sub _load_config { |
| 84 |
my $cache = Koha::Caches->get_instance; |
| 85 |
my $val; |
| 86 |
$val = $cache->get_from_cache("auth_controlled_bibind") if $cache; |
| 87 |
if( $val ) { |
| 88 |
$cached_indicators = $val; |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
# TODO Load the config file (will be done on report 20...) |
| 93 |
# This sample makes UNIMARC always fall back to authority indicators. |
| 94 |
# For MARC21 it only corrects ind1 for 100, 600 and 700 as well as ind2 |
| 95 |
# for 100 and 600. Other indicators are not touched. |
| 96 |
$cached_indicators = { |
| 97 |
UNIMARCAUTH => { |
| 98 |
'*' => { ind1 => 'auth1', ind2 => 'auth2' }, |
| 99 |
}, |
| 100 |
MARC21 => { |
| 101 |
'100' => { ind1 => 'auth1', ind2 => undef }, |
| 102 |
'600' => { ind1 => 'auth1', ind2 => 'thesaurus' }, |
| 103 |
'700' => { ind1 => 'auth1' }, |
| 104 |
}, |
| 105 |
}; |
| 106 |
|
| 107 |
# Save into cache |
| 108 |
$cache->set_in_cache( "auth_controlled_bibind", $cached_indicators ) |
| 109 |
if $cache; |
| 110 |
} |
| 111 |
|
| 112 |
=head3 clear |
| 113 |
|
| 114 |
Clear the cached indicator values |
| 115 |
|
| 116 |
$self->clear; |
| 117 |
|
| 118 |
=cut |
| 119 |
|
| 120 |
sub clear { |
| 121 |
$cached_indicators = undef; |
| 122 |
my $cache = Koha::Caches->get_instance; |
| 123 |
$cache->clear_from_cache("auth_controlled_bibind"); |
| 124 |
} |
| 125 |
|
| 126 |
1; |