Bug 30477: Add new UNIMARC installer translation files
[koha.git] / tools / batch_record_modification.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 qw( catch try );
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::MarcModificationTemplates qw(
31     GetModificationTemplateActions
32     GetModificationTemplates
33 );
34
35 use Koha::Biblios;
36 use Koha::BackgroundJob::BatchUpdateBiblio;
37 use Koha::BackgroundJob::BatchUpdateAuthority;
38 use Koha::MetadataRecord::Authority;
39 use Koha::Virtualshelves;
40
41 my $input = CGI->new;
42 our $dbh = C4::Context->dbh;
43 my $op = $input->param('op') // q|form|;
44 my $recordtype = $input->param('recordtype') // 'biblio';
45 my $mmtid = $input->param('marc_modification_template_id');
46
47 my ( @messages );
48
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
50         template_name => 'tools/batch_record_modification.tt',
51         query => $input,
52         type => "intranet",
53         flagsrequired => { tools => 'records_batchmod' },
54 });
55
56 my $sessionID = $input->cookie("CGISESSID");
57
58 my @templates = GetModificationTemplates( $mmtid );
59 unless ( @templates ) {
60     $op = 'error';
61     $template->param(
62         view => 'errors',
63         errors => ['no_template_defined'],
64     );
65     output_html_with_http_headers $input, $cookie, $template->output;
66     exit;
67 }
68
69 if ( $mmtid ) {
70     my @actions = GetModificationTemplateActions( $mmtid );
71     unless ( @actions ) {
72         $op = 'form';
73         push @messages, {
74             type => 'error',
75             code => 'no_action_defined_for_the_template',
76             mmtid => $mmtid,
77         };
78     }
79 }
80
81 if ( $op eq 'form' ) {
82     # Display the form
83     $template->param(
84         view => 'form',
85         lists => Koha::Virtualshelves->search(
86             [
87                 { public => 0, owner => $loggedinuser },
88                 { public => 1 }
89             ]
90         )
91     );
92 } elsif ( $op eq 'list' ) {
93     # List all records to process
94     my ( @records, @record_ids );
95     if ( my $bib_list = $input->param('bib_list') ) {
96         # Come from the basket
97         @record_ids = split /\//, $bib_list;
98         $recordtype = 'biblio';
99     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
100         # A file of id is given
101         binmode $uploadfile, ':encoding(UTF-8)';
102         while ( my $content = <$uploadfile> ) {
103             next unless $content;
104             $content =~ s/[\r\n]*$//;
105             push @record_ids, $content if $content;
106         }
107     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
108         my $shelf = Koha::Virtualshelves->find($shelf_number);
109         my $contents = $shelf->get_contents;
110         while ( my $content = $contents->next ) {
111             my $biblionumber = $content->biblionumber;
112             push @record_ids, $biblionumber;
113         }
114     } else {
115         # The user enters manually the list of id
116         push @record_ids, split( /\s\n/, scalar $input->param('recordnumber_list') );
117     }
118
119     for my $record_id ( uniq @record_ids ) {
120         if ( $recordtype eq 'biblio' ) {
121             # Retrieve biblio information
122             my $biblio = Koha::Biblios->find( $record_id );
123             unless ( $biblio ) {
124                 push @messages, {
125                     type => 'warning',
126                     code => 'biblio_not_exists',
127                     biblionumber => $record_id,
128                 };
129                 next;
130             }
131             push @records, $biblio;
132         } else {
133             # Retrieve authority information
134             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
135             unless ( $authority ) {
136                 push @messages, {
137                     type => 'warning',
138                     code => 'authority_not_exists',
139                     authid => $record_id,
140                 };
141                 next;
142             }
143
144             push @records, {
145                 authid => $record_id,
146                 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
147             };
148         }
149     }
150     $template->param(
151         records => \@records,
152         mmtid => $mmtid,
153         view => 'list',
154     );
155 } elsif ( $op eq 'modify' ) {
156     # We want to modify selected records!
157     my @record_ids = $input->multi_param('record_id');
158
159     try {
160         my $patron = Koha::Patrons->find( $loggedinuser );
161         my $params = {
162             mmtid           => $mmtid,
163             record_ids      => \@record_ids,
164             overlay_context => {
165                 source       => 'batchmod',
166                 categorycode => $patron->categorycode,
167                 userid       => $patron->userid
168             }
169         };
170
171         my $job_id =
172           $recordtype eq 'biblio'
173           ? Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue($params)
174           : Koha::BackgroundJob::BatchUpdateAuthority->new->enqueue($params);
175
176         $template->param(
177             view => 'enqueued',
178             job_id => $job_id,
179         );
180     } catch {
181         push @messages, {
182             type => 'error',
183             code => 'cannot_enqueue_job',
184             error => $_,
185         };
186         $template->param( view => 'errors' );
187     };
188 }
189
190 $template->param(
191     messages => \@messages,
192     recordtype => $recordtype,
193     MarcModificationTemplatesLoop => \@templates,
194 );
195
196 output_html_with_http_headers $input, $cookie, $template->output;