Bug 32482: (follow-up) Add markup comments
[koha.git] / opac / opac-downloadshelf.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
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 CGI qw ( -utf8 );
23
24 use C4::Auth qw( get_template_and_user );
25 use C4::Biblio qw( GetFrameworkCode GetISBDView );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Record;
28 use C4::Ris qw( marc2ris );
29 use Koha::Biblios;
30 use Koha::CsvProfiles;
31 use Koha::RecordProcessor;
32 use Koha::Virtualshelves;
33
34 use utf8;
35 my $query = CGI->new;
36
37 # if virtualshelves is disabled, leave immediately
38 if ( ! C4::Context->preference('virtualshelves') ) {
39     print $query->redirect("/cgi-bin/koha/errors/404.pl");
40     exit;
41 }
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
44     {
45         template_name   => "opac-downloadshelf.tt",
46         query           => $query,
47         type            => "opac",
48         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
49     }
50 );
51
52 my $patron = Koha::Patrons->find( $borrowernumber );
53
54 my $shelfnumber = $query->param('shelfnumber');
55 my $format  = $query->param('format');
56 my $context = $query->param('context');
57
58 my $shelf = Koha::Virtualshelves->find( $shelfnumber );
59 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
60
61     if ($shelfnumber && $format) {
62
63
64         my $contents = $shelf->get_contents;
65         my $marcflavour = C4::Context->preference('marcflavour');
66         my $output;
67         my $extension;
68         my $type;
69
70        # CSV
71         if ($format =~ /^\d+$/) {
72
73             my $csv_profile = Koha::CsvProfiles->find($format);
74             if ( not $csv_profile or $csv_profile->staff_only ) {
75                 print $query->redirect('/cgi-bin/koha/errors/404.pl');
76                 exit;
77             }
78
79             my @biblios;
80             while ( my $content = $contents->next ) {
81                 push @biblios, $content->biblionumber;
82             }
83             $output = marc2csv(\@biblios, $format);
84         # Other formats
85         } else {
86             my $record_processor = Koha::RecordProcessor->new({
87                 filters => 'ViewPolicy'
88             });
89             while ( my $content = $contents->next ) {
90                 my $biblionumber = $content->biblionumber;
91
92                 my $biblio = Koha::Biblios->find($biblionumber);
93                 my $record = $biblio->metadata->record->(
94                     {
95                         embed_items => 1,
96                         opac        => 1,
97                         patron      => $patron,
98                     }
99                 );
100                 my $framework = $biblio->frameworkcode;
101                 $record_processor->options({
102                     interface => 'opac',
103                     frameworkcode => $framework
104                 });
105                 $record_processor->process($record);
106                 next unless $record;
107
108                 if ($format eq 'iso2709') {
109                     $output .= $record->as_usmarc();
110                 }
111                 elsif ($format eq 'ris' ) {
112                     $output .= marc2ris($record);
113                 }
114                 elsif ($format eq 'bibtex') {
115                     $output .= marc2bibtex($record, $biblionumber);
116                 }
117                 elsif ( $format eq 'isbd' ) {
118                     $output   .= GetISBDView({
119                         'record'    => $record,
120                         'template'  => 'opac',
121                         'framework' => $framework,
122                     });
123                     $extension = "txt";
124                     $type      = "text/plain";
125                 }
126             }
127         }
128
129         # If it was a CSV export we change the format after the export so the file extension is fine
130         $format = "csv" if ($format =~ m/^\d+$/);
131
132         print $query->header(
133                                    -type => ($type) ? $type : 'application/octet-stream',
134             -'Content-Transfer-Encoding' => 'binary',
135                              -attachment => ($extension) ? "shelf.$format.$extension" : "shelf.$format"
136         );
137         print $output;
138
139     } else {
140
141         # if modal context is passed set a variable so that page markup can be different
142         if($context eq "modal"){
143             $template->param(modal => 1);
144         } else {
145             $template->param(fullpage => 1);
146         }
147         $template->param(
148             csv_profiles => Koha::CsvProfiles->search(
149                 {
150                     type       => 'marc',
151                     used_for   => 'export_records',
152                     staff_only => 0
153                 }
154             ),
155             shelf => $shelf,
156         );
157         output_html_with_http_headers $query, $cookie, $template->output;
158     }
159
160 } else {
161     $template->param(invalidlist => 1); 
162     output_html_with_http_headers $query, $cookie, $template->output;
163 }