batch import rework -- implement stage-commit-undo for batch import
[koha_ffzg] / tools / manage-marc-import.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21
22 # standard or CPAN modules used
23 use CGI;
24 use MARC::File::USMARC;
25
26 # Koha modules used
27 use C4::Context;
28 use C4::Auth;
29 use C4::Input;
30 use C4::Output;
31 use C4::Biblio;
32 use C4::ImportBatch;
33 use C4::Matcher;
34
35 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
36
37 my $input = new CGI;
38 my $op = $input->param('op');
39 my $import_batch_id = $input->param('import_batch_id');
40
41 # record list displays
42 my $offset = $input->param('offset') || 0;
43 my $results_per_page = $input->param('results_per_page') || 10; 
44
45 my ($template, $loggedinuser, $cookie)
46     = get_template_and_user({template_name => "tools/manage-marc-import.tmpl",
47                  query => $input,
48                  type => "intranet",
49                  authnotrequired => 0,
50                  flagsrequired => {parameters => 1},
51                  debug => 1,
52                  });
53
54 if ($op) {
55     $template->param(script_name => $script_name, $op => 1);
56 } else {
57     $template->param(script_name => $script_name);
58 }
59
60 if ($op eq "") {
61     # displaying a list
62     if ($import_batch_id eq "") {
63         import_batches_list($template, $offset, $results_per_page);
64     } else {
65         import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
66     }
67 } elsif ($op eq "commit-batch") {
68     commit_batch($template, $import_batch_id);
69     import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
70 } elsif ($op eq "revert-batch") {
71     revert_batch($template, $import_batch_id);
72     import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
73 } elsif ($op eq "clean-batch") {
74     ;
75 }
76
77 output_html_with_http_headers $input, $cookie, $template->output;
78
79 exit 0;
80
81 sub import_batches_list {
82     my ($template, $offset, $results_per_page) = @_;
83     my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
84
85     my @list = ();
86     foreach my $batch (@$batches) {
87         push @list, {
88             import_batch_id => $batch->{'import_batch_id'},
89             num_biblios => $batch->{'num_biblios'},
90             num_items => $batch->{'num_items'},
91             upload_timestamp => $batch->{'upload_timestamp'},
92             import_status => $batch->{'import_status'},
93             file_name => $batch->{'file_name'},
94             comments => $batch->{'comments'}
95         };
96     }
97     $template->param(batch_list => \@list); 
98     my $num_batches = GetNumberOfNonZ3950ImportBatches();
99     add_page_numbers($template, $offset, $results_per_page, $num_batches);
100     $template->param(offset => $offset);
101     $template->param(range_top => $offset + $results_per_page - 1);
102     $template->param(num_results => $num_batches);
103     $template->param(results_per_page => $results_per_page);
104
105 }
106
107 sub commit_batch {
108     my ($template, $import_batch_id) = @_;
109     my ($num_added, $num_updated, $num_ignored) = BatchCommitBibRecords($import_batch_id);
110     $template->param(did_commit => 1);
111     $template->param(num_added => $num_added);
112     $template->param(num_updated => $num_updated);
113     $template->param(num_ignored => $num_ignored);
114 }
115
116 sub revert_batch {
117     my ($template, $import_batch_id) = @_;
118     my ($num_deleted, $num_errors, $num_reverted, $num_ignored) = BatchRevertBibRecords($import_batch_id);
119     $template->param(did_revert => 1);
120     $template->param(num_deleted => $num_deleted);
121     $template->param(num_errors => $num_errors);
122     $template->param(num_reverted => $num_reverted);
123     $template->param(num_ignored => $num_ignored);
124 }
125
126 sub import_biblios_list {
127     my ($template, $import_batch_id, $offset, $results_per_page) = @_;
128
129     my $batch = GetImportBatch($import_batch_id);
130     my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
131     my @list = ();
132     foreach my $biblio (@$biblios) {
133         my $citation = $biblio->{'title'};
134         $citation .= " $biblio->{'author'}" if $biblio->{'author'};
135         $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
136         $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
137         $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
138         $citation .= $biblio->{'issn'} if $biblio->{'issn'};
139         $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
140         my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
141         push @list, {
142             import_record_id => $biblio->{'import_record_id'},
143             citation => $citation,
144             status => $biblio->{'status'},
145             record_sequence => $biblio->{'record_sequence'},
146             overlay_status => $biblio->{'overlay_status'},
147             match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
148             match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
149             match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
150         };
151     }
152     my $num_biblios = $batch->{'num_biblios'};
153     $template->param(biblio_list => \@list); 
154     add_page_numbers($template, $offset, $results_per_page, $num_biblios);
155     $template->param(offset => $offset);
156     $template->param(range_top => $offset + $results_per_page - 1);
157     $template->param(num_results => $num_biblios);
158     $template->param(results_per_page => $results_per_page);
159     $template->param(import_batch_id => $import_batch_id);
160     batch_info($template, $batch);
161     
162 }
163
164 sub batch_info {
165     my ($template, $batch) = @_;
166     $template->param(batch_info => 1);
167     $template->param(file_name => $batch->{'file_name'});
168     $template->param(comments => $batch->{'comments'});
169     $template->param(import_status => $batch->{'import_status'});
170     $template->param(upload_timestamp => $batch->{'upload_timestamp'});
171     $template->param(num_biblios => $batch->{'num_biblios'});
172     $template->param(num_items => $batch->{'num_biblios'});
173     if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
174         $template->param(can_commit => 1);
175     }
176     if ($batch->{'import_status'} eq 'imported') {
177         $template->param(can_revert => 1);
178     }
179 }
180
181 sub add_page_numbers {
182     my ($template, $offset, $results_per_page, $total_results) = @_;
183     my $max_pages = POSIX::ceil($total_results / $results_per_page);
184     return if $max_pages < 2;
185     my $current_page = int($offset / $results_per_page) + 1;
186     my @pages = ();
187     for (my $i = 1; $i <= $max_pages; $i++) {
188         push @pages, {
189             page_number => $i,
190             current_page => ($current_page == $i) ? 1 : 0,
191             offset => ($i - 1) * $results_per_page
192         }
193     }
194     $template->param(pages => \@pages);
195 }
196