Bug 6875 de-nesting C4::Items
[koha.git] / C4 / Heading.pm
blobd9e13d422323ad0d38398bf461650b58384bdcfd
1 package C4::Heading;
3 # Copyright (C) 2008 LibLime
4 #
5 # This file is part of Koha.
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.
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.
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.
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 Carp;
28 our $VERSION = 3.00;
30 =head1 NAME
32 C4::Heading
34 =head1 SYNOPSIS
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();
43 =head1 DESCRIPTION
45 C<C4::Heading> implements a simple class to representing
46 headings found in bibliographic and authority records.
48 =head1 METHODS
50 =head2 new_from_bib_field
52 my $heading = C4::Heading->new_from_bib_field($field[, $marc_flavour]);
54 Given a C<MARC::Field> object containing a heading from a
55 bib record, create a C<C4::Heading> object.
57 The optional second parameter is the MARC flavour (i.e., MARC21
58 or UNIMARC); if this parameter is not supplied, it is
59 taken from the Koha application context.
61 If the MARC field supplied is not a valid heading, undef
62 is returned.
64 =cut
66 sub new_from_bib_field {
67 my $class = shift;
68 my $field = shift;
69 my $marcflavour = @_ ? shift : C4::Context->preference('marcflavour');
71 my $marc_handler = _marc_format_handler($marcflavour);
73 my $tag = $field->tag();
74 return unless $marc_handler->valid_bib_heading_tag($tag);
75 my $self = {};
77 ($self->{'auth_type'}, $self->{'subject_added_entry'}, $self->{'series_added_entry'}, $self->{'main_entry'},
78 $self->{'thesaurus'}, $self->{'search_form'}, $self->{'display_form'}) =
79 $marc_handler->parse_heading($field);
81 bless $self, $class;
82 return $self;
85 =head2 display_form
87 my $display = $heading->display_form();
89 Return the "canonical" display form of the heading.
91 =cut
93 sub display_form {
94 my $self = shift;
95 return $self->{'display_form'};
98 =head2 authorities
100 my $authorities = $heading->authorities;
102 Return a list of authority records for this
103 heading.
105 =cut
107 sub authorities {
108 my $self = shift;
109 my $query = qq(Match-heading,do-not-truncate,ext="$self->{'search_form'}");
110 $query .= $self->_query_limiters();
111 require C4::Search;
112 my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
113 if (defined $error) {
114 carp "Error:$error from search $query";
116 return $results;
119 =head2 preferred_authorities
121 my $preferred_authorities = $heading->preferred_authorities;
123 Return a list of authority records for headings
124 that are a preferred form of the heading.
126 =cut
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 require C4::Search;
133 my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, undef, undef, [ "authorityserver" ] );
134 if (defined $error) {
135 carp "Error:$error from search $query";
137 return $results;
140 =head1 INTERNAL METHODS
142 =head2 _query_limiters
144 =cut
146 sub _query_limiters {
147 my $self = shift;
149 my $limiters = " AND at='$self->{'auth_type'}'";
150 if ($self->{'subject_added_entry'}) {
151 $limiters .= " AND Heading-use-subject-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
152 $limiters .= " AND Subject-heading-thesaurus=$self->{'thesaurus'}";
154 if ($self->{'series_added_entry'}) {
155 $limiters .= " AND Heading-use-series-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
157 if (not $self->{'subject_added_entry'} and not $self->{'series_added_entry'}) {
158 $limiters .= " AND Heading-use-main-or-added-entry=a" # FIXME -- is this properly in C4::Heading::MARC21?
160 return $limiters;
163 =head1 INTERNAL FUNCTIONS
165 =head2 _marc_format_handler
167 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
168 depending on the selected MARC flavour.
170 =cut
172 sub _marc_format_handler {
173 my $marcflavour = shift;
175 if ($marcflavour eq 'UNIMARC') {
176 return C4::Heading::UNIMARC->new();
177 } else {
178 return C4::Heading::MARC21->new();
183 =head1 AUTHOR
185 Koha Development Team <http://koha-community.org/>
187 Galen Charlton <galen.charlton@liblime.com>
189 =cut