Bug 11592: MARCView and ISBD followup
[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 use Koha::RecordProcessor;
30
31 my $query = CGI->new;
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 $biblionumber = int($biblionumber);
36 my $error = q{};
37
38 my $marc_unfiltered;
39 $marc_unfiltered = GetMarcBiblio($biblionumber, 1) if $biblionumber;
40 if(!$marc_unfiltered) {
41     print $query->redirect("/cgi-bin/koha/errors/404.pl");
42     exit;
43 }
44
45 # ASSERT: There is a biblionumber, because GetMarcBiblio returned something.
46
47 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
48 my $marc_filtered    = $marc_unfiltered->clone();
49 my $marc             = $record_processor->process($marc_filtered);
50
51 my $marc_noitems_unfiltered = GetMarcBiblio($biblionumber);
52 my $marc_noitems_filtered   = $marc_noitems_unfiltered->clone();
53 my $marc_noitems            = $record_processor->process($marc_noitems_filtered);
54
55 if ($format =~ /endnote/) {
56     $marc = marc2endnote($marc);
57     $format = 'endnote';
58 }
59 elsif ($format =~ /marcxml/) {
60     $marc = marc2marcxml($marc);
61     $format = 'marcxml';
62 }
63 elsif ($format=~ /mods/) {
64     $marc = marc2modsxml($marc);
65     $format = 'mods';
66 }
67 elsif ($format =~ /ris/) {
68     $marc = marc2ris($marc);
69     $format = 'ris';
70 }
71 elsif ($format =~ /bibtex/) {
72     $marc = marc2bibtex($marc_noitems,$biblionumber);
73     $format = 'bibtex';
74 }
75 elsif ($format =~ /dc$/) {
76     # TODO: Dublin Core leaks fields marked hidden by framework.
77     $marc = marc2dcxml($marc, undef, $biblionumber, $format);
78     $format = "dublin-core.xml";
79 }
80 elsif ($format =~ /marc8/) {
81     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
82     $marc = $marc->as_usmarc() unless $error;
83     $format = 'marc8';
84 }
85 elsif ($format =~ /utf8/) {
86     C4::Charset::SetUTF8Flag($marc,1);
87     $marc = $marc->as_usmarc();
88     $format = 'utf8';
89 }
90 elsif ($format =~ /marcstd/) {
91     C4::Charset::SetUTF8Flag($marc,1);
92     ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
93     $format = 'marcstd';
94 }
95 elsif ( $format =~ /isbd/ ) {
96     my $framework = GetFrameworkCode( $biblionumber );
97     $marc   = GetISBDView({
98         'record'    => $marc,
99         'template'  => 'opac',
100         'framework' => $framework,
101     });
102     $format = 'isbd';
103 }
104 else {
105     $error= "Format $format is not supported.";
106 }
107
108 if ($error){
109     print $query->header();
110     print $query->start_html();
111     print "<h1>An error occurred </h1>";
112     print $query->escapeHTML("$error");
113     print $query->end_html();
114 }
115 else {
116     if ($format eq 'marc8'){
117         print $query->header(
118             -type => 'application/marc',
119             -charset=>'ISO-2022',
120             -attachment=>"bib-$biblionumber.$format");
121     }
122     elsif ( $format eq 'isbd' ) {
123         print $query->header(
124             -type       => 'text/plain',
125             -charset    => 'utf-8',
126             -attachment =>  "bib-$biblionumber.txt"
127         );
128     } else {
129         binmode STDOUT, ':encoding(UTF-8)';
130         print $query->header(
131             -type => 'application/octet-stream',
132             -charset => 'utf-8',
133             -attachment => "bib-$biblionumber.$format"
134         );
135     }
136     print $marc;
137 }
138
139 1;