Bug 10861: (follow-up) for typo in patrons.pref
[koha.git] / admin / import_export_framework.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010-2011 MASmedios.com y Ministerio de Cultura
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 use strict;
22 use warnings;
23 use CGI;
24 use CGI::Cookie;
25 use C4::Context;
26 use C4::Auth qw/check_cookie_auth/;
27 use C4::ImportExportFramework;
28
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' },
36     );
37 }
38 if ($auth_status eq 'ok') {
39     $authenticated = 1;
40 }
41
42 my $input = new CGI;
43
44 unless ($authenticated) {
45     print $input->header(-type => 'text/plain', -status => '403 Forbidden');
46     exit 0;
47 }
48
49 my $frameworkcode = $input->param('frameworkcode') || '';
50 my $action = $input->param('action') || 'export';
51
52 ## Exporting
53 if ($action eq 'export' && $input->request_method() eq 'GET') {
54     my $strXml = '';
55     my $format = $input->param('type_export_' . $frameworkcode);
56     ExportFramework($frameworkcode, \$strXml, $format);
57     if ($format eq 'csv') {
58         # CSV file
59         print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $frameworkcode . '.csv');
60         print $strXml;
61     } elsif ($format eq 'excel') {
62         # Excel-xml file
63         print $input->header(-type => 'application/excel', -attachment => 'export_' . $frameworkcode . '.xml');
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_' . $frameworkcode . '.ods');
70         print $strODS;
71     }
72 ## Importing
73 } elsif ($input->request_method() eq 'POST') {
74     my $ok = -1;
75     my $fieldname = 'file_import_' . $frameworkcode;
76     my $filename = $input->param($fieldname);
77     # upload the input file
78     if ($filename && $filename =~ /\.(csv|ods|xml)$/i) {
79         my $extension = $1;
80         my $uploadFd = $input->upload($fieldname);
81         if ($uploadFd && !$input->cgi_error) {
82             my $tmpfilename = $input->tmpFileName($input->param($fieldname));
83             $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
84             $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename));
85         }
86     }
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);
92     }
93 }