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>.
29 use Koha::RecordProcessor;
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);
38 # Determine logged in user's patron category.
39 # Blank if not logged in.
40 my $userenv = C4::Context->userenv;
43 my $borrowernumber = $userenv->{'number'};
44 if ($borrowernumber) {
45 my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } );
46 $borcat = $borrower ? $borrower->categorycode : $borcat;
50 my $include_items = ($format =~ /bibtex/) ? 0 : 1;
51 my $marc = $biblionumber
53 biblionumber => $biblionumber,
54 embed_items => $include_items,
60 print $query->redirect("/cgi-bin/koha/errors/404.pl");
64 my $file_id = $biblionumber;
65 my $file_pre = "bib-";
66 if( C4::Context->preference('DefaultSaveRecordFileID') eq 'controlnumber' ){
67 my $marcflavour = C4::Context->preference('marcflavour'); #FIXME This option is required but does not change control num behaviour
68 my $control_num = GetMarcControlnumber( $marc, $marcflavour );
70 $file_id = $control_num;
71 $file_pre = "record-";
75 # ASSERT: There is a biblionumber, because GetMarcBiblio returned something.
76 my $framework = GetFrameworkCode( $biblionumber );
77 my $record_processor = Koha::RecordProcessor->new({
78 filters => 'ViewPolicy',
81 frameworkcode => $framework
84 $record_processor->process($marc);
86 if ($format =~ /endnote/) {
87 $marc = marc2endnote($marc);
90 elsif ($format =~ /marcxml/) {
91 $marc = marc2marcxml($marc);
94 elsif ($format=~ /mods/) {
95 $marc = marc2modsxml($marc);
98 elsif ($format =~ /ris/) {
99 $marc = marc2ris($marc);
102 elsif ($format =~ /bibtex/) {
103 $marc = marc2bibtex($marc,$biblionumber);
106 elsif ($format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
107 # TODO: Dublin Core leaks fields marked hidden by framework.
108 $marc = marc2dcxml($marc, undef, $biblionumber, $format);
109 $format = "dublin-core.xml";
111 elsif ($format =~ /marc8/) {
112 ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
113 $marc = $marc->as_usmarc() unless $error;
116 elsif ($format =~ /utf8/) {
117 C4::Charset::SetUTF8Flag($marc,1);
118 $marc = $marc->as_usmarc();
121 elsif ($format =~ /marcstd/) {
122 C4::Charset::SetUTF8Flag($marc,1);
123 ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
126 elsif ( $format =~ /isbd/ ) {
127 $marc = GetISBDView({
129 'template' => 'opac',
130 'framework' => $framework,
135 $error= "Format $format is not supported.";
139 print $query->header();
140 print $query->start_html();
141 print "<h1>An error occurred </h1>";
142 print $query->escapeHTML("$error");
143 print $query->end_html();
146 if ($format eq 'marc8'){
147 print $query->header(
148 -type => 'application/marc',
149 -charset=>'ISO-2022',
150 -attachment=>"$file_pre$file_id.$format");
152 elsif ( $format eq 'isbd' ) {
153 print $query->header(
154 -type => 'text/plain',
156 -attachment => "$file_pre$file_id.txt"
159 elsif ( $format eq 'ris' ) {
160 print $query->header(
161 -type => 'text/plain',
163 -attachment => "$file_pre$file_id.$format"
166 binmode STDOUT, ':encoding(UTF-8)';
167 print $query->header(
168 -type => 'application/octet-stream',
170 -attachment => "$file_pre$file_id.$format"