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',
49 flagsrequired
=> { tools
=> 'records_batchmod' },
52 $template->param( lists
=> scalar Koha
::Virtualshelves
->search([{ category
=> 1, owner
=> $loggedinuser }, { category
=> 2 }]) );
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,
66 recordtype
=> $recordtype,
68 output_html_with_http_headers
$input, $cookie, $template->output;
73 my @templates = GetModificationTemplates
( $mmtid );
74 unless ( @templates ) {
78 errors
=> ['no_template_defined'],
80 output_html_with_http_headers
$input, $cookie, $template->output;
85 my @actions = GetModificationTemplateActions
( $mmtid );
90 code
=> 'no_action_defined_for_the_template',
96 if ( $op eq 'form' ) {
101 } elsif ( $op eq 'list' ) {
102 # List all records to process
103 my ( @records, @record_ids );
104 if ( my $bib_list = $input->param('bib_list') ) {
105 # Come from the basket
106 @record_ids = split /\//, $bib_list;
107 $recordtype = 'biblio';
108 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
109 # A file of id is given
110 binmode $uploadfile, ':encoding(UTF-8)';
111 while ( my $content = <$uploadfile> ) {
112 next unless $content;
113 $content =~ s/[\r\n]*$//;
114 push @record_ids, $content if $content;
116 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
117 my $shelf = Koha
::Virtualshelves
->find($shelf_number);
118 my $contents = $shelf->get_contents;
119 while ( my $content = $contents->next ) {
120 my $biblionumber = $content->biblionumber;
121 push @record_ids, $biblionumber;
124 # The user enters manually the list of id
125 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
128 for my $record_id ( uniq
@record_ids ) {
129 if ( $recordtype eq 'biblio' ) {
130 # Retrieve biblio information
131 my $biblio = Koha
::Biblios
->find( $record_id );
135 code
=> 'biblio_not_exists',
136 biblionumber
=> $record_id,
140 push @records, $biblio;
142 # Retrieve authority information
143 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $record_id );
144 unless ( $authority ) {
147 code
=> 'authority_not_exists',
148 authid
=> $record_id,
154 authid
=> $record_id,
155 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority->record, $record_id ),
160 records
=> \
@records,
164 } elsif ( $op eq 'modify' ) {
165 # We want to modify selected records!
166 my @record_ids = $input->multi_param('record_id');
169 if ( $runinbackground ) {
170 my $job_size = scalar( @record_ids );
171 $job = C4
::BackgroundJob
->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
172 my $job_id = $job->id;
173 if (my $pid = fork) {
174 $dbh->{InactiveDestroy
} = 1;
176 my $reply = CGI
->new("");
177 print $reply->header(-type
=> 'text/html');
178 print '{"jobID":"' . $job_id . '"}';
180 } elsif (defined $pid) {
183 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
193 $dbh->{RaiseError
} = 1;
194 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
195 $report->{total_records
}++;
196 next unless $record_id;
198 if ( $recordtype eq 'biblio' ) {
200 my $biblionumber = $record_id;
202 # Finally, modify the biblio
204 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
205 ModifyRecordWithTemplate
( $mmtid, $record );
206 my $frameworkcode = C4
::Biblio
::GetFrameworkCode
( $biblionumber );
207 ModBiblio
( $record, $biblionumber, $frameworkcode );
209 if ( $error and $error != 1 or $@
) { # ModBiblio returns 1 if everything as gone well
212 code
=> 'biblio_not_modified',
213 biblionumber
=> $biblionumber,
214 error
=> ($@ ?
$@
: $error),
219 code
=> 'biblio_modified',
220 biblionumber
=> $biblionumber,
222 $report->{total_success
}++;
226 my $authid = $record_id;
228 my $authority = Koha
::MetadataRecord
::Authority
->get_from_authid( $authid );
229 my $record = $authority->record;
230 ModifyRecordWithTemplate
( $mmtid, $record );
231 ModAuthority
( $authid, $record, $authority->authtypecode );
233 if ( $error and $error != $authid or $@
) {
236 code
=> 'authority_not_modified',
238 error
=> ($@ ?
$@
: 0),
243 code
=> 'authority_modified',
246 $report->{total_success
}++;
253 messages
=> \
@messages,
255 $job->progress( ++$progress ) if $runinbackground;
258 if ($runinbackground) {
259 $job->finish if defined $job;
264 messages
=> \
@messages,
270 messages
=> \
@messages,
271 recordtype
=> $recordtype,
272 MarcModificationTemplatesLoop
=> \
@templates,
275 output_html_with_http_headers
$input, $cookie, $template->output;