Bug 7679: Fix several bugs in circulation statistics wizard
[koha.git] / admin / import_export_framework.pl
blob5a38a85955e359300f117ed332931eaa201769ea
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 strict;
22 use warnings;
23 use CGI qw ( -utf8 );
24 use CGI::Cookie;
25 use C4::Context;
26 use C4::Auth qw/check_cookie_auth/;
27 use C4::ImportExportFramework;
29 my %cookies = CGI::Cookie->fetch();
30 my $authenticated = 0;
31 my ($auth_status, $sessionID);
32 if (exists $cookies{'CGISESSID'}) {
33 ($auth_status, $sessionID) = check_cookie_auth(
34 $cookies{'CGISESSID'}->value,
35 { parameters => 'parameters_remaining_permissions' },
38 if ($auth_status eq 'ok') {
39 $authenticated = 1;
42 my $input = new CGI;
44 unless ($authenticated) {
45 print $input->header(-type => 'text/plain', -status => '403 Forbidden');
46 exit 0;
49 my $frameworkcode = $input->param('frameworkcode') || 'default';
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_' . $frameworkcode);
56 if ($frameworkcode eq 'default') {
57 ExportFramework('', \$strXml, $format);
58 } else {
59 ExportFramework($frameworkcode, \$strXml, $format);
62 if ($format eq 'csv') {
63 # CSV file
65 # Correctly set the encoding to output plain text in UTF-8
66 binmode(STDOUT,':encoding(UTF-8)');
67 print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $frameworkcode . '.csv');
68 print $strXml;
69 } elsif ($format eq 'excel') {
70 # Excel-xml file
71 print $input->header(-type => 'application/excel', -attachment => 'export_' . $frameworkcode . '.xml');
72 print $strXml;
73 } else {
74 # ODS file
75 my $strODS = '';
76 createODS($strXml, 'en', \$strODS);
77 print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $frameworkcode . '.ods');
78 print $strODS;
80 ## Importing
81 } elsif ($input->request_method() eq 'POST') {
82 my $ok = -1;
83 my $fieldname = 'file_import_' . $frameworkcode;
84 my $filename = $input->param($fieldname);
85 # upload the input file
86 if ($filename && $filename =~ /\.(csv|ods|xml)$/i) {
87 my $extension = $1;
88 my $uploadFd = $input->upload($fieldname);
89 if ($uploadFd && !$input->cgi_error) {
90 my $tmpfilename = $input->tmpFileName($input->param($fieldname));
91 $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
92 $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename));
95 if ($ok >= 0) { # If everything went ok go to the framework marc structure
96 print $input->redirect( -location => '/cgi-bin/koha/admin/marctagstructure.pl?frameworkcode=' . $frameworkcode);
97 } else {
98 # If something failed go to the list of frameworks and show message
99 print $input->redirect( -location => '/cgi-bin/koha/admin/biblio_framework.pl?error_import_export=' . $frameworkcode);