1 package Koha::Biblio::Metadata;
3 # This file is part of Koha.
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.
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.
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>.
21 use Scalar::Util qw( blessed );
23 use C4::Biblio qw( GetMarcFromKohaField );
24 use C4::Items qw( GetMarcItem );
26 use Koha::Exceptions::Metadata;
28 use base qw(Koha::Object);
32 Koha::Metadata - Koha Metadata Object class
42 my $record = $metadata->record;
44 Returns an object representing the metadata record. The expected record type
45 corresponds to this table:
47 -------------------------------
48 | format | object type |
49 -------------------------------
50 | marcxml | MARC::Record |
51 -------------------------------
53 $record = $biblio->metadata->record({
56 itemnumbers => $itemnumbers,
61 Koha::Biblio::Metadata::record(
65 biblionumber => $biblionumber,
66 itemnumbers => $itemnumbers,
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.
76 If $opac is true, then opac-relevant suppressions are included.
78 If opac filtering will be done, patron should be passed to properly
79 override if necessary.
86 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
88 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
96 my ($self, $params) = @_;
98 my $record = $params->{record};
99 my $embed_items = $params->{embed_items};
100 my $format = blessed($self) ? $self->format : $params->{format};
101 $format ||= 'marcxml';
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'
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;
115 Koha::Exceptions::Metadata::Invalid->throw(
117 biblionumber => $self->biblionumber,
118 format => $self->format,
119 schema => $self->schema,
120 decoding_error => $marcxml_error,
125 Koha::Exceptions::Metadata->throw(
126 'Koha::Biblio::Metadata->record called on unhandled format: ' . $format );
129 if ( $embed_items ) {
130 $self->_embed_items({ %$params, format => $format, record => $record });
136 =head2 Internal methods
143 my ( $self, $params ) = @_;
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};
152 if ( $format eq 'marcxml' ) {
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);
160 my $biblio = Koha::Biblios->find($biblionumber);
162 my $items = $biblio->items;
163 if ( @$itemnumbers ) {
164 $items = $items->search({ itemnumber => { -in => $itemnumbers } });
167 $items = $items->filter_by_visible_in_opac({ patron => $patron });
169 my @itemnumbers = $items->get_column('itemnumber');
171 for my $itemnumber ( @itemnumbers ) {
172 my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
173 push @item_fields, $item_marc->field($itemtag);
175 $record->insert_fields_ordered(@item_fields);
179 Koha::Exceptions::Metadata->throw(
180 'Koha::Biblio::Metadata->embed_item called on unhandled format: ' . $format );
192 return 'BiblioMetadata';