Bug 11395: The modification template should be changed on the list view
[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
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::AuthoritiesMarc qw( BuildSummary GetAuthTypeCode ModAuthority );
29 use C4::BackgroundJob;
30 use C4::Biblio qw( GetMarcBiblio ModBiblio );
31 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates ModifyRecordWithTemplate );
32 use Koha::Authority;
33
34 my $input = new CGI;
35 our $dbh = C4::Context->dbh;
36 my $op = $input->param('op') // q|form|;
37 my $recordtype = $input->param('recordtype') // 'biblio';
38 my $mmtid = $input->param('marc_modification_template_id');
39
40 my ( @messages );
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
43         template_name => 'tools/batch_record_modification.tt',
44         query => $input,
45         type => "intranet",
46         authnotrequired => 0,
47         flagsrequired => { tools => 'biblio_batchmod' },
48 });
49
50
51 my $sessionID = $input->cookie("CGISESSID");
52
53 my $runinbackground = $input->param('runinbackground');
54 my $completedJobID = $input->param('completedJobID');
55 if ( $completedJobID ) {
56     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
57     my $report = $job->get('report');
58     my $messages = $job->get('messages');
59     $template->param(
60         report => $report,
61         messages => $messages,
62         view => 'report',
63     );
64     output_html_with_http_headers $input, $cookie, $template->output;
65     exit;
66 }
67
68 my @templates = GetModificationTemplates( $mmtid );
69 unless ( @templates ) {
70     $op = 'error';
71     $template->param(
72         view => 'errors',
73         errors => ['no_template_defined'],
74     );
75     output_html_with_http_headers $input, $cookie, $template->output;
76 }
77
78 if ( $mmtid ) {
79     my @actions = GetModificationTemplateActions( $mmtid );
80     unless ( @actions ) {
81         $op = 'form';
82         push @messages, {
83             type => 'error',
84             code => 'no_action_defined_for_the_template',
85             mmtid => $mmtid,
86         };
87     }
88 }
89
90 if ( $op eq 'form' ) {
91     # Display the form
92     $template->param(
93         view => 'form',
94     );
95 } elsif ( $op eq 'list' ) {
96     # List all records to process
97     my ( @records, @record_ids );
98     if ( my $bib_list = $input->param('bib_list') ) {
99         # Come from the basket
100         @record_ids = split /\//, $bib_list;
101         $recordtype = 'biblio';
102     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
103         # A file of id is given
104         while ( my $content = <$uploadfile> ) {
105             next unless $content;
106             $content =~ s/[\r\n]*$//;
107             push @record_ids, $content if $content;
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 = C4::Biblio::GetBiblio( $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::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->param('record_id');
153
154     my ( $job );
155     if ( $runinbackground ) {
156         my $job_size = scalar( @record_ids );
157         $job = C4::BackgroundJob->new( $sessionID, "FIXME", $ENV{SCRIPT_NAME}, $job_size );
158         my $job_id = $job->id;
159         if (my $pid = fork) {
160             $dbh->{InactiveDestroy}  = 1;
161
162             my $reply = CGI->new("");
163             print $reply->header(-type => 'text/html');
164             print '{"jobID":"' . $job_id . '"}';
165             exit 0;
166         } elsif (defined $pid) {
167             close STDOUT;
168             close STDERR;
169         } else {
170             warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
171             exit 0;
172         }
173     }
174
175     my $report = {
176         total_records => 0,
177         total_success => 0,
178     };
179     my $progress = 0;
180     $dbh->{RaiseError} = 1;
181     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
182         $report->{total_records}++;
183         next unless $record_id;
184
185         if ( $recordtype eq 'biblio' ) {
186             # Biblios
187             my $biblionumber = $record_id;
188
189             # Finally, modify the biblio
190             my $error = eval {
191                 my $record = GetMarcBiblio( $biblionumber );
192                 ModifyRecordWithTemplate( $mmtid, $record );
193                 ModBiblio( $record, $biblionumber );
194             };
195             if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
196                 push @messages, {
197                     type => 'error',
198                     code => 'biblio_not_modified',
199                     biblionumber => $biblionumber,
200                     error => ($@ ? $@ : $error),
201                 };
202             } else {
203                 push @messages, {
204                     type => 'success',
205                     code => 'biblio_modified',
206                     biblionumber => $biblionumber,
207                 };
208                 $report->{total_success}++;
209             }
210         } else {
211             # Authorities
212             my $authid = $record_id;
213             my $error = eval {
214                 my $authority = Koha::Authority->get_from_authid( $authid );
215                 my $record = $authority->record;
216                 ModifyRecordWithTemplate( $mmtid, $record );
217                 ModAuthority( $authid, $record, GetAuthTypeCode( $authid ) );
218             };
219             if ( $error and $error != $authid or $@ ) {
220                 push @messages, {
221                     type => 'error',
222                     code => 'authority_not_modified',
223                     authid => $authid,
224                     error => ($@ ? $@ : 0),
225                 };
226             } else {
227                 push @messages, {
228                     type => 'success',
229                     code => 'authority_modified',
230                     authid => $authid,
231                 };
232                 $report->{total_success}++;
233             }
234         }
235
236         $job->set({
237             view => 'report',
238             report => $report,
239             messages => \@messages,
240         });
241         $job->progress( ++$progress ) if $runinbackground;
242     }
243
244     if ($runinbackground) {
245         $job->finish if defined $job;
246     } else {
247         $template->param(
248             view => 'report',
249             report => $report,
250             messages => \@messages,
251         );
252     }
253 }
254
255 $template->param(
256     messages => \@messages,
257     recordtype => $recordtype,
258     MarcModificationTemplatesLoop => \@templates,
259 );
260
261 output_html_with_http_headers $input, $cookie, $template->output;