Bug 17600: Standardize our EXPORT_OK
[srvgit] / C4 / Linker / Default.pm
1 package C4::Linker::Default;
2
3 # Copyright 2011 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use MARC::Field;
23 use C4::Heading;
24
25 use base qw(C4::Linker);
26
27 sub get_link {
28     my $self        = shift;
29     my $heading     = shift;
30     my $behavior    = shift || 'default';
31     my $search_form = $heading->search_form();
32     my $auth_type = $heading->auth_type();
33     my $authid;
34     my $fuzzy = 0;
35     my $match_count;
36
37     if ( $self->{'cache'}->{$search_form.$auth_type}->{'cached'} ) {
38         $authid = $self->{'cache'}->{$search_form.$auth_type}->{'authid'};
39         $fuzzy  = $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'};
40     }
41     else {
42
43         # look for matching authorities
44         my $authorities = $heading->authorities(1);    # $skipmetadata = true
45         $match_count = scalar @$authorities;
46
47         if ( $behavior eq 'default' && $#{$authorities} == 0 ) {
48             $authid = $authorities->[0]->{'authid'};
49         }
50         elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) {
51             $authid = $authorities->[0]->{'authid'};
52             $fuzzy  = $#{$authorities} > 0;
53         }
54         elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) {
55             $authid = $authorities->[ $#{$authorities} ]->{'authid'};
56             $fuzzy  = $#{$authorities} > 0;
57         }
58
59         if ( !defined $authid && $self->{'broader_headings'} ) {
60             my $field     = $heading->field();
61             my @subfields = grep { $_->[0] ne '9' } $field->subfields();
62             if ( scalar @subfields > 1 ) {
63                 pop @subfields;
64                 $field =
65                     MARC::Field->new(
66                         $field->tag,
67                         $field->indicator(1),
68                         $field->indicator(2),
69                         map { $_->[0] => $_->[1] } @subfields
70                     );
71                 ( $authid, $fuzzy ) =
72                   $self->get_link( C4::Heading->new_from_field($field),
73                     $behavior );
74             }
75         }
76
77         $self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
78         $self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
79         $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'}  = $fuzzy;
80     }
81     return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $match_count;
82 }
83
84 sub update_cache {
85     my $self        = shift;
86     my $heading     = shift;
87     my $authid      = shift;
88     my $search_form = $heading->search_form();
89     my $auth_type = $heading->auth_type();
90     my $fuzzy = 0;
91
92     $self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
93     $self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
94     $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'}  = $fuzzy;
95 }
96
97 sub flip_heading {
98     my $self    = shift;
99     my $heading = shift;
100
101     # TODO: implement
102 }
103
104 1;
105 __END__
106
107 =head1 NAME
108
109 C4::Linker::Default - match only if there is a single matching auth
110
111 =cut