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;
69 my @templates = GetModificationTemplates
( $mmtid );
70 unless ( @templates ) {
74 errors
=> ['no_template_defined'],
76 output_html_with_http_headers
$input, $cookie, $template->output;
81 my @actions = GetModificationTemplateActions
( $mmtid );
86 code
=> 'no_action_defined_for_the_template',
92 if ( $op eq 'form' ) {
97 } elsif ( $op eq 'list' ) {
98 # List all records to process
99 my ( @records, @record_ids );
100 if ( my $bib_list = $input->param('bib_list') ) {
101 # Come from the basket
102 @record_ids = split /\//, $bib_list;
103 $recordtype = 'biblio';
104 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
105 # A file of id is given
106 while ( my $content = <$uploadfile> ) {
107 next unless $content;
108 $content =~ s/[\r\n]*$//;
109 push @record_ids, $content if $content;
112 # The user enters manually the list of id
113 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
116 for my $record_id ( uniq
@record_ids ) {
117 if ( $recordtype eq 'biblio' ) {
118 # Retrieve biblio information
119 my $biblio = C4
::Biblio
::GetBiblio
( $record_id );
123 code
=> 'biblio_not_exists',
124 biblionumber
=> $record_id,
128 push @records, $biblio;
130 # Retrieve authority information
131 my $authority = Koha
::Authority
->get_from_authid( $record_id );
132 unless ( $authority ) {
135 code
=> 'authority_not_exists',
136 authid
=> $record_id,
142 authid
=> $record_id,
143 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority->record, $record_id ),
148 records
=> \
@records,
152 } elsif ( $op eq 'modify' ) {
153 # We want to modify selected records!
154 my @record_ids = $input->param('record_id');
157 if ( $runinbackground ) {
158 my $job_size = scalar( @record_ids );
159 $job = C4
::BackgroundJob
->new( $sessionID, "FIXME", $ENV{SCRIPT_NAME
}, $job_size );
160 my $job_id = $job->id;
161 if (my $pid = fork) {
162 $dbh->{InactiveDestroy
} = 1;
164 my $reply = CGI
->new("");
165 print $reply->header(-type
=> 'text/html');
166 print '{"jobID":"' . $job_id . '"}';
168 } elsif (defined $pid) {
172 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
182 $dbh->{RaiseError
} = 1;
183 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
184 $report->{total_records
}++;
185 next unless $record_id;
187 if ( $recordtype eq 'biblio' ) {
189 my $biblionumber = $record_id;
191 # Finally, modify the biblio
193 my $record = GetMarcBiblio
( $biblionumber );
194 ModifyRecordWithTemplate
( $mmtid, $record );
195 ModBiblio
( $record, $biblionumber );
197 if ( $error and $error != 1 or $@
) { # ModBiblio returns 1 if everything as gone well
200 code
=> 'biblio_not_modified',
201 biblionumber
=> $biblionumber,
202 error
=> ($@ ?
$@
: $error),
207 code
=> 'biblio_modified',
208 biblionumber
=> $biblionumber,
210 $report->{total_success
}++;
214 my $authid = $record_id;
216 my $authority = Koha
::Authority
->get_from_authid( $authid );
217 my $record = $authority->record;
218 ModifyRecordWithTemplate
( $mmtid, $record );
219 ModAuthority
( $authid, $record, GetAuthTypeCode
( $authid ) );
221 if ( $error and $error != $authid or $@
) {
224 code
=> 'authority_not_modified',
226 error
=> ($@ ?
$@
: 0),
231 code
=> 'authority_modified',
234 $report->{total_success
}++;
241 messages
=> \
@messages,
243 $job->progress( ++$progress ) if $runinbackground;
246 if ($runinbackground) {
247 $job->finish if defined $job;
252 messages
=> \
@messages,
258 messages
=> \
@messages,
259 recordtype
=> $recordtype,
260 MarcModificationTemplatesLoop
=> \
@templates,
263 output_html_with_http_headers
$input, $cookie, $template->output;