Bug 16844: (follow-up of 15656) Remove export of GetMemberRelatives from C4::Members
[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
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
22 use C4::Record;
23 use C4::Auth;
24 use C4::Output;
25 use C4::Biblio;
26 use CGI qw ( -utf8 );
27 use C4::Auth;
28 use C4::Ris;
29
30 my $query = new CGI;
31 my $op=$query->param("op")||''; #op=export is currently the only use
32 my $format=$query->param("format")||'utf8';
33 my $biblionumber = $query->param("bib")||0;
34 $biblionumber = int($biblionumber);
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     $format = 'endnote';
45 }
46 elsif ($format =~ /marcxml/) {
47     $marc = marc2marcxml($marc);
48     $format = 'marcxml';
49 }
50 elsif ($format=~ /mods/) {
51     $marc = marc2modsxml($marc);
52     $format = 'mods';
53 }
54 elsif ($format =~ /ris/) {
55     $marc = marc2ris($marc);
56     $format = 'ris';
57 }
58 elsif ($format =~ /bibtex/) {
59     $marc = marc2bibtex(C4::Biblio::GetMarcBiblio($biblionumber),$biblionumber);
60     $format = 'bibtex';
61 }
62 elsif ($format =~ /dc$/) {
63     $marc = marc2dcxml(undef, undef, $biblionumber, $format);
64     $format = "dublin-core.xml";
65 }
66 elsif ($format =~ /marc8/) {
67     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
68     $marc = $marc->as_usmarc() unless $error;
69     $format = 'marc8';
70 }
71 elsif ($format =~ /utf8/) {
72     C4::Charset::SetUTF8Flag($marc,1);
73     $marc = $marc->as_usmarc();
74     $format = 'utf8';
75 }
76 elsif ($format =~ /marcstd/) {
77     C4::Charset::SetUTF8Flag($marc,1);
78     ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
79     $format = 'marcstd';
80 }
81 elsif ( $format =~ /isbd/ ) {
82     $marc   = GetISBDView($biblionumber, "opac");
83     $format = 'isbd';
84 }
85 else {
86     $error= "Format $format is not supported.";
87 }
88
89 if ($error){
90     print $query->header();
91     print $query->start_html();
92     print "<h1>An error occurred </h1>";
93     print $query->escapeHTML("$error");
94     print $query->end_html();
95 }
96 else {
97     if ($format eq 'marc8'){
98         print $query->header(
99             -type => 'application/marc',
100             -charset=>'ISO-2022',
101             -attachment=>"bib-$biblionumber.$format");
102     }
103     elsif ( $format eq 'isbd' ) {
104         print $query->header(
105             -type       => 'text/plain',
106             -charset    => 'utf-8',
107             -attachment =>  "bib-$biblionumber.txt"
108         );
109     } else {
110         binmode STDOUT, ':encoding(UTF-8)';
111         print $query->header(
112             -type => 'application/octet-stream',
113             -charset => 'utf-8',
114             -attachment => "bib-$biblionumber.$format"
115         );
116     }
117     print $marc;
118 }
119
120 1;