Bug 29697: Fix t/db_dependent/Exporter/Record.t
[koha.git] / Koha / Biblio / Metadata.pm
1 package Koha::Biblio::Metadata;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use MARC::File::XML;
21 use Scalar::Util qw( blessed );
22
23 use C4::Biblio qw( GetMarcFromKohaField );
24 use C4::Items qw( GetMarcItem );
25 use Koha::Database;
26 use Koha::Exceptions::Metadata;
27
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::Metadata - Koha Metadata Object class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =cut
39
40 =head3 record
41
42 my $record = $metadata->record;
43
44 Returns an object representing the metadata record. The expected record type
45 corresponds to this table:
46
47     -------------------------------
48     | format     | object type    |
49     -------------------------------
50     | marcxml    | MARC::Record   |
51     -------------------------------
52
53     $record = $biblio->metadata->record({
54         {
55             embed_items => 0|1
56             itemnumbers => $itemnumbers,
57             opac        => $opac
58         }
59     );
60
61     Koha::Biblio::Metadata::record(
62         {
63             record       => $record,
64             embed_items  => 1,
65             biblionumber => $biblionumber,
66             itemnumbers  => $itemnumbers,
67             opac         => $opac
68         }
69     );
70
71 Given a MARC::Record object containing a bib record,
72 modify it to include the items attached to it as 9XX
73 per the bib's MARC framework.
74 if $itemnumbers is defined, only specified itemnumbers are embedded.
75
76 If $opac is true, then opac-relevant suppressions are included.
77
78 If opac filtering will be done, patron should be passed to properly
79 override if necessary.
80
81
82 =head4 Error handling
83
84 =over
85
86 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
87
88 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
89
90 =back
91
92 =cut
93
94 sub record {
95
96     my ($self, $params) = @_;
97
98     my $record = $params->{record};
99     my $embed_items = $params->{embed_items};
100     my $format = blessed($self) ? $self->format : $params->{format};
101     $format ||= 'marcxml';
102
103     if ( !$record && !blessed($self) ) {
104         Koha::Exceptions::Metadata->throw(
105             'Koha::Biblio::Metadata->record must be called on an instantiated object or like a class method with a record passed in parameter'
106         );
107     }
108
109     if ( $format eq 'marcxml' ) {
110         $record ||= eval { MARC::Record::new_from_xml( $self->metadata, 'UTF-8', $self->schema ); };
111         my $marcxml_error = $@;
112         chomp $marcxml_error;
113         unless ($record) {
114             warn $marcxml_error;
115             Koha::Exceptions::Metadata::Invalid->throw(
116                 id             => $self->id,
117                 biblionumber   => $self->biblionumber,
118                 format         => $self->format,
119                 schema         => $self->schema,
120                 decoding_error => $marcxml_error,
121             );
122         }
123     }
124     else {
125         Koha::Exceptions::Metadata->throw(
126             'Koha::Biblio::Metadata->record called on unhandled format: ' . $format );
127     }
128
129     if ( $embed_items ) {
130         $self->_embed_items({ %$params, format => $format, record => $record });
131     }
132
133     return $record;
134 }
135
136 =head2 Internal methods
137
138 =head3 _embed_items
139
140 =cut
141
142 sub _embed_items {
143     my ( $self, $params ) = @_;
144
145     my $record       = $params->{record};
146     my $format       = $params->{format};
147     my $biblionumber = $params->{biblionumber} || $self->biblionumber;
148     my $itemnumbers = $params->{itemnumbers} // [];
149     my $patron      = $params->{patron};
150     my $opac        = $params->{opac};
151
152     if ( $format eq 'marcxml' ) {
153
154         # First remove the existing items from the MARC record
155         my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
156         foreach my $field ( $record->field($itemtag) ) {
157             $record->delete_field($field);
158         }
159
160         my $biblio = Koha::Biblios->find($biblionumber);
161
162         my $items = $biblio->items;
163         if ( @$itemnumbers ) {
164             $items = $items->search({ itemnumber => { -in => $itemnumbers } });
165         }
166         if ( $opac ) {
167             $items = $items->filter_by_visible_in_opac({ patron => $patron });
168         }
169         my @itemnumbers = $items->get_column('itemnumber');
170         my @item_fields;
171         for my $itemnumber ( @itemnumbers ) {
172             my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
173             push @item_fields, $item_marc->field($itemtag);
174         }
175         $record->append_fields(@item_fields);
176
177     }
178     else {
179         Koha::Exceptions::Metadata->throw(
180             'Koha::Biblio::Metadata->embed_item called on unhandled format: ' . $format );
181     }
182
183     return $record;
184 }
185
186
187 =head3 _type
188
189 =cut
190
191 sub _type {
192     return 'BiblioMetadata';
193 }
194
195 1;