Merge branch 'new/bug_6479' into kcmaster
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
21 use strict;
22 use warnings;
23
24 use C4::Record;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use CGI;
29 use C4::Auth;
30 use C4::Ris;
31
32 my $query = new CGI;
33 my $op=$query->param("op")||''; #op=export is currently the only use
34 my $format=$query->param("format")||'utf8';
35 my $biblionumber = $query->param("bib")||0;
36 my ($marc, $error)= ('','');
37
38 $marc = GetMarcBiblio($biblionumber, 1) if $biblionumber;
39 if(!$marc) {
40     print $query->redirect("/cgi-bin/koha/errors/404.pl");
41     exit;
42 }
43 elsif ($format =~ /endnote/) {
44     $marc = marc2endnote($marc);
45 }
46 elsif ($format =~ /marcxml/) {
47     $marc = marc2marcxml($marc);
48 }
49 elsif ($format=~ /mods/) {
50     $marc = marc2modsxml($marc);
51 }
52 elsif ($format =~ /ris/) {
53     $marc = marc2ris($marc);
54 }
55 elsif ($format =~ /bibtex/) {
56     $marc = marc2bibtex(C4::Biblio::GetMarcBiblio($biblionumber),$biblionumber);
57 }
58 elsif ($format =~ /dc/) {
59     ($error,$marc) = marc2dcxml($marc,1);
60     $format = "dublin-core.xml";
61 }
62 elsif ($format =~ /marc8/) {
63     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
64     $marc = $marc->as_usmarc() unless $error;
65 }
66 elsif ($format =~ /utf8/) {
67     C4::Charset::SetUTF8Flag($marc,1);
68     $marc = $marc->as_usmarc();
69 }
70 else {
71     $error= "Format $format is not supported.";
72 }
73
74 if ($error){
75     print $query->header();
76     print $query->start_html();
77     print "<h1>An error occurred </h1>";
78     print $error;
79     print $query->end_html();
80 }
81 else {
82     print $query->header(
83       -type => 'application/octet-stream',
84       -attachment=>"bib-$biblionumber.$format");
85     print $marc;
86 }