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
;
37 our $dbh = C4
::Context
->dbh;
38 my $op = $input->param('op') // q
|form
|;
39 my $recordtype = $input->param('recordtype') // 'biblio';
40 my $mmtid = $input->param('marc_modification_template_id');
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
45 template_name
=> 'tools/batch_record_modification.tt',
49 flagsrequired
=> { tools
=> 'records_batchmod' },
53 my $sessionID = $input->cookie("CGISESSID");
55 my $runinbackground = $input->param('runinbackground');
56 my $completedJobID = $input->param('completedJobID');
57 if ( $completedJobID ) {
58 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
59 my $report = $job->get('report');
60 my $messages = $job->get('messages');
63 messages
=> $messages,
66 output_html_with_http_headers
$input, $cookie, $template->output;
71 my @templates = GetModificationTemplates
( $mmtid );
72 unless ( @templates ) {
76 errors
=> ['no_template_defined'],
78 output_html_with_http_headers
$input, $cookie, $template->output;
83 my @actions = GetModificationTemplateActions
( $mmtid );
88 code
=> 'no_action_defined_for_the_template',
94 if ( $op eq 'form' ) {
99 } elsif ( $op eq 'list' ) {
100 # List all records to process
101 my ( @records, @record_ids );
102 if ( my $bib_list = $input->param('bib_list') ) {
103 # Come from the basket
104 @record_ids = split /\//, $bib_list;
105 $recordtype = 'biblio';
106 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
107 # A file of id is given
108 while ( my $content = <$uploadfile> ) {
109 next unless $content;
110 $content =~ s/[\r\n]*$//;
111 push @record_ids, $content if $content;
114 # The user enters manually the list of id
115 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
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 );
125 code
=> 'biblio_not_exists',
126 biblionumber
=> $record_id,
130 push @records, $biblio;
132 # Retrieve authority information
133 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $record_id );
134 unless ( $authority ) {
137 code
=> 'authority_not_exists',
138 authid
=> $record_id,
144 authid
=> $record_id,
145 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority->record, $record_id ),
150 records
=> \
@records,
154 } elsif ( $op eq 'modify' ) {
155 # We want to modify selected records!
156 my @record_ids = $input->multi_param('record_id');
159 if ( $runinbackground ) {
160 my $job_size = scalar( @record_ids );
161 $job = C4
::BackgroundJob
->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
162 my $job_id = $job->id;
163 if (my $pid = fork) {
164 $dbh->{InactiveDestroy
} = 1;
166 my $reply = CGI
->new("");
167 print $reply->header(-type
=> 'text/html');
168 print '{"jobID":"' . $job_id . '"}';
170 } elsif (defined $pid) {
173 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
183 $dbh->{RaiseError
} = 1;
184 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
185 $report->{total_records
}++;
186 next unless $record_id;
188 if ( $recordtype eq 'biblio' ) {
190 my $biblionumber = $record_id;
192 # Finally, modify the biblio
194 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
195 ModifyRecordWithTemplate
( $mmtid, $record );
196 my $frameworkcode = C4
::Biblio
::GetFrameworkCode
( $biblionumber );
197 ModBiblio
( $record, $biblionumber, $frameworkcode );
199 if ( $error and $error != 1 or $@
) { # ModBiblio returns 1 if everything as gone well
202 code
=> 'biblio_not_modified',
203 biblionumber
=> $biblionumber,
204 error
=> ($@ ?
$@
: $error),
209 code
=> 'biblio_modified',
210 biblionumber
=> $biblionumber,
212 $report->{total_success
}++;
216 my $authid = $record_id;
218 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $authid );
219 my $record = $authority->record;
220 ModifyRecordWithTemplate
( $mmtid, $record );
221 ModAuthority
( $authid, $record, $authority->authtypecode );
223 if ( $error and $error != $authid or $@
) {
226 code
=> 'authority_not_modified',
228 error
=> ($@ ?
$@
: 0),
233 code
=> 'authority_modified',
236 $report->{total_success
}++;
243 messages
=> \
@messages,
245 $job->progress( ++$progress ) if $runinbackground;
248 if ($runinbackground) {
249 $job->finish if defined $job;
254 messages
=> \
@messages,
260 messages
=> \
@messages,
261 recordtype
=> $recordtype,
262 MarcModificationTemplatesLoop
=> \
@templates,
265 output_html_with_http_headers
$input, $cookie, $template->output;