Bug 29754: Don't include patron when fetching guarantees fines
[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
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.
11 #
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.
16 #
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>.
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::MetadataRecord::Authority;
37 use C4::Biblio qw/GetMarcFromKohaField/;
38
39 use base qw(Koha::RecordProcessor::Base);
40 our $NAME = 'EmbedSeeFromHeadings';
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, $self->_processrecord($thisrec);
63         }
64         $newrecord = \@recarray;
65     } elsif (ref $record eq 'MARC::Record') {
66         $newrecord = $self->_processrecord($record);
67     }
68
69     return $newrecord;
70 }
71
72 sub _processrecord {
73     my ($self, $record) = @_;
74
75     $record->append_fields($self->fields($record));
76
77     return $record;
78 }
79
80 =head2 fields
81
82     my @fields = $filter->fields($record);
83
84 Retrieve the fields that would be embedded if the record were processed by the filter.
85 Used during Elasticsearch indexing to give special treatment to these field (i.e. don't
86 include in facets, sorting, or suggestion fields)
87
88 =cut
89 sub fields {
90     my ($self, $record) = @_;
91
92     my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
93     $item_tag ||= '';
94
95     my @newfields;
96     foreach my $field ( $record->fields() ) {
97         next if $field->is_control_field();
98         next if $field->tag() eq $item_tag;
99         my $authid = $field->subfield('9');
100
101         next unless $authid;
102
103         my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
104         next unless $authority;
105         my $auth_marc = $authority->record;
106         my @seefrom = $auth_marc->field('4..');
107         foreach my $authfield (@seefrom) {
108             my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
109             next if MARC::Field->is_controlfield_tag($tag);
110             my $newfield = MARC::Field->new($tag,
111                     'z',
112                     $authfield->indicator(2) || ' ',
113                     '9' => '1');
114             foreach my $sub ($authfield->subfields()) {
115                 my ($code,$val) = @$sub;
116                 $newfield->add_subfields( $code => $val );
117             }
118             $newfield->delete_subfield( code => '9' );
119             push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
120         }
121     }
122
123     return @newfields;
124 }
125
126 1;