3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
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.
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.
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>.
24 use Module::Load qw( load );
34 my $heading = C4::Heading->new_from_field($field, $frameworkcode);
35 my $thesaurus = $heading->thesaurus();
36 my $type = $heading->type();
37 my $display_heading = $heading->display_form();
38 my $search_form = $heading->search_form();
42 C<C4::Heading> implements a simple class to representing
43 headings found in bibliographic and authority records.
49 my $heading = C4::Heading->new_from_field($field, $frameworkcode, [, $auth]);
51 Given a C<MARC::Field> object containing a heading from a
52 bib record, create a C<C4::Heading> object.
54 The optional third parameter is 'auth' - it is handled as boolean. If supplied we treat the field as an auth record field. Otherwise if it is a bib field. The fields checked are the same in a UNIMARC system and this parameter is ignored
56 If the MARC field supplied is not a valid heading, undef
64 my $frameworkcode = shift; #FIXME this is not used?
66 my $marcflavour = C4::Context->preference('marcflavour');
67 my $marc_handler = _marc_format_handler($marcflavour);
69 my $tag = $field->tag();
70 return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth );
73 $self->{'field'} = $field;
75 $self->{'auth_type'}, $self->{'thesaurus'},
76 $self->{'search_form'}, $self->{'display_form'},
78 ) = $marc_handler->parse_heading($field, $auth );
86 my $auth_type = $heading->auth_type();
88 Return the auth_type of the heading.
94 return $self->{'auth_type'};
99 my $field = $heading->field();
101 Return the MARC::Field the heading is based on.
107 return $self->{'field'};
112 my $display = $heading->display_form();
114 Return the "canonical" display form of the heading.
120 return $self->{'display_form'};
125 my $search_form = $heading->search_form();
127 Return the "canonical" search form of the heading.
133 return $self->{'search_form'};
138 my $authorities = $heading->authorities([$skipmetadata]);
140 Return a list of authority records for this
141 heading. If passed a true value for $skipmetadata,
142 SearchAuthorities will return only authids.
148 my $skipmetadata = shift;
149 my ( $results, $total ) = _search( $self, 'match-heading', $skipmetadata );
153 =head2 preferred_authorities
155 my $preferred_authorities = $heading->preferred_authorities;
157 Return a list of authority records for headings
158 that are a preferred form of the heading.
162 sub preferred_authorities {
164 my $skipmetadata = shift || undef;
165 my ( $results, $total ) = _search( 'see-from', $skipmetadata );
169 =head2 valid_heading_subfield
171 if (C4::Heading::valid_heading_subfield('100', 'e', '')) ...
173 Check if the given subfield is valid for the given field.
177 sub valid_heading_subfield {
179 my $subfield = shift;
180 my $marcflavour = C4::Context->preference('marcflavour');
183 my $marc_handler = _marc_format_handler($marcflavour);
184 return $marc_handler->valid_heading_subfield( $tag, $subfield, $auth );
187 =head1 INTERNAL METHODS
195 my $index = shift || undef;
196 my $skipmetadata = shift || undef;
197 my $thesaurus = $self->{thesaurus};
198 my $subject_heading_thesaurus = '';
206 push @marclist, $index;
208 push @operator, $self->{'match_type'};
209 push @value, $self->{'search_form'};
213 # This is calculated in C4/Heading/MARC21.pm - not used for UNIMARC
214 if ($thesaurus eq 'lcsh') {
215 $subject_heading_thesaurus = 'a';
216 } elsif ($thesaurus eq 'lcac') {
217 $subject_heading_thesaurus = 'b';
218 } elsif ($thesaurus eq 'mesh') {
219 $subject_heading_thesaurus = 'c';
220 } elsif ($thesaurus eq 'nal') {
221 $subject_heading_thesaurus = 'd';
222 } elsif ($thesaurus eq 'notspecified') {
223 $subject_heading_thesaurus = 'n';
224 } elsif ($thesaurus eq 'cash') {
225 $subject_heading_thesaurus = 'k';
226 } elsif ($thesaurus eq 'rvm') {
227 $subject_heading_thesaurus = 'v';
228 } else { # We stored the value from $7 as the thesaurus if there was one
229 $subject_heading_thesaurus = 'z';
230 push @marclist, 'thesaurus-conventions';
233 push @operator, 'is';
234 push @value, $self->{'thesaurus'};
236 push @marclist, 'thesaurus';
239 push @operator, 'is';
240 push @value, $subject_heading_thesaurus;
243 require Koha::SearchEngine::QueryBuilder;
244 require Koha::SearchEngine::Search;
246 # Use state variables to avoid recreating the objects every time.
247 # With Elasticsearch this also avoids creating a massive amount of
248 # ES connectors that would eventually run out of file descriptors.
249 state $builder = Koha::SearchEngine::QueryBuilder->new(
250 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
251 state $searcher = Koha::SearchEngine::Search->new(
252 {index => $Koha::SearchEngine::AUTHORITIES_INDEX} );
254 my $search_query = $builder->build_authorities_query_compat(
255 \@marclist, \@and_or, \@excluding, \@operator,
256 \@value, $self->{'auth_type'},
259 return $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
262 =head1 INTERNAL FUNCTIONS
264 =head2 _marc_format_handler
266 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
267 depending on the selected MARC flavour.
271 sub _marc_format_handler {
272 my $marcflavour = uc shift;
273 my $pname = "C4::Heading::$marcflavour";
275 return $pname->new();
280 Koha Development Team <http://koha-community.org/>
282 Galen Charlton <galen.charlton@liblime.com>