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');
93 my $job_id = Koha::BackgroundJob::MARCImportCommitBatch->new->enqueue(
95 import_batch_id => $import_batch_id,
96 frameworkcode => $frameworkcode
111 code => 'cannot_enqueue_job',
115 } elsif ($op eq "revert-batch") {
117 my $job_id = Koha::BackgroundJob::MARCImportRevertBatch->new->enqueue(
118 { import_batch_id => $import_batch_id } );
131 code => 'cannot_enqueue_job',
135 } elsif ($op eq "clean-batch") {
136 CleanBatch($import_batch_id);
137 import_batches_list($template, $offset, $results_per_page);
140 import_batch_id => $import_batch_id,
142 } elsif ($op eq "delete-batch") {
143 DeleteBatch($import_batch_id);
144 import_batches_list($template, $offset, $results_per_page);
148 } elsif ($op eq "redo-matching") {
149 my $new_matcher_id = $input->param('new_matcher_id');
150 my $current_matcher_id = $input->param('current_matcher_id');
151 my $overlay_action = $input->param('overlay_action');
152 my $nomatch_action = $input->param('nomatch_action');
153 my $item_action = $input->param('item_action');
154 redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id,
155 $overlay_action, $nomatch_action, $item_action);
156 import_records_list($template, $import_batch_id, $offset, $results_per_page);
159 output_html_with_http_headers $input, $cookie, $template->output;
164 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_;
165 my $rematch_failed = 0;
166 return if not defined $new_matcher_id and not defined $current_matcher_id;
167 my $old_overlay_action = GetImportBatchOverlayAction($import_batch_id);
168 my $old_nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
169 my $old_item_action = GetImportBatchItemAction($import_batch_id);
170 return if $new_matcher_id eq $current_matcher_id and
171 $old_overlay_action eq $overlay_action and
172 $old_nomatch_action eq $nomatch_action and
173 $old_item_action eq $item_action;
175 if ($old_overlay_action ne $overlay_action) {
176 SetImportBatchOverlayAction($import_batch_id, $overlay_action);
177 $template->param('changed_overlay_action' => 1);
179 if ($old_nomatch_action ne $nomatch_action) {
180 SetImportBatchNoMatchAction($import_batch_id, $nomatch_action);
181 $template->param('changed_nomatch_action' => 1);
183 if ($old_item_action ne $item_action) {
184 SetImportBatchItemAction($import_batch_id, $item_action);
185 $template->param('changed_item_action' => 1);
188 my $num_with_matches = 0;
189 if (defined $new_matcher_id and $new_matcher_id ne "") {
190 my $matcher = C4::Matcher->fetch($new_matcher_id);
191 if (defined $matcher) {
192 $num_with_matches = BatchFindDuplicates($import_batch_id, $matcher);
193 SetImportBatchMatcher($import_batch_id, $new_matcher_id);
198 $num_with_matches = BatchFindDuplicates($import_batch_id, undef);
199 SetImportBatchMatcher($import_batch_id, undef);
200 SetImportBatchOverlayAction('create_new');
202 $template->param(rematch_failed => $rematch_failed);
203 $template->param(rematch_attempted => 1);
204 $template->param(num_with_matches => $num_with_matches);
207 sub create_labelbatch_from_importbatch {
210 my $branch_code = C4::Context->userenv->{'branch'};
211 my $batch = C4::Labels::Batch->new(branch_code => $branch_code);
212 my @items = GetItemNumbersFromImportBatch($batch_id);
213 if (grep{$_ == 0} @items) {
214 warn sprintf('create_labelbatch_from_importbatch() : Call to C4::ImportBatch::GetItemNumbersFromImportBatch returned no item number(s) from import batch #%s.', $batch_id);
217 foreach my $item_number (@items) {
218 $err = $batch->add_item($item_number);
220 warn sprintf('create_labelbatch_from_importbatch() : Error attempting to add item #%s of import batch #%s to label batch.', $item_number, $batch_id);
224 return $batch->get_attr('batch_id');
227 sub import_batches_list {
228 my ($template, $offset, $results_per_page) = @_;
229 my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
232 foreach my $batch (@$batches) {
234 import_batch_id => $batch->{'import_batch_id'},
235 num_records => $batch->{'num_records'},
236 num_items => $batch->{'num_items'},
237 upload_timestamp => $batch->{'upload_timestamp'},
238 import_status => $batch->{'import_status'},
239 file_name => $batch->{'file_name'} || "($batch->{'batch_type'})",
240 comments => $batch->{'comments'},
241 can_clean => ($batch->{'import_status'} ne 'cleaned') ? 1 : 0,
242 record_type => $batch->{'record_type'},
243 profile => $batch->{'profile'},
246 $template->param(batch_list => \@list);
247 my $num_batches = GetNumberOfNonZ3950ImportBatches();
248 add_page_numbers($template, $offset, $results_per_page, $num_batches);
249 $template->param(offset => $offset);
250 $template->param(range_top => $offset + $results_per_page - 1);
251 $template->param(num_results => $num_batches);
252 $template->param(results_per_page => $results_per_page);
256 sub import_records_list {
257 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
259 my $batch = GetImportBatch($import_batch_id);
260 $template->param(import_batch_id => $import_batch_id);
262 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
263 $template->param("overlay_action_${overlay_action}" => 1);
264 $template->param(overlay_action => $overlay_action);
266 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
267 $template->param("nomatch_action_${nomatch_action}" => 1);
268 $template->param(nomatch_action => $nomatch_action);
270 my $item_action = GetImportBatchItemAction($import_batch_id);
271 $template->param("item_action_${item_action}" => 1);
272 $template->param(item_action => $item_action);
274 batch_info($template, $batch);
279 my ($template, $batch) = @_;
280 $template->param(batch_info => 1);
281 $template->param(file_name => $batch->{'file_name'});
282 $template->param(profile => $batch->{'profile'});
283 $template->param(comments => $batch->{'comments'});
284 $template->param(import_status => $batch->{'import_status'});
285 $template->param(upload_timestamp => $batch->{'upload_timestamp'});
286 $template->{VARS}->{'record_type'} = $batch->{'record_type'};
287 $template->param(num_records => $batch->{'num_records'});
288 $template->param(num_items => $batch->{'num_items'});
289 if ($batch->{'import_status'} ne 'cleaned') {
290 $template->param(can_clean => 1);
292 if ($batch->{'num_records'} > 0) {
293 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
294 $template->param(can_commit => 1);
296 if ($batch->{'import_status'} eq 'imported') {
297 $template->param(can_revert => 1);
300 if (defined $batch->{'matcher_id'}) {
301 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
302 if (defined $matcher) {
303 $template->param('current_matcher_id' => $batch->{'matcher_id'});
304 $template->param('current_matcher_code' => $matcher->code());
305 $template->param('current_matcher_description' => $matcher->description());
308 add_matcher_list($template,$batch->{'matcher_id'});
311 sub add_matcher_list {
312 my ($template,$current_matcher_id) = @_;
313 my @matchers = C4::Matcher::GetMatcherList();
314 if (defined $current_matcher_id) {
315 for (my $i = 0; $i <= $#matchers; $i++) {
316 if ($matchers[$i]->{'matcher_id'} eq $current_matcher_id) {
317 $matchers[$i]->{'selected'} = 1;
321 $template->param(available_matchers => \@matchers);
324 sub add_page_numbers {
325 my ($template, $offset, $results_per_page, $total_results) = @_;
326 my $max_pages = POSIX::ceil($total_results / $results_per_page);
327 return if $max_pages < 2;
328 my $current_page = int($offset / $results_per_page) + 1;
330 for (my $i = 1; $i <= $max_pages; $i++) {
333 current_page => ($current_page == $i) ? 1 : 0,
334 offset => ($i - 1) * $results_per_page
337 $template->param(pages => \@pages);