Bug 33568: Restore host records
[koha.git] / svc / new_bib
1 #!/usr/bin/perl
2
3 # Copyright 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19 #
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth qw/check_api_auth/;
25 use C4::Biblio;
26 use C4::Items;
27 use XML::Simple;
28 use C4::Charset;
29
30 use Koha::Biblios;
31
32 my $query = CGI->new;
33 binmode STDOUT, ':encoding(UTF-8)';
34
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);
39     exit 0;
40 }
41
42 if ($query->request_method eq "POST") {
43     add_bib($query);
44 } else {
45     print $query->header(-type => 'text/xml', -status => '400 Bad Request');
46 }
47
48 exit 0;
49
50 sub add_bib {
51     my $query = shift;
52
53     my $result = {};
54     my $inxml = $query->param('POSTDATA');
55     my $frameworkcode = $query->url_param('frameworkcode') // '';
56     print $query->header(-type => 'text/xml', -charset => 'utf-8');
57
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;
61     if ($@) {
62         $result->{'status'} = "failed";
63         $result->{'error'} = $@;
64     } else {
65         # fix character set
66         if ($record->encoding() eq 'MARC-8') {
67             my ($guessed_charset, $charset_errors);
68             ($record, $guessed_charset, $charset_errors) = C4::Charset::MarcToUTF8Record($record, $marcflavour);
69         }
70
71         my $fullrecord = $record->clone();
72
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);
78         }
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 );
87             }
88         }
89
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;
97         $do_not_escape = 1;
98     }
99    
100     print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape); 
101 }