Bug 9573: Lost items report - add KohaTable to itemlost
[koha.git] / svc / new_bib
blobd53f405a9fbcdba047332d246378ff84b93ab8ca
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
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>.
21 use Modern::Perl;
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;
30 my $query = new CGI;
31 binmode STDOUT, ':encoding(UTF-8)';
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;
40 if ($query->request_method eq "POST") {
41 add_bib($query);
42 } else {
43 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
46 exit 0;
48 sub add_bib {
49 my $query = shift;
51 my $result = {};
52 my $inxml = $query->param('POSTDATA');
53 print $query->header(-type => 'text/xml', -charset => 'utf-8');
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);
68 my $fullrecord = $record->clone();
70 # delete any item tags
71 my ( $itemtag, $itemsubfield ) =
72 GetMarcFromKohaField( "items.itemnumber", '' );
73 foreach my $field ( $record->field($itemtag) ) {
74 $record->delete_field($field);
76 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
77 my $new_record = GetMarcBiblio({ biblionumber => $biblionumber });
78 if ( $query->url_param('items') ) {
79 foreach my $field ( $fullrecord->field($itemtag) ) {
80 my $one_item_record = $new_record->clone();
81 $one_item_record->add_fields($field);
82 AddItemFromMarc( $one_item_record, $biblionumber );
86 $new_record = GetMarcBiblio({
87 biblionumber => $biblionumber,
88 embed_items => scalar $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);