Bug 15208: Ease translation for shelves messages
[koha.git] / tools / batch_delete_records.pl
blob65cbf21e3ca687c590ae10e0420643b8cbc07dbc
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;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Biblio;
31 my $input = new CGI;
32 my $dbh = C4::Context->dbh;
33 my $op = $input->param('op') // q|form|;
34 my $recordtype = $input->param('recordtype') // 'biblio';
36 my ($template, $loggedinuser, $cookie) = get_template_and_user({
37 template_name => 'tools/batch_delete_records.tt',
38 query => $input,
39 type => "intranet",
40 authnotrequired => 0,
41 flagsrequired => { tools => 'records_batchdel' },
42 });
44 my @records;
45 my @messages;
46 if ( $op eq 'form' ) {
47 # Display the form
48 $template->param( op => 'form' );
49 } elsif ( $op eq 'list' ) {
50 # List all records to process
51 my @record_ids;
52 if ( my $bib_list = $input->param('bib_list') ) {
53 # Come from the basket
54 @record_ids = split /\//, $bib_list;
55 $recordtype = 'biblio';
56 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
57 # A file of id is given
58 while ( my $content = <$uploadfile> ) {
59 next unless $content;
60 $content =~ s/[\r\n]*$//;
61 push @record_ids, $content if $content;
63 } else {
64 # The user enters manually the list of id
65 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
68 for my $record_id ( uniq @record_ids ) {
69 if ( $recordtype eq 'biblio' ) {
70 # Retrieve biblio information
71 my $biblio = C4::Biblio::GetBiblio( $record_id );
72 unless ( $biblio ) {
73 push @messages, {
74 type => 'warning',
75 code => 'biblio_not_exists',
76 biblionumber => $record_id,
78 next;
80 my $record = &GetMarcBiblio( $record_id );
81 $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
82 $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id );
83 $biblio->{reserves} = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $record_id });
84 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
85 push @records, $biblio;
86 } else {
87 # Retrieve authority information
88 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
89 unless ( $authority ) {
90 push @messages, {
91 type => 'warning',
92 code => 'authority_not_exists',
93 authid => $record_id,
95 next;
98 $authority = {
99 authid => $record_id,
100 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
101 count_usage => C4::AuthoritiesMarc::CountUsage( $record_id ),
103 push @records, $authority;
106 $template->param(
107 records => \@records,
108 op => 'list',
110 } elsif ( $op eq 'delete' ) {
111 # We want to delete selected records!
112 my @record_ids = $input->param('record_id');
113 my $dbh = C4::Context->dbh;
114 $dbh->{AutoCommit} = 0;
115 $dbh->{RaiseError} = 1;
117 my $error;
118 my $report = {
119 total_records => 0,
120 total_success => 0,
122 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
123 $report->{total_records}++;
124 next unless $record_id;
125 if ( $recordtype eq 'biblio' ) {
126 # Biblios
127 my $biblionumber = $record_id;
128 # First, checking if issues exist.
129 # If yes, nothing to do
130 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
131 push @messages, {
132 type => 'warning',
133 code => 'item_issued',
134 biblionumber => $biblionumber,
136 $dbh->rollback;
137 next;
140 # Cancel reserves
141 my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber });
142 for my $reserve ( @$reserves ) {
143 eval{
144 C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } );
146 if ( $@ ) {
147 push @messages, {
148 type => 'error',
149 code => 'reserve_not_cancelled',
150 biblionumber => $biblionumber,
151 reserve_id => $reserve->{reserve_id},
152 error => $@,
154 $dbh->rollback;
155 next RECORD_IDS;
159 # Delete items
160 my @itemnumbers = @{ C4::Items::GetItemnumbersForBiblio( $biblionumber ) };
161 ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
162 my $error = eval { C4::Items::DelItemCheck( $dbh, $biblionumber, $itemnumber ) };
163 if ( $error != 1 or $@ ) {
164 push @messages, {
165 type => 'error',
166 code => 'item_not_deleted',
167 biblionumber => $biblionumber,
168 itemnumber => $itemnumber,
169 error => ($@ ? $@ : $error),
171 $dbh->rollback;
172 next RECORD_IDS;
176 # Finally, delete the biblio
177 my $error = eval {
178 C4::Biblio::DelBiblio( $biblionumber );
180 if ( $error or $@ ) {
181 push @messages, {
182 type => 'error',
183 code => 'biblio_not_deleted',
184 biblionumber => $biblionumber,
185 error => ($@ ? $@ : $error),
187 $dbh->rollback;
188 next;
191 push @messages, {
192 type => 'success',
193 code => 'biblio_deleted',
194 biblionumber => $biblionumber,
196 $report->{total_success}++;
197 $dbh->commit;
198 } else {
199 # Authorities
200 my $authid = $record_id;
201 my $r = eval { C4::AuthoritiesMarc::DelAuthority( $authid ) };
202 if ( $r eq '0E0' or $@ ) {
203 push @messages, {
204 type => 'error',
205 code => 'authority_not_deleted',
206 authid => $authid,
207 error => ($@ ? $@ : 0),
209 $dbh->rollback;
210 next;
211 } else {
212 push @messages, {
213 type => 'success',
214 code => 'authority_deleted',
215 authid => $authid,
217 $report->{total_success}++;
218 $dbh->commit;
222 $template->param(
223 op => 'report',
224 report => $report,
228 $template->param(
229 messages => \@messages,
230 recordtype => $recordtype,
233 output_html_with_http_headers $input, $cookie, $template->output;