fixing syntax error in sysprefs.sql
[koha.git] / 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 } 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);
80 }
81
82 output_html_with_http_headers $input, $cookie, $template->output;
83
84 exit 0;
85
86 sub redo_matching {
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);
97         } else {
98             $rematch_failed = 1;
99         }
100     } else {
101         $num_with_matches = BatchFindBibDuplicates($import_batch_id, undef);
102          SetImportBatchMatcher($import_batch_id, undef);
103     }
104     $template->param(rematch_failed => $rematch_failed);
105     $template->param(rematch_attempted => 1);
106     $template->param(num_with_matches => $num_with_matches);
107 }
108
109 sub import_batches_list {
110     my ($template, $offset, $results_per_page) = @_;
111     my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
112
113     my @list = ();
114     foreach my $batch (@$batches) {
115         push @list, {
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'}
123         };
124     }
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);
132
133 }
134
135 sub commit_batch {
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);
143 }
144
145 sub revert_batch {
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);
154 }
155
156 sub import_biblios_list {
157     my ($template, $import_batch_id, $offset, $results_per_page) = @_;
158
159     my $batch = GetImportBatch($import_batch_id);
160     my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
161     my @list = ();
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);
171         push @list, {
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,
180         };
181     }
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);
191     
192 }
193
194 sub batch_info {
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);
205     }
206     if ($batch->{'import_status'} eq 'imported') {
207         $template->param(can_revert => 1);
208     }
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());
215         }
216     }
217     add_matcher_list($batch->{'matcher_id'});
218 }
219
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;
227             }
228         }
229     }
230     $template->param(available_matchers => \@matchers);
231 }
232
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;
238     my @pages = ();
239     for (my $i = 1; $i <= $max_pages; $i++) {
240         push @pages, {
241             page_number => $i,
242             current_page => ($current_page == $i) ? 1 : 0,
243             offset => ($i - 1) * $results_per_page
244         }
245     }
246     $template->param(pages => \@pages);
247 }
248