3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
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
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.
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
22 # standard or CPAN modules used
24 use MARC::File::USMARC;
35 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
38 my $op = $input->param('op');
39 my $import_batch_id = $input->param('import_batch_id');
41 # record list displays
42 my $offset = $input->param('offset') || 0;
43 my $results_per_page = $input->param('results_per_page') || 10;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user({template_name => "tools/manage-marc-import.tmpl",
50 flagsrequired => {parameters => 1},
55 $template->param(script_name => $script_name, $op => 1);
57 $template->param(script_name => $script_name);
62 if ($import_batch_id eq "") {
63 import_batches_list($template, $offset, $results_per_page);
65 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
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") {
77 output_html_with_http_headers $input, $cookie, $template->output;
81 sub import_batches_list {
82 my ($template, $offset, $results_per_page) = @_;
83 my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
86 foreach my $batch (@$batches) {
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'}
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);
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);
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);
126 sub import_biblios_list {
127 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
129 my $batch = GetImportBatch($import_batch_id);
130 my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
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);
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,
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);
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);
176 if ($batch->{'import_status'} eq 'imported') {
177 $template->param(can_revert => 1);
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;
187 for (my $i = 1; $i <= $max_pages; $i++) {
190 current_page => ($current_page == $i) ? 1 : 0,
191 offset => ($i - 1) * $results_per_page
194 $template->param(pages => \@pages);