Bug 12759: Use a list (shelf) for batch record modification and deletion
[koha.git] / tools / batch_record_modification.pl
blobb6f65f3ad4f16fccd4326f43b16b1ed4e99b6dd7
1 #!/usr/bin/perl
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>
21 use Modern::Perl;
23 use CGI;
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 );
33 use Koha::Biblios;
34 use Koha::MetadataRecord::Authority;
35 use Koha::Virtualshelves;
37 my $input = new CGI;
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');
43 my ( @messages );
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
46 template_name => 'tools/batch_record_modification.tt',
47 query => $input,
48 type => "intranet",
49 authnotrequired => 0,
50 flagsrequired => { tools => 'records_batchmod' },
51 });
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');
62 $template->param(
63 report => $report,
64 messages => $messages,
65 view => 'report',
67 output_html_with_http_headers $input, $cookie, $template->output;
68 $job->clear();
69 exit;
72 my @lists = Koha::Virtualshelves->search({});
73 $template->param( lists => \@lists );
75 my @templates = GetModificationTemplates( $mmtid );
76 unless ( @templates ) {
77 $op = 'error';
78 $template->param(
79 view => 'errors',
80 errors => ['no_template_defined'],
82 output_html_with_http_headers $input, $cookie, $template->output;
83 exit;
86 if ( $mmtid ) {
87 my @actions = GetModificationTemplateActions( $mmtid );
88 unless ( @actions ) {
89 $op = 'form';
90 push @messages, {
91 type => 'error',
92 code => 'no_action_defined_for_the_template',
93 mmtid => $mmtid,
98 if ( $op eq 'form' ) {
99 # Display the form
100 $template->param(
101 view => '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;
125 } else {
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 );
134 unless ( $biblio ) {
135 push @messages, {
136 type => 'warning',
137 code => 'biblio_not_exists',
138 biblionumber => $record_id,
140 next;
142 push @records, $biblio;
143 } else {
144 # Retrieve authority information
145 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
146 unless ( $authority ) {
147 push @messages, {
148 type => 'warning',
149 code => 'authority_not_exists',
150 authid => $record_id,
152 next;
155 push @records, {
156 authid => $record_id,
157 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
161 $template->param(
162 records => \@records,
163 mmtid => $mmtid,
164 view => 'list',
166 } elsif ( $op eq 'modify' ) {
167 # We want to modify selected records!
168 my @record_ids = $input->multi_param('record_id');
170 my ( $job );
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 . '"}';
181 exit 0;
182 } elsif (defined $pid) {
183 close STDOUT;
184 } else {
185 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
186 exit 0;
190 my $report = {
191 total_records => 0,
192 total_success => 0,
194 my $progress = 0;
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' ) {
201 # Biblios
202 my $biblionumber = $record_id;
204 # Finally, modify the biblio
205 my $error = eval {
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
212 push @messages, {
213 type => 'error',
214 code => 'biblio_not_modified',
215 biblionumber => $biblionumber,
216 error => ($@ ? $@ : $error),
218 } else {
219 push @messages, {
220 type => 'success',
221 code => 'biblio_modified',
222 biblionumber => $biblionumber,
224 $report->{total_success}++;
226 } else {
227 # Authorities
228 my $authid = $record_id;
229 my $error = eval {
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 $@ ) {
236 push @messages, {
237 type => 'error',
238 code => 'authority_not_modified',
239 authid => $authid,
240 error => ($@ ? $@ : 0),
242 } else {
243 push @messages, {
244 type => 'success',
245 code => 'authority_modified',
246 authid => $authid,
248 $report->{total_success}++;
252 $job->set({
253 view => 'report',
254 report => $report,
255 messages => \@messages,
257 $job->progress( ++$progress ) if $runinbackground;
260 if ($runinbackground) {
261 $job->finish if defined $job;
262 } else {
263 $template->param(
264 view => 'report',
265 report => $report,
266 messages => \@messages,
271 $template->param(
272 messages => \@messages,
273 recordtype => $recordtype,
274 MarcModificationTemplatesLoop => \@templates,
277 output_html_with_http_headers $input, $cookie, $template->output;