Bug 9580: DBRev 3.19.00.019
[koha.git] / svc / new_bib
blob7ec4d0569023d5f44021fbfdf1e70a14009aedb9
1 #!/usr/bin/perl
3 # Copyright 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 use warnings;
24 use CGI qw ( -utf8 );
25 use C4::Auth qw/check_api_auth/;
26 use C4::Biblio;
27 use C4::Items;
28 use XML::Simple;
29 use C4::Charset;
31 my $query = new CGI;
32 binmode STDOUT, ':encoding(UTF-8)';
34 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
35 unless ($status eq "ok") {
36 print $query->header(-type => 'text/xml', -status => '403 Forbidden');
37 print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
38 exit 0;
41 if ($query->request_method eq "POST") {
42 add_bib($query);
43 } else {
44 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
47 exit 0;
49 sub add_bib {
50 my $query = shift;
52 my $result = {};
53 my $inxml = $query->param('POSTDATA');
54 print $query->header(-type => 'text/xml');
56 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
57 my $record = eval {MARC::Record::new_from_xml( $inxml, "utf8", $marcflavour)};
58 my $do_not_escape = 0;
59 if ($@) {
60 $result->{'status'} = "failed";
61 $result->{'error'} = $@;
62 } else {
63 # fix character set
64 if ($record->encoding() eq 'MARC-8') {
65 my ($guessed_charset, $charset_errors);
66 ($record, $guessed_charset, $charset_errors) = MarcToUTF8Record($record, $marcflavour);
69 my $fullrecord = $record->clone();
71 # delete any item tags
72 my ( $itemtag, $itemsubfield ) =
73 GetMarcFromKohaField( "items.itemnumber", '' );
74 foreach my $field ( $record->field($itemtag) ) {
75 $record->delete_field($field);
77 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
78 my $new_record = GetMarcBiblio($biblionumber);
79 if ( $query->url_param('items') ) {
80 foreach my $field ( $fullrecord->field($itemtag) ) {
81 my $one_item_record = $new_record->clone();
82 $one_item_record->add_fields($field);
83 AddItemFromMarc( $one_item_record, $biblionumber );
87 $new_record =
88 GetMarcBiblio( $biblionumber, $query->url_param('items') );
89 $result->{'status'} = "ok";
90 $result->{'biblionumber'} = $biblionumber;
91 my $xml = $new_record->as_xml_record();
92 $xml =~ s/<\?xml.*?\?>//i;
93 $result->{'marcxml'} = $xml;
94 $do_not_escape = 1;
97 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape);