Bug 16554: rewrite mandatory and sample data - es-ES
[koha.git] / tools / batch_record_modification.pl
blob6ea0317955dba9aa5df35dabe5f07f1a3659fe10
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 );
32 use Koha::MetadataRecord::Authority;
34 my $input = new CGI;
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');
40 my ( @messages );
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
43 template_name => 'tools/batch_record_modification.tt',
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { tools => 'records_batchmod' },
48 });
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');
59 $template->param(
60 report => $report,
61 messages => $messages,
62 view => 'report',
64 output_html_with_http_headers $input, $cookie, $template->output;
65 $job->clear();
66 exit;
69 my @templates = GetModificationTemplates( $mmtid );
70 unless ( @templates ) {
71 $op = 'error';
72 $template->param(
73 view => 'errors',
74 errors => ['no_template_defined'],
76 output_html_with_http_headers $input, $cookie, $template->output;
77 exit;
80 if ( $mmtid ) {
81 my @actions = GetModificationTemplateActions( $mmtid );
82 unless ( @actions ) {
83 $op = 'form';
84 push @messages, {
85 type => 'error',
86 code => 'no_action_defined_for_the_template',
87 mmtid => $mmtid,
92 if ( $op eq 'form' ) {
93 # Display the form
94 $template->param(
95 view => '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;
111 } else {
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 );
120 unless ( $biblio ) {
121 push @messages, {
122 type => 'warning',
123 code => 'biblio_not_exists',
124 biblionumber => $record_id,
126 next;
128 push @records, $biblio;
129 } else {
130 # Retrieve authority information
131 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
132 unless ( $authority ) {
133 push @messages, {
134 type => 'warning',
135 code => 'authority_not_exists',
136 authid => $record_id,
138 next;
141 push @records, {
142 authid => $record_id,
143 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
147 $template->param(
148 records => \@records,
149 mmtid => $mmtid,
150 view => 'list',
152 } elsif ( $op eq 'modify' ) {
153 # We want to modify selected records!
154 my @record_ids = $input->multi_param('record_id');
156 my ( $job );
157 if ( $runinbackground ) {
158 my $job_size = scalar( @record_ids );
159 $job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $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 . '"}';
167 exit 0;
168 } elsif (defined $pid) {
169 close STDOUT;
170 } else {
171 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
172 exit 0;
176 my $report = {
177 total_records => 0,
178 total_success => 0,
180 my $progress = 0;
181 $dbh->{RaiseError} = 1;
182 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
183 $report->{total_records}++;
184 next unless $record_id;
186 if ( $recordtype eq 'biblio' ) {
187 # Biblios
188 my $biblionumber = $record_id;
190 # Finally, modify the biblio
191 my $error = eval {
192 my $record = GetMarcBiblio( $biblionumber );
193 ModifyRecordWithTemplate( $mmtid, $record );
194 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
195 ModBiblio( $record, $biblionumber, $frameworkcode );
197 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
198 push @messages, {
199 type => 'error',
200 code => 'biblio_not_modified',
201 biblionumber => $biblionumber,
202 error => ($@ ? $@ : $error),
204 } else {
205 push @messages, {
206 type => 'success',
207 code => 'biblio_modified',
208 biblionumber => $biblionumber,
210 $report->{total_success}++;
212 } else {
213 # Authorities
214 my $authid = $record_id;
215 my $error = eval {
216 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
217 my $record = $authority->record;
218 ModifyRecordWithTemplate( $mmtid, $record );
219 ModAuthority( $authid, $record, $authority->authtypecode );
221 if ( $error and $error != $authid or $@ ) {
222 push @messages, {
223 type => 'error',
224 code => 'authority_not_modified',
225 authid => $authid,
226 error => ($@ ? $@ : 0),
228 } else {
229 push @messages, {
230 type => 'success',
231 code => 'authority_modified',
232 authid => $authid,
234 $report->{total_success}++;
238 $job->set({
239 view => 'report',
240 report => $report,
241 messages => \@messages,
243 $job->progress( ++$progress ) if $runinbackground;
246 if ($runinbackground) {
247 $job->finish if defined $job;
248 } else {
249 $template->param(
250 view => 'report',
251 report => $report,
252 messages => \@messages,
257 $template->param(
258 messages => \@messages,
259 recordtype => $recordtype,
260 MarcModificationTemplatesLoop => \@templates,
263 output_html_with_http_headers $input, $cookie, $template->output;