3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>
24 use List::MoreUtils qw( uniq );
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 );
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');
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
43 template_name => 'tools/batch_record_modification.tt',
47 flagsrequired => { tools => 'biblio_batchmod' },
51 my $sessionID = $input->cookie("CGISESSID");
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');
61 messages => $messages,
64 output_html_with_http_headers $input, $cookie, $template->output;
68 my @templates = GetModificationTemplates( $mmtid );
69 unless ( @templates ) {
73 errors => ['no_template_defined'],
75 output_html_with_http_headers $input, $cookie, $template->output;
79 my @actions = GetModificationTemplateActions( $mmtid );
84 code => 'no_action_defined_for_the_template',
90 if ( $op eq 'form' ) {
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;
110 # The user enters manually the list of id
111 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
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 );
121 code => 'biblio_not_exists',
122 biblionumber => $record_id,
126 push @records, $biblio;
128 # Retrieve authority information
129 my $authority = Koha::Authority->get_from_authid( $record_id );
130 unless ( $authority ) {
133 code => 'authority_not_exists',
134 authid => $record_id,
140 authid => $record_id,
141 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
146 records => \@records,
150 } elsif ( $op eq 'modify' ) {
151 # We want to modify selected records!
152 my @record_ids = $input->param('record_id');
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;
162 my $reply = CGI->new("");
163 print $reply->header(-type => 'text/html');
164 print '{"jobID":"' . $job_id . '"}';
166 } elsif (defined $pid) {
170 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
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;
185 if ( $recordtype eq 'biblio' ) {
187 my $biblionumber = $record_id;
189 # Finally, modify the biblio
191 my $record = GetMarcBiblio( $biblionumber );
192 ModifyRecordWithTemplate( $mmtid, $record );
193 ModBiblio( $record, $biblionumber );
195 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
198 code => 'biblio_not_modified',
199 biblionumber => $biblionumber,
200 error => ($@ ? $@ : $error),
205 code => 'biblio_modified',
206 biblionumber => $biblionumber,
208 $report->{total_success}++;
212 my $authid = $record_id;
214 my $authority = Koha::Authority->get_from_authid( $authid );
215 my $record = $authority->record;
216 ModifyRecordWithTemplate( $mmtid, $record );
217 ModAuthority( $authid, $record, GetAuthTypeCode( $authid ) );
219 if ( $error and $error != $authid or $@ ) {
222 code => 'authority_not_modified',
224 error => ($@ ? $@ : 0),
229 code => 'authority_modified',
232 $report->{total_success}++;
239 messages => \@messages,
241 $job->progress( ++$progress ) if $runinbackground;
244 if ($runinbackground) {
245 $job->finish if defined $job;
250 messages => \@messages,
256 messages => \@messages,
257 recordtype => $recordtype,
258 MarcModificationTemplatesLoop => \@templates,
261 output_html_with_http_headers $input, $cookie, $template->output;