Bug 15288: (followup) Better wording for OPAC error page
[koha.git] / C4 / Heading / UNIMARC.pm
blobe5f5bc4b66fa0bda9b751524c4a8193f806728b0
1 package C4::Heading::UNIMARC;
3 # Copyright (C) 2011 C & P Bibliography Services
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>.
20 use 5.010;
21 use strict;
22 use warnings;
23 use MARC::Record;
24 use MARC::Field;
25 use C4::Context;
27 our $VERSION = 3.07.00.049;
29 =head1 NAME
31 C4::Heading::UNIMARC
33 =head1 SYNOPSIS
35 use C4::Heading::UNIMARC;
37 =head1 DESCRIPTION
39 This is an internal helper class used by
40 C<C4::Heading> to parse headings data from
41 UNIMARC records. Object of this type
42 do not carry data, instead, they only
43 dispatch functions.
45 =head1 DATA STRUCTURES
47 FIXME - this should be moved to a configuration file.
49 =head2 subdivisions
51 =cut
53 my %subdivisions = (
54 'j' => 'formsubdiv',
55 'x' => 'generalsubdiv',
56 'y' => 'chronologicalsubdiv',
57 'z' => 'geographicsubdiv',
60 my $bib_heading_fields;
62 =head1 METHODS
64 =head2 new
66 my $marc_handler = C4::Heading::UNIMARC->new();
68 =cut
70 sub new {
71 my $class = shift;
73 my $dbh = C4::Context->dbh;
74 my $sth = $dbh->prepare(
75 "SELECT tagfield, authtypecode
76 FROM marc_subfield_structure
77 WHERE frameworkcode = '' AND authtypecode <> ''"
79 $sth->execute();
80 $bib_heading_fields = {};
81 while ( my ( $tag, $auth_type ) = $sth->fetchrow ) {
82 $bib_heading_fields->{$tag} = {
83 auth_type => $auth_type,
84 subfields => 'abcdefghjklmnopqrstvxyz',
88 return bless {}, $class;
91 =head2 valid_bib_heading_tag
93 =cut
95 sub valid_bib_heading_tag {
96 my ( $self, $tag ) = @_;
97 return $bib_heading_fields->{$tag};
100 =head2 parse_heading
102 =cut
104 sub parse_heading {
105 my ( $self, $field ) = @_;
107 my $tag = $field->tag;
108 my $field_info = $bib_heading_fields->{$tag};
109 my $auth_type = $field_info->{'auth_type'};
110 my $search_heading =
111 _get_search_heading( $field, $field_info->{'subfields'} );
112 my $display_heading =
113 _get_display_heading( $field, $field_info->{'subfields'} );
115 return ( $auth_type, undef, $search_heading, $display_heading, 'exact' );
118 =head1 INTERNAL FUNCTIONS
120 =head2 _get_subject_thesaurus
122 =cut
124 sub _get_subject_thesaurus {
125 my $field = shift;
127 my $thesaurus = "notdefined";
128 my $sf2 = $field->subfield('2');
129 $thesaurus = $sf2 if defined($sf2);
131 return $thesaurus;
134 =head2 _get_search_heading
136 =cut
138 sub _get_search_heading {
139 my $field = shift;
140 my $subfields = shift;
142 my $heading = "";
143 my @subfields = $field->subfields();
144 my $first = 1;
145 for ( my $i = 0 ; $i <= $#subfields ; $i++ ) {
146 my $code = $subfields[$i]->[0];
147 my $code_re = quotemeta $code;
148 my $value = $subfields[$i]->[1];
149 $value =~ s/[-,.:=;!%\/]*$//;
150 next unless $subfields =~ qr/$code_re/;
151 if ($first) {
152 $first = 0;
153 $heading = $value;
155 else {
156 $heading .= " $value";
160 # remove characters that are part of CCL syntax
161 $heading =~ s/[)(=]//g;
163 return $heading;
166 =head2 _get_display_heading
168 =cut
170 sub _get_display_heading {
171 my $field = shift;
172 my $subfields = shift;
174 my $heading = "";
175 my @subfields = $field->subfields();
176 my $first = 1;
177 for ( my $i = 0 ; $i <= $#subfields ; $i++ ) {
178 my $code = $subfields[$i]->[0];
179 my $code_re = quotemeta $code;
180 my $value = $subfields[$i]->[1];
181 next unless $subfields =~ qr/$code_re/;
182 if ($first) {
183 $first = 0;
184 $heading = $value;
186 else {
187 if ( exists $subdivisions{$code} ) {
188 $heading .= "--$value";
190 else {
191 $heading .= " $value";
195 return $heading;
198 =head1 AUTHOR
200 Koha Development Team <http://koha-community.org/>
202 Jared Camins-Esakov <jcamins@cpbibliography.com>
204 =cut