Bug 36582: Add option to set library, desk, and register from user menu
[koha.git] / tools / batch_delete_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
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.
11 #
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.
16 #
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>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25 use Try::Tiny;
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Biblio;
31 use C4::AuthoritiesMarc;
32 use Koha::Acquisition::Orders;
33 use Koha::Virtualshelves;
34
35 use Koha::Authorities;
36 use Koha::Biblios;
37 use Koha::Items;
38 use Koha::BackgroundJob::BatchDeleteBiblio;
39 use Koha::BackgroundJob::BatchDeleteAuthority;
40
41 my $input            = CGI->new;
42 my $op               = $input->param('op') // q|form|;
43 my $recordtype       = $input->param('recordtype') // 'biblio';
44 my $skip_open_orders = $input->param('skip_open_orders') // 0;
45
46 my ($template, $loggedinuser, $cookie) = get_template_and_user({
47         template_name => 'tools/batch_delete_records.tt',
48         query => $input,
49         type => "intranet",
50         flagsrequired => { tools => 'records_batchdel' },
51 });
52
53 my @records;
54 my @messages;
55 if ( $op eq 'form' ) {
56     # Display the form
57     $template->param(
58         op => 'form',
59         lists => Koha::Virtualshelves->search(
60             [
61                 { public => 0, owner => $loggedinuser },
62                 { public => 1 }
63             ]
64         )
65     );
66 } elsif ( $op eq 'cud-list' ) {
67     # List all records to process
68     my @record_ids;
69     if ( my $bib_list = $input->param('bib_list') ) {
70         # Come from the basket
71         @record_ids = split /\//, $bib_list;
72         $recordtype = 'biblio';
73     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
74         # A file of id is given
75         binmode $uploadfile, ':encoding(UTF-8)';
76         while ( my $content = <$uploadfile> ) {
77             next unless $content;
78             $content =~ s/[\r\n]*$//;
79             push @record_ids, $content if $content;
80         }
81     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
82         my $shelf = Koha::Virtualshelves->find($shelf_number);
83         my $contents = $shelf->get_contents;
84         while ( my $content = $contents->next ) {
85             my $biblionumber = $content->biblionumber;
86             push @record_ids, $biblionumber;
87         }
88     } else {
89         # The user enters manually the list of id
90         push @record_ids, split( /\s\n/, scalar $input->param('recordnumber_list') );
91     }
92
93     for my $record_id ( uniq @record_ids ) {
94         if ( $recordtype eq 'biblio' ) {
95             # Retrieve biblio information
96             my $biblio_object = Koha::Biblios->find( $record_id );
97             unless ( $biblio_object ) {
98                 push @messages, {
99                     type => 'warning',
100                     code => 'biblio_not_exists',
101                     biblionumber => $record_id,
102                 };
103                 next;
104             }
105             my $biblio = $biblio_object->unblessed;
106             my $record = $biblio_object->metadata->record;
107             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
108             $biblio->{holds_count} = $biblio_object->holds->count;
109             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
110             $biblio->{subscriptions_count} = $biblio_object->subscriptions->count;
111
112             # Respect skip_open_orders
113             next
114                 if $skip_open_orders
115                 && Koha::Acquisition::Orders->search(
116                 { biblionumber => $record_id, orderstatus => [ 'new', 'ordered', 'partial' ] } )->count;
117
118             push @records, $biblio;
119         } else {
120             # Retrieve authority information
121             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
122             unless ( $authority ) {
123                 push @messages, {
124                     type => 'warning',
125                     code => 'authority_not_exists',
126                     authid => $record_id,
127                 };
128                 next;
129             }
130
131             $authority = {
132                 authid => $record_id,
133                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
134                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
135             };
136             push @records, $authority;
137         }
138     }
139     $template->param(
140         records => \@records,
141         op => 'list',
142     );
143 } elsif ( $op eq 'cud-delete' ) {
144     # We want to delete selected records!
145     my @record_ids = $input->multi_param('record_id');
146
147     try {
148         my $params = {
149             record_ids  => \@record_ids,
150         };
151
152         my $job_id =
153           $recordtype eq 'biblio'
154           ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
155           : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
156
157         $template->param(
158             op => 'enqueued',
159             job_id => $job_id,
160         );
161     } catch {
162         push @messages, {
163             type => 'error',
164             code => 'cannot_enqueue_job',
165             error => $_,
166         };
167         $template->param( view => 'errors' );
168     };
169 }
170
171 $template->param(
172     messages => \@messages,
173     recordtype => $recordtype,
174 );
175
176 output_html_with_http_headers $input, $cookie, $template->output;