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
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>.
22 # standard or CPAN modules used
25 use MARC::File::USMARC;
31 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_html_with_http_headers );
33 use C4::ImportBatch qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords );
35 use C4::Labels::Batch;
36 use Koha::BiblioFrameworks;
37 use Koha::BackgroundJob::MARCImportCommitBatch;
38 use Koha::BackgroundJob::MARCImportRevertBatch;
44 my $op = $input->param('op') || '';
45 my $import_batch_id = $input->param('import_batch_id') || '';
48 # record list displays
49 my $offset = $input->param('offset') || 0;
50 my $results_per_page = $input->param('results_per_page') || 25;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "tools/manage-marc-import.tt",
56 flagsrequired => {tools => 'manage_staged_marc'},
59 my $frameworks = Koha::BiblioFrameworks->search({ tagfield => { 'not' => undef } }, { join => 'marc_tag_structure', distinct => 'frameworkcode', order_by => ['frameworktext'] });
60 $template->param( frameworks => $frameworks );
62 if ($op eq "create_labels") {
63 #create a batch of labels, then lose $op & $import_batch_id so we get back to import batch list.
64 my $label_batch_id = create_labelbatch_from_importbatch($import_batch_id);
65 if ($label_batch_id == -1) {
66 $template->param( label_batch_msg => "error",
67 message_type => 'alert',
71 $template->param( label_batch_msg => $label_batch_id,
72 message_type => 'dialog',
80 $template->param($op => 1);
85 if ($import_batch_id eq '') {
86 import_batches_list($template, $offset, $results_per_page);
88 import_records_list($template, $import_batch_id, $offset, $results_per_page);
90 } elsif ($op eq "commit-batch") {
91 my $frameworkcode = $input->param('framework');
92 my $overlay_framework = $input->param('overlay_framework');
94 my $job_id = Koha::BackgroundJob::MARCImportCommitBatch->new->enqueue(
96 import_batch_id => $import_batch_id,
97 frameworkcode => $frameworkcode,
98 overlay_framework => $overlay_framework
113 code => 'cannot_enqueue_job',
117 } elsif ($op eq "revert-batch") {
119 my $job_id = Koha::BackgroundJob::MARCImportRevertBatch->new->enqueue(
120 { import_batch_id => $import_batch_id } );
133 code => 'cannot_enqueue_job',
137 } elsif ($op eq "clean-batch") {
138 CleanBatch($import_batch_id);
139 import_batches_list($template, $offset, $results_per_page);
142 import_batch_id => $import_batch_id,
144 } elsif ($op eq "delete-batch") {
145 DeleteBatch($import_batch_id);
146 import_batches_list($template, $offset, $results_per_page);
150 } elsif ($op eq "redo-matching") {
151 my $new_matcher_id = $input->param('new_matcher_id');
152 my $current_matcher_id = $input->param('current_matcher_id');
153 my $overlay_action = $input->param('overlay_action');
154 my $nomatch_action = $input->param('nomatch_action');
155 my $item_action = $input->param('item_action');
156 redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id,
157 $overlay_action, $nomatch_action, $item_action);
158 import_records_list($template, $import_batch_id, $offset, $results_per_page);
161 output_html_with_http_headers $input, $cookie, $template->output;
166 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_;
167 my $rematch_failed = 0;
168 return if not defined $new_matcher_id and not defined $current_matcher_id;
169 my $old_overlay_action = GetImportBatchOverlayAction($import_batch_id);
170 my $old_nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
171 my $old_item_action = GetImportBatchItemAction($import_batch_id);
172 return if $new_matcher_id eq $current_matcher_id and
173 $old_overlay_action eq $overlay_action and
174 $old_nomatch_action eq $nomatch_action and
175 $old_item_action eq $item_action;
177 if ($old_overlay_action ne $overlay_action) {
178 SetImportBatchOverlayAction($import_batch_id, $overlay_action);
179 $template->param('changed_overlay_action' => 1);
181 if ($old_nomatch_action ne $nomatch_action) {
182 SetImportBatchNoMatchAction($import_batch_id, $nomatch_action);
183 $template->param('changed_nomatch_action' => 1);
185 if ($old_item_action ne $item_action) {
186 SetImportBatchItemAction($import_batch_id, $item_action);
187 $template->param('changed_item_action' => 1);
190 my $num_with_matches = 0;
191 if (defined $new_matcher_id and $new_matcher_id ne "") {
192 my $matcher = C4::Matcher->fetch($new_matcher_id);
193 if (defined $matcher) {
194 $num_with_matches = BatchFindDuplicates($import_batch_id, $matcher);
195 SetImportBatchMatcher($import_batch_id, $new_matcher_id);
200 $num_with_matches = BatchFindDuplicates($import_batch_id, undef);
201 SetImportBatchMatcher($import_batch_id, undef);
202 SetImportBatchOverlayAction('create_new');
204 $template->param(rematch_failed => $rematch_failed);
205 $template->param(rematch_attempted => 1);
206 $template->param(num_with_matches => $num_with_matches);
209 sub create_labelbatch_from_importbatch {
212 my $branch_code = C4::Context->userenv->{'branch'};
213 my $batch = C4::Labels::Batch->new(branch_code => $branch_code);
214 my @items = GetItemNumbersFromImportBatch($batch_id);
215 if (grep{$_ == 0} @items) {
216 warn sprintf('create_labelbatch_from_importbatch() : Call to C4::ImportBatch::GetItemNumbersFromImportBatch returned no item number(s) from import batch #%s.', $batch_id);
219 foreach my $item_number (@items) {
220 $err = $batch->add_item($item_number);
222 warn sprintf('create_labelbatch_from_importbatch() : Error attempting to add item #%s of import batch #%s to label batch.', $item_number, $batch_id);
226 return $batch->get_attr('batch_id');
229 sub import_batches_list {
230 my ($template, $offset, $results_per_page) = @_;
231 my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
234 foreach my $batch (@$batches) {
236 import_batch_id => $batch->{'import_batch_id'},
237 num_records => $batch->{'num_records'},
238 num_items => $batch->{'num_items'},
239 upload_timestamp => $batch->{'upload_timestamp'},
240 import_status => $batch->{'import_status'},
241 file_name => $batch->{'file_name'} || "($batch->{'batch_type'})",
242 comments => $batch->{'comments'},
243 can_clean => ($batch->{'import_status'} ne 'cleaned') ? 1 : 0,
244 record_type => $batch->{'record_type'},
245 profile => $batch->{'profile'},
248 $template->param(batch_list => \@list);
249 my $num_batches = GetNumberOfNonZ3950ImportBatches();
250 add_page_numbers($template, $offset, $results_per_page, $num_batches);
251 $template->param(offset => $offset);
252 $template->param(range_top => $offset + $results_per_page - 1);
253 $template->param(num_results => $num_batches);
254 $template->param(results_per_page => $results_per_page);
258 sub import_records_list {
259 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
261 my $batch = GetImportBatch($import_batch_id);
262 $template->param(import_batch_id => $import_batch_id);
264 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
265 $template->param("overlay_action_${overlay_action}" => 1);
266 $template->param(overlay_action => $overlay_action);
268 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
269 $template->param("nomatch_action_${nomatch_action}" => 1);
270 $template->param(nomatch_action => $nomatch_action);
272 my $item_action = GetImportBatchItemAction($import_batch_id);
273 $template->param("item_action_${item_action}" => 1);
274 $template->param(item_action => $item_action);
276 batch_info($template, $batch);
281 my ($template, $batch) = @_;
282 $template->param(batch_info => 1);
283 $template->param(file_name => $batch->{'file_name'});
284 $template->param(profile => $batch->{'profile'});
285 $template->param(comments => $batch->{'comments'});
286 $template->param(import_status => $batch->{'import_status'});
287 $template->param(upload_timestamp => $batch->{'upload_timestamp'});
288 $template->{VARS}->{'record_type'} = $batch->{'record_type'};
289 $template->param(num_records => $batch->{'num_records'});
290 $template->param(num_items => $batch->{'num_items'});
291 if ($batch->{'import_status'} ne 'cleaned') {
292 $template->param(can_clean => 1);
294 if ($batch->{'num_records'} > 0) {
295 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
296 $template->param(can_commit => 1);
298 if ($batch->{'import_status'} eq 'imported') {
299 $template->param(can_revert => 1);
302 if (defined $batch->{'matcher_id'}) {
303 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
304 if (defined $matcher) {
305 $template->param('current_matcher_id' => $batch->{'matcher_id'});
306 $template->param('current_matcher_code' => $matcher->code());
307 $template->param('current_matcher_description' => $matcher->description());
310 add_matcher_list($template,$batch->{'matcher_id'});
313 sub add_matcher_list {
314 my ($template,$current_matcher_id) = @_;
315 my @matchers = C4::Matcher::GetMatcherList();
316 if (defined $current_matcher_id) {
317 for (my $i = 0; $i <= $#matchers; $i++) {
318 if ($matchers[$i]->{'matcher_id'} eq $current_matcher_id) {
319 $matchers[$i]->{'selected'} = 1;
323 $template->param(available_matchers => \@matchers);
326 sub add_page_numbers {
327 my ($template, $offset, $results_per_page, $total_results) = @_;
328 my $max_pages = POSIX::ceil($total_results / $results_per_page);
329 return if $max_pages < 2;
330 my $current_page = int($offset / $results_per_page) + 1;
332 for (my $i = 1; $i <= $max_pages; $i++) {
335 current_page => ($current_page == $i) ? 1 : 0,
336 offset => ($i - 1) * $results_per_page
339 $template->param(pages => \@pages);