e12bcc788728a02b7b94b21e5410be3f92223598
[koha.git] / admin / import_export_authtype.pl
1 #!/usr/bin/perl
2
3 # Copyright 2016 Aleisha Amohia <aleisha@catalyst.net.nz>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (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 Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use CGI::Cookie;
23 use C4::Context;
24 use C4::Auth qw/check_cookie_auth/;
25 use C4::ImportExportFramework qw( ExportFramework ImportFramework createODS );
26
27 my %cookies = CGI::Cookie->fetch();
28 my ($auth_status, $sessionID);
29 if (exists $cookies{'CGISESSID'}) {
30     ($auth_status, $sessionID) = check_cookie_auth(
31         $cookies{'CGISESSID'}->value,
32         { parameters => 'parameters_remaining_permissions' },
33     );
34 }
35
36 my $input = CGI->new;
37
38 unless ($auth_status eq 'ok') {
39     print $input->header(-type => 'text/plain', -status => '403 Forbidden');
40     exit 0;
41 }
42
43 my $authtypecode = $input->param('authtypecode') || 'default';
44 my $action = $input->param('action') || 'export';
45
46 ## Exporting
47 if ($action eq 'export' && $input->request_method() eq 'GET') {
48     my $strXml = '';
49     my $format = $input->param('type_export_' . $authtypecode);
50     if ($authtypecode eq 'default') {
51         $format = $input->param('type_export_');
52         ExportFramework('', \$strXml, $format, 'authority');
53     } else {
54         ExportFramework($authtypecode, \$strXml, $format, 'authority');
55     }
56
57     if ($format eq 'csv') {
58         # CSV file
59
60         # Correctly set the encoding to output plain text in UTF-8
61         binmode(STDOUT,':encoding(UTF-8)');
62         print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $authtypecode . '.csv');
63         print $strXml;
64     } else {
65         # ODS file
66         my $strODS = '';
67         createODS($strXml, 'en', \$strODS);
68         print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $authtypecode . '.ods');
69         print $strODS;
70     }
71 ## Importing
72 } elsif ($input->request_method() eq 'POST') {
73     my $ok = -1;
74     my $fieldname = 'file_import_' . $authtypecode;
75     if ($authtypecode eq 'default'){
76         $fieldname = 'file_import_';
77     }
78     my $filename = $input->param($fieldname);
79     # upload the input file
80     if ($filename && $filename =~ /\.(csv|ods)$/i) {
81         my $extension = $1;
82         my $uploadFd = $input->upload($fieldname);
83         if ($uploadFd && !$input->cgi_error) {
84             my $tmpfilename = $input->tmpFileName(scalar $input->param($fieldname));
85             $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
86             if ($authtypecode eq 'default') {
87                 $ok = ImportFramework($filename, '', 1, 'authority') if (rename($tmpfilename, $filename));
88             } else {
89                 $ok = ImportFramework($filename, $authtypecode, 1, 'authority') if (rename($tmpfilename, $filename));
90             }
91         }
92     }
93     if ($ok >= 0) { # If everything went ok go to the authority type marc structure
94         if ($authtypecode eq 'default'){
95             print $input->redirect( -location => '/cgi-bin/koha/admin/auth_tag_structure.pl?authtypecode=');
96         } else {
97             print $input->redirect( -location => '/cgi-bin/koha/admin/auth_tag_structure.pl?authtypecode=' . $authtypecode);
98         }
99     } else {
100         # If something failed go to the list of authority types and show message
101         print $input->redirect( -location => '/cgi-bin/koha/admin/authtypes.pl?error_import_export=' . $authtypecode);
102     }
103 }