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>
24 use List::MoreUtils qw( uniq );
25 use Try::Tiny qw( catch try );
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
36 use Koha::BackgroundJob::BatchUpdateBiblio;
37 use Koha::BackgroundJob::BatchUpdateAuthority;
38 use Koha::MetadataRecord::Authority;
39 use Koha::Virtualshelves;
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');
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
50 template_name => 'tools/batch_record_modification.tt',
53 flagsrequired => { tools => 'records_batchmod' },
56 my $sessionID = $input->cookie("CGISESSID");
58 my @templates = GetModificationTemplates( $mmtid );
59 unless ( @templates ) {
63 errors => ['no_template_defined'],
65 output_html_with_http_headers $input, $cookie, $template->output;
70 my @actions = GetModificationTemplateActions( $mmtid );
75 code => 'no_action_defined_for_the_template',
81 if ( $op eq 'form' ) {
86 } elsif ( $op eq 'list' ) {
87 # List all records to process
88 my ( @records, @record_ids );
89 if ( my $bib_list = $input->param('bib_list') ) {
90 # Come from the basket
91 @record_ids = split /\//, $bib_list;
92 $recordtype = 'biblio';
93 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
94 # A file of id is given
95 binmode $uploadfile, ':encoding(UTF-8)';
96 while ( my $content = <$uploadfile> ) {
98 $content =~ s/[\r\n]*$//;
99 push @record_ids, $content if $content;
101 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
102 my $shelf = Koha::Virtualshelves->find($shelf_number);
103 my $contents = $shelf->get_contents;
104 while ( my $content = $contents->next ) {
105 my $biblionumber = $content->biblionumber;
106 push @record_ids, $biblionumber;
109 # The user enters manually the list of id
110 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
113 for my $record_id ( uniq @record_ids ) {
114 if ( $recordtype eq 'biblio' ) {
115 # Retrieve biblio information
116 my $biblio = Koha::Biblios->find( $record_id );
120 code => 'biblio_not_exists',
121 biblionumber => $record_id,
125 push @records, $biblio;
127 # Retrieve authority information
128 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
129 unless ( $authority ) {
132 code => 'authority_not_exists',
133 authid => $record_id,
139 authid => $record_id,
140 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
145 records => \@records,
149 } elsif ( $op eq 'modify' ) {
150 # We want to modify selected records!
151 my @record_ids = $input->multi_param('record_id');
156 record_ids => \@record_ids,
160 $recordtype eq 'biblio'
161 ? Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue($params)
162 : Koha::BackgroundJob::BatchUpdateAuthority->new->enqueue($params);
171 code => 'cannot_enqueue_job',
174 $template->param( view => 'errors' );
179 messages => \@messages,
180 recordtype => $recordtype,
181 MarcModificationTemplatesLoop => \@templates,
184 output_html_with_http_headers $input, $cookie, $template->output;