Bug 33568: Fix columns shift when pref are off
[koha.git] / tools / stage-marc-import.pl
1 #!/usr/bin/perl
2
3 # Script for handling import of MARC data into Koha db
4 #   and Z39.50 lookups
5
6 # Koha library project  www.koha-community.org
7
8 # Licensed under the GPL
9
10 # Copyright 2000-2002 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
14 # Koha is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 3 of the License, or
17 # (at your option) any later version.
18 #
19 # Koha is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26
27 use Modern::Perl;
28
29 # standard or CPAN modules used
30 use CGI qw ( -utf8 );
31 use CGI::Cookie;
32 use MARC::File::USMARC;
33 use Try::Tiny;
34
35 # Koha modules used
36 use C4::Context;
37 use C4::Auth qw( get_template_and_user );
38 use C4::Output qw( output_html_with_http_headers );
39 use C4::Matcher;
40 use Koha::UploadedFiles;
41 use C4::MarcModificationTemplates qw( GetModificationTemplates );
42 use Koha::Plugins;
43 use Koha::ImportBatches;
44 use Koha::BackgroundJob::StageMARCForImport;
45
46 my $input = CGI->new;
47 my $op    = $input->param('op') // q{};
48
49 my $fileID                     = $input->param('uploadedfileid');
50 my $matcher_id                 = $input->param('matcher');
51 my $overlay_action             = $input->param('overlay_action');
52 my $nomatch_action             = $input->param('nomatch_action');
53 my $parse_items                = $input->param('parse_items');
54 my $item_action                = $input->param('item_action');
55 my $comments                   = $input->param('comments');
56 my $record_type                = $input->param('record_type');
57 my $encoding                   = $input->param('encoding') || 'UTF-8';
58 my $format                     = $input->param('format') || 'ISO2709';
59 my $marc_modification_template = $input->param('marc_modification_template_id');
60 my $basketno                   = $input->param('basketno');
61 my $booksellerid               = $input->param('booksellerid');
62 my $profile_id                 = $input->param('profile_id');
63 my @messages;
64
65 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
66     {
67         template_name   => "tools/stage-marc-import.tt",
68         query           => $input,
69         type            => "intranet",
70         flagsrequired   => { tools => 'stage_marc_import' },
71     }
72 );
73
74 $template->param(
75     basketno     => $basketno,
76     booksellerid => $booksellerid,
77 );
78
79 if ( $op eq 'cud-stage' && $fileID ) {
80     my $upload = Koha::UploadedFiles->find( $fileID );
81     my $filepath = $upload->full_path;
82     my $filename = $upload->filename;
83
84     my $params = {
85         record_type                => $record_type,
86         encoding                   => $encoding,
87         format                     => $format,
88         filepath                   => $filepath,
89         filename                   => $filename,
90         marc_modification_template => $marc_modification_template,
91         comments                   => $comments,
92         parse_items                => $parse_items,
93         matcher_id                 => $matcher_id,
94         overlay_action             => $overlay_action,
95         nomatch_action             => $nomatch_action,
96         item_action                => $item_action,
97         basket_id                  => $basketno,
98         vendor_id                  => $booksellerid,
99         profile_id                 => $profile_id,
100     };
101     try {
102         my $job_id = Koha::BackgroundJob::StageMARCForImport->new->enqueue( $params );
103         if ($job_id) {
104             $template->param(
105                 job_enqueued => 1,
106                 job_id => $job_id,
107             );
108         }
109     }
110     catch {
111         warn $_;
112         push @messages,
113           {
114             type  => 'error',
115             code  => 'cannot_enqueue_job',
116             error => $_,
117           };
118     };
119
120 } else {
121     # initial form
122     if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
123         $template->param( "UNIMARC" => 1 );
124     }
125     my @matchers = C4::Matcher::GetMatcherList();
126     $template->param( available_matchers => \@matchers );
127
128     my @templates = GetModificationTemplates();
129     $template->param( MarcModificationTemplatesLoop => \@templates );
130
131     if ( C4::Context->config('enable_plugins') ) {
132
133         my @plugins = Koha::Plugins->new()->GetPlugins({
134             method => 'to_marc',
135         });
136         $template->param( plugins => \@plugins );
137     }
138 }
139
140 $template->param( messages => \@messages );
141
142 output_html_with_http_headers $input, $cookie, $template->output;