Merge remote-tracking branch 'origin/new/bug_5327'
[koha.git] / opac / opac-export.pl
1 #!/usr/bin/perl
2
3 # Parts Copyright Catalyst IT 2011
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 use strict;
21 use warnings;
22
23 use C4::Record;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use CGI;
28 use C4::Auth;
29 use C4::Ris;
30
31 my $query = new CGI;
32 my $op=$query->param("op")||''; #op=export is currently the only use
33 my $format=$query->param("format")||'utf8';
34 my $biblionumber = $query->param("bib")||0;
35 my ($marc, $error)= ('','');
36
37 $marc = GetMarcBiblio($biblionumber, 1) if $biblionumber;
38 if(!$marc) {
39     print $query->redirect("/cgi-bin/koha/errors/404.pl");
40     exit;
41 }
42 elsif ($format =~ /endnote/) {
43     $marc = marc2endnote($marc);
44 }
45 elsif ($format =~ /marcxml/) {
46     $marc = marc2marcxml($marc);
47 }
48 elsif ($format=~ /mods/) {
49     $marc = marc2modsxml($marc);
50 }
51 elsif ($format =~ /ris/) {
52     $marc = marc2ris($marc);
53 }
54 elsif ($format =~ /bibtex/) {
55     $marc = marc2bibtex(C4::Biblio::GetMarcBiblio($biblionumber),$biblionumber);
56 }
57 elsif ($format =~ /dc/) {
58     ($error,$marc) = marc2dcxml($marc,1);
59     $format = "dublin-core.xml";
60 }
61 elsif ($format =~ /marc8/) {
62     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
63     $marc = $marc->as_usmarc() unless $error;
64 }
65 elsif ($format =~ /utf8/) {
66     C4::Charset::SetUTF8Flag($marc,1);
67     $marc = $marc->as_usmarc();
68 }
69 elsif ($format =~ /marcstd/) {
70     C4::Charset::SetUTF8Flag($marc,1);
71     ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
72 }
73 else {
74     $error= "Format $format is not supported.";
75 }
76
77 if ($error){
78     print $query->header();
79     print $query->start_html();
80     print "<h1>An error occurred </h1>";
81     print $error;
82     print $query->end_html();
83 }
84 else {
85     print $query->header(
86       -type => 'application/octet-stream',
87       -attachment=>"bib-$biblionumber.$format");
88     print $marc;
89 }