Merge remote-tracking branch 'kc/new/enh/bug_4877' into kcmaster
[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 C4::Context;
25 use C4::ImportExportFramework;
26
27 my $input = new CGI;
28
29 my $frameworkcode = $input->param('frameworkcode') || '';
30 my $action = $input->param('action') || 'export';
31
32 ## Exporting
33 if ($action eq 'export' && $input->request_method() eq 'GET') {
34     my $strXml = '';
35     my $format = $input->param('type_export_' . $frameworkcode);
36     ExportFramework($frameworkcode, \$strXml, $format);
37     if ($format eq 'csv') {
38         # CSV file
39         print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $frameworkcode . '.csv');
40         print $strXml;
41     } elsif ($format eq 'sql') {
42         # SQL file
43         print $input->header(-type => 'text/plain', -attachment => 'export_' . $frameworkcode . '.sql');
44         print $strXml;
45     } elsif ($format eq 'excel') {
46         # Excel-xml file
47         print $input->header(-type => 'application/excel', -attachment => 'export_' . $frameworkcode . '.xml');
48         print $strXml;
49     } else {
50         # ODS file
51         my $strODS = '';
52         createODS($strXml, 'en', \$strODS);
53         print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $frameworkcode . '.ods');
54         print $strODS;
55     }
56 ## Importing
57 } elsif ($input->request_method() eq 'POST') {
58     my $ok = -1;
59     my $fieldname = 'file_import_' . $frameworkcode;
60     my $filename = $input->param($fieldname);
61     # upload the input file
62     if ($filename && $filename =~ /\.(csv|ods|xml|sql)$/i) {
63         my $extension = $1;
64         my $uploadFd = $input->upload($fieldname);
65         if ($uploadFd && !$input->cgi_error) {
66             my $tmpfilename = $input->tmpFileName($input->param($fieldname));
67             $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
68             $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename));
69         }
70     }
71     if ($ok >= 0) { # If everything went ok go to the framework marc structure
72         print $input->redirect( -location => '/cgi-bin/koha/admin/marctagstructure.pl?frameworkcode=' . $frameworkcode);
73     } else {
74         # If something failed go to the list of frameworks and show message
75         print $input->redirect( -location => '/cgi-bin/koha/admin/biblio_framework.pl?error_import_export=' . $frameworkcode);
76     }
77 }