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") {
75 } elsif ($op eq "redo-matching") {
76 my $new_matcher_id = $input->param('new_matcher_id');
77 my $current_matcher_id = $input->param('current_matcher_id');
78 redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id);
79 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
82 output_html_with_http_headers $input, $cookie, $template->output;
87 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id) = @_;
88 my $rematch_failed = 0;
89 return if not defined $new_matcher_id and not defined $current_matcher_id;
90 return if $new_matcher_id == $current_matcher_id;
91 my $num_with_matches = 0;
92 if (defined $new_matcher_id and $new_matcher_id ne "") {
93 my $matcher = C4::Matcher->fetch($new_matcher_id);
94 if (defined $matcher) {
95 $num_with_matches = BatchFindBibDuplicates($import_batch_id, $matcher);
96 SetImportBatchMatcher($import_batch_id, $new_matcher_id);
101 $num_with_matches = BatchFindBibDuplicates($import_batch_id, undef);
102 SetImportBatchMatcher($import_batch_id, undef);
104 $template->param(rematch_failed => $rematch_failed);
105 $template->param(rematch_attempted => 1);
106 $template->param(num_with_matches => $num_with_matches);
109 sub import_batches_list {
110 my ($template, $offset, $results_per_page) = @_;
111 my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
114 foreach my $batch (@$batches) {
116 import_batch_id => $batch->{'import_batch_id'},
117 num_biblios => $batch->{'num_biblios'},
118 num_items => $batch->{'num_items'},
119 upload_timestamp => $batch->{'upload_timestamp'},
120 import_status => $batch->{'import_status'},
121 file_name => $batch->{'file_name'},
122 comments => $batch->{'comments'}
125 $template->param(batch_list => \@list);
126 my $num_batches = GetNumberOfNonZ3950ImportBatches();
127 add_page_numbers($template, $offset, $results_per_page, $num_batches);
128 $template->param(offset => $offset);
129 $template->param(range_top => $offset + $results_per_page - 1);
130 $template->param(num_results => $num_batches);
131 $template->param(results_per_page => $results_per_page);
136 my ($template, $import_batch_id) = @_;
137 my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($import_batch_id);
138 $template->param(did_commit => 1);
139 $template->param(num_added => $num_added);
140 $template->param(num_updated => $num_updated);
141 $template->param(num_items_added => $num_items_added);
142 $template->param(num_ignored => $num_ignored);
146 my ($template, $import_batch_id) = @_;
147 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($import_batch_id);
148 $template->param(did_revert => 1);
149 $template->param(num_deleted => $num_deleted);
150 $template->param(num_items_deleted => $num_items_deleted);
151 $template->param(num_errors => $num_errors);
152 $template->param(num_reverted => $num_reverted);
153 $template->param(num_ignored => $num_ignored);
156 sub import_biblios_list {
157 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
159 my $batch = GetImportBatch($import_batch_id);
160 my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
162 foreach my $biblio (@$biblios) {
163 my $citation = $biblio->{'title'};
164 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
165 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
166 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
167 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
168 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
169 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
170 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
172 import_record_id => $biblio->{'import_record_id'},
173 citation => $citation,
174 status => $biblio->{'status'},
175 record_sequence => $biblio->{'record_sequence'},
176 overlay_status => $biblio->{'overlay_status'},
177 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
178 match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
179 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
182 my $num_biblios = $batch->{'num_biblios'};
183 $template->param(biblio_list => \@list);
184 add_page_numbers($template, $offset, $results_per_page, $num_biblios);
185 $template->param(offset => $offset);
186 $template->param(range_top => $offset + $results_per_page - 1);
187 $template->param(num_results => $num_biblios);
188 $template->param(results_per_page => $results_per_page);
189 $template->param(import_batch_id => $import_batch_id);
190 batch_info($template, $batch);
195 my ($template, $batch) = @_;
196 $template->param(batch_info => 1);
197 $template->param(file_name => $batch->{'file_name'});
198 $template->param(comments => $batch->{'comments'});
199 $template->param(import_status => $batch->{'import_status'});
200 $template->param(upload_timestamp => $batch->{'upload_timestamp'});
201 $template->param(num_biblios => $batch->{'num_biblios'});
202 $template->param(num_items => $batch->{'num_biblios'});
203 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
204 $template->param(can_commit => 1);
206 if ($batch->{'import_status'} eq 'imported') {
207 $template->param(can_revert => 1);
209 if (defined $batch->{'matcher_id'}) {
210 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
211 if (defined $matcher) {
212 $template->param('current_matcher_id' => $batch->{'matcher_id'});
213 $template->param('current_matcher_code' => $matcher->code());
214 $template->param('current_matcher_description' => $matcher->description());
217 add_matcher_list($batch->{'matcher_id'});
220 sub add_matcher_list {
221 my $current_matcher_id = shift;
222 my @matchers = C4::Matcher::GetMatcherList();
223 if (defined $current_matcher_id) {
224 for (my $i = 0; $i <= $#matchers; $i++) {
225 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
226 $matchers[$i]->{'selected'} = 1;
230 $template->param(available_matchers => \@matchers);
233 sub add_page_numbers {
234 my ($template, $offset, $results_per_page, $total_results) = @_;
235 my $max_pages = POSIX::ceil($total_results / $results_per_page);
236 return if $max_pages < 2;
237 my $current_page = int($offset / $results_per_page) + 1;
239 for (my $i = 1; $i <= $max_pages; $i++) {
242 current_page => ($current_page == $i) ? 1 : 0,
243 offset => ($i - 1) * $results_per_page
246 $template->param(pages => \@pages);