Bug 7047: (QA follow-up) Fix tests
[koha.git] / admin / import_export_framework.pl
bloba1a05f9eae67cc6f48074c11f5634a6f8abf0618
1 #!/usr/bin/perl
3 # Copyright 2010-2011 MASmedios.com y Ministerio de Cultura
5 # This file is part of Koha.
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.
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 Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use CGI::Cookie;
24 use C4::Context;
25 use C4::Auth qw/check_cookie_auth/;
26 use C4::ImportExportFramework;
28 my %cookies = CGI::Cookie->fetch();
29 my $authenticated = 0;
30 my ($auth_status, $sessionID);
31 if (exists $cookies{'CGISESSID'}) {
32 ($auth_status, $sessionID) = check_cookie_auth(
33 $cookies{'CGISESSID'}->value,
34 { parameters => 'manage_marc_frameworks' },
37 if ($auth_status eq 'ok') {
38 $authenticated = 1;
41 my $input = new CGI;
43 unless ($authenticated) {
44 print $input->header(-type => 'text/plain', -status => '403 Forbidden');
45 exit 0;
48 my $framework_name = $input->param('frameworkcode') || 'default';
49 my $frameworkcode = ($framework_name eq 'default') ? q{} : $framework_name;
50 my $action = $input->param('action') || 'export';
52 ## Exporting
53 if ($action eq 'export' && $input->request_method() eq 'GET') {
54 my $strXml = '';
55 my $format = $input->param('type_export_' . $framework_name);
56 ExportFramework($frameworkcode, \$strXml, $format);
58 if ($format eq 'csv') {
59 # CSV file
61 # Correctly set the encoding to output plain text in UTF-8
62 binmode(STDOUT,':encoding(UTF-8)');
63 print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $framework_name . '.csv');
64 print $strXml;
65 } else {
66 # ODS file
67 my $strODS = '';
68 createODS($strXml, 'en', \$strODS);
69 print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $framework_name . '.ods');
70 print $strODS;
72 ## Importing
73 } elsif ($input->request_method() eq 'POST') {
74 my $ok = -1;
75 my $fieldname = 'file_import_' . $framework_name;
76 my $filename = $input->param($fieldname);
77 # upload the input file
78 if ($filename && $filename =~ /\.(csv|ods)$/i) {
79 my $extension = $1;
80 my $uploadFd = $input->upload($fieldname);
81 if ($uploadFd && !$input->cgi_error) {
82 my $tmpfilename = $input->tmpFileName(scalar $input->param($fieldname));
83 $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
84 $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename));
87 if ($ok >= 0) { # If everything went ok go to the framework marc structure
88 print $input->redirect( -location => '/cgi-bin/koha/admin/marctagstructure.pl?frameworkcode=' . $frameworkcode);
89 } else {
90 # If something failed go to the list of frameworks and show message
91 print $input->redirect( -location => '/cgi-bin/koha/admin/biblio_framework.pl?error_import_export=' . $frameworkcode);