Bug 31095: Remove GetDebarments from tools/modborrowers.pl
[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
48 my $fileID                     = $input->param('uploadedfileid');
49 my $matcher_id                 = $input->param('matcher');
50 my $overlay_action             = $input->param('overlay_action');
51 my $nomatch_action             = $input->param('nomatch_action');
52 my $parse_items                = $input->param('parse_items');
53 my $item_action                = $input->param('item_action');
54 my $comments                   = $input->param('comments');
55 my $record_type                = $input->param('record_type');
56 my $encoding                   = $input->param('encoding') || 'UTF-8';
57 my $format                     = $input->param('format') || 'ISO2709';
58 my $marc_modification_template = $input->param('marc_modification_template_id');
59 my $basketno                   = $input->param('basketno');
60 my $booksellerid               = $input->param('booksellerid');
61 my $profile_id                 = $input->param('profile_id');
62 my @messages;
63
64 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
65     {
66         template_name   => "tools/stage-marc-import.tt",
67         query           => $input,
68         type            => "intranet",
69         flagsrequired   => { tools => 'stage_marc_import' },
70     }
71 );
72
73 $template->param(
74     basketno     => $basketno,
75     booksellerid => $booksellerid,
76 );
77
78 if ($fileID) {
79     my $upload = Koha::UploadedFiles->find( $fileID );
80     my $filepath = $upload->full_path;
81     my $filename = $upload->filename;
82
83     my $params = {
84         record_type                => $record_type,
85         encoding                   => $encoding,
86         format                     => $format,
87         filepath                   => $filepath,
88         filename                   => $filename,
89         marc_modification_template => $marc_modification_template,
90         comments                   => $comments,
91         parse_items                => $parse_items,
92         matcher_id                 => $matcher_id,
93         overlay_action             => $overlay_action,
94         nomatch_action             => $nomatch_action,
95         item_action                => $item_action,
96         basket_id                  => $basketno,
97         vendor_id                  => $booksellerid,
98     };
99     try {
100         my $job_id = Koha::BackgroundJob::StageMARCForImport->new->enqueue( $params );
101         if ($job_id) {
102             $template->param(
103                 job_enqueued => 1,
104                 job_id => $job_id,
105             );
106         }
107     }
108     catch {
109         warn $_;
110         push @messages,
111           {
112             type  => 'error',
113             code  => 'cannot_enqueue_job',
114             error => $_,
115           };
116     };
117
118 } else {
119     # initial form
120     if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
121         $template->param( "UNIMARC" => 1 );
122     }
123     my @matchers = C4::Matcher::GetMatcherList();
124     $template->param( available_matchers => \@matchers );
125
126     my @templates = GetModificationTemplates();
127     $template->param( MarcModificationTemplatesLoop => \@templates );
128
129     if ( C4::Context->config('enable_plugins') ) {
130
131         my @plugins = Koha::Plugins->new()->GetPlugins({
132             method => 'to_marc',
133         });
134         $template->param( plugins => \@plugins );
135     }
136 }
137
138 $template->param( messages => \@messages );
139
140 output_html_with_http_headers $input, $cookie, $template->output;