Changing to two-column layout
[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 }
76
77 output_html_with_http_headers $input, $cookie, $template->output;
78
79 exit 0;
80
81 sub import_batches_list {
82     my ($template, $offset, $results_per_page) = @_;
83     my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
84
85     my @list = ();
86     foreach my $batch (@$batches) {
87         push @list, {
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'}
95         };
96     }
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);
104
105 }
106
107 sub commit_batch {
108     my ($template, $import_batch_id) = @_;
109     my ($num_added, $num_updated, $num_items_added, $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_items_added => $num_items_added);
114     $template->param(num_ignored => $num_ignored);
115 }
116
117 sub revert_batch {
118     my ($template, $import_batch_id) = @_;
119     my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($import_batch_id);
120     $template->param(did_revert => 1);
121     $template->param(num_deleted => $num_deleted);
122     $template->param(num_items_deleted => $num_items_deleted);
123     $template->param(num_errors => $num_errors);
124     $template->param(num_reverted => $num_reverted);
125     $template->param(num_ignored => $num_ignored);
126 }
127
128 sub import_biblios_list {
129     my ($template, $import_batch_id, $offset, $results_per_page) = @_;
130
131     my $batch = GetImportBatch($import_batch_id);
132     my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
133     my @list = ();
134     foreach my $biblio (@$biblios) {
135         my $citation = $biblio->{'title'};
136         $citation .= " $biblio->{'author'}" if $biblio->{'author'};
137         $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
138         $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
139         $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
140         $citation .= $biblio->{'issn'} if $biblio->{'issn'};
141         $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
142         my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
143         push @list, {
144             import_record_id => $biblio->{'import_record_id'},
145             citation => $citation,
146             status => $biblio->{'status'},
147             record_sequence => $biblio->{'record_sequence'},
148             overlay_status => $biblio->{'overlay_status'},
149             match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
150             match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
151             match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
152         };
153     }
154     my $num_biblios = $batch->{'num_biblios'};
155     $template->param(biblio_list => \@list); 
156     add_page_numbers($template, $offset, $results_per_page, $num_biblios);
157     $template->param(offset => $offset);
158     $template->param(range_top => $offset + $results_per_page - 1);
159     $template->param(num_results => $num_biblios);
160     $template->param(results_per_page => $results_per_page);
161     $template->param(import_batch_id => $import_batch_id);
162     batch_info($template, $batch);
163     
164 }
165
166 sub batch_info {
167     my ($template, $batch) = @_;
168     $template->param(batch_info => 1);
169     $template->param(file_name => $batch->{'file_name'});
170     $template->param(comments => $batch->{'comments'});
171     $template->param(import_status => $batch->{'import_status'});
172     $template->param(upload_timestamp => $batch->{'upload_timestamp'});
173     $template->param(num_biblios => $batch->{'num_biblios'});
174     $template->param(num_items => $batch->{'num_biblios'});
175     if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
176         $template->param(can_commit => 1);
177     }
178     if ($batch->{'import_status'} eq 'imported') {
179         $template->param(can_revert => 1);
180     }
181 }
182
183 sub add_page_numbers {
184     my ($template, $offset, $results_per_page, $total_results) = @_;
185     my $max_pages = POSIX::ceil($total_results / $results_per_page);
186     return if $max_pages < 2;
187     my $current_page = int($offset / $results_per_page) + 1;
188     my @pages = ();
189     for (my $i = 1; $i <= $max_pages; $i++) {
190         push @pages, {
191             page_number => $i,
192             current_page => ($current_page == $i) ? 1 : 0,
193             offset => ($i - 1) * $results_per_page
194         }
195     }
196     $template->param(pages => \@pages);
197 }
198