Bug 18282: operationId must be unique
[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
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
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;
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 $framework_name = $input->param('frameworkcode') || 'default';
50 my $frameworkcode = ($framework_name eq 'default') ? q{} : $framework_name;
51 my $action = $input->param('action') || 'export';
52
53 ## Exporting
54 if ($action eq 'export' && $input->request_method() eq 'GET') {
55     my $strXml = '';
56     my $format = $input->param('type_export_' . $framework_name);
57     ExportFramework($frameworkcode, \$strXml, $format);
58
59     if ($format eq 'csv') {
60         # CSV file
61
62         # Correctly set the encoding to output plain text in UTF-8
63         binmode(STDOUT,':encoding(UTF-8)');
64         print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $framework_name . '.csv');
65         print $strXml;
66     } elsif ($format eq 'excel') {
67         # Excel-xml file
68         print $input->header(-type => 'application/excel', -attachment => 'export_' . $framework_name . '.xml');
69         print $strXml;
70     } else {
71         # ODS file
72         my $strODS = '';
73         createODS($strXml, 'en', \$strODS);
74         print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $framework_name . '.ods');
75         print $strODS;
76     }
77 ## Importing
78 } elsif ($input->request_method() eq 'POST') {
79     my $ok = -1;
80     my $fieldname = 'file_import_' . $framework_name;
81     my $filename = $input->param($fieldname);
82     # upload the input file
83     if ($filename && $filename =~ /\.(csv|ods|xml)$/i) {
84         my $extension = $1;
85         my $uploadFd = $input->upload($fieldname);
86         if ($uploadFd && !$input->cgi_error) {
87             my $tmpfilename = $input->tmpFileName(scalar $input->param($fieldname));
88             $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
89             $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename));
90         }
91     }
92     if ($ok >= 0) { # If everything went ok go to the framework marc structure
93         print $input->redirect( -location => '/cgi-bin/koha/admin/marctagstructure.pl?frameworkcode=' . $frameworkcode);
94     } else {
95         # If something failed go to the list of frameworks and show message
96         print $input->redirect( -location => '/cgi-bin/koha/admin/biblio_framework.pl?error_import_export=' . $frameworkcode);
97     }
98 }