Bug 29764: Fix incorrect EmbedItems RecordProcessor filter POD
[koha.git] / Koha / Filter / MARC / EmbedItems.pm
1 package Koha::Filter::MARC::EmbedItems;
2
3 # Copyright 2019  Theke Solutions
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::EmbedItems - Appends item information on MARC::Record objects.
23
24 =head1 SYNOPSIS
25
26 my $biblio = Koha::Biblios->find(
27     $biblio_id,
28     { prefetch => [ items, metadata ] }
29 );
30
31 my $patron = Koha::Patrons->find($loggedinuser);
32
33 my $record = $biblio->metadata->record;
34 my @items  = $biblio->items->filter_by_visible_in_opac({ patron => $patron })->as_list;
35
36 my $record_processor = Koha::RecordProcessor->new(
37     {
38         filters => ['EmbedItems'],
39         options => {
40             interface => 'opac',
41             items     => \@items
42         }
43     }
44 );
45
46 $record_processor->process($record);
47
48 =head1 DESCRIPTION
49
50 Filter to embed items information into MARC::Record objects.
51
52 =cut
53
54 use Modern::Perl;
55
56 use C4::Biblio;
57
58 use base qw(Koha::RecordProcessor::Base);
59 our $NAME = 'EmbedItems';
60
61 =head2 filter
62
63 Embed items into the MARC::Record object.
64
65 =cut
66
67 sub filter {
68     my $self   = shift;
69     my $record = shift;
70
71     return unless defined $record and ref($record) eq 'MARC::Record';
72
73     my $items = $self->{params}->{options}->{items};
74     my $mss   = $self->{params}->{options}->{mss}
75       // C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
76
77     my @item_fields;
78
79     foreach my $item ( @{$items} ) {
80         push @item_fields, $item->as_marc_field( { mss => $mss } );
81     }
82
83     $record->append_fields(@item_fields);
84
85     return $record;
86 }
87
88 1;