Bug 28835: Fix ability to pass list contents to batch record modification
[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::BackgroundJob::BatchUpdateAuthority;
37 use Koha::MetadataRecord::Authority;
38 use Koha::Virtualshelves;
39
40 my $input = CGI->new;
41 our $dbh = C4::Context->dbh;
42 my $op = $input->param('op') // q|form|;
43 my $recordtype = $input->param('recordtype') // 'biblio';
44 my $mmtid = $input->param('marc_modification_template_id');
45
46 my ( @messages );
47
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
49         template_name => 'tools/batch_record_modification.tt',
50         query => $input,
51         type => "intranet",
52         flagsrequired => { tools => 'records_batchmod' },
53 });
54
55 my $sessionID = $input->cookie("CGISESSID");
56
57 my @templates = GetModificationTemplates( $mmtid );
58 unless ( @templates ) {
59     $op = 'error';
60     $template->param(
61         view => 'errors',
62         errors => ['no_template_defined'],
63     );
64     output_html_with_http_headers $input, $cookie, $template->output;
65     exit;
66 }
67
68 if ( $mmtid ) {
69     my @actions = GetModificationTemplateActions( $mmtid );
70     unless ( @actions ) {
71         $op = 'form';
72         push @messages, {
73             type => 'error',
74             code => 'no_action_defined_for_the_template',
75             mmtid => $mmtid,
76         };
77     }
78 }
79
80 if ( $op eq 'form' ) {
81     # Display the form
82     $template->param(
83         view => 'form',
84         lists => scalar Koha::Virtualshelves->search(
85             [
86                 { category => 1, owner => $loggedinuser },
87                 { category => 2 }
88             ]
89         )
90     );
91 } elsif ( $op eq 'list' ) {
92     # List all records to process
93     my ( @records, @record_ids );
94     if ( my $bib_list = $input->param('bib_list') ) {
95         # Come from the basket
96         @record_ids = split /\//, $bib_list;
97         $recordtype = 'biblio';
98     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
99         # A file of id is given
100         binmode $uploadfile, ':encoding(UTF-8)';
101         while ( my $content = <$uploadfile> ) {
102             next unless $content;
103             $content =~ s/[\r\n]*$//;
104             push @record_ids, $content if $content;
105         }
106     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
107         my $shelf = Koha::Virtualshelves->find($shelf_number);
108         my $contents = $shelf->get_contents;
109         while ( my $content = $contents->next ) {
110             my $biblionumber = $content->biblionumber;
111             push @record_ids, $biblionumber;
112         }
113     } else {
114         # The user enters manually the list of id
115         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
116     }
117
118     for my $record_id ( uniq @record_ids ) {
119         if ( $recordtype eq 'biblio' ) {
120             # Retrieve biblio information
121             my $biblio = Koha::Biblios->find( $record_id );
122             unless ( $biblio ) {
123                 push @messages, {
124                     type => 'warning',
125                     code => 'biblio_not_exists',
126                     biblionumber => $record_id,
127                 };
128                 next;
129             }
130             push @records, $biblio;
131         } else {
132             # Retrieve authority information
133             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
134             unless ( $authority ) {
135                 push @messages, {
136                     type => 'warning',
137                     code => 'authority_not_exists',
138                     authid => $record_id,
139                 };
140                 next;
141             }
142
143             push @records, {
144                 authid => $record_id,
145                 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
146             };
147         }
148     }
149     $template->param(
150         records => \@records,
151         mmtid => $mmtid,
152         view => 'list',
153     );
154 } elsif ( $op eq 'modify' ) {
155     # We want to modify selected records!
156     my @record_ids = $input->multi_param('record_id');
157
158     try {
159         my $params = {
160             mmtid       => $mmtid,
161             record_ids  => \@record_ids,
162         };
163
164         my $job_id =
165           $recordtype eq 'biblio'
166           ? Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue($params)
167           : Koha::BackgroundJob::BatchUpdateAuthority->new->enqueue($params);
168
169         $template->param(
170             view => 'enqueued',
171             job_id => $job_id,
172         );
173     } catch {
174         push @messages, {
175             type => 'error',
176             code => 'cannot_enqueue_job',
177             error => $_,
178         };
179         $template->param( view => 'errors' );
180     };
181 }
182
183 $template->param(
184     messages => \@messages,
185     recordtype => $recordtype,
186     MarcModificationTemplatesLoop => \@templates,
187 );
188
189 output_html_with_http_headers $input, $cookie, $template->output;