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