Merge remote-tracking branch 'origin/new/bug_8623'
[koha.git] / Koha / Filter / MARC / EmbedSeeFromHeadings.pm
1 package Koha::Filter::MARC::EmbedSeeFromHeadings;
2
3 # Copyright 2012 C & P Bibliography Services
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 =head1 NAME
21
22 Koha::Filter::MARC::EmbedSeeFromHeadings - embeds see from headings into MARC for indexing
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 Filter to embed see from headings into MARC records.
30
31 =cut
32
33 use strict;
34 use warnings;
35 use Carp;
36 use Koha::Authority;
37
38 use base qw(Koha::RecordProcessor::Base);
39 our $NAME = 'EmbedSeeFromHeadings';
40 our $VERSION = '1.0';
41
42 =head2 filter
43
44     my $newrecord = $filter->filter($record);
45     my $newrecords = $filter->filter(\@records);
46
47 Embed see from headings into the specified record(s) and return the result.
48 In order to differentiate added headings from actual headings, a 'z' is
49 put in the first indicator.
50
51 =cut
52 sub filter {
53     my $self = shift;
54     my $record = shift;
55     my $newrecord;
56
57     return unless defined $record;
58
59     if (ref $record eq 'ARRAY') {
60         my @recarray;
61         foreach my $thisrec (@$record) {
62             push @recarray, _processrecord($thisrec);
63         }
64         $newrecord = \@recarray;
65     } elsif (ref $record eq 'MARC::Record') {
66         $newrecord = _processrecord($record);
67     }
68
69     return $newrecord;
70 }
71
72 sub _processrecord {
73     my $record = shift;
74
75     foreach my $field ( $record->fields() ) {
76         next if $field->is_control_field();
77         my $authid = $field->subfield('9');
78
79         next unless $authid;
80
81         my $authority = Koha::Authority->get_from_authid($authid);
82         next unless $authority;
83         my $auth_marc = $authority->record;
84         my @seefrom = $auth_marc->field('4..');
85         my @newfields;
86         foreach my $authfield (@seefrom) {
87             my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
88             my $newfield = MARC::Field->new($tag,
89                     'z',
90                     $authfield->indicator(2) || ' ',
91                     '9' => '1');
92             foreach my $sub ($authfield->subfields()) {
93                 my ($code,$val) = @$sub;
94                 $newfield->add_subfields( $code => $val );
95             }
96             $newfield->delete_subfield( code => '9' );
97             push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
98         }
99         $record->append_fields(@newfields);
100     }
101     return $record;
102 }