Bug 5453 Move declarations out of conditionals in opac
[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 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.
11 #
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.
15 #
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.
19 #
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth qw/check_api_auth/;
26 use C4::Biblio;
27 use XML::Simple;
28 use C4::Charset;
29
30 my $query = new CGI;
31 binmode STDOUT, ":utf8";
32
33 my ($status, $cookie, $sessionID) = check_api_auth($query, { editcatalogue => 'edit_catalogue'} );
34 unless ($status eq "ok") {
35     print $query->header(-type => 'text/xml', -status => '403 Forbidden');
36     print XMLout({ auth_status => $status }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
37     exit 0;
38 }
39
40 if ($query->request_method eq "POST") {
41     add_bib($query);
42 } else {
43     print $query->header(-type => 'text/xml', -status => '400 Bad Request');
44 }
45
46 exit 0;
47
48 sub add_bib {
49     my $query = shift;
50
51     my $result = {};
52     my $inxml = $query->param('POSTDATA');
53     print $query->header(-type => 'text/xml');
54
55     my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
56     my $record = eval {MARC::Record::new_from_xml( $inxml, "utf8", $marcflavour)};
57     my $do_not_escape = 0;
58     if ($@) {
59         $result->{'status'} = "failed";
60         $result->{'error'} = $@;
61     } else {
62         # fix character set
63         if ($record->encoding() eq 'MARC-8') {
64             my ($guessed_charset, $charset_errors);
65             ($record, $guessed_charset, $charset_errors) = MarcToUTF8Record($record, $marcflavour);
66         }
67
68         # delete any item tags
69         my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber", '');
70         foreach my $field ($record->field($itemtag)) {
71             $record->delete_field($field);
72         }
73         my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
74         my $new_record = GetMarcBiblio($biblionumber);
75         $result->{'status'} = "ok";
76         $result->{'biblionumber'} = $biblionumber;
77         my $xml = $new_record->as_xml_record();
78         $xml =~ s/<\?xml.*?\?>//i;
79         $result->{'marcxml'} =  $xml;
80         $do_not_escape = 1;
81     }
82    
83     print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape); 
84 }