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