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