Bug 10860 - Change "in-house use" to "on-site checkout"
[koha.git] / svc / bib
diff --git a/svc/bib b/svc/bib
index 4808559..d7cd169 100755 (executable)
--- a/svc/bib
+++ b/svc/bib
@@ -1,6 +1,7 @@
 #!/usr/bin/perl
 
 # Copyright 2007 LibLime
+# Copyright 2012 software.coop and MJ Ray
 #
 # This file is part of Koha.
 #
@@ -24,6 +25,7 @@ use warnings;
 use CGI;
 use C4::Auth qw/check_api_auth/;
 use C4::Biblio;
+use C4::Items;
 use XML::Simple;
 
 my $query = new CGI;
@@ -46,11 +48,17 @@ if ($path_info =~ m!^/(\d+)$!) {
     print $query->header(-type => 'text/xml', -status => '400 Bad Request');
 }
 
-# are we retrieving or updating a bib?
+# are we retrieving, updating or deleting a bib?
 if ($query->request_method eq "GET") {
     fetch_bib($query, $biblionumber);
-} else {
+} elsif ($query->request_method eq "POST") {
     update_bib($query, $biblionumber);
+} elsif ($query->request_method eq "DELETE") {
+    delete_bib($query, $biblionumber);
+} else {
+    print $query->header(-type => 'text/xml', -status => '405 Method not allowed');
+    print XMLout({ error => 'Method not allowed' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
+    exit 0;
 }
 
 exit 0;
@@ -58,7 +66,7 @@ exit 0;
 sub fetch_bib {
     my $query = shift;
     my $biblionumber = shift;
-    my $record = GetMarcBiblio($biblionumber);
+    my $record = GetMarcBiblio( $biblionumber, $query->url_param('items') );
     if  (defined $record) {
         print $query->header(-type => 'text/xml');
         print $record->as_xml_record();
@@ -86,13 +94,28 @@ sub update_bib {
         $result->{'status'} = "failed";
         $result->{'error'} = $@;
     } else {
+        my $fullrecord = $record->clone();
+        my ( $itemtag, $itemsubfield ) =
+          GetMarcFromKohaField( "items.itemnumber", '' );
+
         # delete any item tags
-        my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber", '');
-        foreach my $field ($record->field($itemtag)) {
+        foreach my $field ( $record->field($itemtag) ) {
             $record->delete_field($field);
         }
-        ModBiblio($record, $biblionumber, '');
-        my $new_record = GetMarcBiblio($biblionumber);
+
+        if ( $query->url_param('items') ) {
+            foreach my $field ( $fullrecord->field($itemtag) ) {
+                my $one_item_record = $record->clone();
+                $one_item_record->add_fields($field);
+                ModItemFromMarc( $one_item_record, $biblionumber,
+                    $field->subfield($itemsubfield) );
+            }
+        }
+
+        ModBiblio( $record, $biblionumber, '' );
+        my $new_record =
+          GetMarcBiblio( $biblionumber, $query->url_param('items') );
+
         $result->{'status'} = "ok";
         $result->{'biblionumber'} = $biblionumber;
         my $xml = $new_record->as_xml_record();
@@ -103,3 +126,18 @@ sub update_bib {
    
     print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1, NoEscape => $do_not_escape); 
 }
+
+sub delete_bib {
+    my $query = shift;
+    my $biblionumber = shift;
+    my $error = DelBiblio($biblionumber);
+
+    if (defined $error) {
+        print $query->header(-type => 'text/xml', -status => '400 Bad request');
+        print XMLout({ error => $error }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
+        exit 0;
+    }
+
+    print $query->header(-type => 'text/xml');
+    print XMLout({ status => 'OK, biblio deleted' }, NoAttr => 1, RootName => 'response', XMLDecl => 1);
+}