Bug 29983: Show the column by default but hide if the feature is disabled
[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 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use CGI::Cookie;
23 use C4::Context;
24 use C4::Auth qw( check_cookie_auth );
25 use C4::ImportExportFramework qw( createODS ExportFramework ImportFramework );
26
27 my %cookies = CGI::Cookie->fetch();
28 my ($auth_status);
29 if (exists $cookies{'CGISESSID'}) {
30     ($auth_status, undef) = check_cookie_auth(
31         $cookies{'CGISESSID'}->value,
32         { parameters => 'manage_marc_frameworks' },
33     );
34 }
35
36 my $input = CGI->new;
37
38 unless ($auth_status eq 'ok') {
39     print $input->header(-type => 'text/plain', -status => '403 Forbidden');
40     exit 0;
41 }
42
43 my $frameworkcode = $input->param('frameworkcode') || 'default';
44 my $action = $input->param('action') || 'export';
45
46 ## Exporting
47 if ($action eq 'export' && $input->request_method() eq 'GET') {
48     my $strXml = '';
49     my $format = $input->param('type_export_' . $frameworkcode);
50     if ($frameworkcode eq 'default') {
51         ExportFramework('', \$strXml, $format, 'biblio');
52     } else {
53         ExportFramework($frameworkcode, \$strXml, $format, 'biblio');
54     }
55
56     if ($format eq 'csv') {
57         # CSV file
58
59         # Correctly set the encoding to output plain text in UTF-8
60         binmode(STDOUT,':encoding(UTF-8)');
61         print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $frameworkcode . '.csv');
62         print $strXml;
63     } else {
64         # ODS file
65         my $strODS = '';
66         createODS($strXml, 'en', \$strODS);
67         print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $frameworkcode . '.ods');
68         print $strODS;
69     }
70 ## Importing
71 } elsif ($input->request_method() eq 'POST') {
72     my $ok = -1;
73     my $fieldname = 'file_import_' . $frameworkcode;
74     my $filename = $input->param($fieldname);
75     # upload the input file
76     if ($filename && $filename =~ /\.(csv|ods)$/i) {
77         my $extension = $1;
78         my $uploadFd = $input->upload($fieldname);
79         if ($uploadFd && !$input->cgi_error) {
80             my $tmpfilename = $input->tmpFileName(scalar $input->param($fieldname));
81             $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension
82             $ok = ImportFramework($filename, $frameworkcode, 1, 'biblio') if (rename($tmpfilename, $filename));
83         }
84     }
85     if ($ok >= 0) { # If everything went ok go to the framework marc structure
86         print $input->redirect( -location => '/cgi-bin/koha/admin/marctagstructure.pl?frameworkcode=' . $frameworkcode);
87     } else {
88         # If something failed go to the list of frameworks and show message
89         print $input->redirect( -location => '/cgi-bin/koha/admin/biblio_framework.pl?error_import_export=' . $frameworkcode);
90     }
91 }