3 # Copyright 2007 LibLime
5 # This file is part of Koha.
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.
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.
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>.
24 use C4::Auth qw/check_api_auth/;
33 binmode STDOUT, ':encoding(UTF-8)';
35 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
36 unless ($status eq "ok") {
37 print $query->header(-type => 'text/xml', -status => '403 Forbidden');
38 print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
42 if ($query->request_method eq "POST") {
45 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
54 my $inxml = $query->param('POSTDATA');
55 my $frameworkcode = $query->url_param('frameworkcode') // '';
56 print $query->header(-type => 'text/xml', -charset => 'utf-8');
58 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
59 my $record = eval {MARC::Record::new_from_xml( $inxml, "UTF-8", $marcflavour)};
60 my $do_not_escape = 0;
62 $result->{'status'} = "failed";
63 $result->{'error'} = $@;
66 if ($record->encoding() eq 'MARC-8') {
67 my ($guessed_charset, $charset_errors);
68 ($record, $guessed_charset, $charset_errors) = C4::Charset::MarcToUTF8Record($record, $marcflavour);
71 my $fullrecord = $record->clone();
73 # delete any item tags
74 my ( $itemtag, $itemsubfield ) =
75 C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
76 foreach my $field ( $record->field($itemtag) ) {
77 $record->delete_field($field);
79 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, $frameworkcode );
80 my $biblio = Koha::Biblios->find( $biblionumber );
81 my $new_record = $biblio->metadata->record;
82 if ( $query->url_param('items') ) {
83 foreach my $field ( $fullrecord->field($itemtag) ) {
84 my $one_item_record = $new_record->clone();
85 $one_item_record->add_fields($field);
86 C4::Items::AddItemFromMarc( $one_item_record, $biblionumber );
90 $biblio = Koha::Biblios->find( $biblionumber );
91 $new_record = $biblio->metadata->record({ embed_items => scalar $query->url_param('items') });
92 $result->{'status'} = "ok";
93 $result->{'biblionumber'} = $biblionumber;
94 my $xml = $new_record->as_xml_record();
95 $xml =~ s/<\?xml.*?\?>//i;
96 $result->{'marcxml'} = $xml;
100 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape);