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' },
53 $template->param( lists
=> scalar Koha
::Virtualshelves
->search([{ category
=> 1, owner
=> $loggedinuser }, { category
=> 2 }]) );
55 my $sessionID = $input->cookie("CGISESSID");
57 my $runinbackground = $input->param('runinbackground');
58 my $completedJobID = $input->param('completedJobID');
59 if ( $completedJobID ) {
60 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
61 my $report = $job->get('report');
62 my $messages = $job->get('messages');
65 messages
=> $messages,
67 recordtype
=> $recordtype,
69 output_html_with_http_headers
$input, $cookie, $template->output;
74 my @templates = GetModificationTemplates
( $mmtid );
75 unless ( @templates ) {
79 errors
=> ['no_template_defined'],
81 output_html_with_http_headers
$input, $cookie, $template->output;
86 my @actions = GetModificationTemplateActions
( $mmtid );
91 code
=> 'no_action_defined_for_the_template',
97 if ( $op eq 'form' ) {
102 } elsif ( $op eq 'list' ) {
103 # List all records to process
104 my ( @records, @record_ids );
105 if ( my $bib_list = $input->param('bib_list') ) {
106 # Come from the basket
107 @record_ids = split /\//, $bib_list;
108 $recordtype = 'biblio';
109 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
110 # A file of id is given
111 binmode $uploadfile, ':encoding(UTF-8)';
112 while ( my $content = <$uploadfile> ) {
113 next unless $content;
114 $content =~ s/[\r\n]*$//;
115 push @record_ids, $content if $content;
117 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
118 my $shelf = Koha
::Virtualshelves
->find($shelf_number);
119 my $contents = $shelf->get_contents;
120 while ( my $content = $contents->next ) {
121 my $biblionumber = $content->biblionumber;
122 push @record_ids, $biblionumber;
125 # The user enters manually the list of id
126 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
129 for my $record_id ( uniq
@record_ids ) {
130 if ( $recordtype eq 'biblio' ) {
131 # Retrieve biblio information
132 my $biblio = Koha
::Biblios
->find( $record_id );
136 code
=> 'biblio_not_exists',
137 biblionumber
=> $record_id,
141 push @records, $biblio;
143 # Retrieve authority information
144 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $record_id );
145 unless ( $authority ) {
148 code
=> 'authority_not_exists',
149 authid
=> $record_id,
155 authid
=> $record_id,
156 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority->record, $record_id ),
161 records
=> \
@records,
165 } elsif ( $op eq 'modify' ) {
166 # We want to modify selected records!
167 my @record_ids = $input->multi_param('record_id');
170 if ( $runinbackground ) {
171 my $job_size = scalar( @record_ids );
172 $job = C4
::BackgroundJob
->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
173 my $job_id = $job->id;
174 if (my $pid = fork) {
175 $dbh->{InactiveDestroy
} = 1;
177 my $reply = CGI
->new("");
178 print $reply->header(-type
=> 'text/html');
179 print '{"jobID":"' . $job_id . '"}';
181 } elsif (defined $pid) {
184 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
194 $dbh->{RaiseError
} = 1;
195 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
196 $report->{total_records
}++;
197 next unless $record_id;
199 if ( $recordtype eq 'biblio' ) {
201 my $biblionumber = $record_id;
203 # Finally, modify the biblio
205 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
206 ModifyRecordWithTemplate
( $mmtid, $record );
207 my $frameworkcode = C4
::Biblio
::GetFrameworkCode
( $biblionumber );
208 ModBiblio
( $record, $biblionumber, $frameworkcode );
210 if ( $error and $error != 1 or $@
) { # ModBiblio returns 1 if everything as gone well
213 code
=> 'biblio_not_modified',
214 biblionumber
=> $biblionumber,
215 error
=> ($@ ?
$@
: $error),
220 code
=> 'biblio_modified',
221 biblionumber
=> $biblionumber,
223 $report->{total_success
}++;
227 my $authid = $record_id;
229 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $authid );
230 my $record = $authority->record;
231 ModifyRecordWithTemplate
( $mmtid, $record );
232 ModAuthority
( $authid, $record, $authority->authtypecode );
234 if ( $error and $error != $authid or $@
) {
237 code
=> 'authority_not_modified',
239 error
=> ($@ ?
$@
: 0),
244 code
=> 'authority_modified',
247 $report->{total_success
}++;
254 messages
=> \
@messages,
256 $job->progress( ++$progress ) if $runinbackground;
259 if ($runinbackground) {
260 $job->finish if defined $job;
265 messages
=> \
@messages,
271 messages
=> \
@messages,
272 recordtype
=> $recordtype,
273 MarcModificationTemplatesLoop
=> \
@templates,
276 output_html_with_http_headers
$input, $cookie, $template->output;