Bug 9701: Configure default indicator
[koha.git] / svc / bib
blob36fa461702856e7d68d1fa82158509b30b18eab0
1 #!/usr/bin/perl
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Auth qw/check_api_auth/;
26 use C4::Biblio;
27 use C4::Items;
28 use XML::Simple;
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 # do initial validation
41 my $path_info = $query->path_info();
43 my $biblionumber = undef;
44 if ($path_info =~ m!^/(\d+)$!) {
45 $biblionumber = $1;
46 } else {
47 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
50 # are we retrieving, updating or deleting a bib?
51 if ($query->request_method eq "GET") {
52 fetch_bib($query, $biblionumber);
53 } elsif ($query->request_method eq "POST") {
54 update_bib($query, $biblionumber);
55 } elsif ($query->request_method eq "DELETE") {
56 delete_bib($query, $biblionumber);
57 } else {
58 print $query->header(-type => 'text/xml', -status => '405 Method not allowed');
59 print XMLout({ error => 'Method not allowed' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
60 exit 0;
63 exit 0;
65 sub fetch_bib {
66 my $query = shift;
67 my $biblionumber = shift;
68 my $record = GetMarcBiblio({
69 biblionumber => $biblionumber,
70 embed_items => scalar $query->param('items') });
71 if (defined $record) {
72 print $query->header(-type => 'text/xml',-charset => 'utf-8',);
73 print $record->as_xml_record();
74 } else {
75 print $query->header(-type => 'text/xml', -status => '404 Not Found');
79 sub update_bib {
80 my $query = shift;
81 my $biblionumber = shift;
82 my $old_record = GetMarcBiblio({ biblionumber => $biblionumber });
83 unless (defined $old_record) {
84 print $query->header(-type => 'text/xml', -status => '404 Not Found');
85 return;
88 my $result = {};
89 my $inxml = $query->param('POSTDATA');
90 print $query->header(-type => 'text/xml', -charset => 'utf-8');
92 my $record = eval {MARC::Record::new_from_xml( $inxml, "utf8", C4::Context->preference('marcflavour'))};
93 my $do_not_escape = 0;
94 if ($@) {
95 $result->{'status'} = "failed";
96 $result->{'error'} = $@;
97 } else {
98 my $fullrecord = $record->clone();
99 my ( $itemtag, $itemsubfield ) =
100 GetMarcFromKohaField( "items.itemnumber", '' );
102 # delete any item tags
103 foreach my $field ( $record->field($itemtag) ) {
104 $record->delete_field($field);
107 if ( $query->url_param('items') ) {
108 foreach my $field ( $fullrecord->field($itemtag) ) {
109 my $one_item_record = $record->clone();
110 $one_item_record->add_fields($field);
111 ModItemFromMarc( $one_item_record, $biblionumber,
112 $field->subfield($itemsubfield) );
116 ModBiblio( $record, $biblionumber, '' );
117 my $new_record = GetMarcBiblio({
118 biblionumber => $biblionumber,
119 embed_items => scalar $query->url_param('items') });
121 $result->{'status'} = "ok";
122 $result->{'biblionumber'} = $biblionumber;
123 my $xml = $new_record->as_xml_record();
124 $xml =~ s/<\?xml.*?\?>//i;
125 $result->{'marcxml'} = $xml;
126 $do_not_escape = 1;
129 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape);
132 sub delete_bib {
133 my $query = shift;
134 my $biblionumber = shift;
135 my $error = DelBiblio($biblionumber);
137 if (defined $error) {
138 print $query->header(-type => 'text/xml', -status => '400 Bad request');
139 print XMLout({ error => $error }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
140 exit 0;
143 print $query->header(-type => 'text/xml');
144 print XMLout({ status => 'OK, biblio deleted' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);