Bug 35418: Fix _get_usage_months
[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::Charset qw( StripNonXmlChars );
25 use C4::Items qw( GetMarcItem );
26 use Koha::Database;
27 use Koha::Exceptions::Metadata;
28
29 use base qw(Koha::Object);
30
31 =head1 NAME
32
33 Koha::Metadata - Koha Metadata Object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =cut
40
41 =head3 record
42
43 my $record = $metadata->record;
44
45 Returns an object representing the metadata record. The expected record type
46 corresponds to this table:
47
48     -------------------------------
49     | format     | object type    |
50     -------------------------------
51     | marcxml    | MARC::Record   |
52     -------------------------------
53
54     $record = $biblio->metadata->record({
55         {
56             embed_items => 0|1
57             itemnumbers => $itemnumbers,
58             opac        => $opac
59         }
60     );
61
62     Koha::Biblio::Metadata::record(
63         {
64             record       => $record,
65             embed_items  => 1,
66             biblionumber => $biblionumber,
67             itemnumbers  => $itemnumbers,
68             opac         => $opac
69         }
70     );
71
72 Given a MARC::Record object containing a bib record,
73 modify it to include the items attached to it as 9XX
74 per the bib's MARC framework.
75 if $itemnumbers is defined, only specified itemnumbers are embedded.
76
77 If $opac is true, then opac-relevant suppressions are included.
78
79 If opac filtering will be done, patron should be passed to properly
80 override if necessary.
81
82
83 =head4 Error handling
84
85 =over
86
87 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
88
89 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
90
91 =back
92
93 =cut
94
95 sub record {
96
97     my ($self, $params) = @_;
98
99     my $record = $params->{record};
100     my $embed_items = $params->{embed_items};
101     my $format = blessed($self) ? $self->format : $params->{format};
102     $format ||= 'marcxml';
103
104     if ( !$record && !blessed($self) ) {
105         Koha::Exceptions::Metadata->throw(
106             'Koha::Biblio::Metadata->record must be called on an instantiated object or like a class method with a record passed in parameter'
107         );
108     }
109
110     if ( $format eq 'marcxml' ) {
111         $record ||= eval { MARC::Record::new_from_xml( $self->metadata, 'UTF-8', $self->schema ); };
112         my $marcxml_error = $@;
113         chomp $marcxml_error;
114         unless ($record) {
115             warn $marcxml_error;
116             Koha::Exceptions::Metadata::Invalid->throw(
117                 id             => $self->id,
118                 biblionumber   => $self->biblionumber,
119                 format         => $self->format,
120                 schema         => $self->schema,
121                 decoding_error => $marcxml_error,
122             );
123         }
124     }
125     else {
126         Koha::Exceptions::Metadata->throw(
127             'Koha::Biblio::Metadata->record called on unhandled format: ' . $format );
128     }
129
130     if ( $embed_items ) {
131         $self->_embed_items({ %$params, format => $format, record => $record });
132     }
133
134     return $record;
135 }
136
137 =head3 record_strip_nonxml
138
139 my $record = $metadata->record_strip_nonxml;
140
141 This subroutine is intended for cases where we encounter a record that cannot be parsed, but want
142 to make a good effort to present the record (for harvesting, deletion, editing) rather than throwing
143 an exception
144
145 Will return undef if the record cannot be built
146
147 =cut
148
149 sub record_strip_nonxml {
150
151     my ( $self, $params ) = @_;
152     $params //= {};
153
154     my $record;
155     my $marcxml_error;
156
157     eval {
158         $record = MARC::Record->new_from_xml(
159             StripNonXmlChars( $self->metadata ), 'UTF-8',
160             $self->schema
161         );
162     };
163     if ($@) {
164         $marcxml_error = $@;
165         chomp $marcxml_error;
166         warn $marcxml_error;
167         return;
168     }
169
170     return $self->record( { %$params, record => $record } );
171 }
172
173 =head2 Internal methods
174
175 =head3 _embed_items
176
177 =cut
178
179 sub _embed_items {
180     my ( $self, $params ) = @_;
181
182     my $record       = $params->{record};
183     my $format       = $params->{format};
184     my $biblionumber = $params->{biblionumber} || $self->biblionumber;
185     my $itemnumbers = $params->{itemnumbers} // [];
186     my $patron      = $params->{patron};
187     my $opac        = $params->{opac};
188
189     if ( $format eq 'marcxml' ) {
190
191         # First remove the existing items from the MARC record
192         my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
193         foreach my $field ( $record->field($itemtag) ) {
194             $record->delete_field($field);
195         }
196
197         my $biblio = Koha::Biblios->find($biblionumber);
198
199         my $items = $biblio->items;
200         if ( @$itemnumbers ) {
201             $items = $items->search({ itemnumber => { -in => $itemnumbers } });
202         }
203         if ( $opac ) {
204             $items = $items->filter_by_visible_in_opac({ patron => $patron });
205         }
206         my @itemnumbers = $items->get_column('itemnumber');
207         my @item_fields;
208         for my $itemnumber ( @itemnumbers ) {
209             my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
210             push @item_fields, $item_marc->field($itemtag);
211         }
212         $record->insert_fields_ordered( reverse @item_fields );
213             # insert_fields_ordered with the reverse keeps 952s in right order
214
215     }
216     else {
217         Koha::Exceptions::Metadata->throw(
218             'Koha::Biblio::Metadata->embed_item called on unhandled format: ' . $format );
219     }
220
221     return $record;
222 }
223
224
225 =head3 _type
226
227 =cut
228
229 sub _type {
230     return 'BiblioMetadata';
231 }
232
233 1;