Bug 22721: Remove frameworkcode parameter in GetMarcFromKohaField calls
[koha.git] / 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 Carp;
23 use MARC::Field;
24 use C4::Heading;
25
26 use base qw(C4::Linker);
27
28 sub get_link {
29     my $self        = shift;
30     my $heading     = shift;
31     my $behavior    = shift || 'default';
32     my $search_form = $heading->search_form();
33     my $auth_type = $heading->auth_type();
34     my $authid;
35     my $fuzzy = 0;
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
46         if ( $behavior eq 'default' && $#{$authorities} == 0 ) {
47             $authid = $authorities->[0]->{'authid'};
48         }
49         elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) {
50             $authid = $authorities->[0]->{'authid'};
51             $fuzzy  = $#{$authorities} > 0;
52         }
53         elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) {
54             $authid = $authorities->[ $#{$authorities} ]->{'authid'};
55             $fuzzy  = $#{$authorities} > 0;
56         }
57
58         if ( !defined $authid && $self->{'broader_headings'} ) {
59             my $field     = $heading->field();
60             my @subfields = grep { $_->[0] ne '9' } $field->subfields();
61             if ( scalar @subfields > 1 ) {
62                 pop @subfields;
63                 $field =
64                     MARC::Field->new(
65                         $field->tag,
66                         $field->indicator(1),
67                         $field->indicator(2),
68                         map { $_->[0] => $_->[1] } @subfields
69                     );
70                 ( $authid, $fuzzy ) =
71                   $self->get_link( C4::Heading->new_from_bib_field($field),
72                     $behavior );
73             }
74         }
75
76         $self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
77         $self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
78         $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'}  = $fuzzy;
79     }
80     return $self->SUPER::_handle_auth_limit($authid), $fuzzy;
81 }
82
83 sub update_cache {
84     my $self        = shift;
85     my $heading     = shift;
86     my $authid      = shift;
87     my $search_form = $heading->search_form();
88     my $auth_type = $heading->auth_type();
89     my $fuzzy = 0;
90
91     $self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
92     $self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
93     $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'}  = $fuzzy;
94 }
95
96 sub flip_heading {
97     my $self    = shift;
98     my $heading = shift;
99
100     # TODO: implement
101 }
102
103 1;
104 __END__
105
106 =head1 NAME
107
108 C4::Linker::Default - match only if there is a single matching auth
109
110 =cut