Bug 31963: Only show hold fee msg on OPAC if patron will be charged
[koha.git] / C4 / Linker.pm
1 package C4::Linker;
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 =head1 NAME
21
22 C4::Linker - Base class for linking authorities to bibliographic records
23
24 =head1 SYNOPSIS
25
26   use C4::Linker (%params );
27
28 =head1 DESCRIPTION
29
30 Base class for C4::Linker::X. Subclasses need to provide the following methods
31
32 B<get_link ($field)> - return the authid for the authority that should be
33 linked to the provided MARC::Field object, and a boolean to indicate whether
34 the match is "fuzzy" (the semantics of "fuzzy" are up to the individual plugin).
35 In order to handle authority limits, get_link should always end with:
36     return $self->SUPER::_handle_auth_limit($authid), $fuzzy;
37
38 B<update_cache ($heading, $authid)> - updates internal linker cache for
39 $heading with $authid of a new created authotiry record
40
41 B<flip_heading ($field)> - return a MARC::Field object with the heading flipped
42 to the preferred form.
43
44 =head1 FUNCTIONS
45
46 =cut
47
48 use strict;
49 use warnings;
50 use C4::Context;
51
52 use base qw(Class::Accessor);
53
54 __PACKAGE__->mk_accessors(qw( ));
55
56 sub new {
57     my $class = shift;
58     my $param = shift;
59
60     my $self = {};
61
62     while ( my ( $key, $value ) = each %$param ) {
63         if ( $key eq 'auth_limit' && $value ) {
64             my $dbh = C4::Context->dbh;
65             my $sql =
66               "SELECT authid FROM auth_header WHERE $value ORDER BY authid ASC";
67             my $sth = $dbh->prepare($sql);
68             $sth->execute();
69             while ( my ($authid) = $sth->fetchrow_array() ) {
70                 push @{ $self->{'auths_to_link'} }, $authid;
71             }
72         }
73         elsif ( $key eq 'options' && $value ) {
74             foreach my $opt ( split( /\|/, $value ) ) {
75                 $self->{$opt} = 1;
76             }
77         }
78         elsif ($value) {
79             $self->{$key} = $value;
80         }
81     }
82
83     bless $self, $class;
84     return $self;
85 }
86
87 =head2 _handle_auth_limit
88
89     return $self->SUPER::_handle_auth_limit($authid), $fuzzy;
90
91 Function to be called by subclasses to handle authority record limits.
92
93 =cut
94
95 sub _handle_auth_limit {
96     my $self   = shift;
97     my $authid = shift;
98
99     if ( defined $self->{'auths_to_link'} && defined $authid && !grep { $_ == $authid }
100         @{ $self->{'auths_to_link'} } )
101     {
102         undef $authid;
103     }
104     return $authid;
105 }
106
107 =head2 EXPORT
108
109 None by default.
110
111 =head1 SEE ALSO
112
113 C4::Linker::Default
114
115 =head1 AUTHOR
116
117 Jared Camins-Esakov, C & P Bibliography Services, E<lt>jcamins@cpbibliography.comE<gt>
118
119 =cut
120
121 1;
122
123 __END__