File: | C4/Heading.pm |
Coverage: | 26.4% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 4 4 4 | 45416 64 155 | use strict; | |||
21 | #use warnings; FIXME - Bug 2505 | |||||
22 | 4 4 4 | 481 16703 690 | use MARC::Record; | |||
23 | 4 4 4 | 62 39 182 | use MARC::Field; | |||
24 | 4 4 4 | 452 24 124 | use C4::Context; | |||
25 | 4 4 4 | 894 7 103 | use C4::Heading::MARC21; | |||
26 | 4 4 4 | 21 4 2531 | use Carp; | |||
27 | ||||||
28 | our $VERSION = 3.00; | |||||
29 | ||||||
30 - 64 | =head1 NAME C4::Heading =head1 SYNOPSIS use C4::Heading; my $heading = C4::Heading->new_from_bib_field($field); my $thesaurus = $heading->thesaurus(); my $type = $heading->type(); my $display_heading = $heading->display(); my $search_string = $heading->search_string(); =head1 DESCRIPTION C<C4::Heading> implements a simple class to representing headings found in bibliographic and authority records. =head1 METHODS =head2 new_from_bib_field my $heading = C4::Heading->new_from_bib_field($field[, $marc_flavour]); Given a C<MARC::Field> object containing a heading from a bib record, create a C<C4::Heading> object. The optional second parameter is the MARC flavour (i.e., MARC21 or UNIMARC); if this parameter is not supplied, it is taken from the Koha application context. If the MARC field supplied is not a valid heading, undef is returned. =cut | |||||
65 | ||||||
66 | sub new_from_bib_field { | |||||
67 | 0 | my $class = shift; | ||||
68 | 0 | my $field = shift; | ||||
69 | 0 | my $marcflavour = @_ ? shift : C4::Context->preference('marcflavour'); | ||||
70 | ||||||
71 | 0 | my $marc_handler = _marc_format_handler($marcflavour); | ||||
72 | ||||||
73 | 0 | my $tag = $field->tag(); | ||||
74 | 0 | return unless $marc_handler->valid_bib_heading_tag($tag); | ||||
75 | 0 | my $self = {}; | ||||
76 | ||||||
77 | 0 | ($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); | |||||
80 | ||||||
81 | 0 | bless $self, $class; | ||||
82 | 0 | return $self; | ||||
83 | } | |||||
84 | ||||||
85 - 91 | =head2 display_form my $display = $heading->display_form(); Return the "canonical" display form of the heading. =cut | |||||
92 | ||||||
93 | sub display_form { | |||||
94 | 0 | my $self = shift; | ||||
95 | 0 | return $self->{'display_form'}; | ||||
96 | } | |||||
97 | ||||||
98 - 105 | =head2 authorities my $authorities = $heading->authorities; Return a list of authority records for this heading. =cut | |||||
106 | ||||||
107 | sub authorities { | |||||
108 | 0 | my $self = shift; | ||||
109 | 0 | my $query = qq(Match-heading,do-not-truncate,ext="$self->{'search_form'}"); | ||||
110 | 0 | $query .= $self->_query_limiters(); | ||||
111 | 0 | require C4::Search; | ||||
112 | 0 | my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, undef, undef, [ "authorityserver" ] ); | ||||
113 | 0 | if (defined $error) { | ||||
114 | 0 | carp "Error:$error from search $query"; | ||||
115 | } | |||||
116 | 0 | return $results; | ||||
117 | } | |||||
118 | ||||||
119 - 126 | =head2 preferred_authorities my $preferred_authorities = $heading->preferred_authorities; Return a list of authority records for headings that are a preferred form of the heading. =cut | |||||
127 | ||||||
128 | sub preferred_authorities { | |||||
129 | 0 | my $self = shift; | ||||
130 | 0 | my $query = "Match-heading-see-from,do-not-truncate,ext='$self->{'search_form'}'"; | ||||
131 | 0 | $query .= $self->_query_limiters(); | ||||
132 | 0 | require C4::Search; | ||||
133 | 0 | my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, undef, undef, [ "authorityserver" ] ); | ||||
134 | 0 | if (defined $error) { | ||||
135 | 0 | carp "Error:$error from search $query"; | ||||
136 | } | |||||
137 | 0 | return $results; | ||||
138 | } | |||||
139 | ||||||
140 - 144 | =head1 INTERNAL METHODS =head2 _query_limiters =cut | |||||
145 | ||||||
146 | sub _query_limiters { | |||||
147 | 0 | my $self = shift; | ||||
148 | ||||||
149 | 0 | my $limiters = " AND at='$self->{'auth_type'}'"; | ||||
150 | 0 | if ($self->{'subject_added_entry'}) { | ||||
151 | 0 | $limiters .= " AND Heading-use-subject-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21? | ||||
152 | 0 | $limiters .= " AND Subject-heading-thesaurus=$self->{'thesaurus'}"; | ||||
153 | } | |||||
154 | 0 | if ($self->{'series_added_entry'}) { | ||||
155 | 0 | $limiters .= " AND Heading-use-series-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21? | ||||
156 | } | |||||
157 | 0 | if (not $self->{'subject_added_entry'} and not $self->{'series_added_entry'}) { | ||||
158 | 0 | $limiters .= " AND Heading-use-main-or-added-entry=a" # FIXME -- is this properly in C4::Heading::MARC21? | ||||
159 | } | |||||
160 | 0 | return $limiters; | ||||
161 | } | |||||
162 | ||||||
163 - 170 | =head1 INTERNAL FUNCTIONS =head2 _marc_format_handler Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object depending on the selected MARC flavour. =cut | |||||
171 | ||||||
172 | sub _marc_format_handler { | |||||
173 | 0 | my $marcflavour = shift; | ||||
174 | ||||||
175 | 0 | if ($marcflavour eq 'UNIMARC') { | ||||
176 | 0 | return C4::Heading::UNIMARC->new(); | ||||
177 | } else { | |||||
178 | 0 | return C4::Heading::MARC21->new(); | ||||
179 | } | |||||
180 | ||||||
181 | } | |||||
182 | ||||||
183 - 189 | =head1 AUTHOR Koha Development Team <http://koha-community.org/> Galen Charlton <galen.charlton@liblime.com> =cut | |||||
190 | ||||||
191 | 1; |