Merge remote-tracking branch 'kc/new/bug_5616' into kcmaster
[koha.git] / C4 / Heading.pm
1 package C4::Heading;
2
3 # Copyright (C) 2008 LibLime
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 2 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 strict;
21 #use warnings; FIXME - Bug 2505
22 use MARC::Record;
23 use MARC::Field;
24 use C4::Context;
25 use C4::Heading::MARC21;
26 use C4::Search;
27 use Carp;
28
29 our $VERSION = 3.00;
30
31 =head1 NAME
32
33 C4::Heading
34
35 =head1 SYNOPSIS
36
37  use C4::Heading;
38  my $heading = C4::Heading->new_from_bib_field($field);
39  my $thesaurus = $heading->thesaurus();
40  my $type = $heading->type();
41  my $display_heading = $heading->display();
42  my $search_string = $heading->search_string();
43
44 =head1 DESCRIPTION
45
46 C<C4::Heading> implements a simple class to representing
47 headings found in bibliographic and authority records.
48
49 =head1 METHODS
50
51 =head2 new_from_bib_field
52
53   my $heading = C4::Heading->new_from_bib_field($field[, $marc_flavour]);
54
55 Given a C<MARC::Field> object containing a heading from a 
56 bib record, create a C<C4::Heading> object.
57
58 The optional second parameter is the MARC flavour (i.e., MARC21
59 or UNIMARC); if this parameter is not supplied, it is
60 taken from the Koha application context.
61
62 If the MARC field supplied is not a valid heading, undef
63 is returned.
64
65 =cut
66
67 sub new_from_bib_field {
68     my $class = shift;
69     my $field = shift;
70     my $marcflavour = @_ ? shift : C4::Context->preference('marcflavour');
71
72     my $marc_handler = _marc_format_handler($marcflavour);
73
74     my $tag = $field->tag();
75     return unless $marc_handler->valid_bib_heading_tag($tag);
76     my $self = {};
77    
78     ($self->{'auth_type'}, $self->{'subject_added_entry'}, $self->{'series_added_entry'}, $self->{'main_entry'},
79      $self->{'thesaurus'}, $self->{'search_form'}, $self->{'display_form'}) =
80         $marc_handler->parse_heading($field);
81
82     bless $self, $class;
83     return $self;
84 }
85
86 =head2 display_form
87
88   my $display = $heading->display_form();
89
90 Return the "canonical" display form of the heading.
91
92 =cut
93
94 sub display_form {
95     my $self = shift;
96     return $self->{'display_form'};
97 }
98
99 =head2 authorities
100
101   my $authorities = $heading->authorities;
102
103 Return a list of authority records for this 
104 heading.
105
106 =cut
107
108 sub authorities {
109     my $self = shift;
110     my $query = qq(Match-heading,do-not-truncate,ext="$self->{'search_form'}");
111     $query .= $self->_query_limiters();
112     my ($error, $results, $total_hits) = SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
113     if (defined $error) {
114         carp "Error:$error from search $query";
115     }
116     return $results;
117 }
118
119 =head2 preferred_authorities
120
121   my $preferred_authorities = $heading->preferred_authorities;
122
123 Return a list of authority records for headings
124 that are a preferred form of the heading.
125
126 =cut
127
128 sub preferred_authorities {
129     my $self = shift;
130     my $query = "Match-heading-see-from,do-not-truncate,ext='$self->{'search_form'}'";
131     $query .= $self->_query_limiters();
132     my ($error, $results, $total_hits) = SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
133     if (defined $error) {
134         carp "Error:$error from search $query";
135     }
136     return $results;
137 }
138
139 =head1 INTERNAL METHODS
140
141 =head2 _query_limiters
142
143 =cut
144
145 sub _query_limiters {
146     my $self = shift;
147
148     my $limiters = " AND at='$self->{'auth_type'}'";
149     if ($self->{'subject_added_entry'}) {
150         $limiters .= " AND Heading-use-subject-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
151         $limiters .= " AND Subject-heading-thesaurus=$self->{'thesaurus'}";
152     }
153     if ($self->{'series_added_entry'}) {
154         $limiters .= " AND Heading-use-series-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
155     }
156     if (not $self->{'subject_added_entry'} and not $self->{'series_added_entry'}) {
157         $limiters .= " AND Heading-use-main-or-added-entry=a" # FIXME -- is this properly in C4::Heading::MARC21?
158     }
159     return $limiters;
160 }
161
162 =head1 INTERNAL FUNCTIONS
163
164 =head2 _marc_format_handler
165
166 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
167 depending on the selected MARC flavour.
168
169 =cut
170
171 sub _marc_format_handler {
172     my $marcflavour = shift;
173
174     if ($marcflavour eq 'UNIMARC') {
175         return C4::Heading::UNIMARC->new();
176     } else {
177         return C4::Heading::MARC21->new();
178     }
179
180 }
181
182 =head1 AUTHOR
183
184 Koha Development Team <http://koha-community.org/>
185
186 Galen Charlton <galen.charlton@liblime.com>
187
188 =cut
189
190 1;