3 # Copyright 2007 LibLime
4 # Copyright 2012 software.coop and MJ Ray
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use C4::Auth qw/check_api_auth/;
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);
41 # do initial validation
42 my $path_info = $query->path_info();
44 my $biblionumber = undef;
45 if ($path_info =~ m!^/(\d+)$!) {
48 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
51 # are we retrieving or updating a bib?
52 if ($query->request_method eq "GET") {
53 fetch_bib($query, $biblionumber);
55 update_bib($query, $biblionumber);
62 my $biblionumber = shift;
63 my $record = GetMarcBiblio( $biblionumber, $query->url_param('items') );
64 if (defined $record) {
65 print $query->header(-type => 'text/xml');
66 print $record->as_xml_record();
68 print $query->header(-type => 'text/xml', -status => '404 Not Found');
74 my $biblionumber = shift;
75 my $old_record = GetMarcBiblio($biblionumber);
76 unless (defined $old_record) {
77 print $query->header(-type => 'text/xml', -status => '404 Not Found');
82 my $inxml = $query->param('POSTDATA');
83 print $query->header(-type => 'text/xml');
85 my $record = eval {MARC::Record::new_from_xml( $inxml, "utf8", C4::Context->preference('marcflavour'))};
86 my $do_not_escape = 0;
88 $result->{'status'} = "failed";
89 $result->{'error'} = $@;
91 my $fullrecord = $record->clone();
92 my ( $itemtag, $itemsubfield ) =
93 GetMarcFromKohaField( "items.itemnumber", '' );
95 # delete any item tags
96 foreach my $field ( $record->field($itemtag) ) {
97 $record->delete_field($field);
100 if ( $query->url_param('items') ) {
101 foreach my $field ( $fullrecord->field($itemtag) ) {
102 my $one_item_record = $record->clone();
103 $one_item_record->add_fields($field);
104 ModItemFromMarc( $one_item_record, $biblionumber,
105 $field->subfield($itemsubfield) );
109 ModBiblio( $record, $biblionumber, '' );
111 GetMarcBiblio( $biblionumber, $query->url_param('items') );
113 $result->{'status'} = "ok";
114 $result->{'biblionumber'} = $biblionumber;
115 my $xml = $new_record->as_xml_record();
116 $xml =~ s/<\?xml.*?\?>//i;
117 $result->{'marcxml'} = $xml;
121 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape);