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