3 # Parts Copyright Catalyst IT 2011
5 # This file is part of Koha.
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.
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.
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>.
32 use C4::Ris qw( marc2ris );
34 use Koha::RecordProcessor;
37 my $op=$query->param("op")||''; #op=export is currently the only use
38 my $format=$query->param("format")||'utf8';
39 my $biblionumber = $query->param("bib")||0;
40 $biblionumber = int($biblionumber);
43 # Determine logged in user's patron category.
44 # Blank if not logged in.
45 my $userenv = C4::Context->userenv;
48 my $borrowernumber = $userenv->{'number'};
49 if ($borrowernumber) {
50 $patron = Koha::Patrons->find( $borrowernumber );
54 my $include_items = ($format =~ /bibtex/) ? 0 : 1;
55 my $biblio = Koha::Biblios->find($biblionumber);
57 ? $biblio->metadata->record(
67 print $query->redirect("/cgi-bin/koha/errors/404.pl");
71 my $file_id = $biblionumber;
72 my $file_pre = "bib-";
73 if( C4::Context->preference('DefaultSaveRecordFileID') eq 'controlnumber' ){
74 my $marcflavour = C4::Context->preference('marcflavour'); #FIXME This option is required but does not change control num behaviour
75 my $control_num = GetMarcControlnumber( $marc, $marcflavour );
77 $file_id = $control_num;
78 $file_pre = "record-";
82 my $framework = $biblio->frameworkcode;
83 my $record_processor = Koha::RecordProcessor->new({
84 filters => 'ViewPolicy',
87 frameworkcode => $framework
90 $record_processor->process($marc);
92 if ($format =~ /endnote/) {
93 $marc = marc2endnote($marc);
96 elsif ($format =~ /marcxml/) {
97 $marc = marc2marcxml($marc);
100 elsif ($format=~ /mods/) {
101 $marc = marc2modsxml($marc);
104 elsif ($format =~ /ris/) {
105 $marc = marc2ris($marc);
108 elsif ($format =~ /bibtex/) {
109 $marc = marc2bibtex($marc,$biblionumber);
112 elsif ($format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
113 # TODO: Dublin Core leaks fields marked hidden by framework.
114 $marc = marc2dcxml($marc, undef, $biblionumber, $format);
115 $format = "dublin-core.xml";
117 elsif ($format =~ /marc8/) {
118 ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
119 $marc = $marc->as_usmarc() unless $error;
122 elsif ($format =~ /utf8/) {
123 C4::Charset::SetUTF8Flag($marc,1);
124 $marc = $marc->as_usmarc();
127 elsif ($format =~ /marcstd/) {
128 C4::Charset::SetUTF8Flag($marc,1);
129 ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
132 elsif ( $format =~ /isbd/ ) {
133 $marc = GetISBDView({
135 'template' => 'opac',
136 'framework' => $framework,
141 $error= "Format $format is not supported.";
145 print $query->header();
146 print $query->start_html();
147 print "<h1>An error occurred </h1>";
148 print $query->escapeHTML("$error");
149 print $query->end_html();
152 if ($format eq 'marc8'){
153 print $query->header(
154 -type => 'application/marc',
155 -charset=>'ISO-2022',
156 -attachment=>"$file_pre$file_id.$format");
158 elsif ( $format eq 'isbd' ) {
159 print $query->header(
160 -type => 'text/plain',
162 -attachment => "$file_pre$file_id.txt"
165 elsif ( $format eq 'ris' ) {
166 print $query->header(
167 -type => 'text/plain',
169 -attachment => "$file_pre$file_id.$format"
172 binmode STDOUT, ':encoding(UTF-8)';
173 print $query->header(
174 -type => 'application/octet-stream',
176 -attachment => "$file_pre$file_id.$format"