Bug 28013: Unit tests
[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 # Determine logged in user's patron category.
39 # Blank if not logged in.
40 my $userenv = C4::Context->userenv;
41 my $borcat = q{};
42 if ($userenv) {
43     my $borrowernumber = $userenv->{'number'};
44     if ($borrowernumber) {
45         my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } );
46         $borcat = $borrower ? $borrower->categorycode : $borcat;
47     }
48 }
49
50 my $include_items = ($format =~ /bibtex/) ? 0 : 1;
51 my $marc = $biblionumber
52     ? GetMarcBiblio({
53         biblionumber => $biblionumber,
54         embed_items  => $include_items,
55         opac         => 1,
56         borcat       => $borcat })
57     : undef;
58
59 if(!$marc) {
60     print $query->redirect("/cgi-bin/koha/errors/404.pl");
61     exit;
62 }
63
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 );
69     if( $control_num ){
70         $file_id = $control_num;
71         $file_pre = "record-";
72     }
73 }
74
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',
79     options => {
80         interface => 'opac',
81         frameworkcode => $framework
82     }
83 });
84 $record_processor->process($marc);
85
86 if ($format =~ /endnote/) {
87     $marc = marc2endnote($marc);
88     $format = 'endnote';
89 }
90 elsif ($format =~ /marcxml/) {
91     $marc = marc2marcxml($marc);
92     $format = 'marcxml';
93 }
94 elsif ($format=~ /mods/) {
95     $marc = marc2modsxml($marc);
96     $format = 'mods';
97 }
98 elsif ($format =~ /ris/) {
99     $marc = marc2ris($marc);
100     $format = 'ris';
101 }
102 elsif ($format =~ /bibtex/) {
103     $marc = marc2bibtex($marc,$biblionumber);
104     $format = 'bibtex';
105 }
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";
110 }
111 elsif ($format =~ /marc8/) {
112     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
113     $marc = $marc->as_usmarc() unless $error;
114     $format = 'marc8';
115 }
116 elsif ($format =~ /utf8/) {
117     C4::Charset::SetUTF8Flag($marc,1);
118     $marc = $marc->as_usmarc();
119     $format = 'utf8';
120 }
121 elsif ($format =~ /marcstd/) {
122     C4::Charset::SetUTF8Flag($marc,1);
123     ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
124     $format = 'marcstd';
125 }
126 elsif ( $format =~ /isbd/ ) {
127     $marc   = GetISBDView({
128         'record'    => $marc,
129         'template'  => 'opac',
130         'framework' => $framework,
131     });
132     $format = 'isbd';
133 }
134 else {
135     $error= "Format $format is not supported.";
136 }
137
138 if ($error){
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();
144 }
145 else {
146     if ($format eq 'marc8'){
147         print $query->header(
148             -type => 'application/marc',
149             -charset=>'ISO-2022',
150             -attachment=>"$file_pre$file_id.$format");
151     }
152     elsif ( $format eq 'isbd' ) {
153         print $query->header(
154             -type       => 'text/plain',
155             -charset    => 'utf-8',
156             -attachment =>  "$file_pre$file_id.txt"
157         );
158     }
159     elsif ( $format eq 'ris' ) {
160         print $query->header(
161             -type => 'text/plain',
162             -charset => 'utf-8',
163             -attachment => "$file_pre$file_id.$format"
164         );
165     } else {
166         binmode STDOUT, ':encoding(UTF-8)';
167         print $query->header(
168             -type => 'application/octet-stream',
169             -charset => 'utf-8',
170             -attachment => "$file_pre$file_id.$format"
171         );
172     }
173     print $marc;
174 }
175
176 1;