3 # Copyright 2012 CatalystIT Ltd
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>.
24 use C4::Auth qw/check_api_auth/;
26 use C4::ImportBatch qw( GetWebserviceBatchId AddBiblioToBatch AddItemsToImportBiblio BatchFindDuplicates BatchCommitRecords );
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);
42 if ($query->request_method eq "POST") {
43 $xml = $query->param('xml');
46 my %params = map { $_ => scalar $query->param($_) } $query->param;
47 my $result = import_bib($xml, \%params );
48 print $query->header(-type => 'text/xml');
49 print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1);
51 print $query->header(-type => 'text/xml', -status => '400 Bad Request');
57 my ($inxml, $params) = @_;
61 my $import_mode = delete $params->{import_mode} || '';
62 my $framework = delete $params->{framework} || '';
63 my $overlay_framework = $params->{overlay_framework};
65 if (my $matcher_code = delete $params->{match}) {
66 $params->{matcher_id} = C4::Matcher::GetMatcherId($matcher_code);
69 my $batch_id = GetWebserviceBatchId($params);
71 $result->{'status'} = "failed";
72 $result->{'error'} = "Batch create error";
76 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
77 my $marc_record = eval {MARC::Record::new_from_xml( $inxml, "UTF-8", $marcflavour)};
79 $result->{'status'} = "failed";
80 $result->{'error'} = $@;
83 if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
84 my @control_num = $marc_record->field('001');
85 $marc_record->delete_fields(@control_num);
88 my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", 1);
89 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS');
91 my $matcher = C4::Matcher->new($params->{record_type} || 'biblio');
92 $matcher = C4::Matcher->fetch($params->{matcher_id});
93 my $number_of_matches = BatchFindDuplicates($batch_id, $matcher);
95 # XXX we are ignoring the result of this;
98 batch_id => $batch_id,
99 framework => $framework,
100 overlay_framework => $overlay_framework
102 ) if lc($import_mode) eq 'direct';
104 my $dbh = C4::Context->dbh();
105 my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?");
106 $sth->execute($import_record_id);
107 my $biblionumber=$sth->fetchrow_arrayref->[0] || '';
108 $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?");
109 $sth->execute($import_record_id);
110 my $match_status = $sth->fetchrow_arrayref->[0] || 'no_match';
111 my $url = C4::Context->preference('staffClientBaseURL') .'/cgi-bin/koha/catalogue/detail.pl?biblionumber='. $biblionumber;
113 $result->{'status'} = "ok";
114 $result->{'import_batch_id'} = $batch_id;
115 $result->{'match_status'} = $match_status;
116 $result->{'biblionumber'} = $biblionumber;
117 $result->{'url'} = $url;