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 ModAuthority );
29 use C4::BackgroundJob;
30 use C4::Biblio qw( GetMarcBiblio ModBiblio );
31 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates ModifyRecordWithTemplate );
34 use Koha::MetadataRecord::Authority;
35 use Koha::Virtualshelves;
38 our $dbh = C4::Context->dbh;
39 my $op = $input->param('op') // q|form|;
40 my $recordtype = $input->param('recordtype') // 'biblio';
41 my $mmtid = $input->param('marc_modification_template_id');
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
46 template_name => 'tools/batch_record_modification.tt',
50 flagsrequired => { tools => 'records_batchmod' },
54 my $sessionID = $input->cookie("CGISESSID");
56 my $runinbackground = $input->param('runinbackground');
57 my $completedJobID = $input->param('completedJobID');
58 if ( $completedJobID ) {
59 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
60 my $report = $job->get('report');
61 my $messages = $job->get('messages');
64 messages => $messages,
67 output_html_with_http_headers $input, $cookie, $template->output;
72 my @lists = Koha::Virtualshelves->search({});
73 $template->param( lists => \@lists );
75 my @templates = GetModificationTemplates( $mmtid );
76 unless ( @templates ) {
80 errors => ['no_template_defined'],
82 output_html_with_http_headers $input, $cookie, $template->output;
87 my @actions = GetModificationTemplateActions( $mmtid );
92 code => 'no_action_defined_for_the_template',
98 if ( $op eq 'form' ) {
103 } elsif ( $op eq 'list' ) {
104 # List all records to process
105 my ( @records, @record_ids );
106 if ( my $bib_list = $input->param('bib_list') ) {
107 # Come from the basket
108 @record_ids = split /\//, $bib_list;
109 $recordtype = 'biblio';
110 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
111 # A file of id is given
112 binmode $uploadfile, ':encoding(UTF-8)';
113 while ( my $content = <$uploadfile> ) {
114 next unless $content;
115 $content =~ s/[\r\n]*$//;
116 push @record_ids, $content if $content;
118 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
119 my $shelf = Koha::Virtualshelves->find($shelf_number);
120 my $contents = $shelf->get_contents;
121 while ( my $content = $contents->next ) {
122 my $biblionumber = $content->biblionumber;
123 push @record_ids, $biblionumber;
126 # The user enters manually the list of id
127 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
130 for my $record_id ( uniq @record_ids ) {
131 if ( $recordtype eq 'biblio' ) {
132 # Retrieve biblio information
133 my $biblio = Koha::Biblios->find( $record_id );
137 code => 'biblio_not_exists',
138 biblionumber => $record_id,
142 push @records, $biblio;
144 # Retrieve authority information
145 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
146 unless ( $authority ) {
149 code => 'authority_not_exists',
150 authid => $record_id,
156 authid => $record_id,
157 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
162 records => \@records,
166 } elsif ( $op eq 'modify' ) {
167 # We want to modify selected records!
168 my @record_ids = $input->multi_param('record_id');
171 if ( $runinbackground ) {
172 my $job_size = scalar( @record_ids );
173 $job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
174 my $job_id = $job->id;
175 if (my $pid = fork) {
176 $dbh->{InactiveDestroy} = 1;
178 my $reply = CGI->new("");
179 print $reply->header(-type => 'text/html');
180 print '{"jobID":"' . $job_id . '"}';
182 } elsif (defined $pid) {
185 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
195 $dbh->{RaiseError} = 1;
196 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
197 $report->{total_records}++;
198 next unless $record_id;
200 if ( $recordtype eq 'biblio' ) {
202 my $biblionumber = $record_id;
204 # Finally, modify the biblio
206 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
207 ModifyRecordWithTemplate( $mmtid, $record );
208 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
209 ModBiblio( $record, $biblionumber, $frameworkcode );
211 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
214 code => 'biblio_not_modified',
215 biblionumber => $biblionumber,
216 error => ($@ ? $@ : $error),
221 code => 'biblio_modified',
222 biblionumber => $biblionumber,
224 $report->{total_success}++;
228 my $authid = $record_id;
230 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
231 my $record = $authority->record;
232 ModifyRecordWithTemplate( $mmtid, $record );
233 ModAuthority( $authid, $record, $authority->authtypecode );
235 if ( $error and $error != $authid or $@ ) {
238 code => 'authority_not_modified',
240 error => ($@ ? $@ : 0),
245 code => 'authority_modified',
248 $report->{total_success}++;
255 messages => \@messages,
257 $job->progress( ++$progress ) if $runinbackground;
260 if ($runinbackground) {
261 $job->finish if defined $job;
266 messages => \@messages,
272 messages => \@messages,
273 recordtype => $recordtype,
274 MarcModificationTemplatesLoop => \@templates,
277 output_html_with_http_headers $input, $cookie, $template->output;