Bug 22417: Handle errors
[koha.git] / tools / batch_record_modification.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
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.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25 use JSON qw( encode_json );
26 use Try::Tiny;
27
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::AuthoritiesMarc qw( BuildSummary ModAuthority );
31 use C4::Biblio qw( GetMarcBiblio ModBiblio );
32 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates );
33
34 use Koha::Biblios;
35 use Koha::BackgroundJob::BatchUpdateBiblio;
36 use Koha::MetadataRecord::Authority;
37 use Koha::Virtualshelves;
38
39 my $input = new CGI;
40 our $dbh = C4::Context->dbh;
41 my $op = $input->param('op') // q|form|;
42 my $recordtype = $input->param('recordtype') // 'biblio';
43 my $mmtid = $input->param('marc_modification_template_id');
44
45 my ( @messages );
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
48         template_name => 'tools/batch_record_modification.tt',
49         query => $input,
50         type => "intranet",
51         flagsrequired => { tools => 'records_batchmod' },
52 });
53
54 $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) );
55
56 my $sessionID = $input->cookie("CGISESSID");
57
58 my @templates = GetModificationTemplates( $mmtid );
59 unless ( @templates ) {
60     $op = 'error';
61     $template->param(
62         view => 'errors',
63         errors => ['no_template_defined'],
64     );
65     output_html_with_http_headers $input, $cookie, $template->output;
66     exit;
67 }
68
69 if ( $mmtid ) {
70     my @actions = GetModificationTemplateActions( $mmtid );
71     unless ( @actions ) {
72         $op = 'form';
73         push @messages, {
74             type => 'error',
75             code => 'no_action_defined_for_the_template',
76             mmtid => $mmtid,
77         };
78     }
79 }
80
81 if ( $op eq 'form' ) {
82     # Display the form
83     $template->param(
84         view => 'form',
85     );
86 } elsif ( $op eq 'list' ) {
87     # List all records to process
88     my ( @records, @record_ids );
89     if ( my $bib_list = $input->param('bib_list') ) {
90         # Come from the basket
91         @record_ids = split /\//, $bib_list;
92         $recordtype = 'biblio';
93     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
94         # A file of id is given
95         binmode $uploadfile, ':encoding(UTF-8)';
96         while ( my $content = <$uploadfile> ) {
97             next unless $content;
98             $content =~ s/[\r\n]*$//;
99             push @record_ids, $content if $content;
100         }
101     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
102         my $shelf = Koha::Virtualshelves->find($shelf_number);
103         my $contents = $shelf->get_contents;
104         while ( my $content = $contents->next ) {
105             my $biblionumber = $content->biblionumber;
106             push @record_ids, $biblionumber;
107         }
108     } else {
109         # The user enters manually the list of id
110         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
111     }
112
113     for my $record_id ( uniq @record_ids ) {
114         if ( $recordtype eq 'biblio' ) {
115             # Retrieve biblio information
116             my $biblio = Koha::Biblios->find( $record_id );
117             unless ( $biblio ) {
118                 push @messages, {
119                     type => 'warning',
120                     code => 'biblio_not_exists',
121                     biblionumber => $record_id,
122                 };
123                 next;
124             }
125             push @records, $biblio;
126         } else {
127             # Retrieve authority information
128             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
129             unless ( $authority ) {
130                 push @messages, {
131                     type => 'warning',
132                     code => 'authority_not_exists',
133                     authid => $record_id,
134                 };
135                 next;
136             }
137
138             push @records, {
139                 authid => $record_id,
140                 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
141             };
142         }
143     }
144     $template->param(
145         records => \@records,
146         mmtid => $mmtid,
147         view => 'list',
148     );
149 } elsif ( $op eq 'modify' ) {
150     # We want to modify selected records!
151     my @record_ids = $input->multi_param('record_id');
152
153     try {
154         my $job_id = Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue(
155             {
156                 mmtid       => $mmtid,
157                 record_type => $recordtype,
158                 record_ids  => \@record_ids,
159             }
160         );
161         $template->param(
162             view => 'enqueued',
163             job_id => $job_id,
164         );
165     } catch {
166         push @messages, {
167             type => 'error',
168             code => 'cannot_enqueue_job',
169             error => $_,
170         };
171         $template->param( view => 'errors' );
172     };
173 }
174
175 $template->param(
176     messages => \@messages,
177     recordtype => $recordtype,
178     MarcModificationTemplatesLoop => \@templates,
179 );
180
181 output_html_with_http_headers $input, $cookie, $template->output;