Heading.pm 5.21 KB
Newer Older
1 2 3
package C4::Heading;

# Copyright (C) 2008 LibLime
4
#
5 6
# This file is part of Koha.
#
7 8 9 10
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
11
#
12 13 14 15
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
16
#
17 18
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19 20

use strict;
21
use warnings;
22 23 24
use MARC::Record;
use MARC::Field;
use C4::Context;
25
use Module::Load;
26
use Carp;
27 28 29 30 31 32 33 34


=head1 NAME

C4::Heading

=head1 SYNOPSIS

Andrew Elwell's avatar
Andrew Elwell committed
35
 use C4::Heading;
36
 my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
Andrew Elwell's avatar
Andrew Elwell committed
37 38
 my $thesaurus = $heading->thesaurus();
 my $type = $heading->type();
39 40
 my $display_heading = $heading->display_form();
 my $search_form = $heading->search_form();
41 42 43 44 45 46 47 48 49 50

=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

51
  my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
52 53 54 55 56 57 58 59 60 61 62 63 64 65

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

sub new_from_bib_field {
66 67 68 69
    my $class         = shift;
    my $field         = shift;
    my $frameworkcode = shift;
    my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
70 71 72 73

    my $marc_handler = _marc_format_handler($marcflavour);

    my $tag = $field->tag();
74
    return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
75
    my $self = {};
76 77 78 79 80 81 82

    $self->{'field'} = $field;
    (
        $self->{'auth_type'},   $self->{'thesaurus'},
        $self->{'search_form'}, $self->{'display_form'},
        $self->{'match_type'}
    ) = $marc_handler->parse_heading($field);
83 84 85 86 87

    bless $self, $class;
    return $self;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
=head2 auth_type

  my $auth_type = $heading->auth_type();

Return the auth_type of the heading.

=cut

sub auth_type {
    my $self = shift;
    return $self->{'auth_type'};
}

=head2 field

  my $field = $heading->field();

Return the MARC::Field the heading is based on.

=cut

sub field {
    my $self = shift;
    return $self->{'field'};
}

114 115
=head2 display_form

Andrew Elwell's avatar
Andrew Elwell committed
116
  my $display = $heading->display_form();
117 118 119 120 121 122 123 124 125 126

Return the "canonical" display form of the heading.

=cut

sub display_form {
    my $self = shift;
    return $self->{'display_form'};
}

127 128 129 130 131 132 133 134 135 136 137 138 139
=head2 search_form

  my $search_form = $heading->search_form();

Return the "canonical" search form of the heading.

=cut

sub search_form {
    my $self = shift;
    return $self->{'search_form'};
}

140 141
=head2 authorities

142
  my $authorities = $heading->authorities([$skipmetadata]);
143 144

Return a list of authority records for this 
145 146
heading. If passed a true value for $skipmetadata,
SearchAuthorities will return only authids.
147 148 149 150

=cut

sub authorities {
151 152 153
    my $self         = shift;
    my $skipmetadata = shift;
    my ( $results, $total ) = _search( $self, 'match-heading', $skipmetadata );
154 155 156 157 158
    return $results;
}

=head2 preferred_authorities

Andrew Elwell's avatar
Andrew Elwell committed
159
  my $preferred_authorities = $heading->preferred_authorities;
160 161 162 163 164 165 166 167

Return a list of authority records for headings
that are a preferred form of the heading.

=cut

sub preferred_authorities {
    my $self = shift;
168 169
    my $skipmetadata = shift || undef;
    my ( $results, $total ) = _search( 'see-from', $skipmetadata );
170 171 172 173 174
    return $results;
}

=head1 INTERNAL METHODS

175
=head2 _search
176 177 178

=cut

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
sub _search {
    my $self         = shift;
    my $index        = shift || undef;
    my $skipmetadata = shift || undef;
    my @marclist;
    my @and_or;
    my @excluding = [];
    my @operator;
    my @value;

    if ($index) {
        push @marclist, $index;
        push @and_or,   'and';
        push @operator, $self->{'match_type'};
        push @value,    $self->{'search_form'};
194
    }
195 196 197 198 199 200 201 202 203 204 205 206

    #    if ($self->{'thesaurus'}) {
    #        push @marclist, 'thesaurus';
    #        push @and_or, 'and';
    #        push @excluding, '';
    #        push @operator, 'is';
    #        push @value, $self->{'thesaurus'};
    #    }
    require C4::AuthoritiesMarc;
    return C4::AuthoritiesMarc::SearchAuthorities(
        \@marclist, \@and_or, \@excluding, \@operator,
        \@value,    0,        20,          $self->{'auth_type'},
207
        'AuthidAsc',         $skipmetadata
208
    );
209 210 211 212 213 214 215 216 217 218 219 220
}

=head1 INTERNAL FUNCTIONS

=head2 _marc_format_handler

Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
depending on the selected MARC flavour.

=cut

sub _marc_format_handler {
221 222 223 224 225
    my $marcflavour = uc shift;
    $marcflavour = 'MARC21' if ( $marcflavour eq 'NORMARC' );
    my $pname = "C4::Heading::$marcflavour";
    load $pname;
    return $pname->new();
226 227 228 229
}

=head1 AUTHOR

230
Koha Development Team <http://koha-community.org/>
231 232 233 234 235 236

Galen Charlton <galen.charlton@liblime.com>

=cut

1;