Merge remote-tracking branch 'origin/new/bug_7310'
[koha.git] / svc / bib
1 #!/usr/bin/perl
2
3 # Copyright 2007 LibLime
4 # Copyright 2012 software.coop and MJ Ray
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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.
20 #
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use C4::Auth qw/check_api_auth/;
27 use C4::Biblio;
28 use C4::Items;
29 use XML::Simple;
30
31 my $query = new CGI;
32 binmode STDOUT, ':encoding(UTF-8)';
33
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;
39 }
40
41 # do initial validation
42 my $path_info = $query->path_info();
43
44 my $biblionumber = undef;
45 if ($path_info =~ m!^/(\d+)$!) {
46     $biblionumber = $1;
47 } else {
48     print $query->header(-type => 'text/xml', -status => '400 Bad Request');
49 }
50
51 # are we retrieving or updating a bib?
52 if ($query->request_method eq "GET") {
53     fetch_bib($query, $biblionumber);
54 } else {
55     update_bib($query, $biblionumber);
56 }
57
58 exit 0;
59
60 sub fetch_bib {
61     my $query = shift;
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();
67     } else {
68         print $query->header(-type => 'text/xml', -status => '404 Not Found');
69     }
70 }
71
72 sub update_bib {
73     my $query = shift;
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');
78         return;
79     }
80
81     my $result = {};
82     my $inxml = $query->param('POSTDATA');
83     print $query->header(-type => 'text/xml');
84
85     my $record = eval {MARC::Record::new_from_xml( $inxml, "utf8", C4::Context->preference('marcflavour'))};
86     my $do_not_escape = 0;
87     if ($@) {
88         $result->{'status'} = "failed";
89         $result->{'error'} = $@;
90     } else {
91         my $fullrecord = $record->clone();
92         my ( $itemtag, $itemsubfield ) =
93           GetMarcFromKohaField( "items.itemnumber", '' );
94
95         # delete any item tags
96         foreach my $field ( $record->field($itemtag) ) {
97             $record->delete_field($field);
98         }
99
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) );
106             }
107         }
108
109         ModBiblio( $record, $biblionumber, '' );
110         my $new_record =
111           GetMarcBiblio( $biblionumber, $query->url_param('items') );
112
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;
118         $do_not_escape = 1;
119     }
120    
121     print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape); 
122 }