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>.
33 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 my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } );
51 $borcat = $borrower ? $borrower->categorycode : $borcat;
55 my $include_items = ($format =~ /bibtex/) ? 0 : 1;
56 my $marc = $biblionumber
58 biblionumber => $biblionumber,
59 embed_items => $include_items,
65 print $query->redirect("/cgi-bin/koha/errors/404.pl");
69 my $file_id = $biblionumber;
70 my $file_pre = "bib-";
71 if( C4::Context->preference('DefaultSaveRecordFileID') eq 'controlnumber' ){
72 my $marcflavour = C4::Context->preference('marcflavour'); #FIXME This option is required but does not change control num behaviour
73 my $control_num = GetMarcControlnumber( $marc, $marcflavour );
75 $file_id = $control_num;
76 $file_pre = "record-";
80 # ASSERT: There is a biblionumber, because GetMarcBiblio returned something.
81 my $framework = GetFrameworkCode( $biblionumber );
82 my $record_processor = Koha::RecordProcessor->new({
83 filters => 'ViewPolicy',
86 frameworkcode => $framework
89 $record_processor->process($marc);
91 if ($format =~ /endnote/) {
92 $marc = marc2endnote($marc);
95 elsif ($format =~ /marcxml/) {
96 $marc = marc2marcxml($marc);
99 elsif ($format=~ /mods/) {
100 $marc = marc2modsxml($marc);
103 elsif ($format =~ /ris/) {
104 $marc = marc2ris($marc);
107 elsif ($format =~ /bibtex/) {
108 $marc = marc2bibtex($marc,$biblionumber);
111 elsif ($format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
112 # TODO: Dublin Core leaks fields marked hidden by framework.
113 $marc = marc2dcxml($marc, undef, $biblionumber, $format);
114 $format = "dublin-core.xml";
116 elsif ($format =~ /marc8/) {
117 ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
118 $marc = $marc->as_usmarc() unless $error;
121 elsif ($format =~ /utf8/) {
122 C4::Charset::SetUTF8Flag($marc,1);
123 $marc = $marc->as_usmarc();
126 elsif ($format =~ /marcstd/) {
127 C4::Charset::SetUTF8Flag($marc,1);
128 ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
131 elsif ( $format =~ /isbd/ ) {
132 $marc = GetISBDView({
134 'template' => 'opac',
135 'framework' => $framework,
140 $error= "Format $format is not supported.";
144 print $query->header();
145 print $query->start_html();
146 print "<h1>An error occurred </h1>";
147 print $query->escapeHTML("$error");
148 print $query->end_html();
151 if ($format eq 'marc8'){
152 print $query->header(
153 -type => 'application/marc',
154 -charset=>'ISO-2022',
155 -attachment=>"$file_pre$file_id.$format");
157 elsif ( $format eq 'isbd' ) {
158 print $query->header(
159 -type => 'text/plain',
161 -attachment => "$file_pre$file_id.txt"
164 elsif ( $format eq 'ris' ) {
165 print $query->header(
166 -type => 'text/plain',
168 -attachment => "$file_pre$file_id.$format"
171 binmode STDOUT, ':encoding(UTF-8)';
172 print $query->header(
173 -type => 'application/octet-stream',
175 -attachment => "$file_pre$file_id.$format"